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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "extensions/renderer/api_binding.h" | 10 #include "extensions/renderer/api_binding.h" |
11 #include "extensions/renderer/api_binding_hooks.h" | 11 #include "extensions/renderer/api_binding_hooks.h" |
| 12 #include "extensions/renderer/api_binding_hooks_test_delegate.h" |
12 #include "extensions/renderer/api_binding_test.h" | 13 #include "extensions/renderer/api_binding_test.h" |
13 #include "extensions/renderer/api_binding_test_util.h" | 14 #include "extensions/renderer/api_binding_test_util.h" |
14 #include "extensions/renderer/api_event_handler.h" | 15 #include "extensions/renderer/api_event_handler.h" |
15 #include "extensions/renderer/api_request_handler.h" | 16 #include "extensions/renderer/api_request_handler.h" |
16 #include "extensions/renderer/api_type_reference_map.h" | 17 #include "extensions/renderer/api_type_reference_map.h" |
17 #include "gin/arguments.h" | 18 #include "gin/arguments.h" |
18 #include "gin/converter.h" | 19 #include "gin/converter.h" |
19 #include "gin/public/context_holder.h" | 20 #include "gin/public/context_holder.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 22 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 void SetProperties(const char* properties) { | 172 void SetProperties(const char* properties) { |
172 binding_properties_ = DictionaryValueFromString(properties); | 173 binding_properties_ = DictionaryValueFromString(properties); |
173 ASSERT_TRUE(binding_properties_); | 174 ASSERT_TRUE(binding_properties_); |
174 } | 175 } |
175 | 176 |
176 void SetHooks(std::unique_ptr<APIBindingHooks> hooks) { | 177 void SetHooks(std::unique_ptr<APIBindingHooks> hooks) { |
177 binding_hooks_ = std::move(hooks); | 178 binding_hooks_ = std::move(hooks); |
178 ASSERT_TRUE(binding_hooks_); | 179 ASSERT_TRUE(binding_hooks_); |
179 } | 180 } |
180 | 181 |
| 182 void SetHooksDelegate( |
| 183 std::unique_ptr<APIBindingHooksDelegate> hooks_delegate) { |
| 184 binding_hooks_delegate_ = std::move(hooks_delegate); |
| 185 ASSERT_TRUE(binding_hooks_delegate_); |
| 186 } |
| 187 |
181 void SetCreateCustomType(const APIBinding::CreateCustomType& callback) { | 188 void SetCreateCustomType(const APIBinding::CreateCustomType& callback) { |
182 create_custom_type_ = callback; | 189 create_custom_type_ = callback; |
183 } | 190 } |
184 | 191 |
185 void InitializeBinding() { | 192 void InitializeBinding() { |
186 if (!binding_hooks_) { | 193 if (!binding_hooks_) { |
187 binding_hooks_ = base::MakeUnique<APIBindingHooks>( | 194 binding_hooks_ = base::MakeUnique<APIBindingHooks>( |
188 kBindingName, binding::RunJSFunctionSync()); | 195 kBindingName, binding::RunJSFunctionSync()); |
189 } | 196 } |
| 197 if (binding_hooks_delegate_) |
| 198 binding_hooks_->SetDelegate(std::move(binding_hooks_delegate_)); |
190 event_handler_ = base::MakeUnique<APIEventHandler>( | 199 event_handler_ = base::MakeUnique<APIEventHandler>( |
191 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 200 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
192 base::Bind(&OnEventListenersChanged)); | 201 base::Bind(&OnEventListenersChanged)); |
193 binding_ = base::MakeUnique<APIBinding>( | 202 binding_ = base::MakeUnique<APIBinding>( |
194 kBindingName, binding_functions_.get(), binding_types_.get(), | 203 kBindingName, binding_functions_.get(), binding_types_.get(), |
195 binding_events_.get(), binding_properties_.get(), create_custom_type_, | 204 binding_events_.get(), binding_properties_.get(), create_custom_type_, |
196 std::move(binding_hooks_), &type_refs_, request_handler_.get(), | 205 std::move(binding_hooks_), &type_refs_, request_handler_.get(), |
197 event_handler_.get()); | 206 event_handler_.get()); |
198 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); | 207 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); |
199 } | 208 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 std::unique_ptr<APIBinding> binding_; | 255 std::unique_ptr<APIBinding> binding_; |
247 std::unique_ptr<APIEventHandler> event_handler_; | 256 std::unique_ptr<APIEventHandler> event_handler_; |
248 std::unique_ptr<APIRequestHandler> request_handler_; | 257 std::unique_ptr<APIRequestHandler> request_handler_; |
249 APITypeReferenceMap type_refs_; | 258 APITypeReferenceMap type_refs_; |
250 | 259 |
251 std::unique_ptr<base::ListValue> binding_functions_; | 260 std::unique_ptr<base::ListValue> binding_functions_; |
252 std::unique_ptr<base::ListValue> binding_events_; | 261 std::unique_ptr<base::ListValue> binding_events_; |
253 std::unique_ptr<base::ListValue> binding_types_; | 262 std::unique_ptr<base::ListValue> binding_types_; |
254 std::unique_ptr<base::DictionaryValue> binding_properties_; | 263 std::unique_ptr<base::DictionaryValue> binding_properties_; |
255 std::unique_ptr<APIBindingHooks> binding_hooks_; | 264 std::unique_ptr<APIBindingHooks> binding_hooks_; |
| 265 std::unique_ptr<APIBindingHooksDelegate> binding_hooks_delegate_; |
256 APIBinding::CreateCustomType create_custom_type_; | 266 APIBinding::CreateCustomType create_custom_type_; |
257 | 267 |
258 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); | 268 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); |
259 }; | 269 }; |
260 | 270 |
261 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, | 271 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, |
262 v8::Local<v8::Object> object, | 272 v8::Local<v8::Object> object, |
263 const std::string& script_source, | 273 const std::string& script_source, |
264 bool should_pass, | 274 bool should_pass, |
265 const std::string& expected_json_arguments, | 275 const std::string& expected_json_arguments, |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 DisposeContext(context_a); | 694 DisposeContext(context_a); |
685 ExpectPass(context_b, binding_object_b, "obj.oneString('foo');", "['foo']", | 695 ExpectPass(context_b, binding_object_b, "obj.oneString('foo');", "['foo']", |
686 false); | 696 false); |
687 } | 697 } |
688 | 698 |
689 // Tests adding custom hooks for an API method. | 699 // Tests adding custom hooks for an API method. |
690 TEST_F(APIBindingUnittest, TestCustomHooks) { | 700 TEST_F(APIBindingUnittest, TestCustomHooks) { |
691 SetFunctions(kFunctions); | 701 SetFunctions(kFunctions); |
692 | 702 |
693 // Register a hook for the test.oneString method. | 703 // Register a hook for the test.oneString method. |
694 auto hooks = base::MakeUnique<APIBindingHooks>( | 704 auto hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
695 kBindingName, base::Bind(&RunFunctionOnGlobalAndReturnHandle)); | |
696 bool did_call = false; | 705 bool did_call = false; |
697 auto hook = [](bool* did_call, const APISignature* signature, | 706 auto hook = [](bool* did_call, const APISignature* signature, |
698 v8::Local<v8::Context> context, | 707 v8::Local<v8::Context> context, |
699 std::vector<v8::Local<v8::Value>>* arguments, | 708 std::vector<v8::Local<v8::Value>>* arguments, |
700 const APITypeReferenceMap& ref_map) { | 709 const APITypeReferenceMap& ref_map) { |
701 *did_call = true; | 710 *did_call = true; |
702 APIBindingHooks::RequestResult result( | 711 APIBindingHooks::RequestResult result( |
703 APIBindingHooks::RequestResult::HANDLED); | 712 APIBindingHooks::RequestResult::HANDLED); |
704 if (arguments->size() != 1u) { // ASSERT* messes with the return type. | 713 if (arguments->size() != 1u) { // ASSERT* messes with the return type. |
705 EXPECT_EQ(1u, arguments->size()); | 714 EXPECT_EQ(1u, arguments->size()); |
706 return result; | 715 return result; |
707 } | 716 } |
708 EXPECT_EQ("foo", gin::V8ToString(arguments->at(0))); | 717 EXPECT_EQ("foo", gin::V8ToString(arguments->at(0))); |
709 return result; | 718 return result; |
710 }; | 719 }; |
711 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); | 720 hooks->AddHandler("test.oneString", base::Bind(hook, &did_call)); |
712 SetHooks(std::move(hooks)); | 721 SetHooksDelegate(std::move(hooks)); |
713 | 722 |
714 InitializeBinding(); | 723 InitializeBinding(); |
715 | 724 |
716 v8::HandleScope handle_scope(isolate()); | 725 v8::HandleScope handle_scope(isolate()); |
717 v8::Local<v8::Context> context = MainContext(); | 726 v8::Local<v8::Context> context = MainContext(); |
718 | 727 |
719 v8::Local<v8::Object> binding_object = | 728 v8::Local<v8::Object> binding_object = |
720 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | 729 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
721 | 730 |
722 // First try calling the oneString() method, which has a custom hook | 731 // First try calling the oneString() method, which has a custom hook |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
983 } | 992 } |
984 | 993 |
985 // Tests that native custom hooks can return results synchronously, or throw | 994 // Tests that native custom hooks can return results synchronously, or throw |
986 // exceptions for bad invocations. | 995 // exceptions for bad invocations. |
987 TEST_F(APIBindingUnittest, | 996 TEST_F(APIBindingUnittest, |
988 TestReturningResultAndThrowingExceptionFromCustomNativeHook) { | 997 TestReturningResultAndThrowingExceptionFromCustomNativeHook) { |
989 v8::HandleScope handle_scope(isolate()); | 998 v8::HandleScope handle_scope(isolate()); |
990 v8::Local<v8::Context> context = MainContext(); | 999 v8::Local<v8::Context> context = MainContext(); |
991 | 1000 |
992 // Register a hook for the test.oneString method. | 1001 // Register a hook for the test.oneString method. |
993 auto hooks = base::MakeUnique<APIBindingHooks>( | 1002 auto hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
994 kBindingName, base::Bind(&RunFunctionOnGlobalAndReturnHandle)); | |
995 bool did_call = false; | 1003 bool did_call = false; |
996 auto hook = [](bool* did_call, const APISignature* signature, | 1004 auto hook = [](bool* did_call, const APISignature* signature, |
997 v8::Local<v8::Context> context, | 1005 v8::Local<v8::Context> context, |
998 std::vector<v8::Local<v8::Value>>* arguments, | 1006 std::vector<v8::Local<v8::Value>>* arguments, |
999 const APITypeReferenceMap& ref_map) { | 1007 const APITypeReferenceMap& ref_map) { |
1000 APIBindingHooks::RequestResult result( | 1008 APIBindingHooks::RequestResult result( |
1001 APIBindingHooks::RequestResult::HANDLED); | 1009 APIBindingHooks::RequestResult::HANDLED); |
1002 if (arguments->size() != 1u) { // ASSERT* messes with the return type. | 1010 if (arguments->size() != 1u) { // ASSERT* messes with the return type. |
1003 EXPECT_EQ(1u, arguments->size()); | 1011 EXPECT_EQ(1u, arguments->size()); |
1004 return result; | 1012 return result; |
1005 } | 1013 } |
1006 v8::Isolate* isolate = context->GetIsolate(); | 1014 v8::Isolate* isolate = context->GetIsolate(); |
1007 std::string arg_value = gin::V8ToString(arguments->at(0)); | 1015 std::string arg_value = gin::V8ToString(arguments->at(0)); |
1008 if (arg_value == "throw") { | 1016 if (arg_value == "throw") { |
1009 isolate->ThrowException(v8::Exception::Error( | 1017 isolate->ThrowException(v8::Exception::Error( |
1010 gin::StringToV8(isolate, "Custom Hook Error"))); | 1018 gin::StringToV8(isolate, "Custom Hook Error"))); |
1011 result.code = APIBindingHooks::RequestResult::THROWN; | 1019 result.code = APIBindingHooks::RequestResult::THROWN; |
1012 return result; | 1020 return result; |
1013 } | 1021 } |
1014 result.return_value = | 1022 result.return_value = |
1015 gin::StringToV8(context->GetIsolate(), arg_value + " pong"); | 1023 gin::StringToV8(context->GetIsolate(), arg_value + " pong"); |
1016 return result; | 1024 return result; |
1017 }; | 1025 }; |
1018 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); | 1026 hooks->AddHandler("test.oneString", base::Bind(hook, &did_call)); |
1019 | 1027 |
1020 SetHooks(std::move(hooks)); | 1028 SetHooksDelegate(std::move(hooks)); |
1021 SetFunctions(kFunctions); | 1029 SetFunctions(kFunctions); |
1022 InitializeBinding(); | 1030 InitializeBinding(); |
1023 | 1031 |
1024 v8::Local<v8::Object> binding_object = | 1032 v8::Local<v8::Object> binding_object = |
1025 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | 1033 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
1026 | 1034 |
1027 { | 1035 { |
1028 // Test an invocation that we expect to throw an exception. | 1036 // Test an invocation that we expect to throw an exception. |
1029 v8::Local<v8::Function> function = | 1037 v8::Local<v8::Function> function = |
1030 FunctionFromString( | 1038 FunctionFromString( |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 | 1164 |
1157 blink::WebScopedUserGesture user_gesture(nullptr); | 1165 blink::WebScopedUserGesture user_gesture(nullptr); |
1158 RunFunction(function, context, arraysize(argv), argv); | 1166 RunFunction(function, context, arraysize(argv), argv); |
1159 ASSERT_TRUE(last_request()); | 1167 ASSERT_TRUE(last_request()); |
1160 EXPECT_TRUE(last_request()->has_user_gesture); | 1168 EXPECT_TRUE(last_request()->has_user_gesture); |
1161 | 1169 |
1162 reset_last_request(); | 1170 reset_last_request(); |
1163 } | 1171 } |
1164 | 1172 |
1165 } // namespace extensions | 1173 } // namespace extensions |
OLD | NEW |