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 "modules/worklet/Worklet.h" | 5 #include "modules/worklet/Worklet.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/core/v8/ScriptSourceCode.h" | 8 #include "bindings/core/v8/ScriptSourceCode.h" |
9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
13 #include "core/inspector/InspectorInstrumentation.h" | 13 #include "core/inspector/InspectorInstrumentation.h" |
14 #include "modules/worklet/WorkletGlobalScope.h" | 14 #include "modules/worklet/WorkletGlobalScope.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 // static | 18 Worklet::Worklet(ExecutionContext* executionContext) |
19 Worklet* Worklet::create(LocalFrame* frame, ExecutionContext* executionContext) | |
20 { | |
21 Worklet* worklet = new Worklet(frame, executionContext); | |
22 worklet->suspendIfNeeded(); | |
23 return worklet; | |
24 } | |
25 | |
26 Worklet::Worklet(LocalFrame* frame, ExecutionContext* executionContext) | |
27 : ActiveDOMObject(executionContext) | 19 : ActiveDOMObject(executionContext) |
28 , m_workletGlobalScope(WorkletGlobalScope::create(frame, executionContext->u
rl(), executionContext->userAgent(), executionContext->getSecurityOrigin(), toIs
olate(executionContext))) | |
29 { | 20 { |
30 } | 21 } |
31 | 22 |
32 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) | 23 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
33 { | 24 { |
34 KURL scriptURL = getExecutionContext()->completeURL(url); | 25 KURL scriptURL = getExecutionContext()->completeURL(url); |
35 if (!scriptURL.isValid()) { | 26 if (!scriptURL.isValid()) { |
36 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); | 27 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); |
37 } | 28 } |
38 | 29 |
(...skipping 26 matching lines...) Expand all Loading... |
65 } | 56 } |
66 | 57 |
67 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver
* resolver) | 58 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver
* resolver) |
68 { | 59 { |
69 if (scriptLoader->failed()) { | 60 if (scriptLoader->failed()) { |
70 resolver->reject(DOMException::create(NetworkError)); | 61 resolver->reject(DOMException::create(NetworkError)); |
71 } else { | 62 } else { |
72 // TODO(ikilpatrick): Worklets don't have the same error behaviour | 63 // TODO(ikilpatrick): Worklets don't have the same error behaviour |
73 // as workers, etc. For a SyntaxError we should reject, however if | 64 // as workers, etc. For a SyntaxError we should reject, however if |
74 // the script throws a normal error, resolve. For now just resolve. | 65 // the script throws a normal error, resolve. For now just resolve. |
75 m_workletGlobalScope->scriptController()->evaluate(ScriptSourceCode(scri
ptLoader->script(), scriptLoader->url())); | 66 workletGlobalScope()->scriptController()->evaluate(ScriptSourceCode(scri
ptLoader->script(), scriptLoader->url())); |
76 InspectorInstrumentation::scriptImported(m_workletGlobalScope.get(), scr
iptLoader->identifier(), scriptLoader->script()); | 67 InspectorInstrumentation::scriptImported(workletGlobalScope(), scriptLoa
der->identifier(), scriptLoader->script()); |
77 resolver->resolve(); | 68 resolver->resolve(); |
78 } | 69 } |
79 | 70 |
80 size_t index = m_scriptLoaders.find(scriptLoader); | 71 size_t index = m_scriptLoaders.find(scriptLoader); |
81 | 72 |
82 ASSERT(index != kNotFound); | 73 ASSERT(index != kNotFound); |
83 ASSERT(m_resolvers[index] == resolver); | 74 ASSERT(m_resolvers[index] == resolver); |
84 | 75 |
85 m_scriptLoaders.remove(index); | 76 m_scriptLoaders.remove(index); |
86 m_resolvers.remove(index); | 77 m_resolvers.remove(index); |
87 } | 78 } |
88 | 79 |
89 void Worklet::stop() | 80 void Worklet::stop() |
90 { | 81 { |
91 m_workletGlobalScope->scriptController()->willScheduleExecutionTermination()
; | 82 workletGlobalScope()->scriptController()->willScheduleExecutionTermination()
; |
92 | 83 |
93 for (auto scriptLoader : m_scriptLoaders) { | 84 for (auto scriptLoader : m_scriptLoaders) { |
94 scriptLoader->cancel(); | 85 scriptLoader->cancel(); |
95 } | 86 } |
96 } | 87 } |
97 | 88 |
98 DEFINE_TRACE(Worklet) | 89 DEFINE_TRACE(Worklet) |
99 { | 90 { |
100 visitor->trace(m_resolvers); | 91 visitor->trace(m_resolvers); |
101 visitor->trace(m_workletGlobalScope); | |
102 ActiveDOMObject::trace(visitor); | 92 ActiveDOMObject::trace(visitor); |
103 } | 93 } |
104 | 94 |
105 } // namespace blink | 95 } // namespace blink |
OLD | NEW |