Chromium Code Reviews| 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 m_scriptLoaders.append(WorkerScriptLoader::create()); | |
| 48 m_scriptLoaders.last()->loadAsynchronously(*executionContext(), scriptURL, D enyCrossOriginRequests, | |
| 49 bind(&Worklet::onResponse, this), | |
| 50 bind(&Worklet::onFinished, this, m_scriptLoaders.last().get(), resolver) ); | |
|
kinuko
2016/02/12 07:25:44
In some error cases onFinished could run before re
ikilpatrick
2016/02/12 22:59:57
Done.
| |
| 51 | |
| 52 return promise; | |
| 53 } | |
| 54 | |
| 55 void Worklet::onResponse() | |
| 56 { | |
| 57 // TODO(ikilpatrick): Add devtools instrumentation on worklet script | |
| 58 // resource loading. | |
| 59 } | |
| 60 | |
| 61 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver * resolver) | |
| 62 { | |
| 63 if (scriptLoader->failed()) { | |
| 64 resolver->reject(DOMException::create(NetworkError)); | |
| 65 } else { | |
| 66 m_workletGlobalScope->script()->evaluate(ScriptSourceCode(scriptLoader-> script(), scriptLoader->url())); | |
|
kinuko
2016/02/12 07:25:45
Should we catch error event here?
ikilpatrick
2016/02/12 22:59:57
Added note for the current thinking at the moment.
| |
| 67 resolver->resolve(); | |
|
kinuko
2016/02/12 07:25:45
Maybe check resolver->executionContext() || resolv
ikilpatrick
2016/02/12 22:59:57
Added guard for the moment.
| |
| 68 } | |
| 69 | |
| 70 size_t index = m_scriptLoaders.find(scriptLoader); | |
| 71 | |
| 72 ASSERT(index != kNotFound); | |
| 73 ASSERT(m_resolvers[index] == resolver); | |
| 74 | |
| 75 m_scriptLoaders.remove(index); | |
| 76 m_resolvers.remove(index); | |
| 77 } | |
| 78 | |
| 79 void Worklet::stop() | |
| 80 { | |
| 81 m_workletGlobalScope->script()->willScheduleExecutionTermination(); | |
| 82 | |
| 83 for (auto scriptLoader : m_scriptLoaders) { | |
| 84 scriptLoader->cancel(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 bool Worklet::hasPendingActivity() const | |
| 89 { | |
| 90 return !m_scriptLoaders.isEmpty(); | |
| 91 } | |
| 92 | |
| 22 DEFINE_TRACE(Worklet) | 93 DEFINE_TRACE(Worklet) |
| 23 { | 94 { |
| 95 visitor->trace(m_resolvers); | |
| 24 visitor->trace(m_workletGlobalScope); | 96 visitor->trace(m_workletGlobalScope); |
| 97 ActiveDOMObject::trace(visitor); | |
| 25 } | 98 } |
| 26 | 99 |
| 27 } // namespace blink | 100 } // namespace blink |
| OLD | NEW |