OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/ThreadedWorklet.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptSourceCode.h" | 7 #include "bindings/core/v8/ScriptSourceCode.h" |
8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
12 #include "core/frame/LocalFrame.h" | 12 #include "core/frame/LocalFrame.h" |
13 #include "core/workers/WorkletGlobalScopeProxy.h" | 13 #include "core/workers/WorkletGlobalScopeProxy.h" |
14 #include "platform/wtf/WTF.h" | 14 #include "platform/wtf/WTF.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 Worklet::Worklet(LocalFrame* frame) | 18 ThreadedWorklet::ThreadedWorklet(LocalFrame* frame) : Worklet(frame) {} |
19 : ContextLifecycleObserver(frame->GetDocument()), frame_(frame) {} | |
20 | 19 |
21 ScriptPromise Worklet::import(ScriptState* script_state, const String& url) { | 20 ScriptPromise ThreadedWorklet::import(ScriptState* script_state, |
| 21 const String& url) { |
22 DCHECK(IsMainThread()); | 22 DCHECK(IsMainThread()); |
23 if (!GetExecutionContext()) { | 23 if (!GetExecutionContext()) { |
24 return ScriptPromise::RejectWithDOMException( | 24 return ScriptPromise::RejectWithDOMException( |
25 script_state, DOMException::Create(kInvalidStateError, | 25 script_state, DOMException::Create(kInvalidStateError, |
26 "This frame is already detached")); | 26 "This frame is already detached")); |
27 } | 27 } |
28 | 28 |
29 KURL script_url = GetExecutionContext()->CompleteURL(url); | 29 KURL script_url = GetExecutionContext()->CompleteURL(url); |
30 if (!script_url.IsValid()) { | 30 if (!script_url.IsValid()) { |
31 return ScriptPromise::RejectWithDOMException( | 31 return ScriptPromise::RejectWithDOMException( |
32 script_state, DOMException::Create( | 32 script_state, DOMException::Create( |
33 kSyntaxError, "'" + url + "' is not a valid URL.")); | 33 kSyntaxError, "'" + url + "' is not a valid URL.")); |
34 } | 34 } |
35 | 35 |
36 if (!IsInitialized()) | 36 if (!IsInitialized()) |
37 Initialize(); | 37 Initialize(); |
38 | 38 |
39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
40 ScriptPromise promise = resolver->Promise(); | 40 ScriptPromise promise = resolver->Promise(); |
41 | 41 |
42 WorkletScriptLoader* script_loader = | 42 WorkletScriptLoader* script_loader = |
43 WorkletScriptLoader::Create(frame_->GetDocument()->Fetcher(), this); | 43 WorkletScriptLoader::Create(frame_->GetDocument()->Fetcher(), this); |
44 loader_and_resolvers_.Set(script_loader, resolver); | 44 loader_to_resolver_map_.Set(script_loader, resolver); |
45 script_loader->FetchScript(script_url); | 45 script_loader->FetchScript(script_url); |
46 return promise; | 46 return promise; |
47 } | 47 } |
48 | 48 |
49 void Worklet::NotifyWorkletScriptLoadingFinished( | 49 void ThreadedWorklet::NotifyWorkletScriptLoadingFinished( |
50 WorkletScriptLoader* script_loader, | 50 WorkletScriptLoader* script_loader, |
51 const ScriptSourceCode& source_code) { | 51 const ScriptSourceCode& source_code) { |
52 DCHECK(IsMainThread()); | 52 DCHECK(IsMainThread()); |
53 ScriptPromiseResolver* resolver = loader_and_resolvers_.at(script_loader); | 53 ScriptPromiseResolver* resolver = loader_to_resolver_map_.at(script_loader); |
54 loader_and_resolvers_.erase(script_loader); | 54 loader_to_resolver_map_.erase(script_loader); |
55 | 55 |
56 if (!script_loader->WasScriptLoadSuccessful()) { | 56 if (!script_loader->WasScriptLoadSuccessful()) { |
57 resolver->Reject(DOMException::Create(kNetworkError)); | 57 resolver->Reject(DOMException::Create(kNetworkError)); |
58 return; | 58 return; |
59 } | 59 } |
60 | 60 |
61 GetWorkletGlobalScopeProxy()->EvaluateScript(source_code); | 61 GetWorkletGlobalScopeProxy()->EvaluateScript(source_code); |
62 resolver->Resolve(); | 62 resolver->Resolve(); |
63 } | 63 } |
64 | 64 |
65 void Worklet::ContextDestroyed(ExecutionContext*) { | 65 void ThreadedWorklet::ContextDestroyed(ExecutionContext* execution_context) { |
66 DCHECK(IsMainThread()); | 66 DCHECK(IsMainThread()); |
67 if (IsInitialized()) | 67 for (const auto& script_loader : loader_to_resolver_map_.Keys()) |
68 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); | |
69 for (const auto& script_loader : loader_and_resolvers_.Keys()) | |
70 script_loader->Cancel(); | 68 script_loader->Cancel(); |
71 loader_and_resolvers_.Clear(); | 69 loader_to_resolver_map_.Clear(); |
72 frame_ = nullptr; | 70 Worklet::ContextDestroyed(execution_context); |
73 } | 71 } |
74 | 72 |
75 DEFINE_TRACE(Worklet) { | 73 DEFINE_TRACE(ThreadedWorklet) { |
76 visitor->Trace(frame_); | 74 visitor->Trace(loader_to_resolver_map_); |
77 visitor->Trace(loader_and_resolvers_); | 75 Worklet::Trace(visitor); |
78 ContextLifecycleObserver::Trace(visitor); | |
79 } | 76 } |
80 | 77 |
81 } // namespace blink | 78 } // namespace blink |
OLD | NEW |