| 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 |
| 20 int32_t GetNextRequestId() { | 21 // Implementation of the "addModule(moduleURL, options)" algorithm: |
| 21 DCHECK(IsMainThread()); | 22 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
| 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 | |
| 33 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state, | 23 ScriptPromise MainThreadWorklet::addModule(ScriptState* script_state, |
| 34 const String& url) { | 24 const String& module_url) { |
| 35 DCHECK(IsMainThread()); | 25 DCHECK(IsMainThread()); |
| 36 if (!GetExecutionContext()) { | 26 if (!GetExecutionContext()) { |
| 37 return ScriptPromise::RejectWithDOMException( | 27 return ScriptPromise::RejectWithDOMException( |
| 38 script_state, DOMException::Create(kInvalidStateError, | 28 script_state, DOMException::Create(kInvalidStateError, |
| 39 "This frame is already detached")); | 29 "This frame is already detached")); |
| 40 } | 30 } |
| 41 | 31 |
| 42 KURL script_url = GetExecutionContext()->CompleteURL(url); | 32 KURL module_url_record = GetExecutionContext()->CompleteURL(module_url); |
| 43 if (!script_url.IsValid()) { | 33 if (!module_url_record.IsValid()) { |
| 44 return ScriptPromise::RejectWithDOMException( | 34 return ScriptPromise::RejectWithDOMException( |
| 45 script_state, DOMException::Create( | 35 script_state, |
| 46 kSyntaxError, "'" + url + "' is not a valid URL.")); | 36 DOMException::Create(kSyntaxError, |
| 37 "'" + module_url + "' is not a valid URL.")); |
| 47 } | 38 } |
| 48 | 39 |
| 49 int32_t request_id = GetNextRequestId(); | |
| 50 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 40 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 51 ScriptPromise promise = resolver->Promise(); | 41 ScriptPromise promise = resolver->Promise(); |
| 52 resolver_map_.Set(request_id, resolver); | 42 |
| 53 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); | 43 // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter |
| 44 // initialized to the length of worklet's WorkletGlobalScopes." |
| 45 // TODO(nhiroki): Introduce the concept of "worklet's WorkletGlobalScopes" and |
| 46 // use the length of it here. |
| 47 constexpr int number_of_global_scopes = 1; |
| 48 WorkletPendingTasks* pending_tasks = |
| 49 new WorkletPendingTasks(number_of_global_scopes, resolver); |
| 50 |
| 51 // Step 12: "For each workletGlobalScope in the worklet's |
| 52 // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and |
| 53 // invoke a worklet script given workletGlobalScope, moduleURLRecord, |
| 54 // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, |
| 55 // and promise." |
| 56 // TODO(nhiroki): Pass the remaining parameters (e.g., credentialOptions). |
| 57 // TODO(nhiroki): Queue a task instead of executing this here. |
| 58 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(module_url_record, |
| 59 pending_tasks); |
| 54 return promise; | 60 return promise; |
| 55 } | 61 } |
| 56 | 62 |
| 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) { | 63 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { |
| 72 DCHECK(IsMainThread()); | 64 DCHECK(IsMainThread()); |
| 73 resolver_map_.clear(); | |
| 74 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); | 65 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); |
| 75 Worklet::ContextDestroyed(execution_context); | 66 Worklet::ContextDestroyed(execution_context); |
| 76 } | 67 } |
| 77 | 68 |
| 78 DEFINE_TRACE(MainThreadWorklet) { | 69 DEFINE_TRACE(MainThreadWorklet) { |
| 79 visitor->Trace(resolver_map_); | |
| 80 Worklet::Trace(visitor); | 70 Worklet::Trace(visitor); |
| 81 } | 71 } |
| 82 | 72 |
| 83 } // namespace blink | 73 } // namespace blink |
| OLD | NEW |