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

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: rebased Created 3 years, 8 months 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/Modulator.h"
36 #include "core/dom/ModuleScript.h"
34 #include "core/dom/ScriptElementBase.h" 37 #include "core/dom/ScriptElementBase.h"
35 #include "core/dom/ScriptRunner.h" 38 #include "core/dom/ScriptRunner.h"
36 #include "core/dom/ScriptableDocumentParser.h" 39 #include "core/dom/ScriptableDocumentParser.h"
37 #include "core/dom/Text.h" 40 #include "core/dom/Text.h"
38 #include "core/events/Event.h" 41 #include "core/events/Event.h"
39 #include "core/frame/LocalFrame.h" 42 #include "core/frame/LocalFrame.h"
40 #include "core/frame/SubresourceIntegrity.h" 43 #include "core/frame/SubresourceIntegrity.h"
41 #include "core/frame/UseCounter.h" 44 #include "core/frame/UseCounter.h"
42 #include "core/frame/csp/ContentSecurityPolicy.h" 45 #include "core/frame/csp/ContentSecurityPolicy.h"
43 #include "core/html/CrossOriginAttribute.h" 46 #include "core/html/CrossOriginAttribute.h"
44 #include "core/html/imports/HTMLImport.h" 47 #include "core/html/imports/HTMLImport.h"
45 #include "core/html/parser/HTMLParserIdioms.h" 48 #include "core/html/parser/HTMLParserIdioms.h"
46 #include "core/inspector/ConsoleMessage.h" 49 #include "core/inspector/ConsoleMessage.h"
50 #include "core/loader/modulescript/ModuleScriptFetchRequest.h"
47 #include "platform/WebFrameScheduler.h" 51 #include "platform/WebFrameScheduler.h"
48 #include "platform/loader/fetch/AccessControlStatus.h" 52 #include "platform/loader/fetch/AccessControlStatus.h"
49 #include "platform/loader/fetch/FetchRequest.h" 53 #include "platform/loader/fetch/FetchRequest.h"
50 #include "platform/loader/fetch/MemoryCache.h" 54 #include "platform/loader/fetch/MemoryCache.h"
51 #include "platform/loader/fetch/ResourceFetcher.h" 55 #include "platform/loader/fetch/ResourceFetcher.h"
52 #include "platform/network/mime/MIMETypeRegistry.h" 56 #include "platform/network/mime/MIMETypeRegistry.h"
53 #include "platform/weborigin/SecurityOrigin.h" 57 #include "platform/weborigin/SecurityOrigin.h"
54 #include "public/platform/WebCachePolicy.h" 58 #include "public/platform/WebCachePolicy.h"
55 #include "wtf/StdLibExtras.h" 59 #include "wtf/StdLibExtras.h"
56 #include "wtf/text/StringBuilder.h" 60 #include "wtf/text/StringBuilder.h"
57 #include "wtf/text/StringHash.h" 61 #include "wtf/text/StringHash.h"
58 62
59 namespace blink { 63 namespace blink {
60 64
65 class ScriptElementModuleClient
66 : public GarbageCollectedFinalized<ScriptElementModuleClient>,
67 public ModuleTreeClient {
68 USING_GARBAGE_COLLECTED_MIXIN(ScriptElementModuleClient);
69
70 public:
71 static ScriptElementModuleClient* create(LocalFrame* frame) {
72 return new ScriptElementModuleClient(frame);
73 }
74 virtual ~ScriptElementModuleClient() = default;
75
76 DECLARE_TRACE();
77
78 private:
79 ScriptElementModuleClient(LocalFrame* frame) : m_frame(frame) {}
80
81 // Implements ModuleTreeClient
82 void notifyModuleTreeLoadFinished(ModuleScript*) override;
83
84 Member<LocalFrame> m_frame;
85 };
86
87 void ScriptElementModuleClient::notifyModuleTreeLoadFinished(
88 ModuleScript* moduleScript) {
89 printf("notifyFinishedModuleTree!!! moduleScript: %p\n", moduleScript);
90 if (!moduleScript)
91 return;
92 DCHECK_EQ(moduleScript->instantiationState(),
93 ModuleInstantiationState::Instantiated);
94
95 // TODO(kouhei): Actually we should just return w/ the prepared script here.
96 // In this prototype, we will execute the module immediately just for fun. :)
97 Modulator::from(m_frame.get())->executeModule(moduleScript->record());
98 }
99
100 DEFINE_TRACE(ScriptElementModuleClient) {
101 visitor->trace(m_frame);
102 ModuleTreeClient::trace(visitor);
103 }
104
61 ScriptLoader::ScriptLoader(ScriptElementBase* element, 105 ScriptLoader::ScriptLoader(ScriptElementBase* element,
62 bool parserInserted, 106 bool parserInserted,
63 bool alreadyStarted, 107 bool alreadyStarted,
64 bool createdDuringDocumentWrite) 108 bool createdDuringDocumentWrite)
65 : m_element(element), 109 : m_element(element),
66 m_startLineNumber(WTF::OrdinalNumber::beforeFirst()), 110 m_startLineNumber(WTF::OrdinalNumber::beforeFirst()),
67 m_haveFiredLoad(false), 111 m_haveFiredLoad(false),
68 m_willBeParserExecuted(false), 112 m_willBeParserExecuted(false),
69 m_willExecuteWhenDocumentFinishedParsing(false), 113 m_willExecuteWhenDocumentFinishedParsing(false),
70 m_createdDuringDocumentWrite(createdDuringDocumentWrite), 114 m_createdDuringDocumentWrite(createdDuringDocumentWrite),
(...skipping 26 matching lines...) Expand all
97 m_startLineNumber = 141 m_startLineNumber =
98 m_element->document().scriptableDocumentParser()->lineNumber(); 142 m_element->document().scriptableDocumentParser()->lineNumber();
99 } 143 }
100 } 144 }
101 145
102 ScriptLoader::~ScriptLoader() {} 146 ScriptLoader::~ScriptLoader() {}
103 147
104 DEFINE_TRACE(ScriptLoader) { 148 DEFINE_TRACE(ScriptLoader) {
105 visitor->trace(m_element); 149 visitor->trace(m_element);
106 visitor->trace(m_resource); 150 visitor->trace(m_resource);
151 visitor->trace(m_elementModuleClient);
107 visitor->trace(m_pendingScript); 152 visitor->trace(m_pendingScript);
108 PendingScriptClient::trace(visitor); 153 PendingScriptClient::trace(visitor);
109 } 154 }
110 155
111 void ScriptLoader::setFetchDocWrittenScriptDeferIdle() { 156 void ScriptLoader::setFetchDocWrittenScriptDeferIdle() {
112 DCHECK(!m_createdDuringDocumentWrite); 157 DCHECK(!m_createdDuringDocumentWrite);
113 m_documentWriteIntervention = 158 m_documentWriteIntervention =
114 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle; 159 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle;
115 } 160 }
116 161
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 const String& encoding, 555 const String& encoding,
511 FetchRequest::DeferOption defer) { 556 FetchRequest::DeferOption defer) {
512 Document* elementDocument = &(m_element->document()); 557 Document* elementDocument = &(m_element->document());
513 if (!m_element->isConnected() || m_element->document() != elementDocument) 558 if (!m_element->isConnected() || m_element->document() != elementDocument)
514 return false; 559 return false;
515 560
516 DCHECK(!m_resource); 561 DCHECK(!m_resource);
517 // 21. "If the element has a src content attribute, run these substeps:" 562 // 21. "If the element has a src content attribute, run these substeps:"
518 if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) { 563 if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) {
519 // 21.4. "Parse src relative to the element's node document." 564 // 21.4. "Parse src relative to the element's node document."
520 ResourceRequest resourceRequest(elementDocument->completeURL(sourceUrl)); 565 KURL url = elementDocument->completeURL(sourceUrl);
566 ResourceRequest resourceRequest(url);
521 567
522 // [Intervention] 568 // [Intervention]
523 if (m_documentWriteIntervention == 569 if (m_documentWriteIntervention ==
524 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) { 570 DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) {
525 resourceRequest.setHTTPHeaderField( 571 resourceRequest.setHTTPHeaderField(
526 "Intervention", 572 "Intervention",
527 "<https://www.chromestatus.com/feature/5718547946799104>"); 573 "<https://www.chromestatus.com/feature/5718547946799104>");
528 } 574 }
529 575
530 FetchRequest request(resourceRequest, m_element->initiatorName()); 576 FetchRequest request(resourceRequest, m_element->initiatorName());
531 577
532 // 15. "Let CORS setting be the current state of the element's 578 // 15. "Let CORS setting be the current state of the element's
533 // crossorigin content attribute." 579 // crossorigin content attribute."
534 CrossOriginAttributeValue crossOrigin = 580 CrossOriginAttributeValue crossOrigin =
535 crossOriginAttributeValue(m_element->crossOriginAttributeValue()); 581 crossOriginAttributeValue(m_element->crossOriginAttributeValue());
536 582
537 // 16. "Let module script credentials mode be determined by switching 583 // 16. "Let module script credentials mode be determined by switching
538 // on CORS setting:" 584 // on CORS setting:"
539 // TODO(hiroshige): Implement this step for "module". 585 // TODO(hiroshige): Implement this step for "module".
586 if (RuntimeEnabledFeatures::moduleScriptsEnabled() &&
587 m_element->typeAttributeValue() == "module") {
588 m_elementModuleClient = ScriptElementModuleClient::create(
589 elementDocument->contextDocument()->frame());
590
591 String nonce;
592 if (m_element->isNonceableElement())
593 nonce = m_element->nonce();
594 ParserDisposition parserState =
595 isParserInserted() ? ParserInserted : NotParserInserted;
596
597 // 16. "Let module script credentials mode be determined by switching
598 // on CORS setting:"
599 WebURLRequest::FetchCredentialsMode credentialsMode;
600 switch (crossOrigin) {
601 case CrossOriginAttributeNotSet:
602 credentialsMode = WebURLRequest::FetchCredentialsModeOmit;
603 break;
604 case CrossOriginAttributeAnonymous:
605 credentialsMode = WebURLRequest::FetchCredentialsModeSameOrigin;
606 break;
607 case CrossOriginAttributeUseCredentials:
608 credentialsMode = WebURLRequest::FetchCredentialsModeInclude;
609 break;
610 default:
611 NOTREACHED();
612 }
613
614 ModuleScriptFetchRequest moduleRequest(url, nonce, parserState,
615 credentialsMode);
616 Modulator::from(elementDocument->frame())
617 ->fetchTree(moduleRequest, m_elementModuleClient);
618 return true;
619 }
540 620
541 // 21.6, "classic": "Fetch a classic script given ... CORS setting 621 // 21.6, "classic": "Fetch a classic script given ... CORS setting
542 // ... and encoding." 622 // ... and encoding."
543 if (crossOrigin != CrossOriginAttributeNotSet) 623 if (crossOrigin != CrossOriginAttributeNotSet)
544 request.setCrossOriginAccessControl(elementDocument->getSecurityOrigin(), 624 request.setCrossOriginAccessControl(elementDocument->getSecurityOrigin(),
545 crossOrigin); 625 crossOrigin);
546 626
547 request.setCharset(encoding); 627 request.setCharset(encoding);
548 628
549 // 17. "If the script element has a nonce attribute, 629 // 17. "If the script element has a nonce attribute,
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 // then abort these steps at this point. The script is not executed. 950 // then abort these steps at this point. The script is not executed.
871 return equalIgnoringCase(eventAttribute, "onload") || 951 return equalIgnoringCase(eventAttribute, "onload") ||
872 equalIgnoringCase(eventAttribute, "onload()"); 952 equalIgnoringCase(eventAttribute, "onload()");
873 } 953 }
874 954
875 String ScriptLoader::scriptContent() const { 955 String ScriptLoader::scriptContent() const {
876 return m_element->textFromChildren(); 956 return m_element->textFromChildren();
877 } 957 }
878 958
879 } // namespace blink 959 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698