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

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

Issue 2339683006: [Blink] Modify SuspendableScriptExecutor to take a v8::Function (Closed)
Patch Set: Haraken's 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,
25 int worldID,
26 int extensionGroup,
27 bool userGesture);
28
29 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override;
30
31 DEFINE_INLINE_VIRTUAL_TRACE() {
32 visitor->trace(m_sources);
33 SuspendableScriptExecutor::Executor::trace(visitor);
34 }
35
36 private:
37 HeapVector<ScriptSourceCode> m_sources;
38 int m_worldID;
39 int m_extensionGroup;
40 bool m_userGesture;
41 };
42
43 WebScriptExecutor::WebScriptExecutor(
44 const HeapVector<ScriptSourceCode>& sources,
45 int worldID,
46 int extensionGroup,
47 bool userGesture)
48 : m_sources(sources),
49 m_worldID(worldID),
50 m_extensionGroup(extensionGroup),
51 m_userGesture(userGesture) {}
52
53 Vector<v8::Local<v8::Value>> WebScriptExecutor::execute(LocalFrame* frame) {
54 std::unique_ptr<UserGestureIndicator> indicator;
55 if (m_userGesture) {
56 indicator = wrapUnique(
57 new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
58 }
59
60 Vector<v8::Local<v8::Value>> results;
61 if (m_worldID) {
62 frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources,
63 m_extensionGroup, &results);
64 } else {
65 v8::Local<v8::Value> scriptValue =
66 frame->script().executeScriptInMainWorldAndReturnValue(
67 m_sources.first());
68 results.append(scriptValue);
69 }
70
71 return results;
72 }
73
74 class V8FunctionExecutor : public SuspendableScriptExecutor::Executor {
75 public:
76 V8FunctionExecutor(v8::Isolate*,
77 v8::Local<v8::Function>,
78 v8::Local<v8::Value> receiver,
79 int argc,
80 v8::Local<v8::Value> argv[]);
81
82 Vector<v8::Local<v8::Value>> execute(LocalFrame*) override;
83
84 private:
85 ScopedPersistent<v8::Function> m_function;
86 ScopedPersistent<v8::Value> m_receiver;
87 V8PersistentValueVector<v8::Value> m_args;
88 };
89
90 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate,
91 v8::Local<v8::Function> function,
92 v8::Local<v8::Value> receiver,
93 int argc,
94 v8::Local<v8::Value> argv[])
95 : m_function(isolate, function),
96 m_receiver(isolate, receiver),
97 m_args(isolate) {
98 m_args.ReserveCapacity(argc);
99 for (int i = 0; i < argc; ++i)
100 m_args.Append(argv[i]);
101 }
102
103 Vector<v8::Local<v8::Value>> V8FunctionExecutor::execute(LocalFrame* frame) {
104 v8::Isolate* isolate = v8::Isolate::GetCurrent();
haraken 2016/10/04 00:21:26 Isolate::GetCurrent() is deprecated. Use toIsolate
105 Vector<v8::Local<v8::Value>> results;
106 v8::Local<v8::Value> singleResult;
107 size_t numArgs = m_args.Size();
108 v8::Local<v8::Value> args[numArgs];
109 for (size_t i = 0; i < numArgs; ++i)
110 args[i] = m_args.Get(i);
111 if (V8ScriptRunner::callFunction(
112 m_function.newLocal(isolate), frame->document(),
113 m_receiver.newLocal(isolate), numArgs,
114 static_cast<v8::Local<v8::Value>*>(args), toIsolate(frame))
115 .ToLocal(&singleResult))
116 results.append(singleResult);
117 return results;
118 }
119
120 } // namespace
121
19 void SuspendableScriptExecutor::createAndRun( 122 void SuspendableScriptExecutor::createAndRun(
20 LocalFrame* frame, 123 LocalFrame* frame,
21 int worldID, 124 int worldID,
22 const HeapVector<ScriptSourceCode>& sources, 125 const HeapVector<ScriptSourceCode>& sources,
23 int extensionGroup, 126 int extensionGroup,
24 bool userGesture, 127 bool userGesture,
25 WebScriptExecutionCallback* callback) { 128 WebScriptExecutionCallback* callback) {
26 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor( 129 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(
27 frame, worldID, sources, extensionGroup, userGesture, callback); 130 frame, callback,
131 new WebScriptExecutor(sources, worldID, extensionGroup, userGesture));
132 executor->run();
133 }
134
135 void SuspendableScriptExecutor::createAndRun(
136 LocalFrame* frame,
137 v8::Isolate* isolate,
haraken 2016/10/04 00:21:26 Move the Isolate* parameter to the first arg.
138 v8::Local<v8::Function> function,
139 v8::Local<v8::Value> receiver,
140 int argc,
141 v8::Local<v8::Value> argv[],
142 WebScriptExecutionCallback* callback) {
143 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(
144 frame, callback,
145 new V8FunctionExecutor(isolate, function, receiver, argc, argv));
28 executor->run(); 146 executor->run();
29 } 147 }
30 148
31 void SuspendableScriptExecutor::contextDestroyed() { 149 void SuspendableScriptExecutor::contextDestroyed() {
32 SuspendableTimer::contextDestroyed(); 150 SuspendableTimer::contextDestroyed();
33 m_callback->completed(Vector<v8::Local<v8::Value>>()); 151 if (m_callback)
152 m_callback->completed(Vector<v8::Local<v8::Value>>());
34 dispose(); 153 dispose();
35 } 154 }
36 155
37 SuspendableScriptExecutor::SuspendableScriptExecutor( 156 SuspendableScriptExecutor::SuspendableScriptExecutor(
38 LocalFrame* frame, 157 LocalFrame* frame,
39 int worldID, 158 WebScriptExecutionCallback* callback,
40 const HeapVector<ScriptSourceCode>& sources, 159 Executor* executor)
41 int extensionGroup,
42 bool userGesture,
43 WebScriptExecutionCallback* callback)
44 : SuspendableTimer(frame->document()), 160 : SuspendableTimer(frame->document()),
45 m_frame(frame), 161 m_frame(frame),
46 m_sources(sources),
47 m_callback(callback), 162 m_callback(callback),
48 m_keepAlive(this), 163 m_keepAlive(this),
49 m_worldID(worldID), 164 m_executor(executor) {}
50 m_extensionGroup(extensionGroup),
51 m_userGesture(userGesture) {}
52 165
53 SuspendableScriptExecutor::~SuspendableScriptExecutor() {} 166 SuspendableScriptExecutor::~SuspendableScriptExecutor() {}
54 167
55 void SuspendableScriptExecutor::fired() { 168 void SuspendableScriptExecutor::fired() {
56 executeAndDestroySelf(); 169 executeAndDestroySelf();
57 } 170 }
58 171
59 void SuspendableScriptExecutor::run() { 172 void SuspendableScriptExecutor::run() {
60 ExecutionContext* context = getExecutionContext(); 173 ExecutionContext* context = getExecutionContext();
61 DCHECK(context); 174 DCHECK(context);
62 if (!context->activeDOMObjectsAreSuspended()) { 175 if (!context->activeDOMObjectsAreSuspended()) {
63 suspendIfNeeded(); 176 suspendIfNeeded();
64 executeAndDestroySelf(); 177 executeAndDestroySelf();
65 return; 178 return;
66 } 179 }
67 startOneShot(0, BLINK_FROM_HERE); 180 startOneShot(0, BLINK_FROM_HERE);
68 suspendIfNeeded(); 181 suspendIfNeeded();
69 } 182 }
70 183
71 void SuspendableScriptExecutor::executeAndDestroySelf() { 184 void SuspendableScriptExecutor::executeAndDestroySelf() {
72 // after calling the destructor of object - object will be unsubscribed from
73 // resumed and contextDestroyed LifecycleObserver methods
74 std::unique_ptr<UserGestureIndicator> indicator;
75 if (m_userGesture)
76 indicator = wrapUnique(
77 new UserGestureIndicator(DefinitelyProcessingNewUserGesture));
78
79 v8::HandleScope scope(v8::Isolate::GetCurrent()); 185 v8::HandleScope scope(v8::Isolate::GetCurrent());
80 Vector<v8::Local<v8::Value>> results; 186 Vector<v8::Local<v8::Value>> results = m_executor->execute(m_frame);
81 if (m_worldID) {
82 m_frame->script().executeScriptInIsolatedWorld(m_worldID, m_sources,
83 m_extensionGroup, &results);
84 } else {
85 v8::Local<v8::Value> scriptValue =
86 m_frame->script().executeScriptInMainWorldAndReturnValue(
87 m_sources.first());
88 results.append(scriptValue);
89 }
90 187
91 // The script may have removed the frame, in which case contextDestroyed() 188 // The script may have removed the frame, in which case contextDestroyed()
92 // will have handled the disposal/callback. 189 // will have handled the disposal/callback.
93 if (!m_frame->client()) 190 if (!m_frame->client())
94 return; 191 return;
95 192
96 m_callback->completed(results); 193 if (m_callback)
194 m_callback->completed(results);
97 dispose(); 195 dispose();
98 } 196 }
99 197
100 void SuspendableScriptExecutor::dispose() { 198 void SuspendableScriptExecutor::dispose() {
101 // Remove object as a ContextLifecycleObserver. 199 // Remove object as a ContextLifecycleObserver.
102 ActiveDOMObject::clearContext(); 200 ActiveDOMObject::clearContext();
103 m_keepAlive.clear(); 201 m_keepAlive.clear();
104 stop(); 202 stop();
105 } 203 }
106 204
107 DEFINE_TRACE(SuspendableScriptExecutor) { 205 DEFINE_TRACE(SuspendableScriptExecutor) {
108 visitor->trace(m_frame); 206 visitor->trace(m_frame);
109 visitor->trace(m_sources); 207 visitor->trace(m_executor);
110 SuspendableTimer::trace(visitor); 208 SuspendableTimer::trace(visitor);
111 } 209 }
112 210
113 } // namespace blink 211 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698