OLD | NEW |
(Empty) | |
| 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/HTMLImportsController.h" |
| 33 |
| 34 #include "core/dom/Document.h" |
| 35 #include "core/dom/DocumentFragment.h" |
| 36 #include "core/dom/DocumentType.h" |
| 37 #include "core/dom/Range.h" |
| 38 #include "core/html/HTMLDocument.h" |
| 39 #include "core/html/HTMLLinkElement.h" |
| 40 #include "core/loader/cache/CachedResourceLoader.h" |
| 41 #include "core/loader/cache/CachedScript.h" |
| 42 #include "weborigin/SecurityOrigin.h" |
| 43 |
| 44 namespace WebCore { |
| 45 |
| 46 PassRefPtr<LinkImport> LinkImport::create(HTMLLinkElement* owner) |
| 47 { |
| 48 return adoptRef(new LinkImport(owner)); |
| 49 } |
| 50 |
| 51 LinkImport::LinkImport(HTMLLinkElement* owner) |
| 52 : LinkResource(owner) |
| 53 , m_controller(0) |
| 54 , m_ofSameLocation(0) |
| 55 , m_state(StatePreparing) |
| 56 { |
| 57 } |
| 58 |
| 59 LinkImport::~LinkImport() |
| 60 { |
| 61 if (m_resource) |
| 62 m_resource->removeClient(this); |
| 63 } |
| 64 |
| 65 LinkImport::State LinkImport::finish() |
| 66 { |
| 67 if (!m_controller) |
| 68 return StateError; |
| 69 |
| 70 if (m_resource->loadFailedOrCanceled()) |
| 71 return StateError; |
| 72 |
| 73 String error; |
| 74 if (!m_controller->securityOrigin()->canRequest(m_resource->response().url()
) |
| 75 && !m_resource->passesAccessControlCheck(m_controller->securityOrigin(),
error)) { |
| 76 m_controller->showSecurityErrorMessage("Import from origin '" + Security
Origin::create(m_resource->response().url())->toString() + "' has been blocked f
rom loading by Cross-Origin Resource Sharing policy: " + error); |
| 77 return StateError; |
| 78 } |
| 79 |
| 80 // FIXME(morrita): This should be done in incremental way. |
| 81 RefPtr<Document> parsingPlaceholder = HTMLDocument::create(0, KURL()); |
| 82 parsingPlaceholder->setContent(m_resource->script()); |
| 83 |
| 84 // Doctypes cannot be moved between documents. So we remove it before the mi
gration. |
| 85 if (RefPtr<Node> doctype = parsingPlaceholder->doctype()) |
| 86 parsingPlaceholder->removeChild(doctype.get()); |
| 87 m_importedFragment->takeAllChildrenFrom(parsingPlaceholder.get()); |
| 88 |
| 89 return StateReady; |
| 90 } |
| 91 |
| 92 void LinkImport::notifyFinished(CachedResource*) |
| 93 { |
| 94 setState(finish()); |
| 95 } |
| 96 |
| 97 void LinkImport::setState(State state) |
| 98 { |
| 99 if (m_state == state) |
| 100 return; |
| 101 m_state = state; |
| 102 |
| 103 if ((m_state == StateReady || m_state == StateError) |
| 104 && m_controller) |
| 105 m_controller->didLoad(); |
| 106 } |
| 107 |
| 108 LinkImport::State LinkImport::startRequest() |
| 109 { |
| 110 ASSERT(m_owner); |
| 111 ASSERT(m_state == StatePreparing); |
| 112 |
| 113 // FIXME(morrita): Should take care of sub-imports whose document doesn't ha
ve frame. |
| 114 if (!m_owner->document()->frame()) |
| 115 return StateError; |
| 116 |
| 117 LinkRequestBuilder builder(m_owner); |
| 118 if (!builder.isValid()) |
| 119 return StateError; |
| 120 |
| 121 m_controller = m_owner->document()->ensureImports(); |
| 122 if (RefPtr<LinkImport> found = m_controller->findLinkFor(builder.url())) { |
| 123 m_ofSameLocation = found.get(); |
| 124 return StateReady; |
| 125 } |
| 126 |
| 127 CachedResourceRequest request = builder.build(true); |
| 128 m_resource = m_owner->document()->cachedResourceLoader()->requestScript(requ
est); |
| 129 if (!m_resource) |
| 130 return StateError; |
| 131 |
| 132 m_resource->addClient(this); |
| 133 m_url = builder.url(); |
| 134 m_controller->addImport(this); |
| 135 m_importedFragment = m_controller->createDocumentFragment(); |
| 136 |
| 137 return StateStarted; |
| 138 } |
| 139 |
| 140 DocumentFragment* LinkImport::importedFragment() const |
| 141 { |
| 142 if (!m_owner) |
| 143 return 0; |
| 144 if (m_state != StateReady) |
| 145 return 0; |
| 146 |
| 147 if (m_ofSameLocation) { |
| 148 ASSERT(!m_importedFragment); |
| 149 return m_ofSameLocation->importedFragment(); |
| 150 } |
| 151 |
| 152 return m_importedFragment.get(); |
| 153 } |
| 154 |
| 155 void LinkImport::process() |
| 156 { |
| 157 if (StatePreparing != m_state) |
| 158 return; |
| 159 setState(startRequest()); |
| 160 } |
| 161 |
| 162 void LinkImport::ownerRemoved() |
| 163 { |
| 164 m_owner = 0; |
| 165 } |
| 166 |
| 167 void LinkImport::importDestroyed() |
| 168 { |
| 169 m_controller = 0; |
| 170 m_importedFragment.clear(); |
| 171 } |
| 172 |
| 173 PassOwnPtr<HTMLImportsController> HTMLImportsController::create(Document* master
) |
| 174 { |
| 175 return adoptPtr(new HTMLImportsController(master)); |
| 176 } |
| 177 |
| 178 HTMLImportsController::HTMLImportsController(Document* master) |
| 179 : m_master(master) |
| 180 , m_importedFragmentOwner(HTMLDocument::create(0, KURL())) |
| 181 { |
| 182 } |
| 183 |
| 184 HTMLImportsController::~HTMLImportsController() |
| 185 { |
| 186 for (size_t i = 0; i < m_imports.size(); ++i) |
| 187 m_imports[i]->importDestroyed(); |
| 188 } |
| 189 |
| 190 void HTMLImportsController::addImport(PassRefPtr<LinkImport> link) |
| 191 { |
| 192 ASSERT(!link->url().isEmpty() && link->url().isValid()); |
| 193 m_imports.append(link); |
| 194 } |
| 195 |
| 196 void HTMLImportsController::showSecurityErrorMessage(const String& message) |
| 197 { |
| 198 m_master->addConsoleMessage(JSMessageSource, ErrorMessageLevel, message); |
| 199 } |
| 200 |
| 201 void HTMLImportsController::didLoad() |
| 202 { |
| 203 if (haveLoaded()) |
| 204 m_master->didLoadAllImports(); |
| 205 } |
| 206 |
| 207 PassRefPtr<DocumentFragment> HTMLImportsController::createDocumentFragment() con
st |
| 208 { |
| 209 return m_importedFragmentOwner->createDocumentFragment(); |
| 210 } |
| 211 |
| 212 PassRefPtr<LinkImport> HTMLImportsController::findLinkFor(const KURL& url) const |
| 213 { |
| 214 for (size_t i = 0; i < m_imports.size(); ++i) { |
| 215 if (m_imports[i]->url() == url) |
| 216 return m_imports[i]; |
| 217 } |
| 218 |
| 219 return 0; |
| 220 } |
| 221 |
| 222 SecurityOrigin* HTMLImportsController::securityOrigin() const |
| 223 { |
| 224 return m_master->securityOrigin(); |
| 225 } |
| 226 |
| 227 bool HTMLImportsController::haveLoaded() const |
| 228 { |
| 229 for (size_t i = 0; i < m_imports.size(); ++i) { |
| 230 if (!m_imports[i]->isDone()) |
| 231 return false; |
| 232 } |
| 233 |
| 234 return true; |
| 235 } |
| 236 |
| 237 } // namespace WebCore |
OLD | NEW |