Index: third_party/WebKit/Source/core/workers/Worklet.cpp |
diff --git a/third_party/WebKit/Source/core/workers/Worklet.cpp b/third_party/WebKit/Source/core/workers/Worklet.cpp |
index dfa704f07e1d6a00388a22978e724ba8a6a7e6a3..58dc60b66a0d64bc26b78743d55e28baeed6858a 100644 |
--- a/third_party/WebKit/Source/core/workers/Worklet.cpp |
+++ b/third_party/WebKit/Source/core/workers/Worklet.cpp |
@@ -4,25 +4,63 @@ |
#include "core/workers/Worklet.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
#include "core/dom/DOMException.h" |
#include "core/dom/Document.h" |
+#include "core/dom/TaskRunnerHelper.h" |
#include "core/frame/LocalFrame.h" |
#include "core/workers/WorkletGlobalScopeProxy.h" |
namespace blink { |
Worklet::Worklet(LocalFrame* frame) |
- : ContextLifecycleObserver(frame->GetDocument()), frame_(frame) { |
+ : ContextLifecycleObserver(frame->GetDocument()) { |
DCHECK(IsMainThread()); |
} |
-void Worklet::ContextDestroyed(ExecutionContext*) { |
+// Implementation of the first half of the "addModule(moduleURL, options)" |
+// algorithm: |
+// https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
+ScriptPromise Worklet::addModule(ScriptState* script_state, |
+ const String& module_url) { |
DCHECK(IsMainThread()); |
- frame_ = nullptr; |
+ if (!GetExecutionContext()) { |
+ return ScriptPromise::RejectWithDOMException( |
+ script_state, DOMException::Create(kInvalidStateError, |
+ "This frame is already detached")); |
+ } |
+ |
+ // Step 1: "Let promise be a new promise." |
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
+ ScriptPromise promise = resolver->Promise(); |
+ |
+ // Step 2: "Let worklet be the current Worklet." |
+ // |this| is the current Worklet. |
+ |
+ // Step 3: "Let moduleURLRecord be the result of parsing the moduleURL |
+ // argument relative to the relevant settings object of this." |
+ KURL module_url_record = GetExecutionContext()->CompleteURL(module_url); |
+ |
+ // Step 4: "If moduleURLRecord is failure, then reject promise with a |
+ // "SyntaxError" DOMException and return promise." |
+ if (!module_url_record.IsValid()) { |
+ resolver->Reject(DOMException::Create( |
+ kSyntaxError, "'" + module_url + "' is not a valid URL.")); |
+ return promise; |
+ } |
+ |
+ // Step 5: "Return promise, and then continue running this algorithm in |
+ // parallel." |
+ // |kUnspecedLoading| is used here because this is a part of script module |
+ // loading. |
+ TaskRunnerHelper::Get(TaskType::kUnspecedLoading, script_state) |
+ ->PostTask(BLINK_FROM_HERE, |
+ WTF::Bind(&Worklet::FetchAndInvokeScript, WrapPersistent(this), |
+ module_url_record, WrapPersistent(resolver))); |
+ return promise; |
} |
DEFINE_TRACE(Worklet) { |
- visitor->Trace(frame_); |
ContextLifecycleObserver::Trace(visitor); |
} |