OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/renderer/api_event_handler.h" | 5 #include "extensions/renderer/api_event_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 { | 440 { |
441 v8::Local<v8::Value> argv[] = {event}; | 441 v8::Local<v8::Value> argv[] = {event}; |
442 RunFunctionOnGlobal(fire_event_function, context, arraysize(argv), argv); | 442 RunFunctionOnGlobal(fire_event_function, context, arraysize(argv), argv); |
443 } | 443 } |
444 | 444 |
445 EXPECT_EQ("[42,\"foo\",{\"bar\":\"baz\"}]", | 445 EXPECT_EQ("[42,\"foo\",{\"bar\":\"baz\"}]", |
446 GetStringPropertyFromObject( | 446 GetStringPropertyFromObject( |
447 context->Global(), context, "eventArgs")); | 447 context->Global(), context, "eventArgs")); |
448 } | 448 } |
449 | 449 |
450 // Test an event listener throwing an exception. | |
451 TEST_F(APIEventHandlerTest, TestEventListenersThrowingExceptions) { | |
452 auto run_js_and_expect_error = [](v8::Local<v8::Function> function, | |
jbroman
2017/01/05 19:52:51
Isn't it the RunJSFunction that's supposed to be c
Devlin
2017/01/05 21:01:46
No, RunJSFunction doesn't always catch exceptions
jbroman
2017/01/05 22:22:14
Should we be catching exceptions in the event disp
Devlin
2017/01/10 21:28:07
Fixed (thanks for the help!). Note that we still
| |
453 v8::Local<v8::Context> context, | |
454 int argc, | |
455 v8::Local<v8::Value> argv[]) { | |
456 v8::MaybeLocal<v8::Value> maybe_result = | |
457 function->Call(context, context->Global(), argc, argv); | |
458 v8::Global<v8::Value> result; | |
459 v8::Local<v8::Value> local; | |
460 if (maybe_result.ToLocal(&local)) | |
461 result.Reset(context->GetIsolate(), local); | |
462 }; | |
463 | |
464 v8::HandleScope handle_scope(isolate()); | |
465 v8::Local<v8::Context> context = ContextLocal(); | |
466 | |
467 APIEventHandler handler(base::Bind(run_js_and_expect_error)); | |
468 const char kEventName[] = "alpha"; | |
469 v8::Local<v8::Object> event = | |
470 handler.CreateEventInstance(kEventName, context); | |
471 ASSERT_FALSE(event.IsEmpty()); | |
472 | |
473 // A listener that will throw an exception. We guarantee that we throw the | |
474 // exception first so that we don't rely on event listener ordering. | |
475 const char kListenerFunction[] = | |
476 "(function() {\n" | |
477 " if (!this.didThrow) {\n" | |
478 " this.didThrow = true;\n" | |
479 " throw new Error('Event handler error');\n" | |
480 " }\n" | |
481 " this.eventArgs = Array.from(arguments);\n" | |
482 "});"; | |
483 | |
484 const char kAddListenerFunction[] = | |
485 "(function(event, listener) { event.addListener(listener); })"; | |
486 v8::Local<v8::Function> add_listener_function = | |
487 FunctionFromString(context, kAddListenerFunction); | |
488 | |
489 for (int i = 0; i < 2; ++i) { | |
490 v8::Local<v8::Function> listener = | |
491 FunctionFromString(context, kListenerFunction); | |
492 v8::Local<v8::Value> argv[] = {event, listener}; | |
493 RunFunctionOnGlobal(add_listener_function, context, arraysize(argv), argv); | |
494 } | |
495 EXPECT_EQ(2u, handler.GetNumEventListenersForTesting(kEventName, context)); | |
496 | |
497 std::unique_ptr<base::ListValue> event_args = ListValueFromString("[42]"); | |
498 ASSERT_TRUE(event_args); | |
499 handler.FireEventInContext(kEventName, context, *event_args); | |
500 | |
501 // An exception should have been thrown by the first listener and the second | |
502 // listener should have recorded the event arguments. | |
503 EXPECT_EQ("true", | |
504 GetStringPropertyFromObject( | |
505 context->Global(), context, "didThrow")); | |
506 EXPECT_EQ("[42]", | |
507 GetStringPropertyFromObject( | |
508 context->Global(), context, "eventArgs")); | |
509 } | |
510 | |
450 } // namespace extensions | 511 } // namespace extensions |
OLD | NEW |