Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Side by Side Diff: third_party/WebKit/Source/modules/worklet/Worklet.cpp

Issue 2178223002: Refactor Worklet class to use ScriptResource (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: set m_fetcher in initialization list, s/override/final, copyright etc. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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)
22 , m_fetcher(frame->loader().documentLoader()->fetcher())
19 { 23 {
20 } 24 }
21 25
22 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) 26 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url)
23 { 27 {
24 KURL scriptURL = getExecutionContext()->completeURL(url); 28 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.
25 if (!scriptURL.isValid()) {
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(SyntaxError, "'" + url + "' is not a valid URL."));
27 }
28 29
29 // TODO(ikilpatrick): Perform upfront CSP checks once we decide on a 30 ResourceRequest resourceRequest(scriptURL);
30 // CSP-policy for worklets. 31 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript);
32 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::internal);
33 ScriptResource* resource = ScriptResource::fetch(request, fetcher());
31 34
32 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 35 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
33 m_resolvers.append(resolver);
34
35 ScriptPromise promise = resolver->promise(); 36 ScriptPromise promise = resolver->promise();
36 37 if (resource) {
37 // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow 38 m_resources.append(resource);
38 // module loading support. For now just fetch a 'classic' script. 39 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolve r, workletGlobalScopeProxy(), this);
39 40 resource->addClient(workletLoader);
40 // NOTE: WorkerScriptLoader may synchronously invoke its callbacks 41 } else {
41 // (resolving the promise) before we return it. 42 resolver->reject(DOMException::create(NetworkError));
42 m_scriptLoaders.append(WorkerScriptLoader::create()); 43 }
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; 44 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) {
ikilpatrick 2016/07/28 22:42:10 auto& ?
Gleb Lanbin 2016/07/29 01:40:23 Done.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698