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/workers/MainThreadWorklet.h" | 5 #include "core/workers/MainThreadWorklet.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/core/v8/ScriptSourceCode.h" | 8 #include "bindings/core/v8/ScriptSourceCode.h" |
9 #include "bindings/core/v8/V8BindingForCore.h" | 9 #include "bindings/core/v8/V8BindingForCore.h" |
10 #include "core/dom/DOMException.h" | |
11 #include "core/dom/Document.h" | |
12 #include "core/dom/ExceptionCode.h" | |
13 #include "core/dom/TaskRunnerHelper.h" | |
14 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
15 #include "core/workers/WorkletGlobalScopeProxy.h" | 11 #include "core/workers/WorkletGlobalScopeProxy.h" |
16 #include "core/workers/WorkletPendingTasks.h" | 12 #include "core/workers/WorkletPendingTasks.h" |
17 #include "platform/wtf/WTF.h" | 13 #include "platform/wtf/WTF.h" |
18 | 14 |
19 namespace blink { | 15 namespace blink { |
20 | 16 |
21 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) {} | 17 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) {} |
22 | 18 |
23 // Implementation of the first half of the "addModule(moduleURL, options)" | |
24 // algorithm: | |
25 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule | |
26 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state, | |
27 const String& module_url) { | |
28 DCHECK(IsMainThread()); | |
29 if (!GetExecutionContext()) { | |
30 return ScriptPromise::RejectWithDOMException( | |
31 script_state, DOMException::Create(kInvalidStateError, | |
32 "This frame is already detached")); | |
33 } | |
34 | |
35 // Step 1: "Let promise be a new promise." | |
36 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
37 ScriptPromise promise = resolver->Promise(); | |
38 | |
39 // Step 2: "Let worklet be the current Worklet." | |
40 // |this| is the current Worklet. | |
41 | |
42 // Step 3: "Let moduleURLRecord be the result of parsing the moduleURL | |
43 // argument relative to the relevant settings object of this." | |
44 KURL module_url_record = GetExecutionContext()->CompleteURL(module_url); | |
45 | |
46 // Step 4: "If moduleURLRecord is failure, then reject promise with a | |
47 // "SyntaxError" DOMException and return promise." | |
48 if (!module_url_record.IsValid()) { | |
49 resolver->Reject(DOMException::Create( | |
50 kSyntaxError, "'" + module_url + "' is not a valid URL.")); | |
51 return promise; | |
52 } | |
53 | |
54 // Step 5: "Return promise, and then continue running this algorithm in | |
55 // parallel." | |
56 // |kUnspecedLoading| is used here because this is a part of script module | |
57 // loading. | |
58 TaskRunnerHelper::Get(TaskType::kUnspecedLoading, script_state) | |
59 ->PostTask(BLINK_FROM_HERE, | |
60 WTF::Bind(&MainThreadWorklet::FetchAndInvokeScript, | |
61 WrapPersistent(this), module_url_record, | |
62 WrapPersistent(resolver))); | |
63 return promise; | |
64 } | |
65 | |
66 // Implementation of the second half of the "addModule(moduleURL, options)" | 19 // Implementation of the second half of the "addModule(moduleURL, options)" |
67 // algorithm: | 20 // algorithm: |
68 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule | 21 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
69 void MainThreadWorklet::FetchAndInvokeScript(const KURL& module_url_record, | 22 void MainThreadWorklet::FetchAndInvokeScript(const KURL& module_url_record, |
70 ScriptPromiseResolver* resolver) { | 23 ScriptPromiseResolver* resolver) { |
71 DCHECK(IsMainThread()); | 24 DCHECK(IsMainThread()); |
72 if (!GetExecutionContext()) | 25 if (!GetExecutionContext()) |
73 return; | 26 return; |
74 | 27 |
75 // Step 6: "Let credentialOptions be the credentials member of options." | 28 // Step 6: "Let credentialOptions be the credentials member of options." |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // and promise." | 64 // and promise." |
112 // TODO(nhiroki): Pass the remaining parameters (e.g., credentialOptions). | 65 // TODO(nhiroki): Pass the remaining parameters (e.g., credentialOptions). |
113 // TODO(nhiroki): Queue a task instead of executing this here. | 66 // TODO(nhiroki): Queue a task instead of executing this here. |
114 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(module_url_record, | 67 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(module_url_record, |
115 pending_tasks); | 68 pending_tasks); |
116 } | 69 } |
117 | 70 |
118 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { | 71 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { |
119 DCHECK(IsMainThread()); | 72 DCHECK(IsMainThread()); |
120 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); | 73 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); |
121 Worklet::ContextDestroyed(execution_context); | |
122 } | 74 } |
123 | 75 |
124 DEFINE_TRACE(MainThreadWorklet) { | 76 DEFINE_TRACE(MainThreadWorklet) { |
125 Worklet::Trace(visitor); | 77 Worklet::Trace(visitor); |
126 } | 78 } |
127 | 79 |
128 } // namespace blink | 80 } // namespace blink |
OLD | NEW |