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 |
70 void MainThreadWorkletGlobalScope::TerminateWorkletGlobalScope() { | 75 void MainThreadWorkletGlobalScope::TerminateWorkletGlobalScope() { |
71 for (const auto& script_loader : loader_set_) | 76 for (const auto& it : loader_map_) { |
77 WorkletScriptLoader* script_loader = it.key; | |
72 script_loader->Cancel(); | 78 script_loader->Cancel(); |
79 WorkletPendingTasks* pending_tasks = it.value; | |
80 pending_tasks->Abort(); | |
81 } | |
73 Dispose(); | 82 Dispose(); |
74 } | 83 } |
75 | 84 |
85 // Implementation of the second half of the "fetch and invoke a worklet script" | |
86 // algorithm: | |
87 // https://drafts.css-houdini.org/worklets/#fetch-and-invoke-a-worklet-script | |
76 void MainThreadWorkletGlobalScope::NotifyWorkletScriptLoadingFinished( | 88 void MainThreadWorkletGlobalScope::NotifyWorkletScriptLoadingFinished( |
77 WorkletScriptLoader* script_loader, | 89 WorkletScriptLoader* script_loader, |
78 const ScriptSourceCode& source_code) { | 90 const ScriptSourceCode& source_code) { |
79 DCHECK(IsMainThread()); | 91 DCHECK(IsMainThread()); |
80 int32_t request_id = script_loader->request_id(); | 92 auto it = loader_map_.find(script_loader); |
81 loader_set_.erase(script_loader); | 93 if (it == loader_map_.end()) |
kouhei (in TOK)
2017/04/27 10:05:33
when is it == end?
nhiroki
2017/04/28 04:41:14
Yeah, this never happens. Replaced this with DCHEC
| |
82 bool success = script_loader->WasScriptLoadSuccessful(); | 94 return; |
83 if (success) | 95 WorkletPendingTasks* pending_tasks = it->value; |
84 ScriptController()->Evaluate(source_code); | 96 loader_map_.erase(it); |
85 object_proxy_->DidFetchAndInvokeScript(request_id, success); | 97 |
98 if (!script_loader->WasScriptLoadSuccessful()) { | |
99 // Step 3: "If script is null, then queue a task on outsideSettings's | |
100 // responsible event loop to run these steps:" | |
101 // The steps are implemented in WorkletPendingTasks::Abort(). | |
102 // TODO(nhiroki): Queue a task instead of executing this here. | |
103 pending_tasks->Abort(); | |
104 return; | |
105 } | |
106 | |
107 // Step 4: "Run a module script given script." | |
108 ScriptController()->Evaluate(source_code); | |
109 | |
110 // Step 5: "Queue a task on outsideSettings's responsible event loop to run | |
111 // these steps:" | |
112 // The steps are implemented in WorkletPendingTasks::DecrementCounter(). | |
113 // TODO(nhiroki): Queue a task instead of executing this here. | |
114 pending_tasks->DecrementCounter(); | |
86 } | 115 } |
87 | 116 |
88 void MainThreadWorkletGlobalScope::AddConsoleMessage( | 117 void MainThreadWorkletGlobalScope::AddConsoleMessage( |
89 ConsoleMessage* console_message) { | 118 ConsoleMessage* console_message) { |
90 GetFrame()->Console().AddMessage(console_message); | 119 GetFrame()->Console().AddMessage(console_message); |
91 } | 120 } |
92 | 121 |
93 void MainThreadWorkletGlobalScope::ExceptionThrown(ErrorEvent* event) { | 122 void MainThreadWorkletGlobalScope::ExceptionThrown(ErrorEvent* event) { |
94 MainThreadDebugger::Instance()->ExceptionThrown(this, event); | 123 MainThreadDebugger::Instance()->ExceptionThrown(this, event); |
95 } | 124 } |
96 | 125 |
97 DEFINE_TRACE(MainThreadWorkletGlobalScope) { | 126 DEFINE_TRACE(MainThreadWorkletGlobalScope) { |
98 visitor->Trace(loader_set_); | 127 visitor->Trace(loader_map_); |
99 visitor->Trace(object_proxy_); | |
100 WorkletGlobalScope::Trace(visitor); | 128 WorkletGlobalScope::Trace(visitor); |
101 ContextClient::Trace(visitor); | 129 ContextClient::Trace(visitor); |
102 } | 130 } |
103 | 131 |
104 } // namespace blink | 132 } // namespace blink |
OLD | NEW |