blob: 6620d822b7d92d4b754b6a80fd0832aaa5d7423f [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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()) {
// Implementation of the first half of the "addModule(moduleURL, options)"
// algorithm:
ScriptPromise Worklet::addModule(ScriptState* script_state,
const String& module_url,
const WorkletOptions& options) {
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()) {
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)
WTF::Bind(&Worklet::FetchAndInvokeScript, WrapPersistent(this),
module_url_record, options, WrapPersistent(resolver)));
return promise;
} // namespace blink