OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/ModuleScript.h" | 5 #include "core/dom/ModuleScript.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptValue.h" | 7 #include "bindings/core/v8/ScriptValue.h" |
8 #include "core/dom/ScriptModuleResolver.h" | 8 #include "core/dom/ScriptModuleResolver.h" |
9 #include "platform/bindings/ScriptState.h" | 9 #include "platform/bindings/ScriptState.h" |
10 #include "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 ScriptModule record, | 71 ScriptModule record, |
72 const KURL& base_url, | 72 const KURL& base_url, |
73 const String& nonce, | 73 const String& nonce, |
74 ParserDisposition parser_state, | 74 ParserDisposition parser_state, |
75 WebURLRequest::FetchCredentialsMode credentials_mode) { | 75 WebURLRequest::FetchCredentialsMode credentials_mode) { |
76 String dummy_source_text = ""; | 76 String dummy_source_text = ""; |
77 return CreateInternal(dummy_source_text, modulator, record, base_url, nonce, | 77 return CreateInternal(dummy_source_text, modulator, record, base_url, nonce, |
78 parser_state, credentials_mode); | 78 parser_state, credentials_mode); |
79 } | 79 } |
80 | 80 |
| 81 ModuleScript::ModuleScript(Modulator* settings_object, |
| 82 ScriptModule record, |
| 83 const KURL& base_url, |
| 84 const String& nonce, |
| 85 ParserDisposition parser_state, |
| 86 WebURLRequest::FetchCredentialsMode credentials_mode, |
| 87 const String& source_text) |
| 88 : settings_object_(settings_object), |
| 89 record_(this), |
| 90 base_url_(base_url), |
| 91 instantiation_error_(this), |
| 92 nonce_(nonce), |
| 93 parser_state_(parser_state), |
| 94 credentials_mode_(credentials_mode), |
| 95 source_text_(source_text) { |
| 96 if (record.IsNull()) { |
| 97 // We allow empty records for module infra tests which never touch records. |
| 98 // This should never happen outside unit tests. |
| 99 return; |
| 100 } |
| 101 |
| 102 DCHECK(settings_object); |
| 103 v8::Isolate* isolate = settings_object_->GetScriptState()->GetIsolate(); |
| 104 v8::HandleScope scope(isolate); |
| 105 record_.Set(isolate, record.NewLocal(isolate)); |
| 106 } |
| 107 |
| 108 ScriptModule ModuleScript::Record() const { |
| 109 if (record_.IsEmpty()) |
| 110 return ScriptModule(); |
| 111 |
| 112 v8::Isolate* isolate = settings_object_->GetScriptState()->GetIsolate(); |
| 113 v8::HandleScope scope(isolate); |
| 114 return ScriptModule(isolate, record_.NewLocal(isolate)); |
| 115 } |
| 116 |
81 void ModuleScript::SetInstantiationErrorAndClearRecord(ScriptValue error) { | 117 void ModuleScript::SetInstantiationErrorAndClearRecord(ScriptValue error) { |
82 // Implements Step 7.1 of: | 118 // Implements Step 7.1 of: |
83 // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-scri
pt-graph-fetching-procedure | 119 // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-scri
pt-graph-fetching-procedure |
84 | 120 |
85 // "set script's instantiation state to "errored", ..." | 121 // "set script's instantiation state to "errored", ..." |
86 DCHECK_EQ(instantiation_state_, ModuleInstantiationState::kUninstantiated); | 122 DCHECK_EQ(instantiation_state_, ModuleInstantiationState::kUninstantiated); |
87 instantiation_state_ = ModuleInstantiationState::kErrored; | 123 instantiation_state_ = ModuleInstantiationState::kErrored; |
88 | 124 |
89 // "its instantiation error to instantiationStatus.[[Value]], and ..." | 125 // "its instantiation error to instantiationStatus.[[Value]], and ..." |
90 DCHECK(!error.IsEmpty()); | 126 DCHECK(!error.IsEmpty()); |
91 { | 127 { |
92 ScriptState::Scope scope(error.GetScriptState()); | 128 ScriptState::Scope scope(error.GetScriptState()); |
93 instantiation_error_.Set(error.GetIsolate(), error.V8Value()); | 129 instantiation_error_.Set(error.GetIsolate(), error.V8Value()); |
94 } | 130 } |
95 | 131 |
96 // "its module record to null." | 132 // "its module record to null." |
97 record_ = ScriptModule(); | 133 record_.Clear(); |
98 } | 134 } |
99 | 135 |
100 void ModuleScript::SetInstantiationSuccess() { | 136 void ModuleScript::SetInstantiationSuccess() { |
101 // Implements Step 7.2 of: | 137 // Implements Step 7.2 of: |
102 // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-scri
pt-graph-fetching-procedure | 138 // https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-scri
pt-graph-fetching-procedure |
103 | 139 |
104 // "set script's instantiation state to "instantiated"." | 140 // "set script's instantiation state to "instantiated"." |
105 DCHECK_EQ(instantiation_state_, ModuleInstantiationState::kUninstantiated); | 141 DCHECK_EQ(instantiation_state_, ModuleInstantiationState::kUninstantiated); |
106 instantiation_state_ = ModuleInstantiationState::kInstantiated; | 142 instantiation_state_ = ModuleInstantiationState::kInstantiated; |
107 } | 143 } |
108 | 144 |
109 DEFINE_TRACE(ModuleScript) { | 145 DEFINE_TRACE(ModuleScript) { |
110 visitor->Trace(settings_object_); | 146 visitor->Trace(settings_object_); |
111 Script::Trace(visitor); | 147 Script::Trace(visitor); |
112 } | 148 } |
113 DEFINE_TRACE_WRAPPERS(ModuleScript) { | 149 DEFINE_TRACE_WRAPPERS(ModuleScript) { |
| 150 // TODO(mlippautz): Support TraceWrappers(const |
| 151 // TraceWrapperV8Reference<v8::Module>&) to remove the cast. |
| 152 visitor->TraceWrappers(record_.Cast<v8::Value>()); |
114 visitor->TraceWrappers(instantiation_error_); | 153 visitor->TraceWrappers(instantiation_error_); |
115 } | 154 } |
116 | 155 |
117 bool ModuleScript::IsEmpty() const { | 156 bool ModuleScript::IsEmpty() const { |
118 return false; | 157 return false; |
119 } | 158 } |
120 | 159 |
121 bool ModuleScript::CheckMIMETypeBeforeRunScript(Document* context_document, | 160 bool ModuleScript::CheckMIMETypeBeforeRunScript(Document* context_document, |
122 const SecurityOrigin*) const { | 161 const SecurityOrigin*) const { |
123 // We don't check MIME type here because we check the MIME type in | 162 // We don't check MIME type here because we check the MIME type in |
124 // ModuleScriptLoader::WasModuleLoadSuccessful(). | 163 // ModuleScriptLoader::WasModuleLoadSuccessful(). |
125 return true; | 164 return true; |
126 } | 165 } |
127 | 166 |
128 void ModuleScript::RunScript(LocalFrame* frame, const SecurityOrigin*) const { | 167 void ModuleScript::RunScript(LocalFrame* frame, const SecurityOrigin*) const { |
129 settings_object_->ExecuteModule(this); | 168 settings_object_->ExecuteModule(this); |
130 } | 169 } |
131 | 170 |
132 String ModuleScript::InlineSourceTextForCSP() const { | 171 String ModuleScript::InlineSourceTextForCSP() const { |
133 return source_text_; | 172 return source_text_; |
134 } | 173 } |
135 | 174 |
136 } // namespace blink | 175 } // namespace blink |
OLD | NEW |