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 "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/TaskRunnerHelper.h" | 10 #include "core/dom/TaskRunnerHelper.h" |
11 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
12 #include "core/workers/WorkletGlobalScopeProxy.h" | 12 #include "core/workers/WorkletGlobalScopeProxy.h" |
| 13 #include "core/workers/WorkletPendingTasks.h" |
| 14 #include "platform/wtf/WTF.h" |
| 15 #include "public/platform/WebURLRequest.h" |
13 | 16 |
14 namespace blink { | 17 namespace blink { |
15 | 18 |
| 19 namespace { |
| 20 |
| 21 WebURLRequest::FetchCredentialsMode ParseCredentialsOption( |
| 22 const String& credentials_option) { |
| 23 if (credentials_option == "omit") |
| 24 return WebURLRequest::kFetchCredentialsModeOmit; |
| 25 if (credentials_option == "same-origin") |
| 26 return WebURLRequest::kFetchCredentialsModeSameOrigin; |
| 27 if (credentials_option == "include") |
| 28 return WebURLRequest::kFetchCredentialsModeInclude; |
| 29 NOTREACHED(); |
| 30 return WebURLRequest::kFetchCredentialsModeOmit; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
16 Worklet::Worklet(LocalFrame* frame) | 35 Worklet::Worklet(LocalFrame* frame) |
17 : ContextLifecycleObserver(frame->GetDocument()) { | 36 : ContextLifecycleObserver(frame->GetDocument()) { |
18 DCHECK(IsMainThread()); | 37 DCHECK(IsMainThread()); |
19 } | 38 } |
20 | 39 |
21 // Implementation of the first half of the "addModule(moduleURL, options)" | 40 // Implementation of the first half of the "addModule(moduleURL, options)" |
22 // algorithm: | 41 // algorithm: |
23 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule | 42 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
24 ScriptPromise Worklet::addModule(ScriptState* script_state, | 43 ScriptPromise Worklet::addModule(ScriptState* script_state, |
25 const String& module_url, | 44 const String& module_url, |
(...skipping 29 matching lines...) Expand all Loading... |
55 // |kUnspecedLoading| is used here because this is a part of script module | 74 // |kUnspecedLoading| is used here because this is a part of script module |
56 // loading. | 75 // loading. |
57 TaskRunnerHelper::Get(TaskType::kUnspecedLoading, script_state) | 76 TaskRunnerHelper::Get(TaskType::kUnspecedLoading, script_state) |
58 ->PostTask( | 77 ->PostTask( |
59 BLINK_FROM_HERE, | 78 BLINK_FROM_HERE, |
60 WTF::Bind(&Worklet::FetchAndInvokeScript, WrapPersistent(this), | 79 WTF::Bind(&Worklet::FetchAndInvokeScript, WrapPersistent(this), |
61 module_url_record, options, WrapPersistent(resolver))); | 80 module_url_record, options, WrapPersistent(resolver))); |
62 return promise; | 81 return promise; |
63 } | 82 } |
64 | 83 |
| 84 void Worklet::ContextDestroyed(ExecutionContext* execution_context) { |
| 85 DCHECK(IsMainThread()); |
| 86 for (const auto& proxy : proxies_) |
| 87 proxy->TerminateWorkletGlobalScope(); |
| 88 } |
| 89 |
| 90 WorkletGlobalScopeProxy* Worklet::FindAvailableGlobalScope() const { |
| 91 DCHECK(IsMainThread()); |
| 92 // TODO(nhiroki): Support the case where there are multiple global scopes. |
| 93 DCHECK_EQ(1u, GetNumberOfGlobalScopes()); |
| 94 return proxies_.begin()->get(); |
| 95 } |
| 96 |
| 97 // Implementation of the second half of the "addModule(moduleURL, options)" |
| 98 // algorithm: |
| 99 // https://drafts.css-houdini.org/worklets/#dom-worklet-addmodule |
| 100 void Worklet::FetchAndInvokeScript(const KURL& module_url_record, |
| 101 const WorkletOptions& options, |
| 102 ScriptPromiseResolver* resolver) { |
| 103 DCHECK(IsMainThread()); |
| 104 if (!GetExecutionContext()) |
| 105 return; |
| 106 |
| 107 // Step 6: "Let credentialOptions be the credentials member of options." |
| 108 // TODO(nhiroki): Add tests for credentialOptions (https://crbug.com/710837). |
| 109 WebURLRequest::FetchCredentialsMode credentials_mode = |
| 110 ParseCredentialsOption(options.credentials()); |
| 111 |
| 112 // Step 7: "Let outsideSettings be the relevant settings object of this." |
| 113 // TODO(nhiroki): outsideSettings will be used for posting a task to the |
| 114 // document's responsible event loop. We could use a task runner for the |
| 115 // purpose. |
| 116 |
| 117 // Step 8: "Let moduleResponsesMap be worklet's module responses map." |
| 118 // TODO(nhiroki): Implement moduleResponsesMap (https://crbug.com/627945). |
| 119 |
| 120 // Step 9: "Let workletGlobalScopeType be worklet's worklet global scope |
| 121 // type." |
| 122 // workletGlobalScopeType is encoded into the class name (e.g., PaintWorklet). |
| 123 |
| 124 // Step 10: "If the worklet's WorkletGlobalScopes is empty, run the following |
| 125 // steps:" |
| 126 // 10.1: "Create a WorkletGlobalScope given workletGlobalScopeType, |
| 127 // moduleResponsesMap, and outsideSettings." |
| 128 // 10.2: "Add the WorkletGlobalScope to worklet's WorkletGlobalScopes." |
| 129 // "Depending on the type of worklet the user agent may create additional |
| 130 // WorkletGlobalScopes at this time." |
| 131 while (NeedsToCreateGlobalScope()) |
| 132 proxies_.insert(CreateGlobalScope()); |
| 133 DCHECK_LT(1u, GetNumberOfGlobalScopes()); |
| 134 |
| 135 // Step 11: "Let pendingTaskStruct be a new pending tasks struct with counter |
| 136 // initialized to the length of worklet's WorkletGlobalScopes." |
| 137 WorkletPendingTasks* pending_tasks = |
| 138 new WorkletPendingTasks(GetNumberOfGlobalScopes(), resolver); |
| 139 |
| 140 // Step 12: "For each workletGlobalScope in the worklet's |
| 141 // WorkletGlobalScopes, queue a task on the workletGlobalScope to fetch and |
| 142 // invoke a worklet script given workletGlobalScope, moduleURLRecord, |
| 143 // moduleResponsesMap, credentialOptions, outsideSettings, pendingTaskStruct, |
| 144 // and promise." |
| 145 // TODO(nhiroki): Queue a task instead of executing this here. |
| 146 for (const auto& proxy : proxies_) { |
| 147 proxy->FetchAndInvokeScript(module_url_record, credentials_mode, |
| 148 pending_tasks); |
| 149 } |
| 150 } |
| 151 |
65 DEFINE_TRACE(Worklet) { | 152 DEFINE_TRACE(Worklet) { |
66 ContextLifecycleObserver::Trace(visitor); | 153 ContextLifecycleObserver::Trace(visitor); |
67 } | 154 } |
68 | 155 |
69 } // namespace blink | 156 } // namespace blink |
OLD | NEW |