1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "core/html/HTMLImport.h" 33 34 #include "core/dom/Document.h" 35 36 namespace WebCore { 37 38 Frame* HTMLImport::frame() 39 { 40 return master()->frame(); 41 } 42 43 Document* HTMLImport::master() 44 { 45 return root()->document(); 46 } 47 48 HTMLImportsController* HTMLImport::controller() 49 { 50 return root()->toController(); 51 } 52 53 void HTMLImport::appendChild(HTMLImport* child) 54 { 55 ASSERT(child->parent() == this); 56 ASSERT(!child->hasChildren()); 57 58 if (isBlocked()) 59 child->block(); 60 m_children.append(child); 61 blockAfter(child); 62 } 63 64 bool HTMLImport::areChilrenLoaded() const 65 { 66 for (size_t i = 0; i < m_children.size(); ++i) { 67 if (!m_children[i]->isLoaded()) 68 return false; 69 } 70 71 return true; 72 } 73 74 bool HTMLImport::arePredecessorsLoaded() const 75 { 76 HTMLImport* parent = this->parent(); 77 if (!parent) 78 return true; 79 80 for (size_t i = 0; i < parent->m_children.size(); ++i) { 81 HTMLImport* sibling = parent->m_children[i]; 82 if (sibling == this) 83 break; 84 if (!sibling->isLoaded()) 85 return false; 86 } 87 88 return true; 89 } 90 91 void HTMLImport::block() 92 { 93 m_blocked = true; 94 } 95 96 void HTMLImport::unblock() 97 { 98 bool wasBlocked = m_blocked; 99 m_blocked = false; 100 if (wasBlocked) 101 didUnblock(); 102 } 103 104 void HTMLImport::didUnblock() 105 { 106 ASSERT(!isBlocked()); 107 if (!isProcessing()) 108 return; 109 110 if (Document* document = this->document()) 111 document->didLoadAllImports(); 112 } 113 114 bool HTMLImport::unblock(HTMLImport* import) 115 { 116 ASSERT(import->arePredecessorsLoaded()); 117 ASSERT(import->isBlocked() || import->areChilrenLoaded()); 118 119 if (import->isBlocked()) { 120 for (size_t i = 0; i < import->m_children.size(); ++i) { 121 if (!unblock(import->m_children[i])) 122 return false; 123 } 124 } 125 126 import->unblock(); 127 return import->isLoaded(); 128 } 129 130 void HTMLImport::block(HTMLImport* import) 131 { 132 import->block(); 133 for (size_t i = 0; i < import->m_children.size(); ++i) 134 block(import->m_children[i]); 135 } 136 137 void HTMLImport::blockAfter(HTMLImport* child) 138 { 139 ASSERT(child->parent() == this); 140 141 for (size_t i = 0; i < m_children.size(); ++i) { 142 HTMLImport* sibling = m_children[m_children.size() - i - 1]; 143 if (sibling == child) 144 break; 145 HTMLImport::block(sibling); 146 } 147 148 this->block(); 149 150 if (HTMLImport* parent = this->parent()) 151 parent->blockAfter(this); 152 } 153 154 155 } // namespace WebCore 156