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/platform/v8_inspector/V8RuntimeAgentImpl.cpp

Issue 2185233002: [DevTools] Added Runtime.awaitPromise protocol method (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: a Created 4 years, 4 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 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 maybeResultValue, 129 maybeResultValue,
130 scope.tryCatch(), 130 scope.tryCatch(),
131 objectGroup.fromMaybe(""), 131 objectGroup.fromMaybe(""),
132 returnByValue.fromMaybe(false), 132 returnByValue.fromMaybe(false),
133 generatePreview.fromMaybe(false), 133 generatePreview.fromMaybe(false),
134 result, 134 result,
135 wasThrown, 135 wasThrown,
136 exceptionDetails); 136 exceptionDetails);
137 } 137 }
138 138
139 namespace {
140
141 class ProtocolPromiseHandler {
dgozman 2016/07/28 20:16:07 Let's move this to separate file.
kozy 2016/07/28 22:54:09 Rewritten.
142 PROTOCOL_DISALLOW_COPY(ProtocolPromiseHandler);
143 public:
144 ProtocolPromiseHandler(V8DebuggerImpl* debugger, int contextGroupId, const S tring16& promiseObjectId, std::unique_ptr<V8RuntimeAgentImpl::PromiseThenCallbac k> callback, bool returnByValue, bool generatePreview)
dgozman 2016/07/28 20:16:07 protocol::Runtime::Backend::PromiseThenCallback
kozy 2016/07/28 22:54:09 Rewritten.
145 : m_debugger(debugger)
146 , m_contextGroupId(contextGroupId)
147 , m_promiseObjectId(promiseObjectId)
148 , m_callback(std::move(callback))
149 , m_returnByValue(returnByValue)
150 , m_generatePreview(generatePreview)
151 , m_wasCalled(false)
152 {
153 }
154
155 ~ProtocolPromiseHandler()
156 {
157 if (!m_wasCalled)
158 m_callback->sendFailure("Promise was collected");
159 }
160
161 void handleThen(v8::Local<v8::Value> value)
162 {
163 if (m_wasCalled)
164 return;
165 m_wasCalled = true;
166
167 std::unique_ptr<protocol::Runtime::RemoteObject> resolvedValue = wrapObj ect(value);
168 if (!resolvedValue)
169 return;
170 m_callback->sendSuccess(std::move(resolvedValue), Maybe<protocol::Runtim e::RemoteObject>());
171 }
172
173 void handleCatch(v8::Local<v8::Value> value)
174 {
175 if (m_wasCalled)
176 return;
177 m_wasCalled = true;
178
179 std::unique_ptr<protocol::Runtime::RemoteObject> rejectedValue = wrapObj ect(value);
180 if (!rejectedValue)
181 return;
182 m_callback->sendSuccess(Maybe<protocol::Runtime::RemoteObject>(), std::m ove(rejectedValue));
183 }
184
185 void sendFailure(const ErrorString& errorString)
186 {
187 if (m_wasCalled)
188 return;
189
190 m_callback->sendFailure(errorString);
191 m_wasCalled = true;
192 }
193
194 private:
195 std::unique_ptr<protocol::Runtime::RemoteObject> wrapObject(v8::Local<v8::Va lue> value)
196 {
197 ErrorString errorString;
198 InjectedScript::ObjectScope scope(&errorString, m_debugger, m_contextGro upId, m_promiseObjectId);
199 if (!scope.initialize()) {
200 m_callback->sendFailure(errorString);
201 return nullptr;
202 }
203 std::unique_ptr<protocol::Runtime::RemoteObject> wrappedValue = scope.in jectedScript()->wrapObject(&errorString, value, scope.objectGroupName(), m_retur nByValue, m_generatePreview);
204 if (!wrappedValue) {
205 m_callback->sendFailure(errorString);
206 return nullptr;
207 }
208 return wrappedValue;
209 }
210
211 V8DebuggerImpl* m_debugger;
212 int m_contextGroupId;
213 String16 m_promiseObjectId;
214 std::unique_ptr<V8RuntimeAgentImpl::PromiseThenCallback> m_callback;
215 bool m_returnByValue;
216 bool m_generatePreview;
217 bool m_wasCalled;
218 };
219
220 void thenCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
221 {
222 ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(info. Data().As<v8::External>()->Value());
223 DCHECK(handler);
224 handler->handleThen(info.Length() > 0 ? info[0] : v8::Local<v8::Value>::Cast (v8::Undefined(info.GetIsolate())));
225 }
226
227 void catchCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
228 {
229 ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(info. Data().As<v8::External>()->Value());
230 DCHECK(handler);
231 handler->handleCatch(info.Length() > 0 ? info[0] : v8::Local<v8::Value>::Cas t(v8::Undefined(info.GetIsolate())));
232 }
233
234 template<typename T>
235 void deletePointer(const v8::WeakCallbackInfo<std::pair<v8::Global<v8::External> , T*>>& data)
236 {
237 data.GetParameter()->first.Reset();
238 delete data.GetParameter()->second;
239 delete data.GetParameter();
240 }
241
242 template<typename T>
243 v8::Local<v8::External> makeExternal(v8::Isolate* isolate, T* pointer)
244 {
245 v8::Local<v8::External> external = v8::External::New(isolate, pointer);
246 v8::Global<v8::External> externalGlobal(isolate, external);
247 std::pair<v8::Global<v8::External>, T*>* data = new std::pair<v8::Global<v8: :External>, T*>(externalGlobal.Pass(), pointer);
248 data->first.SetWeak(data, deletePointer, v8::WeakCallbackType::kParameter);
249 return external;
250 }
251
252 } // namespace
253
254 void V8RuntimeAgentImpl::promiseThen(ErrorString* errorString,
255 const String16& promiseObjectId,
256 const Maybe<bool>& returnByValue,
257 const Maybe<bool>& generatePreview,
258 std::unique_ptr<PromiseThenCallback> callback)
259 {
260 InjectedScript::ObjectScope scope(errorString, m_debugger, m_session->contex tGroupId(), promiseObjectId);
261 if (!scope.initialize())
262 return;
263 v8::Local<v8::Value> value = scope.object();
264 if (!value->IsPromise()) {
265 callback->sendFailure("Could not find promise with given id");
266 return;
267 }
268
269 v8::Local<v8::Promise> promise = value.As<v8::Promise>();
270 ProtocolPromiseHandler* handler = new ProtocolPromiseHandler(m_debugger, m_s ession->contextGroupId(), promiseObjectId, std::move(callback), returnByValue.fr omMaybe(false), generatePreview.fromMaybe(false));
271 v8::Local<v8::External> wrappedCallback = makeExternal(m_debugger->isolate() , handler);
dgozman 2016/07/28 20:16:07 wrappedHandler
kozy 2016/07/28 22:54:09 Rewritten.
272
273 v8::Local<v8::Function> thenCallbackFunction = v8::Function::New(scope.conte xt(), thenCallback, wrappedCallback, 0, v8::ConstructorBehavior::kThrow).ToLocal Checked();
274 if (promise->Then(scope.context(), thenCallbackFunction).IsEmpty()) {
275 handler->sendFailure("Internal error");
276 return;
277 }
278 v8::Local<v8::Function> catchCallbackFunction = v8::Function::New(scope.cont ext(), catchCallback, wrappedCallback, 0, v8::ConstructorBehavior::kThrow).ToLoc alChecked();
279 if (promise->Catch(scope.context(), catchCallbackFunction).IsEmpty()) {
280 handler->sendFailure("Internal error");
281 return;
282 }
283 }
284
139 void V8RuntimeAgentImpl::callFunctionOn(ErrorString* errorString, 285 void V8RuntimeAgentImpl::callFunctionOn(ErrorString* errorString,
140 const String16& objectId, 286 const String16& objectId,
141 const String16& expression, 287 const String16& expression,
142 const Maybe<protocol::Array<protocol::Runtime::CallArgument>>& optionalArgum ents, 288 const Maybe<protocol::Array<protocol::Runtime::CallArgument>>& optionalArgum ents,
143 const Maybe<bool>& doNotPauseOnExceptionsAndMuteConsole, 289 const Maybe<bool>& doNotPauseOnExceptionsAndMuteConsole,
144 const Maybe<bool>& returnByValue, 290 const Maybe<bool>& returnByValue,
145 const Maybe<bool>& generatePreview, 291 const Maybe<bool>& generatePreview,
146 const Maybe<bool>& userGesture, 292 const Maybe<bool>& userGesture,
147 std::unique_ptr<RemoteObject>* result, 293 std::unique_ptr<RemoteObject>* result,
148 Maybe<bool>* wasThrown) 294 Maybe<bool>* wasThrown)
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 reportMessage(message, true); 583 reportMessage(message, true);
438 } 584 }
439 585
440 void V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message, bool generateP review) 586 void V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message, bool generateP review)
441 { 587 {
442 message->reportToFrontend(&m_frontend, m_session, generatePreview); 588 message->reportToFrontend(&m_frontend, m_session, generatePreview);
443 m_frontend.flush(); 589 m_frontend.flush();
444 } 590 }
445 591
446 } // namespace blink 592 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698