Chromium Code Reviews| Index: third_party/WebKit/Source/modules/worklet/Worklet.cpp |
| diff --git a/third_party/WebKit/Source/modules/worklet/Worklet.cpp b/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
| index 54993701b26d3318a46beafde5caadf43c95419a..207da36d94bc5d0f7f355e9d958fc5da21618529 100644 |
| --- a/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
| +++ b/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
| @@ -6,87 +6,65 @@ |
| #include "bindings/core/v8/ScriptPromiseResolver.h" |
| #include "bindings/core/v8/V8Binding.h" |
| -#include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| #include "core/dom/DOMException.h" |
| #include "core/dom/ExceptionCode.h" |
| -#include "core/inspector/InspectorInstrumentation.h" |
| +#include "core/fetch/FetchInitiatorTypeNames.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "core/loader/DocumentLoader.h" |
| +#include "core/loader/FrameFetchContext.h" |
| #include "core/workers/WorkletGlobalScopeProxy.h" |
| +#include "modules/worklet/WorkletScriptLoader.h" |
| namespace blink { |
| -Worklet::Worklet(ExecutionContext* executionContext) |
| +Worklet::Worklet(ExecutionContext* executionContext, LocalFrame* frame) |
| : ActiveDOMObject(executionContext) |
| + , m_fetcher(frame->loader().documentLoader()->fetcher()) |
| { |
| } |
| ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
| { |
| KURL scriptURL = getExecutionContext()->completeURL(url); |
|
ikilpatrick
2016/07/28 22:42:10
should we still do the scriptURL isValid up front
Gleb Lanbin
2016/07/29 01:40:23
Done.
|
| - if (!scriptURL.isValid()) { |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(SyntaxError, "'" + url + "' is not a valid URL.")); |
| - } |
| - // TODO(ikilpatrick): Perform upfront CSP checks once we decide on a |
| - // CSP-policy for worklets. |
| + ResourceRequest resourceRequest(scriptURL); |
| + resourceRequest.setRequestContext(WebURLRequest::RequestContextScript); |
| + FetchRequest request(resourceRequest, FetchInitiatorTypeNames::internal); |
| + ScriptResource* resource = ScriptResource::fetch(request, fetcher()); |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| - m_resolvers.append(resolver); |
| - |
| ScriptPromise promise = resolver->promise(); |
| - |
| - // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow |
| - // module loading support. For now just fetch a 'classic' script. |
| - |
| - // NOTE: WorkerScriptLoader may synchronously invoke its callbacks |
| - // (resolving the promise) before we return it. |
| - m_scriptLoaders.append(WorkerScriptLoader::create()); |
| - m_scriptLoaders.last()->loadAsynchronously(*getExecutionContext(), scriptURL, DenyCrossOriginRequests, |
| - getExecutionContext()->securityContext().addressSpace(), |
| - bind(&Worklet::onResponse, wrapPersistent(this), WTF::unretained(m_scriptLoaders.last().get())), |
| - bind(&Worklet::onFinished, wrapPersistent(this), WTF::unretained(m_scriptLoaders.last().get()), wrapPersistent(resolver))); |
| - |
| + if (resource) { |
| + m_resources.append(resource); |
| + WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolver, workletGlobalScopeProxy(), this); |
| + resource->addClient(workletLoader); |
| + } else { |
| + resolver->reject(DOMException::create(NetworkError)); |
| + } |
| return promise; |
| } |
| -void Worklet::onResponse(WorkerScriptLoader* scriptLoader) |
| +void Worklet::notifyFinished(Resource* resource) |
| { |
| - InspectorInstrumentation::didReceiveScriptResponse(getExecutionContext(), scriptLoader->identifier()); |
| -} |
| - |
| -void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver* resolver) |
| -{ |
| - if (scriptLoader->failed()) { |
| - resolver->reject(DOMException::create(NetworkError)); |
| - } else { |
| - // TODO(ikilpatrick): Worklets don't have the same error behaviour |
| - // as workers, etc. For a SyntaxError we should reject, however if |
| - // the script throws a normal error, resolve. For now just resolve. |
| - workletGlobalScopeProxy()->evaluateScript(scriptLoader->script(), scriptLoader->url()); |
| - InspectorInstrumentation::scriptImported(getExecutionContext(), scriptLoader->identifier(), scriptLoader->script()); |
| - resolver->resolve(); |
| - } |
| - |
| - size_t index = m_scriptLoaders.find(scriptLoader); |
| - |
| - ASSERT(index != kNotFound); |
| - ASSERT(m_resolvers[index] == resolver); |
| - |
| - m_scriptLoaders.remove(index); |
| - m_resolvers.remove(index); |
| + size_t index = m_resources.find(resource); |
| + DCHECK(index != kNotFound); |
| + DCHECK(m_resources[index] == resource); |
| + m_resources.remove(index); |
| } |
| void Worklet::stop() |
| { |
| workletGlobalScopeProxy()->terminateWorkletGlobalScope(); |
| - for (auto scriptLoader : m_scriptLoaders) { |
| - scriptLoader->cancel(); |
| + for (auto resource : m_resources) { |
|
ikilpatrick
2016/07/28 22:42:10
auto& ?
Gleb Lanbin
2016/07/29 01:40:23
Done.
|
| + resource->loader()->cancel(); |
| } |
| } |
| DEFINE_TRACE(Worklet) |
| { |
| - visitor->trace(m_resolvers); |
| + visitor->trace(m_resources); |
| + visitor->trace(m_fetcher); |
| ActiveDOMObject::trace(visitor); |
| } |