1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "core/html/imports/HTMLImportTreeRoot.h" 7 8 #include "core/dom/Document.h" 9 #include "core/dom/StyleEngine.h" 10 #include "core/frame/LocalFrame.h" 11 #include "core/html/imports/HTMLImportChild.h" 12 13 namespace WebCore { 14 15 PassOwnPtrWillBeRawPtr<HTMLImportTreeRoot> HTMLImportTreeRoot::create(Document* document) 16 { 17 return adoptPtrWillBeNoop(new HTMLImportTreeRoot(document)); 18 } 19 20 HTMLImportTreeRoot::HTMLImportTreeRoot(Document* document) 21 : HTMLImport(HTMLImport::Sync) 22 , m_document(document) 23 , m_recalcTimer(this, &HTMLImportTreeRoot::recalcTimerFired) 24 { 25 scheduleRecalcState(); // This recomputes initial state. 26 } 27 28 HTMLImportTreeRoot::~HTMLImportTreeRoot() 29 { 30 #if !ENABLE(OILPAN) 31 for (size_t i = 0; i < m_imports.size(); ++i) 32 m_imports[i]->importDestroyed(); 33 m_imports.clear(); 34 m_document = nullptr; 35 #endif 36 } 37 38 Document* HTMLImportTreeRoot::document() const 39 { 40 return m_document; 41 } 42 43 bool HTMLImportTreeRoot::isDone() const 44 { 45 return !m_document->parsing() && m_document->styleEngine()->haveStylesheetsLoaded(); 46 } 47 48 void HTMLImportTreeRoot::stateWillChange() 49 { 50 scheduleRecalcState(); 51 } 52 53 void HTMLImportTreeRoot::stateDidChange() 54 { 55 HTMLImport::stateDidChange(); 56 57 if (!state().isReady()) 58 return; 59 if (LocalFrame* frame = m_document->frame()) 60 frame->loader().checkCompleted(); 61 } 62 63 void HTMLImportTreeRoot::scheduleRecalcState() 64 { 65 #if ENABLE(OILPAN) 66 ASSERT(m_document); 67 if (m_recalcTimer.isActive() || !m_document->isActive()) 68 return; 69 #else 70 if (m_recalcTimer.isActive() || !m_document) 71 return; 72 #endif 73 m_recalcTimer.startOneShot(0, FROM_HERE); 74 } 75 76 HTMLImportChild* HTMLImportTreeRoot::add(PassOwnPtrWillBeRawPtr<HTMLImportChild> child) 77 { 78 m_imports.append(child); 79 return m_imports.last().get(); 80 } 81 82 HTMLImportChild* HTMLImportTreeRoot::find(const KURL& url) const 83 { 84 for (size_t i = 0; i < m_imports.size(); ++i) { 85 HTMLImportChild* candidate = m_imports[i].get(); 86 if (equalIgnoringFragmentIdentifier(candidate->url(), url)) 87 return candidate; 88 } 89 90 return 0; 91 } 92 93 void HTMLImportTreeRoot::recalcTimerFired(Timer<HTMLImportTreeRoot>*) 94 { 95 ASSERT(m_document); 96 97 do { 98 m_recalcTimer.stop(); 99 HTMLImport::recalcTreeState(this); 100 } while (m_recalcTimer.isActive()); 101 } 102 103 void HTMLImportTreeRoot::trace(Visitor* visitor) 104 { 105 visitor->trace(m_document); 106 visitor->trace(m_imports); 107 HTMLImport::trace(visitor); 108 } 109 110 } 111