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 #ifndef HTMLImport_h 32 #define HTMLImport_h 33 34 #include "core/html/imports/HTMLImportState.h" 35 #include "platform/heap/Handle.h" 36 #include "wtf/TreeNode.h" 37 #include "wtf/Vector.h" 38 39 namespace WebCore { 40 41 class Document; 42 class LocalFrame; 43 class HTMLImportChild; 44 class HTMLImportLoader; 45 class HTMLImportsController; 46 class KURL; 47 48 // 49 // # Basic Data Structure and Algorithms of HTML Imports implemenation. 50 // 51 // ## The Import Tree 52 // 53 // HTML Imports form a tree: 54 // 55 // * The root of the tree is HTMLImportTreeRoot. 56 // 57 // * The HTMLImportTreeRoot is owned HTMLImportsController, which is owned by the master 58 // document as a DocumentSupplement. 59 // 60 // * The non-root nodes are HTMLImportChild. They are all owned by HTMLImporTreeRoot. 61 // LinkStyle is wired into HTMLImportChild by implementing HTMLImportChildClient interface 62 // 63 // * Both HTMLImportTreeRoot and HTMLImportChild are derived from HTMLImport superclass 64 // that models the tree data structure using WTF::TreeNode and provides a set of 65 // virtual functions. 66 // 67 // HTMLImportsController also owns all loaders in the tree and manages their lifetime through it. 68 // One assumption is that the tree is append-only and nodes are never inserted in the middle of the tree nor removed. 69 // 70 // Full diagram is here: 71 // https://docs.google.com/drawings/d/1jFQrO0IupWrlykTNzQ3Nv2SdiBiSz4UE9-V3-vDgBb0/ 72 // 73 // # Import Sharing and HTMLImportLoader 74 // 75 // The HTML Imports spec calls for de-dup mechanism to share already loaded imports. 76 // To implement this, the actual loading machinery is split out from HTMLImportChild to 77 // HTMLImportLoader, and each loader shares HTMLImportLoader with other loader if the URL is same. 78 // Check around HTMLImportsController::findLink() for more detail. 79 // 80 // HTMLImportLoader can be shared by multiple imports. 81 // 82 // HTMLImportChild (1)-->(*) HTMLImportLoader 83 // 84 // 85 // # Script Blocking 86 // 87 // - An import blocks the HTML parser of its own imported document from running <script> 88 // until all of its children are loaded. 89 // Note that dynamically added import won't block the parser. 90 // 91 // - An import under loading also blocks imported documents that follow from being created. 92 // This is because an import can include another import that has same URLs of following ones. 93 // In such case, the preceding import should be loaded and following ones should be de-duped. 94 // 95 96 // The superclass of HTMLImportTreeRoot and HTMLImportChild 97 // This represents the import tree data structure. 98 class HTMLImport : public NoBaseWillBeGarbageCollectedFinalized<HTMLImport>, public TreeNode<HTMLImport> { 99 public: 100 enum SyncMode { 101 Sync = 0, 102 Async = 1 103 }; 104 105 virtual ~HTMLImport() { } 106 107 // FIXME: Consider returning HTMLImportTreeRoot. 108 HTMLImport* root(); 109 bool precedes(HTMLImport*); 110 bool isRoot() const { return !parent(); } 111 bool isSync() const { return SyncMode(m_sync) == Sync; } 112 bool formsCycle() const; 113 const HTMLImportState& state() const { return m_state; } 114 115 void appendImport(HTMLImport*); 116 117 virtual Document* document() const = 0; 118 virtual bool isDone() const = 0; // FIXME: Should be renamed to haveFinishedLoading() 119 virtual HTMLImportLoader* loader() const { return 0; } 120 virtual void stateWillChange() { } 121 virtual void stateDidChange(); 122 123 virtual void trace(Visitor*) { } 124 125 protected: 126 // Stating from most conservative state. 127 // It will be corrected through state update flow. 128 explicit HTMLImport(SyncMode sync) 129 : m_sync(sync) 130 { } 131 132 static void recalcTreeState(HTMLImport* root); 133 134 #if !defined(NDEBUG) 135 void show(); 136 void showTree(HTMLImport* highlight, unsigned depth); 137 virtual void showThis(); 138 #endif 139 140 private: 141 HTMLImportState m_state; 142 unsigned m_sync : 1; 143 }; 144 145 } // namespace WebCore 146 147 #endif // HTMLImport_h 148