| 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..77d5a93696e0f16d6d153079c7133309d3cccacb 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)
|
| {
|
| + if (frame) {
|
| + m_fetcher = frame->loader().documentLoader()->fetcher();
|
| + } else {
|
| + m_fetcher = ResourceFetcher::create(/* context */ nullptr);
|
| + }
|
| }
|
|
|
| ScriptPromise Worklet::import(ScriptState* scriptState, const String& url)
|
| {
|
| KURL scriptURL = getExecutionContext()->completeURL(url);
|
| - 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());
|
| + m_resources.append(resource);
|
|
|
| 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)));
|
| -
|
| - return promise;
|
| -}
|
|
|
| -void Worklet::onResponse(WorkerScriptLoader* scriptLoader)
|
| -{
|
| - InspectorInstrumentation::didReceiveScriptResponse(getExecutionContext(), scriptLoader->identifier());
|
| + WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolver, workletGlobalScopeProxy(), this);
|
| + resource->addClient(workletLoader);
|
| + return resolver->promise();
|
| }
|
|
|
| -void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver* resolver)
|
| +void Worklet::notifyFinished(Resource* resource)
|
| {
|
| - 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) {
|
| + resource->loader()->cancel();
|
| }
|
| }
|
|
|
| DEFINE_TRACE(Worklet)
|
| {
|
| - visitor->trace(m_resolvers);
|
| + visitor->trace(m_resources);
|
| + visitor->trace(m_fetcher);
|
| ActiveDOMObject::trace(visitor);
|
| }
|
|
|
|
|