Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: third_party/WebKit/Source/core/dom/ScriptLoader.cpp

Issue 2555653002: [WIP Prototype] ES6 https://html.spec.whatwg.org/#fetch-a-single-module-script implementation (Closed)
Patch Set: snapshot Dec19 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights
6 * reserved. 6 * reserved.
7 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> 7 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either 11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version. 12 * version 2 of the License, or (at your option) any later version.
13 * 13 *
14 * This library is distributed in the hope that it will be useful, 14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details. 17 * Library General Public License for more details.
18 * 18 *
19 * You should have received a copy of the GNU Library General Public License 19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to 20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA. 22 * Boston, MA 02110-1301, USA.
23 */ 23 */
24 24
25 #include "core/dom/ScriptLoader.h" 25 #include "core/dom/ScriptLoader.h"
26 26
27 #include "bindings/core/v8/ScriptController.h" 27 #include "bindings/core/v8/ScriptController.h"
28 #include "bindings/core/v8/ScriptSourceCode.h" 28 #include "bindings/core/v8/ScriptSourceCode.h"
29 #include "bindings/core/v8/ScriptState.h"
29 #include "core/HTMLNames.h" 30 #include "core/HTMLNames.h"
30 #include "core/SVGNames.h" 31 #include "core/SVGNames.h"
31 #include "core/dom/Document.h" 32 #include "core/dom/Document.h"
32 #include "core/dom/DocumentParserTiming.h" 33 #include "core/dom/DocumentParserTiming.h"
33 #include "core/dom/IgnoreDestructiveWriteCountIncrementer.h" 34 #include "core/dom/IgnoreDestructiveWriteCountIncrementer.h"
35 #include "core/dom/ModuleMap.h"
34 #include "core/dom/ScriptLoaderClient.h" 36 #include "core/dom/ScriptLoaderClient.h"
35 #include "core/dom/ScriptRunner.h" 37 #include "core/dom/ScriptRunner.h"
36 #include "core/dom/ScriptableDocumentParser.h" 38 #include "core/dom/ScriptableDocumentParser.h"
37 #include "core/dom/Text.h" 39 #include "core/dom/Text.h"
38 #include "core/events/Event.h" 40 #include "core/events/Event.h"
39 #include "core/fetch/AccessControlStatus.h" 41 #include "core/fetch/AccessControlStatus.h"
40 #include "core/fetch/FetchRequest.h" 42 #include "core/fetch/FetchRequest.h"
41 #include "core/fetch/MemoryCache.h" 43 #include "core/fetch/MemoryCache.h"
42 #include "core/fetch/ResourceFetcher.h" 44 #include "core/fetch/ResourceFetcher.h"
43 #include "core/frame/LocalFrame.h" 45 #include "core/frame/LocalFrame.h"
44 #include "core/frame/SubresourceIntegrity.h" 46 #include "core/frame/SubresourceIntegrity.h"
45 #include "core/frame/UseCounter.h" 47 #include "core/frame/UseCounter.h"
46 #include "core/frame/csp/ContentSecurityPolicy.h" 48 #include "core/frame/csp/ContentSecurityPolicy.h"
47 #include "core/html/CrossOriginAttribute.h" 49 #include "core/html/CrossOriginAttribute.h"
48 #include "core/html/HTMLScriptElement.h" 50 #include "core/html/HTMLScriptElement.h"
49 #include "core/html/imports/HTMLImport.h" 51 #include "core/html/imports/HTMLImport.h"
50 #include "core/html/parser/HTMLParserIdioms.h" 52 #include "core/html/parser/HTMLParserIdioms.h"
51 #include "core/inspector/ConsoleMessage.h" 53 #include "core/inspector/ConsoleMessage.h"
52 #include "core/svg/SVGScriptElement.h" 54 #include "core/svg/SVGScriptElement.h"
53 #include "platform/WebFrameScheduler.h" 55 #include "platform/WebFrameScheduler.h"
54 #include "platform/network/mime/MIMETypeRegistry.h" 56 #include "platform/network/mime/MIMETypeRegistry.h"
55 #include "platform/weborigin/SecurityOrigin.h" 57 #include "platform/weborigin/SecurityOrigin.h"
56 #include "public/platform/WebCachePolicy.h" 58 #include "public/platform/WebCachePolicy.h"
57 #include "wtf/StdLibExtras.h" 59 #include "wtf/StdLibExtras.h"
58 #include "wtf/text/StringBuilder.h" 60 #include "wtf/text/StringBuilder.h"
59 #include "wtf/text/StringHash.h" 61 #include "wtf/text/StringHash.h"
60 62
61 namespace blink { 63 namespace blink {
62 64
65 class ScriptElementModuleClient
66 : public GarbageCollectedFinalized<ScriptElementModuleClient>,
67 public ModuleMap::Client {
68 USING_GARBAGE_COLLECTED_MIXIN(ScriptElementModuleClient);
69
70 public:
71 static ScriptElementModuleClient* create(Element* element) {
72 return new ScriptElementModuleClient(element);
73 }
74 virtual ~ScriptElementModuleClient() = default;
75
76 DECLARE_TRACE();
77
78 private:
79 ScriptElementModuleClient(Element* element) : m_element(element) {}
80
81 // Implements ModuleMap::Client
82 void notifyFinished(ScriptModule) override;
83
84 Member<Element> m_element;
85 };
86
87 void ScriptElementModuleClient::notifyFinished(ScriptModule module) {
88 // The code below is entirely wrong o(`ω´*)o
89 // TODO(kouhei? dominicc?): Fix this as a part of implementing "fetch a module
90 // script graph" algorithm.
91 if (module.isNull()) {
92 printf("ScriptElementModuleClient::notifyFinished: load has failed\n");
93 return;
94 }
95 printf("ScriptElementModuleClient::notifyFinished: load successful\n");
96
97 Document* contextDocument = m_element->document().contextDocument();
98 DCHECK(contextDocument);
99
100 // Below should belong to ScriptController.cpp
101 LocalFrame* frame = contextDocument->frame();
102 if (!frame)
103 return;
104 ScriptState* scriptState =
105 ScriptState::forMainWorld(contextDocument->frame());
106 DCHECK(scriptState);
107 ScriptState::Scope scope(scriptState);
108
109 CHECK(module.instantiate(scriptState));
110 module.evaluate(scriptState);
111 }
112
113 DEFINE_TRACE(ScriptElementModuleClient) {
114 visitor->trace(m_element);
115 ModuleMap::Client::trace(visitor);
116 }
117
63 ScriptLoader::ScriptLoader(Element* element, 118 ScriptLoader::ScriptLoader(Element* element,
64 bool parserInserted, 119 bool parserInserted,
65 bool alreadyStarted, 120 bool alreadyStarted,
66 bool createdDuringDocumentWrite) 121 bool createdDuringDocumentWrite)
67 : m_element(element), 122 : m_element(element),
68 m_startLineNumber(WTF::OrdinalNumber::beforeFirst()), 123 m_startLineNumber(WTF::OrdinalNumber::beforeFirst()),
69 m_parserInserted(parserInserted), 124 m_parserInserted(parserInserted),
70 m_isExternalScript(false), 125 m_isExternalScript(false),
71 m_alreadyStarted(alreadyStarted), 126 m_alreadyStarted(alreadyStarted),
72 m_haveFiredLoad(false), 127 m_haveFiredLoad(false),
(...skipping 10 matching lines...) Expand all
83 !element->document().isInDocumentWrite()) 138 !element->document().isInDocumentWrite())
84 m_startLineNumber = 139 m_startLineNumber =
85 element->document().scriptableDocumentParser()->lineNumber(); 140 element->document().scriptableDocumentParser()->lineNumber();
86 } 141 }
87 142
88 ScriptLoader::~ScriptLoader() {} 143 ScriptLoader::~ScriptLoader() {}
89 144
90 DEFINE_TRACE(ScriptLoader) { 145 DEFINE_TRACE(ScriptLoader) {
91 visitor->trace(m_element); 146 visitor->trace(m_element);
92 visitor->trace(m_resource); 147 visitor->trace(m_resource);
148 visitor->trace(m_elementModuleClient);
93 visitor->trace(m_pendingScript); 149 visitor->trace(m_pendingScript);
94 PendingScriptClient::trace(visitor); 150 PendingScriptClient::trace(visitor);
95 } 151 }
96 152
97 void ScriptLoader::setFetchDocWrittenScriptDeferIdle() { 153 void ScriptLoader::setFetchDocWrittenScriptDeferIdle() {
98 DCHECK(!m_createdDuringDocumentWrite); 154 DCHECK(!m_createdDuringDocumentWrite);
99 m_documentWriteIntervention = 155 m_documentWriteIntervention =
100 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle; 156 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle;
101 } 157 }
102 158
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 m_characterEncoding = elementDocument.characterSet(); 309 m_characterEncoding = elementDocument.characterSet();
254 310
255 if (client->hasSourceAttribute()) { 311 if (client->hasSourceAttribute()) {
256 FetchRequest::DeferOption defer = FetchRequest::NoDefer; 312 FetchRequest::DeferOption defer = FetchRequest::NoDefer;
257 if (!m_parserInserted || client->asyncAttributeValue() || 313 if (!m_parserInserted || client->asyncAttributeValue() ||
258 client->deferAttributeValue()) 314 client->deferAttributeValue())
259 defer = FetchRequest::LazyLoad; 315 defer = FetchRequest::LazyLoad;
260 if (m_documentWriteIntervention == 316 if (m_documentWriteIntervention ==
261 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) 317 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle)
262 defer = FetchRequest::IdleLoad; 318 defer = FetchRequest::IdleLoad;
319
263 if (!fetchScript(client->sourceAttributeValue(), defer)) 320 if (!fetchScript(client->sourceAttributeValue(), defer))
264 return false; 321 return false;
265 } 322 }
266 323
267 // Since the asynchronous, low priority fetch for doc.written blocked 324 // Since the asynchronous, low priority fetch for doc.written blocked
268 // script is not for execution, return early from here. Watch for its 325 // script is not for execution, return early from here. Watch for its
269 // completion to be able to remove it from the memory cache. 326 // completion to be able to remove it from the memory cache.
270 if (m_documentWriteIntervention == 327 if (m_documentWriteIntervention ==
271 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) { 328 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) {
272 m_pendingScript = PendingScript::create(m_element, m_resource.get()); 329 m_pendingScript = PendingScript::create(m_element, m_resource.get());
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 385
329 bool ScriptLoader::fetchScript(const String& sourceUrl, 386 bool ScriptLoader::fetchScript(const String& sourceUrl,
330 FetchRequest::DeferOption defer) { 387 FetchRequest::DeferOption defer) {
331 DCHECK(m_element); 388 DCHECK(m_element);
332 389
333 Document* elementDocument = &(m_element->document()); 390 Document* elementDocument = &(m_element->document());
334 if (!m_element->isConnected() || m_element->document() != elementDocument) 391 if (!m_element->isConnected() || m_element->document() != elementDocument)
335 return false; 392 return false;
336 393
337 DCHECK(!m_resource); 394 DCHECK(!m_resource);
395
338 if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) { 396 if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) {
339 FetchRequest request( 397 KURL url = elementDocument->completeURL(sourceUrl);
340 ResourceRequest(elementDocument->completeURL(sourceUrl)), 398
341 m_element->localName()); 399 if (RuntimeEnabledFeatures::moduleScriptsEnabled() &&
400 client()->typeAttributeValue() == "module") {
401 m_elementModuleClient = ScriptElementModuleClient::create(m_element);
402
403 // TODO(kouhei,dominicc): Below should trigger "fetch a module script
404 // graph", not "fetch a single module script" algorithm.
405 elementDocument->ensureModuleMap()->fetchSingle(url,
406 m_elementModuleClient);
407 return true;
408 }
409
410 FetchRequest request(ResourceRequest(url), m_element->localName());
342 411
343 CrossOriginAttributeValue crossOrigin = crossOriginAttributeValue( 412 CrossOriginAttributeValue crossOrigin = crossOriginAttributeValue(
344 m_element->fastGetAttribute(HTMLNames::crossoriginAttr)); 413 m_element->fastGetAttribute(HTMLNames::crossoriginAttr));
345 if (crossOrigin != CrossOriginAttributeNotSet) 414 if (crossOrigin != CrossOriginAttributeNotSet)
346 request.setCrossOriginAccessControl(elementDocument->getSecurityOrigin(), 415 request.setCrossOriginAccessControl(elementDocument->getSecurityOrigin(),
347 crossOrigin); 416 crossOrigin);
348 request.setCharset(scriptCharset()); 417 request.setCharset(scriptCharset());
349 418
350 if (ContentSecurityPolicy::isNonceableElement(m_element.get())) { 419 if (ContentSecurityPolicy::isNonceableElement(m_element.get())) {
351 request.setContentSecurityPolicyNonce( 420 request.setContentSecurityPolicyNonce(
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 if (isHTMLScriptLoader(element)) 716 if (isHTMLScriptLoader(element))
648 return toHTMLScriptElement(element)->loader(); 717 return toHTMLScriptElement(element)->loader();
649 718
650 if (isSVGScriptLoader(element)) 719 if (isSVGScriptLoader(element))
651 return toSVGScriptElement(element)->loader(); 720 return toSVGScriptElement(element)->loader();
652 721
653 return 0; 722 return 0;
654 } 723 }
655 724
656 } // namespace blink 725 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698