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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/RejectedPromises.cpp

Issue 2035653006: [DevTools] Move Console to v8 inspector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improved api a bit Created 4 years, 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "bindings/core/v8/RejectedPromises.h" 5 #include "bindings/core/v8/RejectedPromises.h"
6 6
7 #include "bindings/core/v8/ScopedPersistent.h" 7 #include "bindings/core/v8/ScopedPersistent.h"
8 #include "bindings/core/v8/ScriptState.h" 8 #include "bindings/core/v8/ScriptState.h"
9 #include "bindings/core/v8/ScriptValue.h" 9 #include "bindings/core/v8/ScriptValue.h"
10 #include "bindings/core/v8/V8Binding.h" 10 #include "bindings/core/v8/V8Binding.h"
11 #include "bindings/core/v8/V8PerIsolateData.h"
11 #include "core/dom/ExecutionContext.h" 12 #include "core/dom/ExecutionContext.h"
12 #include "core/events/EventTarget.h" 13 #include "core/events/EventTarget.h"
13 #include "core/events/PromiseRejectionEvent.h" 14 #include "core/events/PromiseRejectionEvent.h"
14 #include "core/inspector/ConsoleMessage.h" 15 #include "core/inspector/ThreadDebugger.h"
15 #include "core/inspector/ScriptArguments.h"
16 #include "public/platform/Platform.h" 16 #include "public/platform/Platform.h"
17 #include "public/platform/WebScheduler.h" 17 #include "public/platform/WebScheduler.h"
18 #include "public/platform/WebTaskRunner.h" 18 #include "public/platform/WebTaskRunner.h"
19 #include "public/platform/WebThread.h" 19 #include "public/platform/WebThread.h"
20 #include "wtf/Functional.h" 20 #include "wtf/Functional.h"
21 #include "wtf/PtrUtil.h" 21 #include "wtf/PtrUtil.h"
22 #include <memory> 22 #include <memory>
23 23
24 namespace blink { 24 namespace blink {
25 25
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 PromiseRejectionEventInit init; 67 PromiseRejectionEventInit init;
68 init.setPromise(ScriptPromise(m_scriptState, value)); 68 init.setPromise(ScriptPromise(m_scriptState, value));
69 init.setReason(ScriptValue(m_scriptState, reason)); 69 init.setReason(ScriptValue(m_scriptState, reason));
70 init.setCancelable(true); 70 init.setCancelable(true);
71 PromiseRejectionEvent* event = PromiseRejectionEvent::create(m_scrip tState, EventTypeNames::unhandledrejection, init); 71 PromiseRejectionEvent* event = PromiseRejectionEvent::create(m_scrip tState, EventTypeNames::unhandledrejection, init);
72 // Log to console if event was not canceled. 72 // Log to console if event was not canceled.
73 m_shouldLogToConsole = target->dispatchEvent(event) == DispatchEvent Result::NotCanceled; 73 m_shouldLogToConsole = target->dispatchEvent(event) == DispatchEvent Result::NotCanceled;
74 } 74 }
75 75
76 if (m_shouldLogToConsole) { 76 if (m_shouldLogToConsole) {
77 const String errorMessage = "Uncaught (in promise)"; 77 V8PerIsolateData* data = V8PerIsolateData::from(m_scriptState->isola te());
78 Vector<ScriptValue> args; 78 if (data->threadDebugger())
79 args.append(ScriptValue(m_scriptState, v8String(m_scriptState->isola te(), errorMessage))); 79 m_promiseRejectionId = data->threadDebugger()->debugger()->promi seRejected(m_scriptState->context(), m_errorMessage, reason, m_location->url(), m_location->lineNumber(), m_location->columnNumber(), m_location->cloneStackTrac e(), m_location->scriptId());
80 args.append(ScriptValue(m_scriptState, reason));
81 ScriptArguments* arguments = ScriptArguments::create(m_scriptState, args);
82
83 String embedderErrorMessage = m_errorMessage;
84 if (embedderErrorMessage.isEmpty())
85 embedderErrorMessage = errorMessage;
86 else if (embedderErrorMessage.startsWith("Uncaught "))
87 embedderErrorMessage.insert(" (in promise)", 8);
88
89 ConsoleMessage* consoleMessage = ConsoleMessage::create(JSMessageSou rce, ErrorMessageLevel, embedderErrorMessage, std::move(m_location), arguments);
90 m_consoleMessageId = consoleMessage->assignMessageId();
91 executionContext->addConsoleMessage(consoleMessage);
92 } 80 }
93 81
94 m_location.reset(); 82 m_location.reset();
95 } 83 }
96 84
97 void revoke() 85 void revoke()
98 { 86 {
99 ExecutionContext* executionContext = m_scriptState->getExecutionContext( ); 87 ExecutionContext* executionContext = m_scriptState->getExecutionContext( );
100 if (!executionContext) 88 if (!executionContext)
101 return; 89 return;
102 90
103 ScriptState::Scope scope(m_scriptState); 91 ScriptState::Scope scope(m_scriptState);
104 v8::Local<v8::Value> value = m_promise.newLocal(m_scriptState->isolate() ); 92 v8::Local<v8::Value> value = m_promise.newLocal(m_scriptState->isolate() );
105 v8::Local<v8::Value> reason = m_exception.newLocal(m_scriptState->isolat e()); 93 v8::Local<v8::Value> reason = m_exception.newLocal(m_scriptState->isolat e());
106 // Either collected or https://crbug.com/450330 94 // Either collected or https://crbug.com/450330
107 if (value.IsEmpty() || !value->IsPromise()) 95 if (value.IsEmpty() || !value->IsPromise())
108 return; 96 return;
109 97
110 EventTarget* target = executionContext->errorEventTarget(); 98 EventTarget* target = executionContext->errorEventTarget();
111 if (target && !executionContext->shouldSanitizeScriptError(m_resourceNam e, m_corsStatus)) { 99 if (target && !executionContext->shouldSanitizeScriptError(m_resourceNam e, m_corsStatus)) {
112 PromiseRejectionEventInit init; 100 PromiseRejectionEventInit init;
113 init.setPromise(ScriptPromise(m_scriptState, value)); 101 init.setPromise(ScriptPromise(m_scriptState, value));
114 init.setReason(ScriptValue(m_scriptState, reason)); 102 init.setReason(ScriptValue(m_scriptState, reason));
115 PromiseRejectionEvent* event = PromiseRejectionEvent::create(m_scrip tState, EventTypeNames::rejectionhandled, init); 103 PromiseRejectionEvent* event = PromiseRejectionEvent::create(m_scrip tState, EventTypeNames::rejectionhandled, init);
116 target->dispatchEvent(event); 104 target->dispatchEvent(event);
117 } 105 }
118 106
119 if (m_shouldLogToConsole) { 107 if (m_shouldLogToConsole && m_promiseRejectionId) {
120 ConsoleMessage* consoleMessage = ConsoleMessage::create(JSMessageSou rce, RevokedErrorMessageLevel, "Handler added to rejected promise"); 108 V8PerIsolateData* data = V8PerIsolateData::from(m_scriptState->isola te());
121 consoleMessage->setRelatedMessageId(m_consoleMessageId); 109 if (data->threadDebugger())
122 executionContext->addConsoleMessage(consoleMessage); 110 data->threadDebugger()->debugger()->promiseRejectionRevoked(m_sc riptState->context(), m_promiseRejectionId);
123 } 111 }
124 } 112 }
125 113
126 void makePromiseWeak() 114 void makePromiseWeak()
127 { 115 {
128 ASSERT(!m_promise.isEmpty() && !m_promise.isWeak()); 116 ASSERT(!m_promise.isEmpty() && !m_promise.isWeak());
129 m_promise.setWeak(this, &Message::didCollectPromise); 117 m_promise.setWeak(this, &Message::didCollectPromise);
130 m_exception.setWeak(this, &Message::didCollectException); 118 m_exception.setWeak(this, &Message::didCollectException);
131 } 119 }
132 120
(...skipping 13 matching lines...) Expand all
146 } 134 }
147 135
148 private: 136 private:
149 Message(ScriptState* scriptState, v8::Local<v8::Promise> promise, v8::Local< v8::Value> exception, const String& errorMessage, std::unique_ptr<SourceLocation > location, AccessControlStatus corsStatus) 137 Message(ScriptState* scriptState, v8::Local<v8::Promise> promise, v8::Local< v8::Value> exception, const String& errorMessage, std::unique_ptr<SourceLocation > location, AccessControlStatus corsStatus)
150 : m_scriptState(scriptState) 138 : m_scriptState(scriptState)
151 , m_promise(scriptState->isolate(), promise) 139 , m_promise(scriptState->isolate(), promise)
152 , m_exception(scriptState->isolate(), exception) 140 , m_exception(scriptState->isolate(), exception)
153 , m_errorMessage(errorMessage) 141 , m_errorMessage(errorMessage)
154 , m_resourceName(location->url()) 142 , m_resourceName(location->url())
155 , m_location(std::move(location)) 143 , m_location(std::move(location))
156 , m_consoleMessageId(0) 144 , m_promiseRejectionId(0)
157 , m_collected(false) 145 , m_collected(false)
158 , m_shouldLogToConsole(true) 146 , m_shouldLogToConsole(true)
159 , m_corsStatus(corsStatus) 147 , m_corsStatus(corsStatus)
160 { 148 {
161 } 149 }
162 150
163 static void didCollectPromise(const v8::WeakCallbackInfo<Message>& data) 151 static void didCollectPromise(const v8::WeakCallbackInfo<Message>& data)
164 { 152 {
165 data.GetParameter()->m_collected = true; 153 data.GetParameter()->m_collected = true;
166 data.GetParameter()->m_promise.clear(); 154 data.GetParameter()->m_promise.clear();
167 } 155 }
168 156
169 static void didCollectException(const v8::WeakCallbackInfo<Message>& data) 157 static void didCollectException(const v8::WeakCallbackInfo<Message>& data)
170 { 158 {
171 data.GetParameter()->m_exception.clear(); 159 data.GetParameter()->m_exception.clear();
172 } 160 }
173 161
174 ScriptState* m_scriptState; 162 ScriptState* m_scriptState;
175 ScopedPersistent<v8::Promise> m_promise; 163 ScopedPersistent<v8::Promise> m_promise;
176 ScopedPersistent<v8::Value> m_exception; 164 ScopedPersistent<v8::Value> m_exception;
177 String m_errorMessage; 165 String m_errorMessage;
178 String m_resourceName; 166 String m_resourceName;
179 std::unique_ptr<SourceLocation> m_location; 167 std::unique_ptr<SourceLocation> m_location;
180 unsigned m_consoleMessageId; 168 unsigned m_promiseRejectionId;
181 bool m_collected; 169 bool m_collected;
182 bool m_shouldLogToConsole; 170 bool m_shouldLogToConsole;
183 AccessControlStatus m_corsStatus; 171 AccessControlStatus m_corsStatus;
184 }; 172 };
185 173
186 RejectedPromises::RejectedPromises() 174 RejectedPromises::RejectedPromises()
187 { 175 {
188 } 176 }
189 177
190 RejectedPromises::~RejectedPromises() 178 RejectedPromises::~RejectedPromises()
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 } 254 }
267 } 255 }
268 } 256 }
269 257
270 void RejectedPromises::revokeNow(std::unique_ptr<Message> message) 258 void RejectedPromises::revokeNow(std::unique_ptr<Message> message)
271 { 259 {
272 message->revoke(); 260 message->revoke();
273 } 261 }
274 262
275 } // namespace blink 263 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698