OLD | NEW |
1 // Copyright 2017 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/MainThreadWorklet.h" | 5 #include "core/workers/MainThreadWorklet.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptSourceCode.h" | 7 #include "bindings/core/v8/ScriptSourceCode.h" |
8 #include "bindings/core/v8/V8BindingForCore.h" | 8 #include "bindings/core/v8/V8BindingForCore.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" |
(...skipping 28 matching lines...) Expand all Loading... |
39 "This frame is already detached")); | 39 "This frame is already detached")); |
40 } | 40 } |
41 | 41 |
42 KURL script_url = GetExecutionContext()->CompleteURL(url); | 42 KURL script_url = GetExecutionContext()->CompleteURL(url); |
43 if (!script_url.IsValid()) { | 43 if (!script_url.IsValid()) { |
44 return ScriptPromise::RejectWithDOMException( | 44 return ScriptPromise::RejectWithDOMException( |
45 script_state, DOMException::Create( | 45 script_state, DOMException::Create( |
46 kSyntaxError, "'" + url + "' is not a valid URL.")); | 46 kSyntaxError, "'" + url + "' is not a valid URL.")); |
47 } | 47 } |
48 | 48 |
49 if (!IsInitialized()) | |
50 Initialize(); | |
51 | |
52 int32_t request_id = GetNextRequestId(); | 49 int32_t request_id = GetNextRequestId(); |
53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 50 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
54 ScriptPromise promise = resolver->Promise(); | 51 ScriptPromise promise = resolver->Promise(); |
55 resolver_map_.Set(request_id, resolver); | 52 resolver_map_.Set(request_id, resolver); |
56 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); | 53 GetWorkletGlobalScopeProxy()->FetchAndInvokeScript(request_id, script_url); |
57 return promise; | 54 return promise; |
58 } | 55 } |
59 | 56 |
60 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id, | 57 void MainThreadWorklet::DidFetchAndInvokeScript(int32_t request_id, |
61 bool success) { | 58 bool success) { |
62 DCHECK(IsMainThread()); | 59 DCHECK(IsMainThread()); |
63 ScriptPromiseResolver* resolver = resolver_map_.at(request_id); | 60 ScriptPromiseResolver* resolver = resolver_map_.at(request_id); |
64 if (!resolver) | 61 if (!resolver) |
65 return; | 62 return; |
66 resolver_map_.erase(request_id); | 63 resolver_map_.erase(request_id); |
67 if (!success) { | 64 if (!success) { |
68 resolver->Reject(DOMException::Create(kNetworkError)); | 65 resolver->Reject(DOMException::Create(kNetworkError)); |
69 return; | 66 return; |
70 } | 67 } |
71 resolver->Resolve(); | 68 resolver->Resolve(); |
72 } | 69 } |
73 | 70 |
74 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { | 71 void MainThreadWorklet::ContextDestroyed(ExecutionContext* execution_context) { |
75 DCHECK(IsMainThread()); | 72 DCHECK(IsMainThread()); |
76 resolver_map_.clear(); | 73 resolver_map_.clear(); |
| 74 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); |
77 Worklet::ContextDestroyed(execution_context); | 75 Worklet::ContextDestroyed(execution_context); |
78 } | 76 } |
79 | 77 |
80 DEFINE_TRACE(MainThreadWorklet) { | 78 DEFINE_TRACE(MainThreadWorklet) { |
81 visitor->Trace(resolver_map_); | 79 visitor->Trace(resolver_map_); |
82 Worklet::Trace(visitor); | 80 Worklet::Trace(visitor); |
83 } | 81 } |
84 | 82 |
85 } // namespace blink | 83 } // namespace blink |
OLD | NEW |