2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/html/imports/HTMLImportsController.h"
34 #include "core/dom/Document.h"
35 #include "core/fetch/ResourceFetcher.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/frame/UseCounter.h"
38 #include "core/html/imports/HTMLImportChild.h"
39 #include "core/html/imports/HTMLImportChildClient.h"
40 #include "core/html/imports/HTMLImportLoader.h"
41 #include "core/html/imports/HTMLImportTreeRoot.h"
45 const char* HTMLImportsController::supplementName()
47 DEFINE_STATIC_LOCAL(const char*, name, ("HTMLImportsController"));
51 void HTMLImportsController::provideTo(Document& master)
53 OwnPtrWillBeRawPtr<HTMLImportsController> controller = adoptPtrWillBeNoop(new HTMLImportsController(master));
54 master.setImportsController(controller.get());
55 DocumentSupplement::provideTo(master, supplementName(), controller.release());
58 void HTMLImportsController::removeFrom(Document& master)
60 static_cast<DocumentSupplementable&>(master).removeSupplement(supplementName());
61 master.setImportsController(nullptr);
64 HTMLImportsController::HTMLImportsController(Document& master)
65 : m_root(HTMLImportTreeRoot::create(&master))
67 UseCounter::count(master, UseCounter::HTMLImports);
70 HTMLImportsController::~HTMLImportsController()
75 for (size_t i = 0; i < m_loaders.size(); ++i)
76 m_loaders[i]->importDestroyed();
81 static bool makesCycle(HTMLImport* parent, const KURL& url)
83 for (HTMLImport* ancestor = parent; ancestor; ancestor = ancestor->parent()) {
84 if (!ancestor->isRoot() && equalIgnoringFragmentIdentifier(toHTMLImportChild(parent)->url(), url))
91 HTMLImportChild* HTMLImportsController::createChild(const KURL& url, HTMLImportLoader* loader, HTMLImport* parent, HTMLImportChildClient* client)
93 HTMLImport::SyncMode mode = client->isSync() && !makesCycle(parent, url) ? HTMLImport::Sync : HTMLImport::Async;
94 if (mode == HTMLImport::Async)
95 UseCounter::count(root()->document(), UseCounter::HTMLImportsAsyncAttribute);
97 OwnPtrWillBeRawPtr<HTMLImportChild> child = adoptPtrWillBeNoop(new HTMLImportChild(url, loader, mode));
98 child->setClient(client);
99 parent->appendImport(child.get());
100 loader->addImport(child.get());
101 return root()->add(child.release());
104 HTMLImportChild* HTMLImportsController::load(HTMLImport* parent, HTMLImportChildClient* client, FetchRequest request)
106 ASSERT(!request.url().isEmpty() && request.url().isValid());
107 ASSERT(parent == root() || toHTMLImportChild(parent)->loader()->isFirstImport(toHTMLImportChild(parent)));
109 if (HTMLImportChild* childToShareWith = root()->find(request.url())) {
110 HTMLImportLoader* loader = childToShareWith->loader();
112 HTMLImportChild* child = createChild(request.url(), loader, parent, client);
113 child->didShareLoader();
117 bool sameOriginRequest = master()->securityOrigin()->canRequest(request.url());
118 request.setCrossOriginAccessControl(
119 master()->securityOrigin(), sameOriginRequest ? AllowStoredCredentials : DoNotAllowStoredCredentials,
120 ClientDidNotRequestCredentials);
121 ResourcePtr<RawResource> resource = parent->document()->fetcher()->fetchImport(request);
125 HTMLImportLoader* loader = createLoader();
126 HTMLImportChild* child = createChild(request.url(), loader, parent, client);
127 // We set resource after the import tree is built since
128 // Resource::addClient() immediately calls back to feed the bytes when the resource is cached.
129 loader->startLoading(resource);
130 child->didStartLoading();
134 Document* HTMLImportsController::master() const
136 return root()->document();
139 bool HTMLImportsController::shouldBlockScriptExecution(const Document& document) const
141 ASSERT(document.importsController() == this);
142 if (HTMLImportLoader* loader = loaderFor(document))
143 return loader->shouldBlockScriptExecution();
144 return root()->state().shouldBlockScriptExecution();
147 HTMLImportLoader* HTMLImportsController::createLoader()
149 m_loaders.append(HTMLImportLoader::create(this));
150 return m_loaders.last().get();
153 HTMLImportLoader* HTMLImportsController::loaderFor(const Document& document) const
155 for (size_t i = 0; i < m_loaders.size(); ++i) {
156 if (m_loaders[i]->document() == &document)
157 return m_loaders[i].get();
163 Document* HTMLImportsController::loaderDocumentAt(size_t i) const
165 return loaderAt(i)->document();
168 void HTMLImportsController::trace(Visitor* visitor)
170 visitor->trace(m_root);
171 visitor->trace(m_loaders);
172 DocumentSupplement::trace(visitor);