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

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: synced to the head 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/ScriptSourceCode.h"
8 #include "bindings/core/v8/V8Binding.h" 9 #include "bindings/core/v8/V8Binding.h"
9 #include "bindings/core/v8/WorkerOrWorkletScriptController.h"
10 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/inspector/InspectorInstrumentation.h" 12 #include "core/fetch/FetchInitiatorTypeNames.h"
13 #include "core/frame/LocalFrame.h"
14 #include "core/loader/DocumentLoader.h"
15 #include "core/loader/FrameFetchContext.h"
13 #include "core/workers/WorkletGlobalScopeProxy.h" 16 #include "core/workers/WorkletGlobalScopeProxy.h"
17 #include "modules/worklet/WorkletScriptLoader.h"
14 18
15 namespace blink { 19 namespace blink {
16 20
17 Worklet::Worklet(ExecutionContext* executionContext) 21 Worklet::Worklet(LocalFrame* frame)
18 : ActiveDOMObject(executionContext) 22 : ActiveDOMObject(frame->document())
23 , m_fetcher(frame->loader().documentLoader()->fetcher())
19 { 24 {
20 } 25 }
21 26
22 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) 27 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url)
23 { 28 {
24 KURL scriptURL = getExecutionContext()->completeURL(url); 29 KURL scriptURL = getExecutionContext()->completeURL(url);
25 if (!scriptURL.isValid()) { 30 if (!scriptURL.isValid()) {
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(SyntaxError, "'" + url + "' is not a valid URL.")); 31 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(SyntaxError, "'" + url + "' is not a valid URL."));
27 } 32 }
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());
31 38
32 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
33 m_resolvers.append(resolver);
34
35 ScriptPromise promise = resolver->promise(); 40 ScriptPromise promise = resolver->promise();
36 41 if (resource) {
37 // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow 42 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolve r, this, resource);
38 // module loading support. For now just fetch a 'classic' script. 43 m_scriptLoaders.add(workletLoader);
39 44 } else {
40 // NOTE: WorkerScriptLoader may synchronously invoke its callbacks 45 resolver->reject(DOMException::create(NetworkError));
41 // (resolving the promise) before we return it. 46 }
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; 47 return promise;
49 } 48 }
50 49
51 void Worklet::onResponse(WorkerScriptLoader* scriptLoader) 50 void Worklet::notifyFinished(WorkletScriptLoader* scriptLoader)
52 { 51 {
53 InspectorInstrumentation::didReceiveScriptResponse(getExecutionContext(), sc riptLoader->identifier()); 52 workletGlobalScopeProxy()->evaluateScript(ScriptSourceCode(scriptLoader->res ource()));
54 } 53 m_scriptLoaders.remove(scriptLoader);
55
56 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver * resolver)
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 } 54 }
77 55
78 void Worklet::stop() 56 void Worklet::stop()
79 { 57 {
80 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); 58 workletGlobalScopeProxy()->terminateWorkletGlobalScope();
81 59 for (const auto& scriptLoader : m_scriptLoaders) {
82 for (auto scriptLoader : m_scriptLoaders) {
83 scriptLoader->cancel(); 60 scriptLoader->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_fetcher);
67 visitor->trace(m_scriptLoaders);
90 ActiveDOMObject::trace(visitor); 68 ActiveDOMObject::trace(visitor);
91 } 69 }
92 70
93 } // namespace blink 71 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/worklet/Worklet.h ('k') | third_party/WebKit/Source/modules/worklet/WorkletScriptLoader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698