| 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/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/optional.h" | 10 #include "base/optional.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 class APIEventHandlerTest : public APIBindingTest { | 34 class APIEventHandlerTest : public APIBindingTest { |
| 35 protected: | 35 protected: |
| 36 APIEventHandlerTest() {} | 36 APIEventHandlerTest() {} |
| 37 ~APIEventHandlerTest() override {} | 37 ~APIEventHandlerTest() override {} |
| 38 | 38 |
| 39 void SetUp() override { | 39 void SetUp() override { |
| 40 APIBindingTest::SetUp(); | 40 APIBindingTest::SetUp(); |
| 41 handler_ = base::MakeUnique<APIEventHandler>( | 41 handler_ = base::MakeUnique<APIEventHandler>( |
| 42 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 42 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 43 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 43 base::Bind(&DoNothingOnEventListenersChanged)); | 44 base::Bind(&DoNothingOnEventListenersChanged)); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void TearDown() override { | 47 void TearDown() override { |
| 47 DisposeAllContexts(); | 48 DisposeAllContexts(); |
| 48 handler_.reset(); | 49 handler_.reset(); |
| 49 APIBindingTest::TearDown(); | 50 APIBindingTest::TearDown(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void OnWillDisposeContext(v8::Local<v8::Context> context) override { | 53 void OnWillDisposeContext(v8::Local<v8::Context> context) override { |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 v8::MaybeLocal<v8::Value> maybe_result = | 550 v8::MaybeLocal<v8::Value> maybe_result = |
| 550 function->Call(context, context->Global(), argc, argv); | 551 function->Call(context, context->Global(), argc, argv); |
| 551 v8::Global<v8::Value> result; | 552 v8::Global<v8::Value> result; |
| 552 v8::Local<v8::Value> local; | 553 v8::Local<v8::Value> local; |
| 553 if (maybe_result.ToLocal(&local)) | 554 if (maybe_result.ToLocal(&local)) |
| 554 result.Reset(context->GetIsolate(), local); | 555 result.Reset(context->GetIsolate(), local); |
| 555 }; | 556 }; |
| 556 | 557 |
| 557 SetHandler(base::MakeUnique<APIEventHandler>( | 558 SetHandler(base::MakeUnique<APIEventHandler>( |
| 558 base::Bind(run_js_and_expect_error), | 559 base::Bind(run_js_and_expect_error), |
| 560 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 559 base::Bind(&DoNothingOnEventListenersChanged))); | 561 base::Bind(&DoNothingOnEventListenersChanged))); |
| 560 | 562 |
| 561 v8::HandleScope handle_scope(isolate()); | 563 v8::HandleScope handle_scope(isolate()); |
| 562 v8::Local<v8::Context> context = MainContext(); | 564 v8::Local<v8::Context> context = MainContext(); |
| 563 | 565 |
| 564 const char kEventName[] = "alpha"; | 566 const char kEventName[] = "alpha"; |
| 565 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 567 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 566 kEventName, false, binding::kNoListenerMax, true, context); | 568 kEventName, false, binding::kNoListenerMax, true, context); |
| 567 ASSERT_FALSE(event.IsEmpty()); | 569 ASSERT_FALSE(event.IsEmpty()); |
| 568 | 570 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 "didThrow")); | 622 "didThrow")); |
| 621 EXPECT_EQ("[42]", GetStringPropertyFromObject(context->Global(), context, | 623 EXPECT_EQ("[42]", GetStringPropertyFromObject(context->Global(), context, |
| 622 "eventArgs")); | 624 "eventArgs")); |
| 623 EXPECT_TRUE(did_throw); | 625 EXPECT_TRUE(did_throw); |
| 624 } | 626 } |
| 625 | 627 |
| 626 // Tests being notified as listeners are added or removed from events. | 628 // Tests being notified as listeners are added or removed from events. |
| 627 TEST_F(APIEventHandlerTest, CallbackNotifications) { | 629 TEST_F(APIEventHandlerTest, CallbackNotifications) { |
| 628 MockEventChangeHandler change_handler; | 630 MockEventChangeHandler change_handler; |
| 629 SetHandler(base::MakeUnique<APIEventHandler>( | 631 SetHandler(base::MakeUnique<APIEventHandler>( |
| 630 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), change_handler.Get())); | 632 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 633 base::Bind(&RunFunctionOnGlobalAndReturnHandle), change_handler.Get())); |
| 631 | 634 |
| 632 v8::HandleScope handle_scope(isolate()); | 635 v8::HandleScope handle_scope(isolate()); |
| 633 | 636 |
| 634 v8::Local<v8::Context> context_a = MainContext(); | 637 v8::Local<v8::Context> context_a = MainContext(); |
| 635 v8::Local<v8::Context> context_b = AddContext(); | 638 v8::Local<v8::Context> context_b = AddContext(); |
| 636 | 639 |
| 637 const char kEventName1[] = "onFoo"; | 640 const char kEventName1[] = "onFoo"; |
| 638 const char kEventName2[] = "onBar"; | 641 const char kEventName2[] = "onBar"; |
| 639 v8::Local<v8::Object> event1_a = handler()->CreateEventInstance( | 642 v8::Local<v8::Object> event1_a = handler()->CreateEventInstance( |
| 640 kEventName1, false, binding::kNoListenerMax, true, context_a); | 643 kEventName1, false, binding::kNoListenerMax, true, context_a); |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 // that we don't crash.) | 910 // that we don't crash.) |
| 908 } | 911 } |
| 909 | 912 |
| 910 // Test creating a custom event, as is done by a few of our custom bindings. | 913 // Test creating a custom event, as is done by a few of our custom bindings. |
| 911 TEST_F(APIEventHandlerTest, TestCreateCustomEvent) { | 914 TEST_F(APIEventHandlerTest, TestCreateCustomEvent) { |
| 912 v8::HandleScope handle_scope(isolate()); | 915 v8::HandleScope handle_scope(isolate()); |
| 913 v8::Local<v8::Context> context = MainContext(); | 916 v8::Local<v8::Context> context = MainContext(); |
| 914 | 917 |
| 915 MockEventChangeHandler change_handler; | 918 MockEventChangeHandler change_handler; |
| 916 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 919 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 920 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 917 change_handler.Get()); | 921 change_handler.Get()); |
| 918 | 922 |
| 919 v8::Local<v8::Object> event = handler.CreateAnonymousEventInstance(context); | 923 v8::Local<v8::Object> event = handler.CreateAnonymousEventInstance(context); |
| 920 ASSERT_FALSE(event.IsEmpty()); | 924 ASSERT_FALSE(event.IsEmpty()); |
| 921 | 925 |
| 922 const char kAddListenerFunction[] = | 926 const char kAddListenerFunction[] = |
| 923 "(function(event) {\n" | 927 "(function(event) {\n" |
| 924 " event.addListener(function() {\n" | 928 " event.addListener(function() {\n" |
| 925 " this.eventArgs = Array.from(arguments);\n" | 929 " this.eventArgs = Array.from(arguments);\n" |
| 926 " });\n" | 930 " });\n" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 955 "eventArgs")); | 959 "eventArgs")); |
| 956 } | 960 } |
| 957 | 961 |
| 958 // Test adding a custom event with a cyclic dependency. Nothing should leak. | 962 // Test adding a custom event with a cyclic dependency. Nothing should leak. |
| 959 TEST_F(APIEventHandlerTest, TestCreateCustomEventWithCyclicDependency) { | 963 TEST_F(APIEventHandlerTest, TestCreateCustomEventWithCyclicDependency) { |
| 960 v8::HandleScope handle_scope(isolate()); | 964 v8::HandleScope handle_scope(isolate()); |
| 961 v8::Local<v8::Context> context = MainContext(); | 965 v8::Local<v8::Context> context = MainContext(); |
| 962 | 966 |
| 963 MockEventChangeHandler change_handler; | 967 MockEventChangeHandler change_handler; |
| 964 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 968 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 969 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 965 change_handler.Get()); | 970 change_handler.Get()); |
| 966 | 971 |
| 967 v8::Local<v8::Object> event = handler.CreateAnonymousEventInstance(context); | 972 v8::Local<v8::Object> event = handler.CreateAnonymousEventInstance(context); |
| 968 ASSERT_FALSE(event.IsEmpty()); | 973 ASSERT_FALSE(event.IsEmpty()); |
| 969 | 974 |
| 970 const char kAddListenerFunction[] = | 975 const char kAddListenerFunction[] = |
| 971 "(function(event) {\n" | 976 "(function(event) {\n" |
| 972 " event.addListener(function() {}.bind(null, event));\n" | 977 " event.addListener(function() {}.bind(null, event));\n" |
| 973 "})"; | 978 "})"; |
| 974 v8::Local<v8::Value> add_listener_argv[] = {event}; | 979 v8::Local<v8::Value> add_listener_argv[] = {event}; |
| 975 RunFunction(FunctionFromString(context, kAddListenerFunction), context, | 980 RunFunction(FunctionFromString(context, kAddListenerFunction), context, |
| 976 arraysize(add_listener_argv), add_listener_argv); | 981 arraysize(add_listener_argv), add_listener_argv); |
| 977 | 982 |
| 978 DisposeContext(context); | 983 DisposeContext(context); |
| 979 } | 984 } |
| 980 | 985 |
| 981 TEST_F(APIEventHandlerTest, TestUnmanagedEvents) { | 986 TEST_F(APIEventHandlerTest, TestUnmanagedEvents) { |
| 982 v8::HandleScope handle_scope(isolate()); | 987 v8::HandleScope handle_scope(isolate()); |
| 983 v8::Local<v8::Context> context = MainContext(); | 988 v8::Local<v8::Context> context = MainContext(); |
| 984 | 989 |
| 985 auto fail_on_notified = | 990 auto fail_on_notified = |
| 986 [](const std::string& event_name, binding::EventListenersChanged changed, | 991 [](const std::string& event_name, binding::EventListenersChanged changed, |
| 987 const base::DictionaryValue* filter, bool was_manual, | 992 const base::DictionaryValue* filter, bool was_manual, |
| 988 v8::Local<v8::Context> context) { ADD_FAILURE(); }; | 993 v8::Local<v8::Context> context) { ADD_FAILURE(); }; |
| 989 | 994 |
| 990 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 995 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 996 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 991 base::Bind(fail_on_notified)); | 997 base::Bind(fail_on_notified)); |
| 992 | 998 |
| 993 const char kEventName[] = "alpha"; | 999 const char kEventName[] = "alpha"; |
| 994 v8::Local<v8::Object> event = handler.CreateEventInstance( | 1000 v8::Local<v8::Object> event = handler.CreateEventInstance( |
| 995 kEventName, false, binding::kNoListenerMax, false, context); | 1001 kEventName, false, binding::kNoListenerMax, false, context); |
| 996 | 1002 |
| 997 const char kListener[] = | 1003 const char kListener[] = |
| 998 "(function() {\n" | 1004 "(function() {\n" |
| 999 " this.eventArgs = Array.from(arguments);\n" | 1005 " this.eventArgs = Array.from(arguments);\n" |
| 1000 "});"; | 1006 "});"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1022 "(function(event, listener) { event.removeListener(listener); })"; | 1028 "(function(event, listener) { event.removeListener(listener); })"; |
| 1023 v8::Local<v8::Value> args[] = {event, listener}; | 1029 v8::Local<v8::Value> args[] = {event, listener}; |
| 1024 RunFunction(FunctionFromString(context, kRemoveListener), context, | 1030 RunFunction(FunctionFromString(context, kRemoveListener), context, |
| 1025 arraysize(args), args); | 1031 arraysize(args), args); |
| 1026 } | 1032 } |
| 1027 | 1033 |
| 1028 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); | 1034 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 1029 } | 1035 } |
| 1030 | 1036 |
| 1031 } // namespace extensions | 1037 } // namespace extensions |
| OLD | NEW |