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

Side by Side Diff: Source/bindings/core/v8/V8Initializer.cpp

Issue 897523002: Handle rejected promises better for workers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: tidying Created 5 years, 10 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) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "bindings/core/v8/V8Initializer.h" 27 #include "bindings/core/v8/V8Initializer.h"
28 28
29 #include "bindings/core/v8/DOMWrapperWorld.h" 29 #include "bindings/core/v8/DOMWrapperWorld.h"
30 #include "bindings/core/v8/RejectedPromises.h"
30 #include "bindings/core/v8/ScriptCallStackFactory.h" 31 #include "bindings/core/v8/ScriptCallStackFactory.h"
31 #include "bindings/core/v8/ScriptController.h" 32 #include "bindings/core/v8/ScriptController.h"
32 #include "bindings/core/v8/ScriptProfiler.h" 33 #include "bindings/core/v8/ScriptProfiler.h"
33 #include "bindings/core/v8/ScriptValue.h" 34 #include "bindings/core/v8/ScriptValue.h"
34 #include "bindings/core/v8/V8Binding.h" 35 #include "bindings/core/v8/V8Binding.h"
35 #include "bindings/core/v8/V8DOMException.h" 36 #include "bindings/core/v8/V8DOMException.h"
36 #include "bindings/core/v8/V8ErrorEvent.h" 37 #include "bindings/core/v8/V8ErrorEvent.h"
37 #include "bindings/core/v8/V8ErrorHandler.h" 38 #include "bindings/core/v8/V8ErrorHandler.h"
38 #include "bindings/core/v8/V8GCController.h" 39 #include "bindings/core/v8/V8GCController.h"
39 #include "bindings/core/v8/V8History.h" 40 #include "bindings/core/v8/V8History.h"
40 #include "bindings/core/v8/V8Location.h" 41 #include "bindings/core/v8/V8Location.h"
41 #include "bindings/core/v8/V8PerContextData.h" 42 #include "bindings/core/v8/V8PerContextData.h"
42 #include "bindings/core/v8/V8Window.h" 43 #include "bindings/core/v8/V8Window.h"
44 #include "bindings/core/v8/WorkerScriptController.h"
43 #include "core/dom/Document.h" 45 #include "core/dom/Document.h"
44 #include "core/dom/ExceptionCode.h" 46 #include "core/dom/ExceptionCode.h"
45 #include "core/fetch/AccessControlStatus.h" 47 #include "core/fetch/AccessControlStatus.h"
46 #include "core/frame/ConsoleTypes.h" 48 #include "core/frame/ConsoleTypes.h"
47 #include "core/frame/LocalDOMWindow.h" 49 #include "core/frame/LocalDOMWindow.h"
48 #include "core/frame/LocalFrame.h" 50 #include "core/frame/LocalFrame.h"
49 #include "core/frame/csp/ContentSecurityPolicy.h" 51 #include "core/frame/csp/ContentSecurityPolicy.h"
50 #include "core/inspector/ConsoleMessage.h"
51 #include "core/inspector/ScriptArguments.h" 52 #include "core/inspector/ScriptArguments.h"
52 #include "core/inspector/ScriptCallStack.h" 53 #include "core/inspector/ScriptCallStack.h"
54 #include "core/workers/WorkerGlobalScope.h"
53 #include "platform/EventDispatchForbiddenScope.h" 55 #include "platform/EventDispatchForbiddenScope.h"
54 #include "platform/RuntimeEnabledFeatures.h" 56 #include "platform/RuntimeEnabledFeatures.h"
55 #include "platform/TraceEvent.h" 57 #include "platform/TraceEvent.h"
56 #include "platform/heap/AddressSanitizer.h" 58 #include "platform/heap/AddressSanitizer.h"
57 #include "platform/scheduler/Scheduler.h" 59 #include "platform/scheduler/Scheduler.h"
58 #include "public/platform/Platform.h" 60 #include "public/platform/Platform.h"
59 #include "wtf/ArrayBufferContents.h" 61 #include "wtf/ArrayBufferContents.h"
60 #include "wtf/RefPtr.h" 62 #include "wtf/RefPtr.h"
61 #include "wtf/ThreadSpecific.h"
62 #include "wtf/text/WTFString.h" 63 #include "wtf/text/WTFString.h"
63 #include <v8-debug.h> 64 #include <v8-debug.h>
64 65
65 namespace blink { 66 namespace blink {
66 67
67 static Frame* findFrame(v8::Local<v8::Object> host, v8::Local<v8::Value> data, v 8::Isolate* isolate) 68 static Frame* findFrame(v8::Local<v8::Object> host, v8::Local<v8::Value> data, v 8::Isolate* isolate)
68 { 69 {
69 const WrapperTypeInfo* type = WrapperTypeInfo::unwrap(data); 70 const WrapperTypeInfo* type = WrapperTypeInfo::unwrap(data);
70 71
71 if (V8Window::wrapperTypeInfo.equals(type)) { 72 if (V8Window::wrapperTypeInfo.equals(type)) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 // in user's scripts). 179 // in user's scripts).
179 EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents; 180 EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents;
180 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus); 181 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus);
181 } else { 182 } else {
182 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus); 183 enteredWindow->document()->reportException(event.release(), scriptId, ca llStack, corsStatus);
183 } 184 }
184 } 185 }
185 186
186 namespace { 187 namespace {
187 188
188 class PromiseRejectMessage { 189 static RejectedPromises& rejectedPromisesOnMainThread()
189 ALLOW_ONLY_INLINE_ALLOCATION(); 190 {
190 public: 191 ASSERT(isMainThread());
191 PromiseRejectMessage(const ScriptValue& promise, const ScriptValue& exceptio n, const String& errorMessage, const String& resourceName, int scriptId, int lin eNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack) 192 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<RejectedPromises>, rejectedPromis es, (adoptPtrWillBeNoop(new RejectedPromises())));
192 : m_promise(promise) 193 return *rejectedPromises;
193 , m_exception(exception) 194 }
194 , m_errorMessage(errorMessage)
195 , m_resourceName(resourceName)
196 , m_scriptId(scriptId)
197 , m_lineNumber(lineNumber)
198 , m_columnNumber(columnNumber)
199 , m_callStack(callStack)
200 {
201 }
202
203 void trace(Visitor* visitor)
204 {
205 visitor->trace(m_callStack);
206 }
207
208 const ScriptValue m_promise;
209 const ScriptValue m_exception;
210 const String m_errorMessage;
211 const String m_resourceName;
212 const int m_scriptId;
213 const int m_lineNumber;
214 const int m_columnNumber;
215 const RefPtrWillBeMember<ScriptCallStack> m_callStack;
216 };
217
218 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
219 virtual void* Allocate(size_t size) override
220 {
221 void* data;
222 WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents: :ZeroInitialize, data);
223 return data;
224 }
225
226 virtual void* AllocateUninitialized(size_t size) override
227 {
228 void* data;
229 WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents: :DontInitialize, data);
230 return data;
231 }
232
233 virtual void Free(void* data, size_t size) override
234 {
235 WTF::ArrayBufferContents::freeMemory(data, size);
236 }
237 };
238 195
239 } // namespace 196 } // namespace
240 197
241 typedef Deque<PromiseRejectMessage> PromiseRejectMessageQueue; 198 void V8Initializer::reportRejectedPromisesOnMainThread()
242
243 static PromiseRejectMessageQueue& promiseRejectMessageQueue()
244 { 199 {
245 AtomicallyInitializedStaticReference(ThreadSpecific<PromiseRejectMessageQueu e>, queue, new ThreadSpecific<PromiseRejectMessageQueue>); 200 rejectedPromisesOnMainThread().processQueue();
246 return *queue;
247 }
248
249 void V8Initializer::reportRejectedPromises()
250 {
251 PromiseRejectMessageQueue& queue = promiseRejectMessageQueue();
252 while (!queue.isEmpty()) {
253 PromiseRejectMessage message = queue.takeFirst();
254 ScriptState* scriptState = message.m_promise.scriptState();
255 if (!scriptState->contextIsValid())
256 continue;
257 // If execution termination has been triggered, quietly bail out.
258 if (v8::V8::IsExecutionTerminating(scriptState->isolate()))
259 continue;
260 ExecutionContext* executionContext = scriptState->executionContext();
261 if (!executionContext)
262 continue;
263
264 ScriptState::Scope scope(scriptState);
265
266 ASSERT(!message.m_promise.isEmpty());
267 v8::Handle<v8::Value> value = message.m_promise.v8Value();
268 ASSERT(!value.IsEmpty() && value->IsPromise());
269 if (v8::Handle<v8::Promise>::Cast(value)->HasHandler())
270 continue;
271
272 const String errorMessage = "Uncaught (in promise)";
273 Vector<ScriptValue> args;
274 args.append(ScriptValue(scriptState, v8String(scriptState->isolate(), er rorMessage)));
275 args.append(message.m_exception);
276 RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::create( scriptState, args);
277
278 String embedderErrorMessage = message.m_errorMessage;
279 if (embedderErrorMessage.isEmpty()) {
280 embedderErrorMessage = errorMessage;
281 } else {
282 if (embedderErrorMessage.startsWith("Uncaught "))
283 embedderErrorMessage.insert(" (in promise)", 8);
284 }
285
286 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::crea te(JSMessageSource, ErrorMessageLevel, embedderErrorMessage, message.m_resourceN ame, message.m_lineNumber, message.m_columnNumber);
287 consoleMessage->setScriptArguments(arguments);
288 consoleMessage->setCallStack(message.m_callStack);
289 consoleMessage->setScriptId(message.m_scriptId);
290 executionContext->addConsoleMessage(consoleMessage.release());
291 }
292 } 201 }
293 202
294 static void promiseRejectHandlerInMainThread(v8::PromiseRejectMessage data) 203 static void promiseRejectHandlerInMainThread(v8::PromiseRejectMessage data)
295 { 204 {
296 ASSERT(isMainThread()); 205 ASSERT(isMainThread());
297 206
298 if (data.GetEvent() != v8::kPromiseRejectWithNoHandler) 207 if (data.GetEvent() != v8::kPromiseRejectWithNoHandler)
299 return; 208 return;
300 209
301 // It's possible that promiseRejectHandlerInMainThread() is invoked while we 're initializing a window. 210 // It's possible that promiseRejectHandlerInMainThread() is invoked while we 're initializing a window.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 } else if (!exception.IsEmpty() && exception->IsInt32()) { 257 } else if (!exception.IsEmpty() && exception->IsInt32()) {
349 // For Smi's the message would be empty. 258 // For Smi's the message would be empty.
350 errorMessage = "Uncaught " + String::number(exception.As<v8::Integer>()- >Value()); 259 errorMessage = "Uncaught " + String::number(exception.As<v8::Integer>()- >Value());
351 } 260 }
352 261
353 String messageForConsole = extractMessageForConsole(data.GetValue()); 262 String messageForConsole = extractMessageForConsole(data.GetValue());
354 if (!messageForConsole.isEmpty()) 263 if (!messageForConsole.isEmpty())
355 errorMessage = "Uncaught " + messageForConsole; 264 errorMessage = "Uncaught " + messageForConsole;
356 265
357 ScriptState* scriptState = ScriptState::from(context); 266 ScriptState* scriptState = ScriptState::from(context);
358 promiseRejectMessageQueue().append(PromiseRejectMessage(ScriptValue(scriptSt ate, promise), ScriptValue(scriptState, data.GetValue()), errorMessage, resource Name, scriptId, lineNumber, columnNumber, callStack)); 267 rejectedPromisesOnMainThread().add(scriptState, data, errorMessage, resource Name, scriptId, lineNumber, columnNumber, callStack);
359 } 268 }
360 269
361 static void promiseRejectHandlerInWorker(v8::PromiseRejectMessage data) 270 static void promiseRejectHandlerInWorker(v8::PromiseRejectMessage data)
362 { 271 {
363 if (data.GetEvent() != v8::kPromiseRejectWithNoHandler) 272 if (data.GetEvent() != v8::kPromiseRejectWithNoHandler)
364 return; 273 return;
365 274
366 v8::Handle<v8::Promise> promise = data.GetPromise(); 275 v8::Handle<v8::Promise> promise = data.GetPromise();
367 276
368 // Bail out if called during context initialization. 277 // Bail out if called during context initialization.
369 v8::Isolate* isolate = promise->GetIsolate(); 278 v8::Isolate* isolate = promise->GetIsolate();
370 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); 279 ScriptState* scriptState = ScriptState::current(isolate);
371 if (context.IsEmpty()) 280 if (!scriptState->contextIsValid())
372 return; 281 return;
373 282
283 ExecutionContext* executionContext = scriptState->executionContext();
284 if (!executionContext)
285 return;
286
287 ASSERT(executionContext->isWorkerGlobalScope());
288 WorkerScriptController* scriptController = toWorkerGlobalScope(executionCont ext)->script();
289 ASSERT(scriptController);
290
374 int scriptId = 0; 291 int scriptId = 0;
375 int lineNumber = 0; 292 int lineNumber = 0;
376 int columnNumber = 0; 293 int columnNumber = 0;
377 String resourceName; 294 String resourceName;
378 String errorMessage; 295 String errorMessage;
379 296
380 v8::Handle<v8::Message> message = v8::Exception::CreateMessage(data.GetValue ()); 297 v8::Handle<v8::Message> message = v8::Exception::CreateMessage(data.GetValue ());
381 if (!message.IsEmpty()) { 298 if (!message.IsEmpty()) {
382 TOSTRING_VOID(V8StringResource<>, resourceName, message->GetScriptOrigin ().ResourceName()); 299 TOSTRING_VOID(V8StringResource<>, resourceName, message->GetScriptOrigin ().ResourceName());
383 scriptId = message->GetScriptOrigin().ScriptID()->Value(); 300 scriptId = message->GetScriptOrigin().ScriptID()->Value();
384 lineNumber = message->GetLineNumber(); 301 lineNumber = message->GetLineNumber();
385 columnNumber = message->GetStartColumn() + 1; 302 columnNumber = message->GetStartColumn() + 1;
386 errorMessage = toCoreString(message->Get()); 303 errorMessage = toCoreString(message->Get());
387 } 304 }
388 305 scriptController->rejectedPromises()->add(scriptState, data, errorMessage, r esourceName, scriptId, lineNumber, columnNumber, nullptr);
389 ScriptState* scriptState = ScriptState::from(context);
390 promiseRejectMessageQueue().append(PromiseRejectMessage(ScriptValue(scriptSt ate, promise), ScriptValue(scriptState, data.GetValue()), errorMessage, resource Name, scriptId, lineNumber, columnNumber, nullptr));
391 } 306 }
392 307
393 static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8 ::AccessType type, v8::Local<v8::Value> data) 308 static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8 ::AccessType type, v8::Local<v8::Value> data)
394 { 309 {
395 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 310 v8::Isolate* isolate = v8::Isolate::GetCurrent();
396 Frame* target = findFrame(host, data, isolate); 311 Frame* target = findFrame(host, data, isolate);
397 if (!target) 312 if (!target)
398 return; 313 return;
399 DOMWindow* targetWindow = target->domWindow(); 314 DOMWindow* targetWindow = target->domWindow();
400 315
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 static void initializeV8Common(v8::Isolate* isolate) 365 static void initializeV8Common(v8::Isolate* isolate)
451 { 366 {
452 v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue); 367 v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue);
453 v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue); 368 v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue);
454 369
455 v8::Debug::SetLiveEditEnabled(isolate, false); 370 v8::Debug::SetLiveEditEnabled(isolate, false);
456 371
457 isolate->SetAutorunMicrotasks(false); 372 isolate->SetAutorunMicrotasks(false);
458 } 373 }
459 374
375 namespace {
376
377 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
378 virtual void* Allocate(size_t size) override
379 {
380 void* data;
381 WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents: :ZeroInitialize, data);
382 return data;
383 }
384
385 virtual void* AllocateUninitialized(size_t size) override
386 {
387 void* data;
388 WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents: :DontInitialize, data);
389 return data;
390 }
391
392 virtual void Free(void* data, size_t size) override
393 {
394 WTF::ArrayBufferContents::freeMemory(data, size);
395 }
396 };
397
398 } // namespace
399
460 void V8Initializer::initializeMainThreadIfNeeded() 400 void V8Initializer::initializeMainThreadIfNeeded()
461 { 401 {
462 ASSERT(isMainThread()); 402 ASSERT(isMainThread());
463 403
464 static bool initialized = false; 404 static bool initialized = false;
465 if (initialized) 405 if (initialized)
466 return; 406 return;
467 initialized = true; 407 initialized = true;
468 408
469 DEFINE_STATIC_LOCAL(ArrayBufferAllocator, arrayBufferAllocator, ()); 409 DEFINE_STATIC_LOCAL(ArrayBufferAllocator, arrayBufferAllocator, ());
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 476
537 v8::V8::AddMessageListener(messageHandlerInWorker); 477 v8::V8::AddMessageListener(messageHandlerInWorker);
538 v8::V8::SetFatalErrorHandler(reportFatalErrorInWorker); 478 v8::V8::SetFatalErrorHandler(reportFatalErrorInWorker);
539 479
540 uint32_t here; 480 uint32_t here;
541 isolate->SetStackLimit(reinterpret_cast<uintptr_t>(&here - kWorkerMaxStackSi ze / sizeof(uint32_t*))); 481 isolate->SetStackLimit(reinterpret_cast<uintptr_t>(&here - kWorkerMaxStackSi ze / sizeof(uint32_t*)));
542 isolate->SetPromiseRejectCallback(promiseRejectHandlerInWorker); 482 isolate->SetPromiseRejectCallback(promiseRejectHandlerInWorker);
543 } 483 }
544 484
545 } // namespace blink 485 } // namespace blink
OLDNEW
« no previous file with comments | « Source/bindings/core/v8/V8Initializer.h ('k') | Source/bindings/core/v8/WorkerScriptController.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698