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" |
| 8 #include "bindings/core/v8/ScriptSourceCode.h" |
7 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" |
8 | 13 |
9 namespace blink { | 14 namespace blink { |
10 | 15 |
11 // static | 16 // static |
12 Worklet* Worklet::create(ExecutionContext* executionContext) | 17 Worklet* Worklet::create(ExecutionContext* executionContext) |
13 { | 18 { |
14 return new Worklet(executionContext); | 19 Worklet* worklet = new Worklet(executionContext); |
| 20 worklet->suspendIfNeeded(); |
| 21 return worklet; |
15 } | 22 } |
16 | 23 |
17 Worklet::Worklet(ExecutionContext* executionContext) | 24 Worklet::Worklet(ExecutionContext* executionContext) |
18 : m_workletGlobalScope(WorkletGlobalScope::create(executionContext->url(), e
xecutionContext->userAgent(), toIsolate(executionContext))) | 25 : ActiveDOMObject(executionContext) |
| 26 , m_workletGlobalScope(WorkletGlobalScope::create(executionContext->url(), e
xecutionContext->userAgent(), executionContext->securityOrigin(), toIsolate(exec
utionContext))) |
19 { | 27 { |
20 } | 28 } |
21 | 29 |
| 30 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
| 31 { |
| 32 KURL scriptURL = executionContext()->completeURL(url); |
| 33 if (!scriptURL.isValid()) { |
| 34 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); |
| 35 } |
| 36 |
| 37 // TODO(ikilpatrick): Perform upfront CSP checks once we decide on a |
| 38 // CSP-policy for worklets. |
| 39 |
| 40 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 41 m_resolvers.append(resolver); |
| 42 |
| 43 ScriptPromise promise = resolver->promise(); |
| 44 |
| 45 // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow |
| 46 // module loading support. For now just fetch a 'classic' script. |
| 47 |
| 48 // NOTE: WorkerScriptLoader may synchronously invoke its callbacks |
| 49 // (resolving the promise) before we return it. |
| 50 m_scriptLoaders.append(WorkerScriptLoader::create()); |
| 51 m_scriptLoaders.last()->loadAsynchronously(*executionContext(), scriptURL, D
enyCrossOriginRequests, |
| 52 bind(&Worklet::onResponse, this), |
| 53 bind(&Worklet::onFinished, this, m_scriptLoaders.last().get(), resolver)
); |
| 54 |
| 55 return promise; |
| 56 } |
| 57 |
| 58 void Worklet::onResponse() |
| 59 { |
| 60 // TODO(ikilpatrick): Add devtools instrumentation on worklet script |
| 61 // resource loading. |
| 62 } |
| 63 |
| 64 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver
* resolver) |
| 65 { |
| 66 if (resolver->executionContext() || resolver->executionContext()->activeDOMO
bjectsAreStopped()) { |
| 67 if (scriptLoader->failed()) { |
| 68 resolver->reject(DOMException::create(NetworkError)); |
| 69 } else { |
| 70 // TODO(ikilpatrick): Worklets don't have the same error behaviour |
| 71 // as workers, etc. For a SyntaxError we should reject, however if |
| 72 // the script throws a normal error, resolve. For now just resolve. |
| 73 m_workletGlobalScope->script()->evaluate(ScriptSourceCode(scriptLoad
er->script(), scriptLoader->url())); |
| 74 resolver->resolve(); |
| 75 } |
| 76 } |
| 77 |
| 78 size_t index = m_scriptLoaders.find(scriptLoader); |
| 79 |
| 80 ASSERT(index != kNotFound); |
| 81 ASSERT(m_resolvers[index] == resolver); |
| 82 |
| 83 m_scriptLoaders.remove(index); |
| 84 m_resolvers.remove(index); |
| 85 } |
| 86 |
| 87 void Worklet::stop() |
| 88 { |
| 89 m_workletGlobalScope->script()->willScheduleExecutionTermination(); |
| 90 |
| 91 for (auto scriptLoader : m_scriptLoaders) { |
| 92 scriptLoader->cancel(); |
| 93 } |
| 94 } |
| 95 |
| 96 bool Worklet::hasPendingActivity() const |
| 97 { |
| 98 return !m_scriptLoaders.isEmpty(); |
| 99 } |
| 100 |
22 DEFINE_TRACE(Worklet) | 101 DEFINE_TRACE(Worklet) |
23 { | 102 { |
| 103 visitor->trace(m_resolvers); |
24 visitor->trace(m_workletGlobalScope); | 104 visitor->trace(m_workletGlobalScope); |
| 105 ActiveDOMObject::trace(visitor); |
25 } | 106 } |
26 | 107 |
27 } // namespace blink | 108 } // namespace blink |
OLD | NEW |