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/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(ExecutionContext* executionContext, LocalFrame* frame) |
yhirano
2016/07/29 02:19:23
Can you tell me the relationship between |frame| a
haraken
2016/07/29 08:26:35
It should be. If the two are different, you're doi
Gleb Lanbin
2016/07/29 19:23:14
Done.
| |
18 : ActiveDOMObject(executionContext) | 22 : ActiveDOMObject(executionContext) |
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 m_resources.append(resource); |
38 // module loading support. For now just fetch a 'classic' script. | 43 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolve r, this); |
39 | 44 resource->addClient(workletLoader); |
yhirano
2016/07/29 02:19:23
resource -> workerLoader is a weak reference (actu
yhirano
2016/07/29 02:19:23
You need to remove the client manually from the re
Gleb Lanbin
2016/07/29 19:23:14
Done. Thanks!
Gleb Lanbin
2016/07/29 19:23:14
Done.
| |
40 // NOTE: WorkerScriptLoader may synchronously invoke its callbacks | 45 } else { |
41 // (resolving the promise) before we return it. | 46 resolver->reject(DOMException::create(NetworkError)); |
42 m_scriptLoaders.append(WorkerScriptLoader::create()); | 47 } |
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; | 48 return promise; |
49 } | 49 } |
50 | 50 |
51 void Worklet::onResponse(WorkerScriptLoader* scriptLoader) | 51 void Worklet::notifyFinished(Resource* resource) |
52 { | 52 { |
53 InspectorInstrumentation::didReceiveScriptResponse(getExecutionContext(), sc riptLoader->identifier()); | 53 workletGlobalScopeProxy()->evaluateScript(ScriptSourceCode(toScriptResource( resource))); |
54 } | |
55 | 54 |
56 void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver * resolver) | 55 size_t index = m_resources.find(resource); |
57 { | 56 DCHECK(index != kNotFound); |
yhirano
2016/07/29 02:19:23
DCHECK_NE
Gleb Lanbin
2016/07/29 19:23:14
Done.
| |
58 if (scriptLoader->failed()) { | 57 DCHECK(m_resources[index] == resource); |
yhirano
2016/07/29 02:19:23
DCHECK_EQ
Gleb Lanbin
2016/07/29 19:23:14
Done.
| |
59 resolver->reject(DOMException::create(NetworkError)); | 58 m_resources.remove(index); |
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 } | 59 } |
77 | 60 |
78 void Worklet::stop() | 61 void Worklet::stop() |
79 { | 62 { |
80 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); | 63 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); |
81 | 64 for (const auto& resource : m_resources) { |
82 for (auto scriptLoader : m_scriptLoaders) { | 65 resource->loader()->cancel(); |
83 scriptLoader->cancel(); | |
84 } | 66 } |
85 } | 67 } |
86 | 68 |
87 DEFINE_TRACE(Worklet) | 69 DEFINE_TRACE(Worklet) |
88 { | 70 { |
89 visitor->trace(m_resolvers); | 71 visitor->trace(m_resources); |
72 visitor->trace(m_fetcher); | |
90 ActiveDOMObject::trace(visitor); | 73 ActiveDOMObject::trace(visitor); |
91 } | 74 } |
92 | 75 |
93 } // namespace blink | 76 } // namespace blink |
OLD | NEW |