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/MainThreadWorkletGlobalScope.h" | 5 #include "core/workers/MainThreadWorkletGlobalScope.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptSourceCode.h" | 7 #include "bindings/core/v8/ScriptSourceCode.h" |
8 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 8 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/frame/Deprecation.h" | 10 #include "core/frame/Deprecation.h" |
11 #include "core/frame/FrameConsole.h" | 11 #include "core/frame/FrameConsole.h" |
12 #include "core/frame/LocalFrame.h" | 12 #include "core/frame/LocalFrame.h" |
13 #include "core/inspector/MainThreadDebugger.h" | 13 #include "core/inspector/MainThreadDebugger.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 | 16 |
17 MainThreadWorkletGlobalScope::MainThreadWorkletGlobalScope( | 17 MainThreadWorkletGlobalScope::MainThreadWorkletGlobalScope( |
18 LocalFrame* frame, | 18 LocalFrame* frame, |
19 const KURL& url, | 19 const KURL& url, |
20 const String& user_agent, | 20 const String& user_agent, |
21 PassRefPtr<SecurityOrigin> security_origin, | 21 PassRefPtr<SecurityOrigin> security_origin, |
22 v8::Isolate* isolate, | 22 v8::Isolate* isolate) |
23 WorkletObjectProxy* object_proxy) | |
24 : WorkletGlobalScope(url, user_agent, std::move(security_origin), isolate), | 23 : WorkletGlobalScope(url, user_agent, std::move(security_origin), isolate), |
25 ContextClient(frame), | 24 ContextClient(frame) {} |
26 object_proxy_(object_proxy) {} | |
27 | 25 |
28 MainThreadWorkletGlobalScope::~MainThreadWorkletGlobalScope() {} | 26 MainThreadWorkletGlobalScope::~MainThreadWorkletGlobalScope() {} |
29 | 27 |
30 void MainThreadWorkletGlobalScope::CountFeature(UseCounter::Feature feature) { | 28 void MainThreadWorkletGlobalScope::CountFeature(UseCounter::Feature feature) { |
31 DCHECK(IsMainThread()); | 29 DCHECK(IsMainThread()); |
32 // A parent document is on the same thread, so just record API use in the | 30 // A parent document is on the same thread, so just record API use in the |
33 // document's UseCounter. | 31 // document's UseCounter. |
34 UseCounter::Count(GetFrame(), feature); | 32 UseCounter::Count(GetFrame(), feature); |
35 } | 33 } |
36 | 34 |
37 void MainThreadWorkletGlobalScope::CountDeprecation( | 35 void MainThreadWorkletGlobalScope::CountDeprecation( |
38 UseCounter::Feature feature) { | 36 UseCounter::Feature feature) { |
39 DCHECK(IsMainThread()); | 37 DCHECK(IsMainThread()); |
40 // A parent document is on the same thread, so just record API use in the | 38 // A parent document is on the same thread, so just record API use in the |
41 // document's UseCounter. | 39 // document's UseCounter. |
42 AddDeprecationMessage(feature); | 40 AddDeprecationMessage(feature); |
43 Deprecation::CountDeprecation(GetFrame(), feature); | 41 Deprecation::CountDeprecation(GetFrame(), feature); |
44 } | 42 } |
45 | 43 |
46 WorkerThread* MainThreadWorkletGlobalScope::GetThread() const { | 44 WorkerThread* MainThreadWorkletGlobalScope::GetThread() const { |
47 NOTREACHED(); | 45 NOTREACHED(); |
48 return nullptr; | 46 return nullptr; |
49 } | 47 } |
50 | 48 |
| 49 // Implementation of the first half of the "fetch and invoke a worklet script" |
| 50 // algorithm: |
| 51 // https://drafts.css-houdini.org/worklets/#fetch-and-invoke-a-worklet-script |
51 void MainThreadWorkletGlobalScope::FetchAndInvokeScript( | 52 void MainThreadWorkletGlobalScope::FetchAndInvokeScript( |
52 int32_t request_id, | 53 const KURL& module_url_record, |
53 const KURL& script_url) { | 54 WorkletPendingTasks* pending_tasks) { |
54 DCHECK(IsMainThread()); | 55 DCHECK(IsMainThread()); |
| 56 // Step 1: "Let insideSettings be the workletGlobalScope's associated |
| 57 // environment settings object." |
| 58 // Step 2: "Let script by the result of fetch a worklet script given |
| 59 // moduleURLRecord, moduleResponsesMap, credentialOptions, outsideSettings, |
| 60 // and insideSettings when it asynchronously completes." |
55 // TODO(nhiroki): Replace this with module script loading. | 61 // TODO(nhiroki): Replace this with module script loading. |
56 WorkletScriptLoader* script_loader = | 62 WorkletScriptLoader* script_loader = |
57 WorkletScriptLoader::Create(GetFrame()->GetDocument()->Fetcher(), this); | 63 WorkletScriptLoader::Create(GetFrame()->GetDocument()->Fetcher(), this); |
58 script_loader->set_request_id(request_id); | 64 loader_map_.Set(script_loader, pending_tasks); |
59 loader_set_.insert(script_loader); | 65 script_loader->FetchScript(module_url_record); |
60 script_loader->FetchScript(script_url); | |
61 } | 66 } |
62 | 67 |
63 void MainThreadWorkletGlobalScope::EvaluateScript( | 68 void MainThreadWorkletGlobalScope::EvaluateScript( |
64 const ScriptSourceCode& script_source_code) { | 69 const ScriptSourceCode& script_source_code) { |
65 // This should be called only for threaded worklets that still use classic | 70 // This should be called only for threaded worklets that still use classic |
66 // script loading. | 71 // script loading. |
67 NOTREACHED(); | 72 NOTREACHED(); |
68 } | 73 } |
69 | 74 |
| 75 // TODO(nhiroki): Add tests for termination. |
70 void MainThreadWorkletGlobalScope::TerminateWorkletGlobalScope() { | 76 void MainThreadWorkletGlobalScope::TerminateWorkletGlobalScope() { |
71 for (const auto& script_loader : loader_set_) | 77 for (auto it = loader_map_.begin(); it != loader_map_.end();) { |
| 78 WorkletScriptLoader* script_loader = it->key; |
| 79 // Cancel() eventually calls NotifyWorkletScriptLoadingFinished() and |
| 80 // removes |it| from |loader_map_|, so increment it in advance. |
| 81 ++it; |
72 script_loader->Cancel(); | 82 script_loader->Cancel(); |
| 83 } |
73 Dispose(); | 84 Dispose(); |
74 } | 85 } |
75 | 86 |
| 87 // Implementation of the second half of the "fetch and invoke a worklet script" |
| 88 // algorithm: |
| 89 // https://drafts.css-houdini.org/worklets/#fetch-and-invoke-a-worklet-script |
76 void MainThreadWorkletGlobalScope::NotifyWorkletScriptLoadingFinished( | 90 void MainThreadWorkletGlobalScope::NotifyWorkletScriptLoadingFinished( |
77 WorkletScriptLoader* script_loader, | 91 WorkletScriptLoader* script_loader, |
78 const ScriptSourceCode& source_code) { | 92 const ScriptSourceCode& source_code) { |
79 DCHECK(IsMainThread()); | 93 DCHECK(IsMainThread()); |
80 int32_t request_id = script_loader->request_id(); | 94 auto it = loader_map_.find(script_loader); |
81 loader_set_.erase(script_loader); | 95 DCHECK(it != loader_map_.end()); |
82 bool success = script_loader->WasScriptLoadSuccessful(); | 96 WorkletPendingTasks* pending_tasks = it->value; |
83 if (success) | 97 loader_map_.erase(it); |
84 ScriptController()->Evaluate(source_code); | 98 |
85 object_proxy_->DidFetchAndInvokeScript(request_id, success); | 99 if (!script_loader->WasScriptLoadSuccessful()) { |
| 100 // Step 3: "If script is null, then queue a task on outsideSettings's |
| 101 // responsible event loop to run these steps:" |
| 102 // The steps are implemented in WorkletPendingTasks::Abort(). |
| 103 // TODO(nhiroki): Queue a task instead of executing this here. |
| 104 pending_tasks->Abort(); |
| 105 return; |
| 106 } |
| 107 |
| 108 // Step 4: "Run a module script given script." |
| 109 ScriptController()->Evaluate(source_code); |
| 110 |
| 111 // Step 5: "Queue a task on outsideSettings's responsible event loop to run |
| 112 // these steps:" |
| 113 // The steps are implemented in WorkletPendingTasks::DecrementCounter(). |
| 114 // TODO(nhiroki): Queue a task instead of executing this here. |
| 115 pending_tasks->DecrementCounter(); |
86 } | 116 } |
87 | 117 |
88 void MainThreadWorkletGlobalScope::AddConsoleMessage( | 118 void MainThreadWorkletGlobalScope::AddConsoleMessage( |
89 ConsoleMessage* console_message) { | 119 ConsoleMessage* console_message) { |
90 GetFrame()->Console().AddMessage(console_message); | 120 GetFrame()->Console().AddMessage(console_message); |
91 } | 121 } |
92 | 122 |
93 void MainThreadWorkletGlobalScope::ExceptionThrown(ErrorEvent* event) { | 123 void MainThreadWorkletGlobalScope::ExceptionThrown(ErrorEvent* event) { |
94 MainThreadDebugger::Instance()->ExceptionThrown(this, event); | 124 MainThreadDebugger::Instance()->ExceptionThrown(this, event); |
95 } | 125 } |
96 | 126 |
97 DEFINE_TRACE(MainThreadWorkletGlobalScope) { | 127 DEFINE_TRACE(MainThreadWorkletGlobalScope) { |
98 visitor->Trace(loader_set_); | 128 visitor->Trace(loader_map_); |
99 visitor->Trace(object_proxy_); | |
100 WorkletGlobalScope::Trace(visitor); | 129 WorkletGlobalScope::Trace(visitor); |
101 ContextClient::Trace(visitor); | 130 ContextClient::Trace(visitor); |
102 } | 131 } |
103 | 132 |
104 } // namespace blink | 133 } // namespace blink |
OLD | NEW |