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

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: alligned output 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 19 matching lines...) Expand all
30 30
31 #include "platform/v8_inspector/V8RuntimeAgentImpl.h" 31 #include "platform/v8_inspector/V8RuntimeAgentImpl.h"
32 32
33 #include "platform/inspector_protocol/Values.h" 33 #include "platform/inspector_protocol/Values.h"
34 #include "platform/v8_inspector/InjectedScript.h" 34 #include "platform/v8_inspector/InjectedScript.h"
35 #include "platform/v8_inspector/InspectedContext.h" 35 #include "platform/v8_inspector/InspectedContext.h"
36 #include "platform/v8_inspector/RemoteObjectId.h" 36 #include "platform/v8_inspector/RemoteObjectId.h"
37 #include "platform/v8_inspector/V8ConsoleMessage.h" 37 #include "platform/v8_inspector/V8ConsoleMessage.h"
38 #include "platform/v8_inspector/V8DebuggerImpl.h" 38 #include "platform/v8_inspector/V8DebuggerImpl.h"
39 #include "platform/v8_inspector/V8InspectorSessionImpl.h" 39 #include "platform/v8_inspector/V8InspectorSessionImpl.h"
40 #include "platform/v8_inspector/V8StackTraceImpl.h"
40 #include "platform/v8_inspector/V8StringUtil.h" 41 #include "platform/v8_inspector/V8StringUtil.h"
41 #include "platform/v8_inspector/public/V8DebuggerClient.h" 42 #include "platform/v8_inspector/public/V8DebuggerClient.h"
42 43
43 namespace blink { 44 namespace blink {
44 45
45 namespace V8RuntimeAgentImplState { 46 namespace V8RuntimeAgentImplState {
46 static const char customObjectFormatterEnabled[] = "customObjectFormatterEnabled "; 47 static const char customObjectFormatterEnabled[] = "customObjectFormatterEnabled ";
47 static const char runtimeEnabled[] = "runtimeEnabled"; 48 static const char runtimeEnabled[] = "runtimeEnabled";
48 }; 49 };
49 50
50 using protocol::Runtime::ExceptionDetails; 51 using protocol::Runtime::ExceptionDetails;
51 using protocol::Runtime::RemoteObject; 52 using protocol::Runtime::RemoteObject;
52 53
53 static bool hasInternalError(ErrorString* errorString, bool hasError) 54 static bool hasInternalError(ErrorString* errorString, bool hasError)
54 { 55 {
55 if (hasError) 56 if (hasError)
56 *errorString = "Internal error"; 57 *errorString = "Internal error";
57 return hasError; 58 return hasError;
58 } 59 }
59 60
61 namespace {
62
63 class ProtocolPromiseHandler {
64 public:
65 static void add(V8DebuggerImpl* debugger, int contextGroupId, const String16 & promiseObjectId, std::unique_ptr<protocol::Runtime::Backend::AwaitPromiseCallb ack> callback, bool returnByValue, bool generatePreview)
66 {
67 ErrorString errorString;
68 InjectedScript::ObjectScope scope(&errorString, debugger, contextGroupId , promiseObjectId);
69 if (!scope.initialize()) {
70 callback->sendFailure(errorString);
71 return;
72 }
73 if (!scope.object()->IsPromise()) {
74 callback->sendFailure("Could not find promise with given id");
75 return;
76 }
77
78 protocol::Runtime::Backend::AwaitPromiseCallback* rawCallback = callback .get();
79 ProtocolPromiseHandler* handler = new ProtocolPromiseHandler(debugger, c ontextGroupId, promiseObjectId, std::move(callback), returnByValue, generatePrev iew);
80 v8::Local<v8::Value> wrapper = handler->m_wrapper.Get(debugger->isolate( ));
81 v8::Local<v8::Promise> promise = v8::Local<v8::Promise>::Cast(scope.obje ct());
82
83 v8::Local<v8::Function> thenCallbackFunction = v8::Function::New(scope.c ontext(), thenCallback, wrapper, 0, v8::ConstructorBehavior::kThrow).ToLocalChec ked();
84 if (promise->Then(scope.context(), thenCallbackFunction).IsEmpty()) {
85 rawCallback->sendFailure("Internal error");
86 return;
87 }
88 v8::Local<v8::Function> catchCallbackFunction = v8::Function::New(scope. context(), catchCallback, wrapper, 0, v8::ConstructorBehavior::kThrow).ToLocalCh ecked();
89 if (promise->Catch(scope.context(), catchCallbackFunction).IsEmpty()) {
90 rawCallback->sendFailure("Internal error");
91 return;
92 }
93 }
94
95 private:
96 static void thenCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
97 {
98 ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(i nfo.Data().As<v8::External>()->Value());
99 DCHECK(handler);
100 v8::Local<v8::Value> value = info.Length() > 0 ? info[0] : v8::Local<v8: :Value>::Cast(v8::Undefined(info.GetIsolate()));
101 handler->m_callback->sendSuccess(handler->wrapObject(value), Maybe<bool> (), Maybe<protocol::Runtime::ExceptionDetails>());
102 }
103
104 static void catchCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
105 {
106 ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(i nfo.Data().As<v8::External>()->Value());
107 DCHECK(handler);
108 v8::Local<v8::Value> value = info.Length() > 0 ? info[0] : v8::Local<v8: :Value>::Cast(v8::Undefined(info.GetIsolate()));
109
110 std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails;
111 std::unique_ptr<V8StackTraceImpl> stack = handler->m_debugger->captureSt ackTraceImpl(true);
112 if (stack) {
113 exceptionDetails = protocol::Runtime::ExceptionDetails::create()
114 .setText("Promise was rejected")
115 .setLineNumber(!stack->isEmpty() ? stack->topLineNumber() : 0)
116 .setColumnNumber(!stack->isEmpty() ? stack->topColumnNumber() : 0)
117 .setScriptId(!stack->isEmpty() ? stack->topScriptId() : String16 ())
118 .setStackTrace(stack->buildInspectorObjectImpl())
119 .build();
120 }
121 handler->m_callback->sendSuccess(handler->wrapObject(value), true, std:: move(exceptionDetails));
122 }
123
124 ProtocolPromiseHandler(V8DebuggerImpl* debugger, int contextGroupId, const S tring16& promiseObjectId, std::unique_ptr<V8RuntimeAgentImpl::AwaitPromiseCallba ck> callback, bool returnByValue, bool generatePreview)
125 : m_debugger(debugger)
126 , m_contextGroupId(contextGroupId)
127 , m_promiseObjectId(promiseObjectId)
128 , m_callback(std::move(callback))
129 , m_returnByValue(returnByValue)
130 , m_generatePreview(generatePreview)
131 , m_wrapper(debugger->isolate(), v8::External::New(debugger->isolate(), this))
132 {
133 m_wrapper.SetWeak(this, cleanup, v8::WeakCallbackType::kParameter);
134 }
135
136 static void cleanup(const v8::WeakCallbackInfo<ProtocolPromiseHandler>& data )
137 {
138 if (!data.GetParameter()->m_wrapper.IsEmpty()) {
139 data.GetParameter()->m_wrapper.Reset();
140 data.SetSecondPassCallback(cleanup);
141 } else {
142 data.GetParameter()->m_callback->sendFailure("Promise was collected" );
143 delete data.GetParameter();
144 }
145 }
146
147 std::unique_ptr<protocol::Runtime::RemoteObject> wrapObject(v8::Local<v8::Va lue> value)
148 {
149 ErrorString errorString;
150 InjectedScript::ObjectScope scope(&errorString, m_debugger, m_contextGro upId, m_promiseObjectId);
151 if (!scope.initialize()) {
152 m_callback->sendFailure(errorString);
153 return nullptr;
154 }
155 std::unique_ptr<protocol::Runtime::RemoteObject> wrappedValue = scope.in jectedScript()->wrapObject(&errorString, value, scope.objectGroupName(), m_retur nByValue, m_generatePreview);
156 if (!wrappedValue) {
157 m_callback->sendFailure(errorString);
158 return nullptr;
159 }
160 return wrappedValue;
161 }
162
163 V8DebuggerImpl* m_debugger;
164 int m_contextGroupId;
165 String16 m_promiseObjectId;
166 std::unique_ptr<V8RuntimeAgentImpl::AwaitPromiseCallback> m_callback;
167 bool m_returnByValue;
168 bool m_generatePreview;
169 v8::Global<v8::External> m_wrapper;
170 };
171
172 } // namespace
173
60 V8RuntimeAgentImpl::V8RuntimeAgentImpl(V8InspectorSessionImpl* session, protocol ::FrontendChannel* FrontendChannel, protocol::DictionaryValue* state) 174 V8RuntimeAgentImpl::V8RuntimeAgentImpl(V8InspectorSessionImpl* session, protocol ::FrontendChannel* FrontendChannel, protocol::DictionaryValue* state)
61 : m_session(session) 175 : m_session(session)
62 , m_state(state) 176 , m_state(state)
63 , m_frontend(FrontendChannel) 177 , m_frontend(FrontendChannel)
64 , m_debugger(session->debugger()) 178 , m_debugger(session->debugger())
65 , m_enabled(false) 179 , m_enabled(false)
66 { 180 {
67 } 181 }
68 182
69 V8RuntimeAgentImpl::~V8RuntimeAgentImpl() 183 V8RuntimeAgentImpl::~V8RuntimeAgentImpl()
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 maybeResultValue, 243 maybeResultValue,
130 scope.tryCatch(), 244 scope.tryCatch(),
131 objectGroup.fromMaybe(""), 245 objectGroup.fromMaybe(""),
132 returnByValue.fromMaybe(false), 246 returnByValue.fromMaybe(false),
133 generatePreview.fromMaybe(false), 247 generatePreview.fromMaybe(false),
134 result, 248 result,
135 wasThrown, 249 wasThrown,
136 exceptionDetails); 250 exceptionDetails);
137 } 251 }
138 252
253 void V8RuntimeAgentImpl::awaitPromise(ErrorString* errorString,
254 const String16& promiseObjectId,
255 const Maybe<bool>& returnByValue,
256 const Maybe<bool>& generatePreview,
257 std::unique_ptr<AwaitPromiseCallback> callback)
258 {
259 ProtocolPromiseHandler::add(m_debugger, m_session->contextGroupId(), promise ObjectId, std::move(callback), returnByValue.fromMaybe(false), generatePreview.f romMaybe(false));
260 }
261
139 void V8RuntimeAgentImpl::callFunctionOn(ErrorString* errorString, 262 void V8RuntimeAgentImpl::callFunctionOn(ErrorString* errorString,
140 const String16& objectId, 263 const String16& objectId,
141 const String16& expression, 264 const String16& expression,
142 const Maybe<protocol::Array<protocol::Runtime::CallArgument>>& optionalArgum ents, 265 const Maybe<protocol::Array<protocol::Runtime::CallArgument>>& optionalArgum ents,
143 const Maybe<bool>& doNotPauseOnExceptionsAndMuteConsole, 266 const Maybe<bool>& doNotPauseOnExceptionsAndMuteConsole,
144 const Maybe<bool>& returnByValue, 267 const Maybe<bool>& returnByValue,
145 const Maybe<bool>& generatePreview, 268 const Maybe<bool>& generatePreview,
146 const Maybe<bool>& userGesture, 269 const Maybe<bool>& userGesture,
147 std::unique_ptr<RemoteObject>* result, 270 std::unique_ptr<RemoteObject>* result,
148 Maybe<bool>* wasThrown) 271 Maybe<bool>* wasThrown)
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 reportMessage(message, true); 560 reportMessage(message, true);
438 } 561 }
439 562
440 void V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message, bool generateP review) 563 void V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message, bool generateP review)
441 { 564 {
442 message->reportToFrontend(&m_frontend, m_session, generatePreview); 565 message->reportToFrontend(&m_frontend, m_session, generatePreview);
443 m_frontend.flush(); 566 m_frontend.flush();
444 } 567 }
445 568
446 } // namespace blink 569 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698