| 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" |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 // builds. | 443 // builds. |
| 444 } | 444 } |
| 445 | 445 |
| 446 // Tests adding custom hooks for an API method. | 446 // Tests adding custom hooks for an API method. |
| 447 TEST_F(APIBindingUnittest, TestCustomHooks) { | 447 TEST_F(APIBindingUnittest, TestCustomHooks) { |
| 448 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 448 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 449 ASSERT_TRUE(functions); | 449 ASSERT_TRUE(functions); |
| 450 ArgumentSpec::RefMap refs; | 450 ArgumentSpec::RefMap refs; |
| 451 | 451 |
| 452 // Register a hook for the test.oneString method. | 452 // Register a hook for the test.oneString method. |
| 453 auto hooks = base::MakeUnique<APIBindingHooks>(); | 453 auto hooks = base::MakeUnique<APIBindingHooks>( |
| 454 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 454 bool did_call = false; | 455 bool did_call = false; |
| 455 auto hook = [](bool* did_call, const binding::APISignature* signature, | 456 auto hook = [](bool* did_call, const binding::APISignature* signature, |
| 456 gin::Arguments* arguments) { | 457 gin::Arguments* arguments) { |
| 457 *did_call = true; | 458 *did_call = true; |
| 458 EXPECT_EQ(1, arguments->Length()); | 459 EXPECT_EQ(1, arguments->Length()); |
| 459 std::string argument; | 460 std::string argument; |
| 460 EXPECT_TRUE(arguments->GetNext(&argument)); | 461 EXPECT_TRUE(arguments->GetNext(&argument)); |
| 461 EXPECT_EQ("foo", argument); | 462 EXPECT_EQ("foo", argument); |
| 462 }; | 463 }; |
| 463 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); | 464 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 481 v8::Local<v8::Function> func = | 482 v8::Local<v8::Function> func = |
| 482 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); | 483 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); |
| 483 v8::Local<v8::Value> args[] = {binding_object}; | 484 v8::Local<v8::Value> args[] = {binding_object}; |
| 484 RunFunction(func, context, 1, args); | 485 RunFunction(func, context, 1, args); |
| 485 EXPECT_TRUE(did_call); | 486 EXPECT_TRUE(did_call); |
| 486 | 487 |
| 487 // Other methods, like stringAndInt(), should behave normally. | 488 // Other methods, like stringAndInt(), should behave normally. |
| 488 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); | 489 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
| 489 } | 490 } |
| 490 | 491 |
| 492 TEST_F(APIBindingUnittest, TestJSCustomHook) { |
| 493 // Register a hook for the test.oneString method. |
| 494 auto hooks = base::MakeUnique<APIBindingHooks>( |
| 495 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 496 |
| 497 v8::HandleScope handle_scope(isolate()); |
| 498 v8::Local<v8::Context> context = ContextLocal(); |
| 499 const char kRegisterHook[] = |
| 500 "(function(hooks) {\n" |
| 501 " hooks.setHandleRequest('oneString', function() {\n" |
| 502 " this.requestArguments = Array.from(arguments);\n" |
| 503 " });\n" |
| 504 "})"; |
| 505 v8::Local<v8::String> source_string = |
| 506 gin::StringToV8(isolate(), kRegisterHook); |
| 507 hooks->RegisterJsSource(v8::Global<v8::String>(isolate(), source_string)); |
| 508 |
| 509 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 510 ASSERT_TRUE(functions); |
| 511 ArgumentSpec::RefMap refs; |
| 512 |
| 513 APIBinding binding( |
| 514 "test", *functions, nullptr, nullptr, |
| 515 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 516 std::move(hooks), &refs); |
| 517 EXPECT_TRUE(refs.empty()); |
| 518 |
| 519 APIEventHandler event_handler( |
| 520 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 521 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 522 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 523 |
| 524 // First try calling the oneString() method, which has a custom hook |
| 525 // installed. |
| 526 v8::Local<v8::Function> func = |
| 527 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); |
| 528 v8::Local<v8::Value> args[] = {binding_object}; |
| 529 RunFunction(func, context, 1, args); |
| 530 |
| 531 std::unique_ptr<base::Value> response_args = |
| 532 GetBaseValuePropertyFromObject(context->Global(), context, |
| 533 "requestArguments"); |
| 534 ASSERT_TRUE(response_args); |
| 535 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args)); |
| 536 |
| 537 // Other methods, like stringAndInt(), should behave normally. |
| 538 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
| 539 } |
| 540 |
| 491 } // namespace extensions | 541 } // namespace extensions |
| OLD | NEW |