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

Side by Side Diff: third_party/WebKit/Source/core/workers/MainThreadWorklet.cpp

Issue 2840523002: [DONT COMMIT] Worklet: Implement "addModule()" algorithm for main thread worklets (Closed)
Patch Set: Created 3 years, 7 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 // 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/ScriptSourceCode.h" 7 #include "bindings/core/v8/ScriptSourceCode.h"
8 #include "bindings/core/v8/V8BindingForCore.h" 8 #include "bindings/core/v8/V8BindingForCore.h"
9 #include "core/dom/DOMException.h" 9 #include "core/dom/DOMException.h"
10 #include "core/dom/Document.h" 10 #include "core/dom/Document.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/frame/LocalFrame.h" 12 #include "core/frame/LocalFrame.h"
13 #include "core/workers/WorkletGlobalScopeProxy.h" 13 #include "core/workers/WorkletGlobalScopeProxy.h"
14 #include "platform/wtf/WTF.h" 14 #include "platform/wtf/WTF.h"
15 15
16 namespace blink { 16 namespace blink {
17 17
18 namespace { 18 namespace {
19 19
20 int32_t GetNextRequestId() { 20 int32_t GetNextRequestId() {
21 DCHECK(IsMainThread()); 21 DCHECK(IsMainThread());
22 static int32_t next_request_id = 1; 22 static int32_t next_request_id = 1;
23 CHECK_LT(next_request_id, std::numeric_limits<int32_t>::max()); 23 CHECK_LT(next_request_id, std::numeric_limits<int32_t>::max());
24 return next_request_id++; 24 return next_request_id++;
25 } 25 }
26 26
27 } // namespace 27 } // namespace
28 28
29 // Implementation of the "pending tasks struct":
30 // https://drafts.css-houdini.org/worklets/#pending-tasks-struct
31 struct MainThreadWorklet::PendingTasks : public GarbageCollected<PendingTasks> {
kouhei (in TOK) 2017/04/25 11:55:17 please make this a proper class
32 PendingTasks(int counter, ScriptPromiseResolver* resolver)
33 : counter(counter), resolver(resolver) {}
34 ~PendingTasks() = default;
35
36 // The number of pending tasks.
37 int counter;
38
39 Member<ScriptPromiseResolver> resolver;
40
41 DEFINE_INLINE_VIRTUAL_TRACE() { visitor->Trace(resolver); }
42 };
43
29 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) { 44 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) {
30 DCHECK(resolver_map_.IsEmpty()); 45 DCHECK(pending_tasks_map_.IsEmpty());
kouhei (in TOK) 2017/04/25 11:55:18 I think we can safely omit this DCHECK.
31 } 46 }
32 47
48 // Implementation of the "addModule" algorithm:
49 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule
33 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state, 50 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state,
34 const String& url) { 51 const String& url) {
35 DCHECK(IsMainThread()); 52 DCHECK(IsMainThread());
kouhei (in TOK) 2017/04/25 11:55:17 Optional: I'd prefer to have all the steps in the
36 if (!GetExecutionContext()) { 53 if (!GetExecutionContext()) {
37 return ScriptPromise::RejectWithDOMException( 54 return ScriptPromise::RejectWithDOMException(
38 script_state, DOMException::Create(kInvalidStateError, 55 script_state, DOMException::Create(kInvalidStateError,
39 "This frame is already detached")); 56 "This frame is already detached"));
40 } 57 }
41 58
59 // Step 4: "If moduleURLRecord is failure, then reject promise with a
kouhei (in TOK) 2017/04/25 11:55:18 Can we rename the script_url var to module_url_rec
60 // "SyntaxError" DOMException and return promise."
42 KURL script_url = GetExecutionContext()->CompleteURL(url); 61 KURL script_url = GetExecutionContext()->CompleteURL(url);
kouhei (in TOK) 2017/04/25 11:55:17 Looks like CompleteURL part is the Step 3?
43 if (!script_url.IsValid()) { 62 if (!script_url.IsValid()) {
44 return ScriptPromise::RejectWithDOMException( 63 return ScriptPromise::RejectWithDOMException(
45 script_state, DOMException::Create( 64 script_state, DOMException::Create(
46 kSyntaxError, "'" + url + "' is not a valid URL.")); 65 kSyntaxError, "'" + url + "' is not a valid URL."));
47 } 66 }
48 67
49 if (!IsInitialized()) 68 // Step 10: "If the worklet's WorkletGlobalScopes is empty, run the following
50 Initialize(); 69 // steps:"
70 // 10.1: "Create a WorkletGlobalScope given workletGlobalScopeType,
71 // moduleResponsesMap, and outsideSettings."
kouhei (in TOK) 2017/04/25 11:55:18 Add TODO(nhiroki) comment about the workletGlobalS
72 // 10.2: "Add the WorkletGlobalScope to worklet's WorkletGlobalScopes."
73 if (global_scope_proxies_.IsEmpty())
74 CreateWorkletGlobalScope();
75 DCHECK(!global_scope_proxies_.IsEmpty());
51 76
52 int32_t request_id = GetNextRequestId(); 77 // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter
78 // initialized to the length of worklet' s WorkletGlobalScopes."
79 const int32_t request_id = GetNextRequestId();
53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 80 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
81 pending_tasks_map_.Set(
82 request_id, new PendingTasks(global_scope_proxies_.size(), resolver));
83
84 // Step 12: "For each workletGlobalScope in the worklet's
85 // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and
86 // invoke a worklet script given workletGlobalScope, moduleURLRecord,
87 // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct,
88 // and promise."
54 ScriptPromise promise = resolver->Promise(); 89 ScriptPromise promise = resolver->Promise();
55 resolver_map_.Set(request_id, resolver); 90 for (const auto& proxy : global_scope_proxies_)
56 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); 91 proxy->FetchAndInvokeScript(request_id, script_url);
92
57 return promise; 93 return promise;
58 } 94 }
59 95
96 // Worklet-side implementation of the "fetch and invoke a worklet script"
97 // algorithm:
98 // https://drafts.css-houdini.org/worklets/#fetch-and-invoke-a-worklet-script
60 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id, 99 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id,
61 bool success) { 100 bool success) {
62 DCHECK(IsMainThread()); 101 DCHECK(IsMainThread());
63 ScriptPromiseResolver* resolver = resolver_map_.at(request_id); 102 PendingTasks* pending_tasks = pending_tasks_map_.at(request_id);
kouhei (in TOK) 2017/04/25 11:55:18 Optional: map_.Find here to get iterator
64 if (!resolver) 103 if (!pending_tasks)
65 return; 104 return;
66 resolver_map_.erase(request_id); 105
106 // Step 3: "If script is null, then queue a task on outsideSettings's
107 // responsible event loop to run these steps:"
kouhei (in TOK) 2017/04/25 11:55:18 Add TODO(nhiroki) or add note to justify not queui
108 // 3.1.2: "Reject promise with an "AbortError" DOMException."
67 if (!success) { 109 if (!success) {
68 resolver->Reject(DOMException::Create(kNetworkError)); 110 pending_tasks_map_.erase(request_id);
kouhei (in TOK) 2017/04/25 11:55:18 and trigger erase on the iterator?
111 // TODO(nhiroki): This should be kAbortError.
112 pending_tasks->resolver->Reject(DOMException::Create(kNetworkError));
69 return; 113 return;
70 } 114 }
71 resolver->Resolve(); 115
116 // Step 5: "Queue a task on outsideSettings's responsible event loop to run
117 // these steps:"
kouhei (in TOK) 2017/04/25 11:55:17 Ditto.
118 // 5.1.1: "Decrement pendingTaskStruct's counter by 1."
119 // 5.1.2: "If pendingTaskStruct's counter is 0, then resolve promise."
120 DCHECK_GT(0, pending_tasks->counter);
121 --pending_tasks->counter;
122 if (pending_tasks->counter == 0) {
123 pending_tasks_map_.erase(request_id);
124 pending_tasks->resolver->Resolve();
125 }
126 }
127
128 void MainThreadWorklet::CreateWorkletGlobalScope() {
129 if (!IsInitialized())
130 Initialize();
72 } 131 }
73 132
74 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { 133 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) {
75 DCHECK(IsMainThread()); 134 DCHECK(IsMainThread());
76 resolver_map_.clear(); 135 pending_tasks_map_.clear();
136 for (const auto& proxy : global_scope_proxies_)
137 proxy->TerminateWorkletGlobalScope();
77 Worklet::ContextDestroyed(execution_context); 138 Worklet::ContextDestroyed(execution_context);
78 } 139 }
79 140
80 DEFINE_TRACE(MainThreadWorklet) { 141 DEFINE_TRACE(MainThreadWorklet) {
81 visitor->Trace(resolver_map_); 142 visitor->Trace(pending_tasks_map_);
82 Worklet::Trace(visitor); 143 Worklet::Trace(visitor);
83 } 144 }
84 145
85 } // namespace blink 146 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698