Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: third_party/WebKit/Source/core/workers/ThreadedWorklet.cpp

Issue 2878833002: Worklet: Move common code of addModule from Main/ThreadedWorklet to Worklet (Closed)
Patch Set: fix crashes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ThreadedWorklet.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/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"
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 ThreadedWorklet::ThreadedWorklet(LocalFrame* frame) : Worklet(frame) {} 18 ThreadedWorklet::ThreadedWorklet(LocalFrame* frame)
19 : Worklet(frame), frame_(frame) {}
19 20
20 ScriptPromise ThreadedWorklet::addModule(ScriptState* script_state, 21 void ThreadedWorklet::FetchAndInvokeScript(const KURL& module_url_record,
21 const String& url) { 22 ScriptPromiseResolver* resolver) {
22 DCHECK(IsMainThread()); 23 DCHECK(IsMainThread());
23 if (!GetExecutionContext()) { 24 if (!GetExecutionContext())
24 return ScriptPromise::RejectWithDOMException( 25 return;
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 26
36 if (!IsInitialized()) 27 if (!IsInitialized())
37 Initialize(); 28 Initialize();
38 29
39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
40 ScriptPromise promise = resolver->Promise();
41
42 WorkletScriptLoader* script_loader = 30 WorkletScriptLoader* script_loader =
43 WorkletScriptLoader::Create(frame_->GetDocument()->Fetcher(), this); 31 WorkletScriptLoader::Create(frame_->GetDocument()->Fetcher(), this);
44 loader_to_resolver_map_.Set(script_loader, resolver); 32 loader_to_resolver_map_.Set(script_loader, resolver);
45 script_loader->FetchScript(script_url); 33 script_loader->FetchScript(module_url_record);
46 return promise;
47 } 34 }
48 35
49 void ThreadedWorklet::NotifyWorkletScriptLoadingFinished( 36 void ThreadedWorklet::NotifyWorkletScriptLoadingFinished(
50 WorkletScriptLoader* script_loader, 37 WorkletScriptLoader* script_loader,
51 const ScriptSourceCode& source_code) { 38 const ScriptSourceCode& source_code) {
52 DCHECK(IsMainThread()); 39 DCHECK(IsMainThread());
53 ScriptPromiseResolver* resolver = loader_to_resolver_map_.at(script_loader); 40 ScriptPromiseResolver* resolver = loader_to_resolver_map_.at(script_loader);
54 loader_to_resolver_map_.erase(script_loader); 41 loader_to_resolver_map_.erase(script_loader);
55 42
56 if (!script_loader->WasScriptLoadSuccessful()) { 43 if (!script_loader->WasScriptLoadSuccessful()) {
57 resolver->Reject(DOMException::Create(kNetworkError)); 44 resolver->Reject(DOMException::Create(kNetworkError));
58 return; 45 return;
59 } 46 }
60 47
61 GetWorkletGlobalScopeProxy()->EvaluateScript(source_code); 48 GetWorkletGlobalScopeProxy()->EvaluateScript(source_code);
62 resolver->Resolve(); 49 resolver->Resolve();
63 } 50 }
64 51
65 void ThreadedWorklet::ContextDestroyed(ExecutionContext* execution_context) { 52 void ThreadedWorklet::ContextDestroyed(ExecutionContext* execution_context) {
66 DCHECK(IsMainThread()); 53 DCHECK(IsMainThread());
67 for (const auto& script_loader : loader_to_resolver_map_.Keys()) 54 for (const auto& script_loader : loader_to_resolver_map_.Keys())
68 script_loader->Cancel(); 55 script_loader->Cancel();
69 loader_to_resolver_map_.clear(); 56 loader_to_resolver_map_.clear();
70 if (IsInitialized()) 57 if (IsInitialized())
71 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope(); 58 GetWorkletGlobalScopeProxy()->TerminateWorkletGlobalScope();
72 Worklet::ContextDestroyed(execution_context); 59 frame_ = nullptr;
73 } 60 }
74 61
75 DEFINE_TRACE(ThreadedWorklet) { 62 DEFINE_TRACE(ThreadedWorklet) {
63 visitor->Trace(frame_);
76 visitor->Trace(loader_to_resolver_map_); 64 visitor->Trace(loader_to_resolver_map_);
77 Worklet::Trace(visitor); 65 Worklet::Trace(visitor);
78 } 66 }
79 67
80 } // namespace blink 68 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/workers/ThreadedWorklet.h ('k') | third_party/WebKit/Source/core/workers/Worklet.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698