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 "core/workers/Worklet.h" | 5 #include "core/workers/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/ScriptSourceCode.h" |
9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
11 #include "core/dom/Document.h" | 11 #include "core/dom/Document.h" |
12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
13 #include "core/fetch/FetchInitiatorTypeNames.h" | 13 #include "core/fetch/FetchInitiatorTypeNames.h" |
14 #include "core/frame/LocalFrame.h" | 14 #include "core/frame/LocalFrame.h" |
15 #include "core/loader/DocumentLoader.h" | 15 #include "core/loader/DocumentLoader.h" |
16 #include "core/loader/FrameFetchContext.h" | 16 #include "core/loader/FrameFetchContext.h" |
17 #include "core/workers/WorkletGlobalScopeProxy.h" | 17 #include "core/workers/WorkletGlobalScopeProxy.h" |
18 #include "core/workers/WorkletScriptLoader.h" | 18 #include "core/workers/WorkletScriptLoader.h" |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 Worklet::Worklet(LocalFrame* frame) | 22 Worklet::Worklet(LocalFrame* frame) |
23 : ActiveDOMObject(frame->document()) | 23 : ActiveDOMObject(frame->document()) |
24 , m_fetcher(frame->loader().documentLoader()->fetcher()) | 24 , m_fetcher(frame->loader().documentLoader()->fetcher()) |
25 { | 25 { |
26 } | 26 } |
27 | 27 |
28 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) | 28 ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
29 { | 29 { |
| 30 if (!isInitialized()) { |
| 31 initialize(); |
| 32 } |
| 33 |
30 KURL scriptURL = getExecutionContext()->completeURL(url); | 34 KURL scriptURL = getExecutionContext()->completeURL(url); |
31 if (!scriptURL.isValid()) { | 35 if (!scriptURL.isValid()) { |
32 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); | 36 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SyntaxError, "'" + url + "' is not a valid URL.")); |
33 } | 37 } |
34 | 38 |
35 ResourceRequest resourceRequest(scriptURL); | 39 ResourceRequest resourceRequest(scriptURL); |
36 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript); | 40 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript); |
37 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::internal); | 41 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::internal); |
38 ScriptResource* resource = ScriptResource::fetch(request, fetcher()); | 42 ScriptResource* resource = ScriptResource::fetch(request, fetcher()); |
39 | 43 |
40 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 44 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
41 ScriptPromise promise = resolver->promise(); | 45 ScriptPromise promise = resolver->promise(); |
42 if (resource) { | 46 if (resource) { |
43 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolve
r, this, resource); | 47 WorkletScriptLoader* workletLoader = WorkletScriptLoader::create(resolve
r, this, resource); |
44 m_scriptLoaders.add(workletLoader); | 48 m_scriptLoaders.add(workletLoader); |
45 } else { | 49 } else { |
46 resolver->reject(DOMException::create(NetworkError)); | 50 resolver->reject(DOMException::create(NetworkError)); |
47 } | 51 } |
48 return promise; | 52 return promise; |
49 } | 53 } |
50 | 54 |
51 void Worklet::notifyFinished(WorkletScriptLoader* scriptLoader) | 55 void Worklet::notifyFinished(WorkletScriptLoader* scriptLoader) |
52 { | 56 { |
53 workletGlobalScopeProxy()->evaluateScript(ScriptSourceCode(scriptLoader->res
ource())); | 57 workletGlobalScopeProxy()->evaluateScript(ScriptSourceCode(scriptLoader->res
ource())); |
54 m_scriptLoaders.remove(scriptLoader); | 58 m_scriptLoaders.remove(scriptLoader); |
55 } | 59 } |
56 | 60 |
57 void Worklet::stop() | 61 void Worklet::stop() |
58 { | 62 { |
59 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); | 63 if (isInitialized()) { |
| 64 workletGlobalScopeProxy()->terminateWorkletGlobalScope(); |
| 65 } |
| 66 |
60 for (const auto& scriptLoader : m_scriptLoaders) { | 67 for (const auto& scriptLoader : m_scriptLoaders) { |
61 scriptLoader->cancel(); | 68 scriptLoader->cancel(); |
62 } | 69 } |
63 } | 70 } |
64 | 71 |
65 DEFINE_TRACE(Worklet) | 72 DEFINE_TRACE(Worklet) |
66 { | 73 { |
67 visitor->trace(m_fetcher); | 74 visitor->trace(m_fetcher); |
68 visitor->trace(m_scriptLoaders); | 75 visitor->trace(m_scriptLoaders); |
69 ActiveDOMObject::trace(visitor); | 76 ActiveDOMObject::trace(visitor); |
70 } | 77 } |
71 | 78 |
72 } // namespace blink | 79 } // namespace blink |
OLD | NEW |