Chromium Code Reviews| 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/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 int32_t request_id = GetNextRequestId(); | |
| 50 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 38 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 51 ScriptPromise promise = resolver->Promise(); | 39 ScriptPromise promise = resolver->Promise(); |
| 52 resolver_map_.Set(request_id, resolver); | 40 |
| 53 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); | 41 // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter |
|
kouhei (in TOK)
2017/04/27 10:05:33
Add context link https://drafts.css-houdini.org/wo
nhiroki
2017/04/28 04:41:14
Done.
| |
| 42 // initialized to the length of worklet's WorkletGlobalScopes." | |
| 43 // TODO(nhiroki): Introduce the concept of "worklet's WorkletGlobalScopes" and | |
| 44 // use the length of it here. | |
| 45 const int number_of_global_scopes = 1; | |
|
kouhei (in TOK)
2017/04/27 10:05:33
constexpr?
nhiroki
2017/04/28 04:41:14
Done.
| |
| 46 WorkletPendingTasks* pending_tasks = | |
| 47 new WorkletPendingTasks(number_of_global_scopes, resolver); | |
| 48 | |
| 49 // Step 12: "For each workletGlobalScope in the worklet's | |
| 50 // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and | |
| 51 // invoke a worklet script given workletGlobalScope, moduleURLRecord, | |
| 52 // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, | |
| 53 // and promise." | |
| 54 // TODO(nhiroki): Pass the remaining parameters (e.g., credentialOptions). | |
|
kouhei (in TOK)
2017/04/27 10:05:33
Add a note about skipping "queue a task". TODO or
nhiroki
2017/04/28 04:41:14
Done.
| |
| 55 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(module_url_record, | |
| 56 pending_tasks); | |
| 54 return promise; | 57 return promise; |
| 55 } | 58 } |
| 56 | 59 |
| 57 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id, | |
| 58 bool success) { | |
| 59 DCHECK(IsMainThread()); | |
| 60 ScriptPromiseResolver* resolver = resolver_map_.at(request_id); | |
| 61 if (!resolver) | |
| 62 return; | |
| 63 resolver_map_.erase(request_id); | |
| 64 if (!success) { | |
| 65 resolver->Reject(DOMException::Create(kNetworkError)); | |
| 66 return; | |
| 67 } | |
| 68 resolver->Resolve(); | |
| 69 } | |
| 70 | |
| 71 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { | 60 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { |
| 72 DCHECK(IsMainThread()); | 61 DCHECK(IsMainThread()); |
| 73 resolver_map_.clear(); | |
| 74 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); | 62 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); |
| 75 Worklet::ContextDestroyed(execution_context); | 63 Worklet::ContextDestroyed(execution_context); |
| 76 } | 64 } |
| 77 | 65 |
| 78 DEFINE_TRACE(MainThreadWorklet) { | 66 DEFINE_TRACE(MainThreadWorklet) { |
| 79 visitor->Trace(resolver_map_); | |
| 80 Worklet::Trace(visitor); | 67 Worklet::Trace(visitor); |
| 81 } | 68 } |
| 82 | 69 |
| 83 } // namespace blink | 70 } // namespace blink |
| OLD | NEW |