Chromium Code Reviews| Index: Source/core/html/HTMLImports.cpp |
| diff --git a/Source/core/html/HTMLImports.cpp b/Source/core/html/HTMLImports.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f3c57ef3d83bfb015e15c4874465016812de9dc9 |
| --- /dev/null |
| +++ b/Source/core/html/HTMLImports.cpp |
| @@ -0,0 +1,216 @@ |
| +/* |
| + * Copyright (C) 2013 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "core/html/HTMLImports.h" |
| + |
| +#include "core/dom/DocumentFragment.h" |
| +#include "core/dom/Range.h" |
| +#include "core/html/HTMLDocument.h" |
| +#include "core/html/HTMLLinkElement.h" |
| +#include "core/loader/cache/CachedResourceLoader.h" |
| +#include "weborigin/SecurityOrigin.h" |
| + |
| +namespace WebCore { |
| + |
| +PassRefPtr<LinkImport> LinkImport::create(HTMLLinkElement* owner) |
| +{ |
| + return adoptRef(new LinkImport(owner)); |
| +} |
| + |
| +LinkImport::LinkImport(HTMLLinkElement* owner) |
| + : LinkResource(owner) |
| + , m_imports(0) |
| + , m_ofSameLocation(0) |
| + , m_state(StatePreparing) |
| +{ |
| +} |
| + |
| +LinkImport::~LinkImport() |
| +{ |
| + if (m_cachedDocument) |
| + m_cachedDocument->removeClient(this); |
| +} |
| + |
| +LinkImport::State LinkImport::finish() |
| +{ |
| + if (!m_imports || !m_cachedDocument->document()) |
| + return StateDone; |
| + |
| + if (!m_imports->securityOrigin()->canRequest(m_cachedDocument->response().url()) |
| + && !m_cachedDocument->passesAccessControlCheck(m_imports->securityOrigin())) |
| + return StateDone; |
| + |
| + m_importedFragment = m_imports->createFragmentFrom(m_cachedDocument->document()); |
| + return StateDone; |
| +} |
| + |
| +void LinkImport::notifyFinished(CachedResource*) |
| +{ |
| + setState(finish()); |
| +} |
| + |
| +void LinkImport::setState(State state) |
| +{ |
| + if (m_state == state) |
| + return; |
| + m_state = state; |
| + if (m_state == StateDone && m_imports) |
| + m_imports->didLoad(); |
| +} |
| + |
| +LinkImport::State LinkImport::startRequest() |
| +{ |
| + ASSERT(m_owner); |
| + ASSERT(m_state == StatePreparing); |
| + |
| + // FIXME(morrita): Should take care of sub-imports whose document doesn't have frame. |
| + if (!m_owner->document()->frame()) |
| + return StateDone; |
| + |
| + LinkRequestBuilder builder(m_owner); |
| + if (!builder.isValid()) |
| + return StateDone; |
| + |
| + m_imports = m_owner->document()->imports(); |
| + if (RefPtr<LinkImport> found = m_imports->findLinkFor(builder.url())) { |
| + m_ofSameLocation = found.get(); |
| + return StateDone; |
| + } |
| + |
| + // XXX: take care of CORS and cookies |
| + CachedResourceRequest request = builder.build(true); |
| + m_cachedDocument = m_owner->document()->cachedResourceLoader()->requestHTMLDocument(request); |
|
dglazkov
2013/05/23 18:32:00
I don't know if we need to be creating a new docum
Hajime Morrita
2013/05/24 02:28:27
That's what I've been wondering, too. There are so
dglazkov
2013/05/24 03:15:16
Can you explain why?
|
| + if (!m_cachedDocument) |
| + return StateDone; |
| + |
| + m_cachedDocument->addClient(this); |
| + m_url = builder.url(); |
| + m_imports->addLink(this); |
| + |
| + return StateStarted; |
| +} |
| + |
| +DocumentFragment* LinkImport::importedFragment() const |
| +{ |
| + if (!m_owner) |
| + return 0; |
| + |
| + if (m_ofSameLocation) { |
| + ASSERT(!m_importedFragment); |
| + return m_ofSameLocation->importedFragment(); |
| + } |
| + |
| + return m_importedFragment.get(); |
| +} |
| + |
| +void LinkImport::process() |
| +{ |
| + if (StatePreparing != m_state) |
| + return; |
| + setState(startRequest()); |
| +} |
| + |
| +void LinkImport::ownerRemoved() |
| +{ |
| + m_owner = 0; |
| +} |
| + |
| +void LinkImport::importDestroyed() |
| +{ |
| + m_imports = 0; |
| + m_importedFragment.clear(); |
| +} |
| + |
| +PassOwnPtr<HTMLImports> HTMLImports::create(HTMLImportsHost* host) |
| +{ |
| + return adoptPtr(new HTMLImports(host)); |
| +} |
| + |
| +HTMLImports::HTMLImports(HTMLImportsHost* host) |
| + : m_host(host) |
| + , m_owner(HTMLDocument::create(0, KURL())) |
| +{ |
| +} |
| + |
| +HTMLImports::~HTMLImports() |
| +{ |
| + for (size_t i = 0; i < m_links.size(); ++i) |
| + m_links[i]->importDestroyed(); |
| +} |
| + |
| +void HTMLImports::addLink(PassRefPtr<LinkImport> link) |
| +{ |
| + ASSERT(!link->url().isEmpty() && link->url().isValid()); |
| + m_links.append(link); |
| +} |
| + |
| +void HTMLImports::didLoad() |
| +{ |
| + if (haveLoaded()) |
| + m_host->didLoadAllImports(); |
| +} |
| + |
| +PassRefPtr<DocumentFragment> HTMLImports::createFragmentFrom(Document* source) const |
| +{ |
| + RefPtr<Range> toClone = Range::create(source); |
| + toClone->setStartBefore(source->documentElement(), ASSERT_NO_EXCEPTION); |
| + toClone->setEndAfter(source->documentElement(), ASSERT_NO_EXCEPTION); |
| + RefPtr<DocumentFragment> created = toClone->cloneContents(ASSERT_NO_EXCEPTION); |
| + m_owner->adoptNode(created, ASSERT_NO_EXCEPTION); |
| + return created.release(); |
| +} |
| + |
| +PassRefPtr<LinkImport> HTMLImports::findLinkFor(const KURL& url) const |
| +{ |
| + for (size_t i = 0; i < m_links.size(); ++i) { |
| + if (m_links[i]->url() == url) |
| + return m_links[i]; |
| + } |
| + |
| + return 0; |
| +} |
| + |
| +SecurityOrigin* HTMLImports::securityOrigin() const |
| +{ |
| + return m_host->importsContext()->securityOrigin(); |
| +} |
| + |
| +bool HTMLImports::haveLoaded() const |
| +{ |
| + for (size_t i = 0; i < m_links.size(); ++i) { |
| + if (!m_links[i]->isDone()) |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace WebCore |