| 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/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | |
| 10 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/inspector/InspectorInstrumentation.h" | 11 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 12 #include "core/frame/LocalFrame.h" |
| 13 #include "core/loader/DocumentLoader.h" |
| 14 #include "core/loader/FrameFetchContext.h" |
| 13 #include "core/workers/WorkletGlobalScopeProxy.h" | 15 #include "core/workers/WorkletGlobalScopeProxy.h" |
| 16 #include "modules/worklet/WorkletScriptLoader.h" |
| 14 | 17 |
| 15 namespace blink { | 18 namespace blink { |
| 16 | 19 |
| 17 Worklet::Worklet(ExecutionContext* executionContext) | 20 Worklet::Worklet(ExecutionContext* executionContext, LocalFrame* frame) |
| 18 : ActiveDOMObject(executionContext) | 21 : ActiveDOMObject(executionContext) |
| 19 { | 22 { |
| 23 if (frame) { |
| 24 m_fetcher = frame->loader().documentLoader()->fetcher(); |
| 25 } else { |
| 26 m_fetcher = ResourceFetcher::create(/* context */ nullptr); |
| 27 } |
| 20 } | 28 } |
| 21 | 29 |
| 22 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) | 30 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
| 23 { | 31 { |
| 24 KURL scriptURL = getExecutionContext()->completeURL(url); | 32 KURL scriptURL = getExecutionContext()->completeURL(url); |
| 25 if (!scriptURL.isValid()) { | |
| 26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); | |
| 27 } | |
| 28 | 33 |
| 29 // TODO(ikilpatrick): Perform upfront CSP checks once we decide on a | 34 ResourceRequest resourceRequest(scriptURL); |
| 30 // CSP-policy for worklets. | 35 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript); |
| 36 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::internal); |
| 37 ScriptResource* resource = ScriptResource::fetch(request, fetcher()); |
| 38 m_resources.append(resource); |
| 31 | 39 |
| 32 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 40 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 33 m_resolvers.append(resolver); | |
| 34 | 41 |
| 35 ScriptPromise promise = resolver->promise(); | 42 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolver, w
orkletGlobalScopeProxy(), this); |
| 36 | 43 resource->addClient(workletLoader); |
| 37 // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow | 44 return resolver->promise(); |
| 38 // module loading support. For now just fetch a 'classic' script. | |
| 39 | |
| 40 // NOTE: WorkerScriptLoader may synchronously invoke its callbacks | |
| 41 // (resolving the promise) before we return it. | |
| 42 m_scriptLoaders.append(WorkerScriptLoader::create()); | |
| 43 m_scriptLoaders.last()->loadAsynchronously(*getExecutionContext(), scriptURL
, DenyCrossOriginRequests, | |
| 44 getExecutionContext()->securityContext().addressSpace(), | |
| 45 bind(&Worklet::onResponse, wrapPersistent(this), WTF::unretained(m_scrip
tLoaders.last().get())), | |
| 46 bind(&Worklet::onFinished, wrapPersistent(this), WTF::unretained(m_scrip
tLoaders.last().get()), wrapPersistent(resolver))); | |
| 47 | |
| 48 return promise; | |
| 49 } | 45 } |
| 50 | 46 |
| 51 void Worklet::onResponse(WorkerScriptLoader* scriptLoader) | 47 void Worklet::notifyFinished(Resource* resource) |
| 52 { | 48 { |
| 53 InspectorInstrumentation::didReceiveScriptResponse(getExecutionContext(), sc
riptLoader->identifier()); | 49 size_t index = m_resources.find(resource); |
| 54 } | 50 DCHECK(index != kNotFound); |
| 55 | 51 DCHECK(m_resources[index] == resource); |
| 56 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver
* resolver) | 52 m_resources.remove(index); |
| 57 { | |
| 58 if (scriptLoader->failed()) { | |
| 59 resolver->reject(DOMException::create(NetworkError)); | |
| 60 } else { | |
| 61 // TODO(ikilpatrick): Worklets don't have the same error behaviour | |
| 62 // as workers, etc. For a SyntaxError we should reject, however if | |
| 63 // the script throws a normal error, resolve. For now just resolve. | |
| 64 workletGlobalScopeProxy()->evaluateScript(scriptLoader->script(), script
Loader->url()); | |
| 65 InspectorInstrumentation::scriptImported(getExecutionContext(), scriptLo
ader->identifier(), scriptLoader->script()); | |
| 66 resolver->resolve(); | |
| 67 } | |
| 68 | |
| 69 size_t index = m_scriptLoaders.find(scriptLoader); | |
| 70 | |
| 71 ASSERT(index != kNotFound); | |
| 72 ASSERT(m_resolvers[index] == resolver); | |
| 73 | |
| 74 m_scriptLoaders.remove(index); | |
| 75 m_resolvers.remove(index); | |
| 76 } | 53 } |
| 77 | 54 |
| 78 void Worklet::stop() | 55 void Worklet::stop() |
| 79 { | 56 { |
| 80 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); | 57 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); |
| 81 | 58 |
| 82 for (auto scriptLoader : m_scriptLoaders) { | 59 for (auto resource : m_resources) { |
| 83 scriptLoader->cancel(); | 60 resource->loader()->cancel(); |
| 84 } | 61 } |
| 85 } | 62 } |
| 86 | 63 |
| 87 DEFINE_TRACE(Worklet) | 64 DEFINE_TRACE(Worklet) |
| 88 { | 65 { |
| 89 visitor->trace(m_resolvers); | 66 visitor->trace(m_resources); |
| 67 visitor->trace(m_fetcher); |
| 90 ActiveDOMObject::trace(visitor); | 68 ActiveDOMObject::trace(visitor); |
| 91 } | 69 } |
| 92 | 70 |
| 93 } // namespace blink | 71 } // namespace blink |
| OLD | NEW |