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_bindings_system.h" | 5 #include "extensions/renderer/api_bindings_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "extensions/common/event_filtering_info.h" | 13 #include "extensions/common/event_filtering_info.h" |
14 #include "extensions/common/extension_api.h" | 14 #include "extensions/common/extension_api.h" |
15 #include "extensions/renderer/api_binding.h" | 15 #include "extensions/renderer/api_binding.h" |
16 #include "extensions/renderer/api_binding_hooks.h" | 16 #include "extensions/renderer/api_binding_hooks.h" |
17 #include "extensions/renderer/api_binding_hooks_delegate.h" | 17 #include "extensions/renderer/api_binding_hooks_test_delegate.h" |
18 #include "extensions/renderer/api_binding_test_util.h" | 18 #include "extensions/renderer/api_binding_test_util.h" |
19 #include "extensions/renderer/api_binding_types.h" | 19 #include "extensions/renderer/api_binding_types.h" |
20 #include "extensions/renderer/api_bindings_system_unittest.h" | 20 #include "extensions/renderer/api_bindings_system_unittest.h" |
21 #include "gin/arguments.h" | 21 #include "gin/arguments.h" |
22 #include "gin/converter.h" | 22 #include "gin/converter.h" |
23 #include "gin/try_catch.h" | 23 #include "gin/try_catch.h" |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 | 26 |
27 namespace { | 27 namespace { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 " 'functions': [{" | 87 " 'functions': [{" |
88 " 'name': 'functionWithExternalRef'," | 88 " 'name': 'functionWithExternalRef'," |
89 " 'parameters': [{ 'name': 'someRef', '$ref': 'alpha.objRef' }]" | 89 " 'parameters': [{ 'name': 'someRef', '$ref': 'alpha.objRef' }]" |
90 " }]" | 90 " }]" |
91 "}"; | 91 "}"; |
92 | 92 |
93 bool AllowAllAPIs(const std::string& name) { | 93 bool AllowAllAPIs(const std::string& name) { |
94 return true; | 94 return true; |
95 } | 95 } |
96 | 96 |
97 class TestHooks : public APIBindingHooksDelegate { | |
98 public: | |
99 TestHooks() {} | |
100 ~TestHooks() override {} | |
101 | |
102 using CustomEventFactory = base::Callback<v8::Local<v8::Value>( | |
103 v8::Local<v8::Context>, | |
104 const binding::RunJSFunctionSync& run_js, | |
105 const std::string& event_name)>; | |
106 | |
107 bool CreateCustomEvent(v8::Local<v8::Context> context, | |
108 const binding::RunJSFunctionSync& run_js_sync, | |
109 const std::string& event_name, | |
110 v8::Local<v8::Value>* event_out) override { | |
111 if (!custom_event_.is_null()) { | |
112 *event_out = custom_event_.Run(context, run_js_sync, event_name); | |
113 return true; | |
114 } | |
115 return false; | |
116 } | |
117 | |
118 void RegisterHooks(APIBindingHooks* hooks) { | |
119 for (const auto& request_handler : request_handlers_) { | |
120 hooks->RegisterHandleRequest(request_handler.first, | |
121 request_handler.second); | |
122 } | |
123 } | |
124 | |
125 void AddHandler(base::StringPiece name, | |
126 const APIBindingHooks::HandleRequestHook& hook) { | |
127 request_handlers_[name.as_string()] = hook; | |
128 } | |
129 | |
130 void SetCustomEvent(const CustomEventFactory& custom_event) { | |
131 custom_event_ = custom_event; | |
132 } | |
133 | |
134 private: | |
135 std::map<std::string, APIBindingHooks::HandleRequestHook> request_handlers_; | |
136 CustomEventFactory custom_event_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(TestHooks); | |
139 }; | |
140 | |
141 } // namespace | 97 } // namespace |
142 | 98 |
143 APIBindingsSystemTest::APIBindingsSystemTest() {} | 99 APIBindingsSystemTest::APIBindingsSystemTest() {} |
144 APIBindingsSystemTest::~APIBindingsSystemTest() = default; | 100 APIBindingsSystemTest::~APIBindingsSystemTest() = default; |
145 | 101 |
146 void APIBindingsSystemTest::SetUp() { | 102 void APIBindingsSystemTest::SetUp() { |
147 APIBindingTest::SetUp(); | 103 APIBindingTest::SetUp(); |
148 | 104 |
149 // Create the fake API schemas. | 105 // Create the fake API schemas. |
150 for (const auto& api : GetAPIs()) { | 106 for (const auto& api : GetAPIs()) { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 return result; | 318 return result; |
363 } | 319 } |
364 v8::Local<v8::String> response = | 320 v8::Local<v8::String> response = |
365 gin::StringToV8(context->GetIsolate(), "bar"); | 321 gin::StringToV8(context->GetIsolate(), "bar"); |
366 v8::Local<v8::Value> response_args[] = {response}; | 322 v8::Local<v8::Value> response_args[] = {response}; |
367 RunFunctionOnGlobal(arguments->at(1).As<v8::Function>(), | 323 RunFunctionOnGlobal(arguments->at(1).As<v8::Function>(), |
368 context, 1, response_args); | 324 context, 1, response_args); |
369 return result; | 325 return result; |
370 }; | 326 }; |
371 | 327 |
372 auto test_hooks = base::MakeUnique<TestHooks>(); | 328 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
373 test_hooks->AddHandler("alpha.functionWithCallback", | 329 test_hooks->AddHandler("alpha.functionWithCallback", |
374 base::Bind(hook, &did_call)); | 330 base::Bind(hook, &did_call)); |
375 APIBindingHooks* binding_hooks = | 331 APIBindingHooks* binding_hooks = |
376 bindings_system()->GetHooksForAPI(kAlphaAPIName); | 332 bindings_system()->GetHooksForAPI(kAlphaAPIName); |
377 test_hooks->RegisterHooks(binding_hooks); | |
378 binding_hooks->SetDelegate(std::move(test_hooks)); | 333 binding_hooks->SetDelegate(std::move(test_hooks)); |
379 | 334 |
380 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 335 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
381 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 336 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
382 ASSERT_FALSE(alpha_api.IsEmpty()); | 337 ASSERT_FALSE(alpha_api.IsEmpty()); |
383 | 338 |
384 { | 339 { |
385 // Test a simple call -> response. | 340 // Test a simple call -> response. |
386 const char kTestCall[] = | 341 const char kTestCall[] = |
387 "obj.functionWithCallback('foo', function() {\n" | 342 "obj.functionWithCallback('foo', function() {\n" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 const binding::RunJSFunctionSync& run_js, | 434 const binding::RunJSFunctionSync& run_js, |
480 const std::string& event_name) { | 435 const std::string& event_name) { |
481 v8::Isolate* isolate = context->GetIsolate(); | 436 v8::Isolate* isolate = context->GetIsolate(); |
482 v8::Local<v8::Object> ret = v8::Object::New(isolate); | 437 v8::Local<v8::Object> ret = v8::Object::New(isolate); |
483 ret->Set(context, gin::StringToSymbol(isolate, "name"), | 438 ret->Set(context, gin::StringToSymbol(isolate, "name"), |
484 gin::StringToSymbol(isolate, event_name)) | 439 gin::StringToSymbol(isolate, event_name)) |
485 .ToChecked(); | 440 .ToChecked(); |
486 return ret.As<v8::Value>(); | 441 return ret.As<v8::Value>(); |
487 }; | 442 }; |
488 | 443 |
489 auto test_hooks = base::MakeUnique<TestHooks>(); | 444 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
490 test_hooks->SetCustomEvent(base::Bind(create_custom_event)); | 445 test_hooks->SetCustomEvent(base::Bind(create_custom_event)); |
491 APIBindingHooks* binding_hooks = | 446 APIBindingHooks* binding_hooks = |
492 bindings_system()->GetHooksForAPI(kAlphaAPIName); | 447 bindings_system()->GetHooksForAPI(kAlphaAPIName); |
493 binding_hooks->SetDelegate(std::move(test_hooks)); | 448 binding_hooks->SetDelegate(std::move(test_hooks)); |
494 | 449 |
495 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( | 450 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( |
496 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 451 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
497 | 452 |
498 v8::Local<v8::Value> event = | 453 v8::Local<v8::Value> event = |
499 GetPropertyFromObject(api, context, "alphaEvent"); | 454 GetPropertyFromObject(api, context, "alphaEvent"); |
500 ASSERT_TRUE(event->IsObject()); | 455 ASSERT_TRUE(event->IsObject()); |
501 EXPECT_EQ( | 456 EXPECT_EQ( |
502 "\"alpha.alphaEvent\"", | 457 "\"alpha.alphaEvent\"", |
503 GetStringPropertyFromObject(event.As<v8::Object>(), context, "name")); | 458 GetStringPropertyFromObject(event.As<v8::Object>(), context, "name")); |
504 v8::Local<v8::Value> event2 = | 459 v8::Local<v8::Value> event2 = |
505 GetPropertyFromObject(api, context, "alphaEvent"); | 460 GetPropertyFromObject(api, context, "alphaEvent"); |
506 EXPECT_EQ(event, event2); | 461 EXPECT_EQ(event, event2); |
507 | 462 |
508 v8::Local<v8::Value> other_event = | 463 v8::Local<v8::Value> other_event = |
509 GetPropertyFromObject(api, context, "alphaOtherEvent"); | 464 GetPropertyFromObject(api, context, "alphaOtherEvent"); |
510 ASSERT_TRUE(other_event->IsObject()); | 465 ASSERT_TRUE(other_event->IsObject()); |
511 EXPECT_EQ("\"alpha.alphaOtherEvent\"", | 466 EXPECT_EQ("\"alpha.alphaOtherEvent\"", |
512 GetStringPropertyFromObject(other_event.As<v8::Object>(), context, | 467 GetStringPropertyFromObject(other_event.As<v8::Object>(), context, |
513 "name")); | 468 "name")); |
514 EXPECT_NE(event, other_event); | 469 EXPECT_NE(event, other_event); |
515 } | 470 } |
516 | 471 |
517 } // namespace extensions | 472 } // namespace extensions |
OLD | NEW |