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