| 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 945 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 v8::Local<v8::Value> args[] = {binding_object}; | 956 v8::Local<v8::Value> args[] = {binding_object}; |
| 957 v8::Local<v8::Value> result = | 957 v8::Local<v8::Value> result = |
| 958 RunFunction(function, context, arraysize(args), args); | 958 RunFunction(function, context, arraysize(args), args); |
| 959 ASSERT_FALSE(result.IsEmpty()); | 959 ASSERT_FALSE(result.IsEmpty()); |
| 960 std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context); | 960 std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context); |
| 961 ASSERT_TRUE(json_result); | 961 ASSERT_TRUE(json_result); |
| 962 EXPECT_EQ("\"ping pong\"", ValueToString(*json_result)); | 962 EXPECT_EQ("\"ping pong\"", ValueToString(*json_result)); |
| 963 } | 963 } |
| 964 } | 964 } |
| 965 | 965 |
| 966 // Tests the updateArgumentsPostValidate hook. |
| 967 TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidate) { |
| 968 // Register a hook for the test.oneString method. |
| 969 auto hooks = base::MakeUnique<APIBindingHooks>( |
| 970 base::Bind(&RunFunctionOnGlobalAndReturnHandle)); |
| 971 |
| 972 v8::HandleScope handle_scope(isolate()); |
| 973 v8::Local<v8::Context> context = ContextLocal(); |
| 974 const char kRegisterHook[] = |
| 975 "(function(hooks) {\n" |
| 976 " hooks.setUpdateArgumentsPostValidate('oneString', function() {\n" |
| 977 " this.requestArguments = Array.from(arguments);\n" |
| 978 " return ['pong'];\n" |
| 979 " });\n" |
| 980 "})"; |
| 981 v8::Local<v8::String> source_string = |
| 982 gin::StringToV8(isolate(), kRegisterHook); |
| 983 v8::Local<v8::String> source_name = |
| 984 gin::StringToV8(isolate(), "custom_hook"); |
| 985 hooks->RegisterJsSource( |
| 986 v8::Global<v8::String>(isolate(), source_string), |
| 987 v8::Global<v8::String>(isolate(), source_name)); |
| 988 |
| 989 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 990 ASSERT_TRUE(functions); |
| 991 ArgumentSpec::RefMap refs; |
| 992 |
| 993 APIBinding binding( |
| 994 "test", functions.get(), nullptr, nullptr, |
| 995 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 996 std::move(hooks), &refs); |
| 997 EXPECT_TRUE(refs.empty()); |
| 998 |
| 999 APIEventHandler event_handler( |
| 1000 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 1001 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 1002 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 1003 |
| 1004 // Try calling the method with an invalid signature. Since it's invalid, we |
| 1005 // should never enter the hook. |
| 1006 ExpectFailure(binding_object, "obj.oneString(false);", kError); |
| 1007 EXPECT_EQ("undefined", GetStringPropertyFromObject( |
| 1008 context->Global(), context, "requestArguments")); |
| 1009 |
| 1010 // Call the method with a valid signature. The hook should be entered and |
| 1011 // manipulate the arguments. |
| 1012 ExpectPass(binding_object, "obj.oneString('ping');", "['pong']"); |
| 1013 EXPECT_EQ("[\"ping\"]", GetStringPropertyFromObject( |
| 1014 context->Global(), context, "requestArguments")); |
| 1015 |
| 1016 // Other methods, like stringAndInt(), should behave normally. |
| 1017 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
| 1018 } |
| 1019 |
| 966 } // namespace extensions | 1020 } // namespace extensions |
| OLD | NEW |