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

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

Issue 2839123003: Worklet: Introduce "pending tasks struct" concept defined in the Worklet spec (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 "core/workers/WorkletPendingTasks.h"
14 #include "platform/wtf/WTF.h" 15 #include "platform/wtf/WTF.h"
15 16
16 namespace blink { 17 namespace blink {
17 18
18 namespace { 19 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) {}
19
20 int32_t GetNextRequestId() {
21 DCHECK(IsMainThread());
22 static int32_t next_request_id = 1;
23 CHECK_LT(next_request_id, std::numeric_limits<int32_t>::max());
24 return next_request_id++;
25 }
26
27 } // namespace
28
29 MainThreadWorklet::MainThreadWorklet(LocalFrame* frame) : Worklet(frame) {
30 DCHECK(resolver_map_.IsEmpty());
31 }
32 20
33 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state, 21 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state,
34 const String& url) { 22 const String& module_url) {
35 DCHECK(IsMainThread()); 23 DCHECK(IsMainThread());
36 if (!GetExecutionContext()) { 24 if (!GetExecutionContext()) {
37 return ScriptPromise::RejectWithDOMException( 25 return ScriptPromise::RejectWithDOMException(
38 script_state, DOMException::Create(kInvalidStateError, 26 script_state, DOMException::Create(kInvalidStateError,
39 "This frame is already detached")); 27 "This frame is already detached"));
40 } 28 }
41 29
42 KURL script_url = GetExecutionContext()->CompleteURL(url); 30 KURL module_url_record = GetExecutionContext()->CompleteURL(module_url);
43 if (!script_url.IsValid()) { 31 if (!module_url_record.IsValid()) {
44 return ScriptPromise::RejectWithDOMException( 32 return ScriptPromise::RejectWithDOMException(
45 script_state, DOMException::Create( 33 script_state,
46 kSyntaxError, "'" + url + "' is not a valid URL.")); 34 DOMException::Create(kSyntaxError,
35 "'" + module_url + "' is not a valid URL."));
47 } 36 }
48 37
49 if (!IsInitialized()) 38 if (!IsInitialized())
50 Initialize(); 39 Initialize();
51 40
52 int32_t request_id = GetNextRequestId();
53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 41 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
54 ScriptPromise promise = resolver->Promise(); 42 ScriptPromise promise = resolver->Promise();
55 resolver_map_.Set(request_id, resolver); 43
56 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); 44 // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter
nhiroki 2017/04/26 10:58:53 FYI: Other step comments will be added by https://
45 // initialized to the length of worklet's WorkletGlobalScopes."
46 // TODO(nhiroki): Introduce the concept of "worklet's WorkletGlobalScopes" and
47 // use the length of it here.
48 const int number_of_global_scopes = 1;
49 WorkletPendingTasks* pending_tasks =
50 new WorkletPendingTasks(number_of_global_scopes, resolver);
51
52 // Step 12: "For each workletGlobalScope in the worklet's
53 // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and
54 // invoke a worklet script given workletGlobalScope, moduleURLRecord,
55 // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct,
56 // and promise."
57 // TODO(nhiroki): Pass the remaining parameters (e.g., credentialOptions).
58 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(module_url_record,
59 pending_tasks);
57 return promise; 60 return promise;
58 } 61 }
59 62
60 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id,
61 bool success) {
62 DCHECK(IsMainThread());
63 ScriptPromiseResolver* resolver = resolver_map_.at(request_id);
64 if (!resolver)
65 return;
66 resolver_map_.erase(request_id);
67 if (!success) {
68 resolver->Reject(DOMException::Create(kNetworkError));
69 return;
70 }
71 resolver->Resolve();
72 }
73
74 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { 63 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) {
75 DCHECK(IsMainThread()); 64 DCHECK(IsMainThread());
76 resolver_map_.clear();
77 Worklet::ContextDestroyed(execution_context); 65 Worklet::ContextDestroyed(execution_context);
78 } 66 }
79 67
80 DEFINE_TRACE(MainThreadWorklet) { 68 DEFINE_TRACE(MainThreadWorklet) {
81 visitor->Trace(resolver_map_);
82 Worklet::Trace(visitor); 69 Worklet::Trace(visitor);
83 } 70 }
84 71
85 } // namespace blink 72 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698