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 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
455 bool did_call = false; | 455 bool did_call = false; |
456 auto hook = [](bool* did_call, const APISignature* signature, | 456 auto hook = [](bool* did_call, const APISignature* signature, |
457 gin::Arguments* arguments) { | 457 gin::Arguments* arguments, |
| 458 const ArgumentSpec::RefMap& ref_map) { |
458 *did_call = true; | 459 *did_call = true; |
459 EXPECT_EQ(1, arguments->Length()); | 460 EXPECT_EQ(1, arguments->Length()); |
460 std::string argument; | 461 std::string argument; |
461 EXPECT_TRUE(arguments->GetNext(&argument)); | 462 EXPECT_TRUE(arguments->GetNext(&argument)); |
462 EXPECT_EQ("foo", argument); | 463 EXPECT_EQ("foo", argument); |
| 464 return APIBindingHooks::RequestResult::HANDLED; |
463 }; | 465 }; |
464 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); | 466 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); |
465 | 467 |
466 APIBinding binding( | 468 APIBinding binding( |
467 "test", *functions, nullptr, nullptr, | 469 "test", *functions, nullptr, nullptr, |
468 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 470 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
469 std::move(hooks), &refs); | 471 std::move(hooks), &refs); |
470 EXPECT_TRUE(refs.empty()); | 472 EXPECT_TRUE(refs.empty()); |
471 | 473 |
472 v8::HandleScope handle_scope(isolate()); | 474 v8::HandleScope handle_scope(isolate()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 "test", *functions, nullptr, nullptr, | 520 "test", *functions, nullptr, nullptr, |
519 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 521 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
520 std::move(hooks), &refs); | 522 std::move(hooks), &refs); |
521 EXPECT_TRUE(refs.empty()); | 523 EXPECT_TRUE(refs.empty()); |
522 | 524 |
523 APIEventHandler event_handler( | 525 APIEventHandler event_handler( |
524 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 526 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
525 v8::Local<v8::Object> binding_object = binding.CreateInstance( | 527 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
526 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 528 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
527 | 529 |
528 // First try calling the oneString() method, which has a custom hook | 530 // First try calling with an invalid invocation. An error should be raised and |
529 // installed. | 531 // the hook should never have been called, since the arguments didn't match. |
| 532 ExpectFailure(binding_object, "obj.oneString(1);", kError); |
| 533 v8::Local<v8::Value> property = |
| 534 GetPropertyFromObject(context->Global(), context, "requestArguments"); |
| 535 ASSERT_FALSE(property.IsEmpty()); |
| 536 EXPECT_TRUE(property->IsUndefined()); |
| 537 |
| 538 // Try calling the oneString() method with valid arguments. The hook should |
| 539 // be called. |
530 v8::Local<v8::Function> func = | 540 v8::Local<v8::Function> func = |
531 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); | 541 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); |
532 v8::Local<v8::Value> args[] = {binding_object}; | 542 v8::Local<v8::Value> args[] = {binding_object}; |
533 RunFunction(func, context, 1, args); | 543 RunFunction(func, context, 1, args); |
534 | 544 |
535 std::unique_ptr<base::Value> response_args = | 545 std::unique_ptr<base::Value> response_args = |
536 GetBaseValuePropertyFromObject(context->Global(), context, | 546 GetBaseValuePropertyFromObject(context->Global(), context, |
537 "requestArguments"); | 547 "requestArguments"); |
538 ASSERT_TRUE(response_args); | 548 ASSERT_TRUE(response_args); |
539 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args)); | 549 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args)); |
540 | 550 |
541 // Other methods, like stringAndInt(), should behave normally. | 551 // Other methods, like stringAndInt(), should behave normally. |
542 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); | 552 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
543 } | 553 } |
544 | 554 |
545 } // namespace extensions | 555 } // namespace extensions |
OLD | NEW |