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

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

Issue 2832743003: Move SuspendableScriptExecutor.* from web/ -> core/frame/. (Closed)
Patch Set: Created 3 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "web/SuspendableScriptExecutor.h"
6
7 #include <memory>
8 #include "bindings/core/v8/ScriptController.h"
9 #include "bindings/core/v8/ScriptSourceCode.h"
10 #include "bindings/core/v8/V8Binding.h"
11 #include "bindings/core/v8/V8PersistentValueVector.h"
12 #include "bindings/core/v8/WindowProxy.h"
13 #include "core/dom/Document.h"
14 #include "core/dom/DocumentUserGestureToken.h"
15 #include "core/dom/TaskRunnerHelper.h"
16 #include "core/frame/LocalFrame.h"
17 #include "platform/UserGestureIndicator.h"
18 #include "platform/wtf/PtrUtil.h"
19 #include "platform/wtf/Vector.h"
20 #include "public/platform/WebVector.h"
21 #include "public/web/WebScriptExecutionCallback.h"
22
23 namespace blink {
24
25 namespace {
26
27 class WebScriptExecutor : public SuspendableScriptExecutor::Executor {
28 public:
29 WebScriptExecutor(const HeapVector<ScriptSourceCode>& sources,
30 int world_id,
31 bool user_gesture);
32
33 Vector<v8::Local<v8::Value>> Execute(LocalFrame*) override;
34
35 DEFINE_INLINE_VIRTUAL_TRACE() {
36 visitor->Trace(sources_);
37 SuspendableScriptExecutor::Executor::Trace(visitor);
38 }
39
40 private:
41 HeapVector<ScriptSourceCode> sources_;
42 int world_id_;
43 bool user_gesture_;
44 };
45
46 WebScriptExecutor::WebScriptExecutor(
47 const HeapVector<ScriptSourceCode>& sources,
48 int world_id,
49 bool user_gesture)
50 : sources_(sources), world_id_(world_id), user_gesture_(user_gesture) {}
51
52 Vector<v8::Local<v8::Value>> WebScriptExecutor::Execute(LocalFrame* frame) {
53 std::unique_ptr<UserGestureIndicator> indicator;
54 if (user_gesture_) {
55 indicator = WTF::WrapUnique(
56 new UserGestureIndicator(DocumentUserGestureToken::Create(
57 frame->GetDocument(), UserGestureToken::kNewGesture)));
58 }
59
60 Vector<v8::Local<v8::Value>> results;
61 if (world_id_) {
62 frame->GetScriptController().ExecuteScriptInIsolatedWorld(
63 world_id_, sources_, &results);
64 } else {
65 v8::Local<v8::Value> script_value =
66 frame->GetScriptController().ExecuteScriptInMainWorldAndReturnValue(
67 sources_.front());
68 results.push_back(script_value);
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> function_;
86 ScopedPersistent<v8::Value> receiver_;
87 V8PersistentValueVector<v8::Value> args_;
88 RefPtr<UserGestureToken> gesture_token_;
89 };
90
91 V8FunctionExecutor::V8FunctionExecutor(v8::Isolate* isolate,
92 v8::Local<v8::Function> function,
93 v8::Local<v8::Value> receiver,
94 int argc,
95 v8::Local<v8::Value> argv[])
96 : function_(isolate, function),
97 receiver_(isolate, receiver),
98 args_(isolate),
99 gesture_token_(UserGestureIndicator::CurrentToken()) {
100 args_.ReserveCapacity(argc);
101 for (int i = 0; i < argc; ++i)
102 args_.Append(argv[i]);
103 }
104
105 Vector<v8::Local<v8::Value>> V8FunctionExecutor::Execute(LocalFrame* frame) {
106 v8::Isolate* isolate = v8::Isolate::GetCurrent();
107 Vector<v8::Local<v8::Value>> results;
108 v8::Local<v8::Value> single_result;
109 Vector<v8::Local<v8::Value>> args;
110 args.ReserveCapacity(args_.Size());
111 for (size_t i = 0; i < args_.Size(); ++i)
112 args.push_back(args_.Get(i));
113 {
114 std::unique_ptr<UserGestureIndicator> gesture_indicator;
115 if (gesture_token_) {
116 gesture_indicator =
117 WTF::WrapUnique(new UserGestureIndicator(gesture_token_.Release()));
118 }
119 if (V8ScriptRunner::CallFunction(function_.NewLocal(isolate),
120 frame->GetDocument(),
121 receiver_.NewLocal(isolate), args.size(),
122 args.Data(), ToIsolate(frame))
123 .ToLocal(&single_result))
124 results.push_back(single_result);
125 }
126 return results;
127 }
128
129 } // namespace
130
131 SuspendableScriptExecutor* SuspendableScriptExecutor::Create(
132 LocalFrame* frame,
133 RefPtr<DOMWrapperWorld> world,
134 const HeapVector<ScriptSourceCode>& sources,
135 bool user_gesture,
136 WebScriptExecutionCallback* callback) {
137 ScriptState* script_state = ToScriptState(frame, *world);
138 return new SuspendableScriptExecutor(
139 frame, script_state, callback,
140 new WebScriptExecutor(sources, world->GetWorldId(), user_gesture));
141 }
142
143 void SuspendableScriptExecutor::CreateAndRun(
144 LocalFrame* frame,
145 v8::Isolate* isolate,
146 v8::Local<v8::Context> context,
147 v8::Local<v8::Function> function,
148 v8::Local<v8::Value> receiver,
149 int argc,
150 v8::Local<v8::Value> argv[],
151 WebScriptExecutionCallback* callback) {
152 ScriptState* script_state = ScriptState::From(context);
153 if (!script_state->ContextIsValid()) {
154 if (callback)
155 callback->Completed(Vector<v8::Local<v8::Value>>());
156 return;
157 }
158 SuspendableScriptExecutor* executor = new SuspendableScriptExecutor(
159 frame, script_state, callback,
160 new V8FunctionExecutor(isolate, function, receiver, argc, argv));
161 executor->Run();
162 }
163
164 void SuspendableScriptExecutor::ContextDestroyed(
165 ExecutionContext* destroyed_context) {
166 SuspendableTimer::ContextDestroyed(destroyed_context);
167 if (callback_)
168 callback_->Completed(Vector<v8::Local<v8::Value>>());
169 Dispose();
170 }
171
172 SuspendableScriptExecutor::SuspendableScriptExecutor(
173 LocalFrame* frame,
174 ScriptState* script_state,
175 WebScriptExecutionCallback* callback,
176 Executor* executor)
177 : SuspendableTimer(frame->GetDocument(), TaskType::kTimer),
178 script_state_(script_state),
179 callback_(callback),
180 blocking_option_(kNonBlocking),
181 keep_alive_(this),
182 executor_(executor) {
183 CHECK(script_state_);
184 CHECK(script_state_->ContextIsValid());
185 }
186
187 SuspendableScriptExecutor::~SuspendableScriptExecutor() {}
188
189 void SuspendableScriptExecutor::Fired() {
190 ExecuteAndDestroySelf();
191 }
192
193 void SuspendableScriptExecutor::Run() {
194 ExecutionContext* context = GetExecutionContext();
195 DCHECK(context);
196 if (!context->IsContextSuspended()) {
197 SuspendIfNeeded();
198 ExecuteAndDestroySelf();
199 return;
200 }
201 StartOneShot(0, BLINK_FROM_HERE);
202 SuspendIfNeeded();
203 }
204
205 void SuspendableScriptExecutor::RunAsync(BlockingOption blocking) {
206 ExecutionContext* context = GetExecutionContext();
207 DCHECK(context);
208 blocking_option_ = blocking;
209 if (blocking_option_ == kOnloadBlocking)
210 ToDocument(GetExecutionContext())->IncrementLoadEventDelayCount();
211
212 StartOneShot(0, BLINK_FROM_HERE);
213 SuspendIfNeeded();
214 }
215
216 void SuspendableScriptExecutor::ExecuteAndDestroySelf() {
217 CHECK(script_state_->ContextIsValid());
218
219 if (callback_)
220 callback_->WillExecute();
221
222 ScriptState::Scope script_scope(script_state_.Get());
223 Vector<v8::Local<v8::Value>> results =
224 executor_->Execute(ToDocument(GetExecutionContext())->GetFrame());
225
226 // The script may have removed the frame, in which case contextDestroyed()
227 // will have handled the disposal/callback.
228 if (!script_state_->ContextIsValid())
229 return;
230
231 if (blocking_option_ == kOnloadBlocking)
232 ToDocument(GetExecutionContext())->DecrementLoadEventDelayCount();
233
234 if (callback_)
235 callback_->Completed(results);
236
237 Dispose();
238 }
239
240 void SuspendableScriptExecutor::Dispose() {
241 // Remove object as a ContextLifecycleObserver.
242 SuspendableObject::ClearContext();
243 keep_alive_.Clear();
244 Stop();
245 }
246
247 DEFINE_TRACE(SuspendableScriptExecutor) {
248 visitor->Trace(executor_);
249 SuspendableTimer::Trace(visitor);
250 }
251
252 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/SuspendableScriptExecutor.h ('k') | third_party/WebKit/Source/web/WebLocalFrameImpl.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698