15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 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)
31e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#ifndef HTMLImport_h
32e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#define HTMLImport_h
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/html/imports/HTMLImportState.h"
35f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include "platform/heap/Handle.h"
3651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "wtf/TreeNode.h"
37f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
40e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdochclass Document;
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class LocalFrame;
42a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)class HTMLImportChild;
4310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochclass HTMLImportLoader;
44e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdochclass HTMLImportsController;
45a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)class KURL;
46a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
47a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
48a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// # Basic Data Structure and Algorithms of HTML Imports implemenation.
49a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
50a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// ## The Import Tree
51a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// HTML Imports form a tree:
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
54f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// * The root of the tree is HTMLImportTreeRoot.
55f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)//
56f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// * The HTMLImportTreeRoot is owned HTMLImportsController, which is owned by the master
576f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch//   document as a DocumentSupplement.
58a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
59f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// * The non-root nodes are HTMLImportChild. They are all owned by HTMLImporTreeRoot.
60a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//   LinkStyle is wired into HTMLImportChild by implementing HTMLImportChildClient interface
61a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
62f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// * Both HTMLImportTreeRoot and HTMLImportChild are derived from HTMLImport superclass
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//   that models the tree data structure using WTF::TreeNode and provides a set of
64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//   virtual functions.
65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// HTMLImportsController also owns all loaders in the tree and manages their lifetime through it.
67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// One assumption is that the tree is append-only and nodes are never inserted in the middle of the tree nor removed.
68a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
69f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// Full diagram is here:
70f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// https://docs.google.com/drawings/d/1jFQrO0IupWrlykTNzQ3Nv2SdiBiSz4UE9-V3-vDgBb0/
71a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// # Import Sharing and HTMLImportLoader
73a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
74a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// The HTML Imports spec calls for de-dup mechanism to share already loaded imports.
75a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// To implement this, the actual loading machinery is split out from HTMLImportChild to
7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// HTMLImportLoader, and each loader shares HTMLImportLoader with other loader if the URL is same.
77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// Check around HTMLImportsController::findLink() for more detail.
78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
7907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch// HTMLImportLoader can be shared by multiple imports.
80a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)//    HTMLImportChild (1)-->(*) HTMLImportLoader
82a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
83a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
84a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// # Script Blocking
85a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// - An import blocks the HTML parser of its own imported document from running <script>
8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)//   until all of its children are loaded.
8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)//   Note that dynamically added import won't block the parser.
89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
9009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// - An import under loading also blocks imported documents that follow from being created.
9109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)//   This is because an import can include another import that has same URLs of following ones.
9209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)//   In such case, the preceding import should be loaded and following ones should be de-duped.
93a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)//
94a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
95f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)// The superclass of HTMLImportTreeRoot and HTMLImportChild
96a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// This represents the import tree data structure.
97f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)class HTMLImport : public NoBaseWillBeGarbageCollectedFinalized<HTMLImport>, public TreeNode<HTMLImport> {
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
9907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    enum SyncMode {
10007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        Sync  = 0,
10107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        Async = 1
10207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    };
10307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
104e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    virtual ~HTMLImport() { }
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
106f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    // FIXME: Consider returning HTMLImportTreeRoot.
1076f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    HTMLImport* root();
10810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    bool precedes(HTMLImport*);
109f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    bool isRoot() const { return !parent(); }
11007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    bool isSync() const { return SyncMode(m_sync) == Sync; }
111323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    bool formsCycle() const;
11209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    const HTMLImportState& state() const { return m_state; }
113a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
11410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    void appendImport(HTMLImport*);
115f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
116f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    virtual Document* document() const = 0;
11709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool isDone() const = 0; // FIXME: Should be renamed to haveFinishedLoading()
11810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    virtual HTMLImportLoader* loader() const { return 0; }
1196f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    virtual void stateWillChange() { }
12009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void stateDidChange();
121f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
122f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    virtual void trace(Visitor*) { }
123f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
124f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)protected:
12509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // Stating from most conservative state.
12609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // It will be corrected through state update flow.
12707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    explicit HTMLImport(SyncMode sync)
128d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        : m_sync(sync)
129f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    { }
130f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
13109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    static void recalcTreeState(HTMLImport* root);
132a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
13309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#if !defined(NDEBUG)
13409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    void show();
13509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    void showTree(HTMLImport* highlight, unsigned depth);
13609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void showThis();
13709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#endif
138f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
13909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)private:
14009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    HTMLImportState m_state;
14107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    unsigned m_sync : 1;
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
144c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
146e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#endif // HTMLImport_h
147