Home | History | Annotate | Download | only in imports
      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/imports/HTMLImportsController.h"
     33 
     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"
     42 
     43 namespace blink {
     44 
     45 const char* HTMLImportsController::supplementName()
     46 {
     47     DEFINE_STATIC_LOCAL(const char*, name, ("HTMLImportsController"));
     48     return name;
     49 }
     50 
     51 void HTMLImportsController::provideTo(Document& master)
     52 {
     53     OwnPtrWillBeRawPtr<HTMLImportsController> controller = adoptPtrWillBeNoop(new HTMLImportsController(master));
     54     master.setImportsController(controller.get());
     55     DocumentSupplement::provideTo(master, supplementName(), controller.release());
     56 }
     57 
     58 void HTMLImportsController::removeFrom(Document& master)
     59 {
     60     static_cast<DocumentSupplementable&>(master).removeSupplement(supplementName());
     61     master.setImportsController(nullptr);
     62 }
     63 
     64 HTMLImportsController::HTMLImportsController(Document& master)
     65     : m_root(HTMLImportTreeRoot::create(&master))
     66 {
     67     UseCounter::count(master, UseCounter::HTMLImports);
     68 }
     69 
     70 HTMLImportsController::~HTMLImportsController()
     71 {
     72 #if !ENABLE(OILPAN)
     73     m_root.clear();
     74 
     75     for (size_t i = 0; i < m_loaders.size(); ++i)
     76         m_loaders[i]->importDestroyed();
     77     m_loaders.clear();
     78 #endif
     79 }
     80 
     81 static bool makesCycle(HTMLImport* parent, const KURL& url)
     82 {
     83     for (HTMLImport* ancestor = parent; ancestor; ancestor = ancestor->parent()) {
     84         if (!ancestor->isRoot() && equalIgnoringFragmentIdentifier(toHTMLImportChild(parent)->url(), url))
     85             return true;
     86     }
     87 
     88     return false;
     89 }
     90 
     91 HTMLImportChild* HTMLImportsController::createChild(const KURL& url, HTMLImportLoader* loader, HTMLImport* parent, HTMLImportChildClient* client)
     92 {
     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);
     96 
     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());
    102 }
    103 
    104 HTMLImportChild* HTMLImportsController::load(HTMLImport* parent, HTMLImportChildClient* client, FetchRequest request)
    105 {
    106     ASSERT(!request.url().isEmpty() && request.url().isValid());
    107     ASSERT(parent == root() || toHTMLImportChild(parent)->loader()->isFirstImport(toHTMLImportChild(parent)));
    108 
    109     if (HTMLImportChild* childToShareWith = root()->find(request.url())) {
    110         HTMLImportLoader* loader = childToShareWith->loader();
    111         ASSERT(loader);
    112         HTMLImportChild* child = createChild(request.url(), loader, parent, client);
    113         child->didShareLoader();
    114         return child;
    115     }
    116 
    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);
    122     if (!resource)
    123         return 0;
    124 
    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();
    131     return child;
    132 }
    133 
    134 Document* HTMLImportsController::master() const
    135 {
    136     return root()->document();
    137 }
    138 
    139 bool HTMLImportsController::shouldBlockScriptExecution(const Document& document) const
    140 {
    141     ASSERT(document.importsController() == this);
    142     if (HTMLImportLoader* loader = loaderFor(document))
    143         return loader->shouldBlockScriptExecution();
    144     return root()->state().shouldBlockScriptExecution();
    145 }
    146 
    147 HTMLImportLoader* HTMLImportsController::createLoader()
    148 {
    149     m_loaders.append(HTMLImportLoader::create(this));
    150     return m_loaders.last().get();
    151 }
    152 
    153 HTMLImportLoader* HTMLImportsController::loaderFor(const Document& document) const
    154 {
    155     for (size_t i = 0; i < m_loaders.size(); ++i) {
    156         if (m_loaders[i]->document() == &document)
    157             return m_loaders[i].get();
    158     }
    159 
    160     return 0;
    161 }
    162 
    163 Document* HTMLImportsController::loaderDocumentAt(size_t i) const
    164 {
    165     return loaderAt(i)->document();
    166 }
    167 
    168 void HTMLImportsController::trace(Visitor* visitor)
    169 {
    170     visitor->trace(m_root);
    171     visitor->trace(m_loaders);
    172     DocumentSupplement::trace(visitor);
    173 }
    174 
    175 } // namespace blink
    176