OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/Worklet.h" | 5 #include "core/workers/Worklet.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
7 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
8 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/TaskRunnerHelper.h" |
9 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
10 #include "core/workers/WorkletGlobalScopeProxy.h" | 12 #include "core/workers/WorkletGlobalScopeProxy.h" |
11 | 13 |
12 namespace blink { | 14 namespace blink { |
13 | 15 |
14 Worklet::Worklet(LocalFrame* frame) | 16 Worklet::Worklet(LocalFrame* frame) |
15 : ContextLifecycleObserver(frame->GetDocument()), frame_(frame) { | 17 : ContextLifecycleObserver(frame->GetDocument()) { |
16 DCHECK(IsMainThread()); | 18 DCHECK(IsMainThread()); |
17 } | 19 } |
18 | 20 |
19 void Worklet::ContextDestroyed(ExecutionContext*) { | 21 // Implementation of the first half of the "addModule(moduleURL, options)" |
| 22 // algorithm: |
| 23 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
| 24 ScriptPromise Worklet::addModule(ScriptState* script_state, |
| 25 const String& module_url) { |
20 DCHECK(IsMainThread()); | 26 DCHECK(IsMainThread()); |
21 frame_ = nullptr; | 27 if (!GetExecutionContext()) { |
| 28 return ScriptPromise::RejectWithDOMException( |
| 29 script_state, DOMException::Create(kInvalidStateError, |
| 30 "This frame is already detached")); |
| 31 } |
| 32 |
| 33 // Step 1: "Let promise be a new promise." |
| 34 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 35 ScriptPromise promise = resolver->Promise(); |
| 36 |
| 37 // Step 2: "Let worklet be the current Worklet." |
| 38 // |this| is the current Worklet. |
| 39 |
| 40 // Step 3: "Let moduleURLRecord be the result of parsing the moduleURL |
| 41 // argument relative to the relevant settings object of this." |
| 42 KURL module_url_record = GetExecutionContext()->CompleteURL(module_url); |
| 43 |
| 44 // Step 4: "If moduleURLRecord is failure, then reject promise with a |
| 45 // "SyntaxError" DOMException and return promise." |
| 46 if (!module_url_record.IsValid()) { |
| 47 resolver->Reject(DOMException::Create( |
| 48 kSyntaxError, "'" + module_url + "' is not a valid URL.")); |
| 49 return promise; |
| 50 } |
| 51 |
| 52 // Step 5: "Return promise, and then continue running this algorithm in |
| 53 // parallel." |
| 54 // |kUnspecedLoading| is used here because this is a part of script module |
| 55 // loading. |
| 56 TaskRunnerHelper::Get(TaskType::kUnspecedLoading, script_state) |
| 57 ->PostTask(BLINK_FROM_HERE, |
| 58 WTF::Bind(&Worklet::FetchAndInvokeScript, WrapPersistent(this), |
| 59 module_url_record, WrapPersistent(resolver))); |
| 60 return promise; |
22 } | 61 } |
23 | 62 |
24 DEFINE_TRACE(Worklet) { | 63 DEFINE_TRACE(Worklet) { |
25 visitor->Trace(frame_); | |
26 ContextLifecycleObserver::Trace(visitor); | 64 ContextLifecycleObserver::Trace(visitor); |
27 } | 65 } |
28 | 66 |
29 } // namespace blink | 67 } // namespace blink |
OLD | NEW |