15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 2e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch * Copyright (C) 2013 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions of source code must retain the above copyright 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions in binary form must reproduce the above 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission. 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h" 32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/html/imports/HTMLImport.h" 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 343c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch#include "core/dom/Document.h" 35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/html/imports/HTMLImportStateResolver.h" 36c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "wtf/Vector.h" 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 406f543c786fc42989f552b4daa774ca5ff32fa697Ben MurdochHTMLImport* HTMLImport::root() 4102772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch{ 426f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch HTMLImport* i = this; 436f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch while (i->parent()) 446f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch i = i->parent(); 456f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch return i; 4602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch} 4702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch 4810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochbool HTMLImport::precedes(HTMLImport* import) 49f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){ 5010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch for (HTMLImport* i = this; i; i = traverseNext(i)) { 5110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch if (i == import) 5210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch return true; 5310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch } 5410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch 5510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch return false; 5610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch} 5710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch 58323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)bool HTMLImport::formsCycle() const 59323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles){ 60323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) for (const HTMLImport* i = this->parent(); i; i = i->parent()) { 61323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) if (i->document() == this->document()) 62323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) return true; 63323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) } 64323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) 65323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) return false; 66323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) 67323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)} 68323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) 6910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochvoid HTMLImport::appendImport(HTMLImport* child) 7010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch{ 7110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch appendChild(child); 72f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // This prevents HTML parser from going beyond the 7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // blockage line before the precise state is computed by recalcState(). 75d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) if (child->isSync()) 7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) m_state = HTMLImportState::blockedState(); 77f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 7809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) stateWillChange(); 79f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)} 80f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void HTMLImport::stateDidChange() 82f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){ 8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (!state().shouldBlockScriptExecution()) { 8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (Document* document = this->document()) 8509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) document->didLoadAllImports(); 86f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) } 87f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)} 88f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 8909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void HTMLImport::recalcTreeState(HTMLImport* root) 90f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){ 91197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch WillBeHeapHashMap<RawPtrWillBeMember<HTMLImport>, HTMLImportState> snapshot; 92197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch WillBeHeapVector<RawPtrWillBeMember<HTMLImport> > updated; 93f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) for (HTMLImport* i = root; i; i = traverseNext(i)) { 9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) snapshot.add(i, i->state()); 9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) i->m_state = HTMLImportState::invalidState(); 9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) } 98a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // The post-visit DFS order matters here because 10009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // HTMLImportStateResolver in recalcState() Depends on 10109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // |m_state| of its children and precedents of ancestors. 10209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // Accidental cycle dependency of state computation is prevented 10309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // by invalidateCachedState() and isStateCacheValid() check. 10409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) for (HTMLImport* i = traverseFirstPostOrder(root); i; i = traverseNextPostOrder(i)) { 10509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) ASSERT(!i->m_state.isValid()); 10609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) i->m_state = HTMLImportStateResolver(i).resolve(); 10709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 10809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) HTMLImportState newState = i->state(); 10909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) HTMLImportState oldState = snapshot.get(i); 11009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // Once the state reaches Ready, it shouldn't go back. 11109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) ASSERT(!oldState.isReady() || oldState <= newState); 11209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (newState != oldState) 11309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) updated.append(i); 11409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) } 115a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 11609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) for (size_t i = 0; i < updated.size(); ++i) 11709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) updated[i]->stateDidChange(); 118a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)} 119a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 12009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#if !defined(NDEBUG) 12109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void HTMLImport::show() 122f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){ 12309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) root()->showTree(this, 0); 124f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)} 125f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 12609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void HTMLImport::showTree(HTMLImport* highlight, unsigned depth) 127f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){ 12809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) for (unsigned i = 0; i < depth*4; ++i) 12909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) fprintf(stderr, " "); 130f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 13109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) fprintf(stderr, "%s", this == highlight ? "*" : " "); 13209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) showThis(); 13309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) fprintf(stderr, "\n"); 13409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) for (HTMLImport* child = firstChild(); child; child = child->next()) 13509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) child->showTree(highlight, depth + 1); 136f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)} 137f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 13809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void HTMLImport::showThis() 139a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){ 14009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) fprintf(stderr, "%p state=%d", this, m_state.peekValueForDebug()); 141a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)} 14209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#endif 143f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 144c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 145