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 6620d822b7d92d4b754b6a80fd0832aaa5d7423f..fd85146ec383ab1de10938df39583ff0bb8202a2 100644 |
--- a/third_party/WebKit/Source/core/workers/Worklet.cpp |
+++ b/third_party/WebKit/Source/core/workers/Worklet.cpp |
@@ -10,9 +10,28 @@ |
#include "core/dom/TaskRunnerHelper.h" |
#include "core/frame/LocalFrame.h" |
#include "core/workers/WorkletGlobalScopeProxy.h" |
+#include "core/workers/WorkletPendingTasks.h" |
+#include "platform/wtf/WTF.h" |
+#include "public/platform/WebURLRequest.h" |
namespace blink { |
+namespace { |
+ |
+WebURLRequest::FetchCredentialsMode ParseCredentialsOption( |
+ const String& credentials_option) { |
+ if (credentials_option == "omit") |
+ return WebURLRequest::kFetchCredentialsModeOmit; |
+ if (credentials_option == "same-origin") |
+ return WebURLRequest::kFetchCredentialsModeSameOrigin; |
+ if (credentials_option == "include") |
+ return WebURLRequest::kFetchCredentialsModeInclude; |
+ NOTREACHED(); |
+ return WebURLRequest::kFetchCredentialsModeOmit; |
+} |
+ |
+} // namespace |
+ |
Worklet::Worklet(LocalFrame* frame) |
: ContextLifecycleObserver(frame->GetDocument()) { |
DCHECK(IsMainThread()); |
@@ -62,6 +81,74 @@ ScriptPromise Worklet::addModule(ScriptState* script_state, |
return promise; |
} |
+void Worklet::ContextDestroyed(ExecutionContext* execution_context) { |
+ DCHECK(IsMainThread()); |
+ for (const auto& proxy : proxies_) |
+ proxy->TerminateWorkletGlobalScope(); |
+} |
+ |
+WorkletGlobalScopeProxy* Worklet::FindAvailableGlobalScope() const { |
+ DCHECK(IsMainThread()); |
+ // TODO(nhiroki): Support the case where there are multiple global scopes. |
+ DCHECK_EQ(1u, GetNumberOfGlobalScopes()); |
+ return proxies_.begin()->get(); |
+} |
+ |
+// Implementation of the second half of the "addModule(moduleURL, options)" |
+// algorithm: |
+// https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
+void Worklet::FetchAndInvokeScript(const KURL& module_url_record, |
+ const WorkletOptions& options, |
+ ScriptPromiseResolver* resolver) { |
+ DCHECK(IsMainThread()); |
+ if (!GetExecutionContext()) |
+ return; |
+ |
+ // Step 6: "Let credentialOptions be the credentials member of options." |
+ // TODO(nhiroki): Add tests for credentialOptions (https://crbug.com/710837). |
+ WebURLRequest::FetchCredentialsMode credentials_mode = |
+ ParseCredentialsOption(options.credentials()); |
+ |
+ // Step 7: "Let outsideSettings be the relevant settings object of this." |
+ // TODO(nhiroki): outsideSettings will be used for posting a task to the |
+ // document's responsible event loop. We could use a task runner for the |
+ // purpose. |
+ |
+ // Step 8: "Let moduleResponsesMap be worklet's module responses map." |
+ // TODO(nhiroki): Implement moduleResponsesMap (https://crbug.com/627945). |
+ |
+ // Step 9: "Let workletGlobalScopeType be worklet's worklet global scope |
+ // type." |
+ // workletGlobalScopeType is encoded into the class name (e.g., PaintWorklet). |
+ |
+ // Step 10: "If the worklet's WorkletGlobalScopes is empty, run the following |
+ // steps:" |
+ // 10.1: "Create a WorkletGlobalScope given workletGlobalScopeType, |
+ // moduleResponsesMap, and outsideSettings." |
+ // 10.2: "Add the WorkletGlobalScope to worklet's WorkletGlobalScopes." |
+ // "Depending on the type of worklet the user agent may create additional |
+ // WorkletGlobalScopes at this time." |
+ while (NeedsToCreateGlobalScope()) |
+ proxies_.insert(CreateGlobalScope()); |
+ DCHECK_LT(1u, GetNumberOfGlobalScopes()); |
+ |
+ // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter |
+ // initialized to the length of worklet's WorkletGlobalScopes." |
+ WorkletPendingTasks* pending_tasks = |
+ new WorkletPendingTasks(GetNumberOfGlobalScopes(), resolver); |
+ |
+ // Step 12: "For each workletGlobalScope in the worklet's |
+ // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and |
+ // invoke a worklet script given workletGlobalScope, moduleURLRecord, |
+ // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, |
+ // and promise." |
+ // TODO(nhiroki): Queue a task instead of executing this here. |
+ for (const auto& proxy : proxies_) { |
+ proxy->FetchAndInvokeScript(module_url_record, credentials_mode, |
+ pending_tasks); |
+ } |
+} |
+ |
DEFINE_TRACE(Worklet) { |
ContextLifecycleObserver::Trace(visitor); |
} |