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

Side by Side Diff: third_party/WebKit/Source/web/SuspendableScriptExecutor.cpp

Issue 2339683006: [Blink] Modify SuspendableScriptExecutor to take a v8::Function (Closed)
Patch Set: Daniels Created 4 years, 2 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "web/SuspendableScriptExecutor.h" 5 #include "web/SuspendableScriptExecutor.h"
6 6
7 #include "bindings/core/v8/ScriptController.h" 7 #include "bindings/core/v8/ScriptController.h"
8 #include "bindings/core/v8/ScriptSourceCode.h" 8 #include "bindings/core/v8/ScriptSourceCode.h"
9 #include "bindings/core/v8/V8PersistentValueVector.h"
9 #include "core/dom/Document.h" 10 #include "core/dom/Document.h"
10 #include "core/frame/LocalFrame.h" 11 #include "core/frame/LocalFrame.h"
11 #include "platform/UserGestureIndicator.h" 12 #include "platform/UserGestureIndicator.h"
12 #include "public/platform/WebVector.h" 13 #include "public/platform/WebVector.h"
13 #include "public/web/WebScriptExecutionCallback.h" 14 #include "public/web/WebScriptExecutionCallback.h"
14 #include "wtf/PtrUtil.h" 15 #include "wtf/PtrUtil.h"
15 #include <memory> 16 #include <memory>
16 17
17 namespace blink { 18 namespace blink {
18 19
20 namespace {
21
22 class WebScriptExecutor : public SuspendableScriptExecutor::Executor {
23 public:
24 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources, int worldID, int extensionGroup, bool userGesture);
25
26 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override;
27
28 void trace(Visitor*) override;
29
30 private:
31 HeapVector<ScriptSourceCode> m_sources;
32 int m_worldID;
33 int m_extensionGroup;
34 bool m_userGesture;
35 };
36
37 WebScriptExecutor::WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources , int worldID, int extensionGroup, bool userGesture)
38 : m_sources(sources)
39 , m_worldID(worldID)
40 , m_extensionGroup(extensionGroup)
41 , m_userGesture(userGesture)
42 {
43 }
44
45 Vector<v8::Local<v8::Value>> WebScriptExecutor::execute(LocalFrame* frame)
46 {
47 std::unique_ptr<UserGestureIndicator> indicator;
48 if (m_userGesture)
49 indicator = wrapUnique(new UserGestureIndicator(DefinitelyProcessingNewU serGesture));
50
51 Vector<v8::Local<v8::Value>> results;
52 if (m_worldID) {
53 frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_ext ensionGroup, &results);
54 } else {
55 v8::Local<v8::Value> scriptValue = frame->script().executeScriptInMainWo rldAndReturnValue(m_sources.first());
56 results.append(scriptValue);
57 }
58
59 return results;
60 }
61
62 void WebScriptExecutor::trace(Visitor* visitor)
63 {
64 visitor->trace(m_sources);
65 SuspendableScriptExecutor::Executor::trace(visitor);
66 }
67
68 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor {
69 public:
70 V8FunctionExecutor(v8::Isolate*, v8::Local<v8::Function>, v8::Local<v8::Valu e> receiver, int argc, v8::Local<v8::Value> argv[]);
71
72 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override;
73
74 private:
75 ScopedPersistent<v8::Function> m_function;
76 ScopedPersistent<v8::Value> m_receiver;
77 V8PersistentValueVector<v8::Value> m_args;
78 };
79
80 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate, v8::Local<v8::Funct ion> function, v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> arg v[])
81 : m_function(isolate, function)
82 , m_receiver(isolate, receiver)
83 , m_args(isolate)
84 {
85 m_args.ReserveCapacity(argc);
86 for (int i = 0; i < argc; ++i)
87 m_args.Append(argv[i]);
88 }
89
90 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame)
91 {
92 v8::Isolate* isolate = v8::Isolate::GetCurrent();
93 Vector<v8::Local<v8::Value>> results;
94 v8::Local<v8::Value> singleResult;
95 size_t numArgs = m_args.Size();
96 v8::Local<v8::Value> args[numArgs];
97 for (size_t i = 0; i < numArgs; ++i)
98 args[i] = m_args.Get(i);
99 if (V8ScriptRunner::callFunction(m_function.newLocal(isolate), frame->docume nt(), m_receiver.newLocal(isolate), numArgs, static_cast<v8::Local<v8::Value>*>( args), toIsolate(frame)).ToLocal(&singleResult))
100 results.append(singleResult);
101 return results;
102 }
103
104 } // namespace
105
19 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con st HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback) 106 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, int worldID, con st HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userGesture, WebScriptExecutionCallback* callback)
20 { 107 {
21 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, w orldID, sources, extensionGroup, userGesture, callback); 108 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, c allback, std::unique_ptr<WebScriptExecutor>(new WebScriptExecutor(sources, world ID, extensionGroup, userGesture)));
109 executor->run();
110 }
111
112 void SuspendableScriptExecutor::createAndRun(LocalFrame* frame, v8::Isolate* iso late, v8::Local<v8::Function> function, v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> argv[], WebScriptExecutionCallback* callback)
113 {
114 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(frame, c allback, std::unique_ptr<V8FunctionExecutor>(new V8FunctionExecutor(isolate, fun ction, receiver, argc, argv)));
22 executor->run(); 115 executor->run();
23 } 116 }
24 117
25 void SuspendableScriptExecutor::contextDestroyed() 118 void SuspendableScriptExecutor::contextDestroyed()
26 { 119 {
27 SuspendableTimer::contextDestroyed(); 120 SuspendableTimer::contextDestroyed();
28 m_callback->completed(Vector<v8::Local<v8::Value>>()); 121 if (m_callback)
122 m_callback->completed(Vector<v8::Local<v8::Value>>());
29 dispose(); 123 dispose();
30 } 124 }
31 125
32 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, int worl dID, const HeapVector<ScriptSourceCode>& sources, int extensionGroup, bool userG esture, WebScriptExecutionCallback* callback) 126 SuspendableScriptExecutor::SuspendableScriptExecutor(LocalFrame* frame, WebScrip tExecutionCallback* callback, std::unique_ptr<Executor> executor)
33 : SuspendableTimer(frame->document()) 127 : SuspendableTimer(frame->document())
34 , m_frame(frame) 128 , m_frame(frame)
35 , m_sources(sources)
36 , m_callback(callback) 129 , m_callback(callback)
37 , m_keepAlive(this) 130 , m_keepAlive(this)
38 , m_worldID(worldID) 131 , m_executor(std::move(executor))
39 , m_extensionGroup(extensionGroup)
40 , m_userGesture(userGesture)
41 { 132 {
42 } 133 }
43 134
44 SuspendableScriptExecutor::~SuspendableScriptExecutor() 135 SuspendableScriptExecutor::~SuspendableScriptExecutor()
45 { 136 {
46 } 137 }
47 138
48 void SuspendableScriptExecutor::fired() 139 void SuspendableScriptExecutor::fired()
49 { 140 {
50 executeAndDestroySelf(); 141 executeAndDestroySelf();
51 } 142 }
52 143
53 void SuspendableScriptExecutor::run() 144 void SuspendableScriptExecutor::run()
54 { 145 {
55 ExecutionContext* context = getExecutionContext(); 146 ExecutionContext* context = getExecutionContext();
56 DCHECK(context); 147 DCHECK(context);
57 if (!context->activeDOMObjectsAreSuspended()) { 148 if (!context->activeDOMObjectsAreSuspended()) {
58 suspendIfNeeded(); 149 suspendIfNeeded();
59 executeAndDestroySelf(); 150 executeAndDestroySelf();
60 return; 151 return;
61 } 152 }
62 startOneShot(0, BLINK_FROM_HERE); 153 startOneShot(0, BLINK_FROM_HERE);
63 suspendIfNeeded(); 154 suspendIfNeeded();
64 } 155 }
65 156
66 void SuspendableScriptExecutor::executeAndDestroySelf() 157 void SuspendableScriptExecutor::executeAndDestroySelf()
67 { 158 {
68 // after calling the destructor of object - object will be unsubscribed from
69 // resumed and contextDestroyed LifecycleObserver methods
70 std::unique_ptr<UserGestureIndicator> indicator;
71 if (m_userGesture)
72 indicator = wrapUnique(new UserGestureIndicator(DefinitelyProcessingNewU serGesture));
73
74 v8::HandleScope scope(v8::Isolate::GetCurrent()); 159 v8::HandleScope scope(v8::Isolate::GetCurrent());
75 Vector<v8::Local<v8::Value>> results; 160 Vector<v8::Local<v8::Value>> results = m_executor->execute(m_frame);
76 if (m_worldID) {
77 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources, m_e xtensionGroup, &results);
78 } else {
79 v8::Local<v8::Value> scriptValue = m_frame->script().executeScriptInMain WorldAndReturnValue(m_sources.first());
80 results.append(scriptValue);
81 }
82 161
83 // The script may have removed the frame, in which case contextDestroyed() 162 // The script may have removed the frame, in which case contextDestroyed()
84 // will have handled the disposal/callback. 163 // will have handled the disposal/callback.
85 if (!m_frame->client()) 164 if (!m_frame->client())
86 return; 165 return;
87 166
88 m_callback->completed(results); 167 if (m_callback)
168 m_callback->completed(results);
89 dispose(); 169 dispose();
90 } 170 }
91 171
92 void SuspendableScriptExecutor::dispose() 172 void SuspendableScriptExecutor::dispose()
93 { 173 {
94 // Remove object as a ContextLifecycleObserver. 174 // Remove object as a ContextLifecycleObserver.
95 ActiveDOMObject::clearContext(); 175 ActiveDOMObject::clearContext();
96 m_keepAlive.clear(); 176 m_keepAlive.clear();
97 stop(); 177 stop();
98 } 178 }
99 179
100 DEFINE_TRACE(SuspendableScriptExecutor) 180 DEFINE_TRACE(SuspendableScriptExecutor)
101 { 181 {
102 visitor->trace(m_frame); 182 visitor->trace(m_frame);
103 visitor->trace(m_sources);
104 SuspendableTimer::trace(visitor); 183 SuspendableTimer::trace(visitor);
105 } 184 }
106 185
107 } // namespace blink 186 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698