| 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 998 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1009     v8::Local<v8::Value> args[] = {binding_object}; |  1009     v8::Local<v8::Value> args[] = {binding_object}; | 
|  1010     v8::Local<v8::Value> result = |  1010     v8::Local<v8::Value> result = | 
|  1011         RunFunction(function, context, arraysize(args), args); |  1011         RunFunction(function, context, arraysize(args), args); | 
|  1012     ASSERT_FALSE(result.IsEmpty()); |  1012     ASSERT_FALSE(result.IsEmpty()); | 
|  1013     std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context); |  1013     std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context); | 
|  1014     ASSERT_TRUE(json_result); |  1014     ASSERT_TRUE(json_result); | 
|  1015     EXPECT_EQ("\"ping pong\"", ValueToString(*json_result)); |  1015     EXPECT_EQ("\"ping pong\"", ValueToString(*json_result)); | 
|  1016   } |  1016   } | 
|  1017 } |  1017 } | 
|  1018  |  1018  | 
 |  1019 // Tests the updateArgumentsPostValidate hook. | 
 |  1020 TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidate) { | 
 |  1021   // Register a hook for the test.oneString method. | 
 |  1022   auto hooks = base::MakeUnique<APIBindingHooks>( | 
 |  1023       base::Bind(&RunFunctionOnGlobalAndReturnHandle)); | 
 |  1024  | 
 |  1025   v8::HandleScope handle_scope(isolate()); | 
 |  1026   v8::Local<v8::Context> context = ContextLocal(); | 
 |  1027   const char kRegisterHook[] = | 
 |  1028       "(function(hooks) {\n" | 
 |  1029       "  hooks.setUpdateArgumentsPostValidate('oneString', function() {\n" | 
 |  1030       "    this.requestArguments = Array.from(arguments);\n" | 
 |  1031       "    return ['pong'];\n" | 
 |  1032       "  });\n" | 
 |  1033       "})"; | 
 |  1034   v8::Local<v8::String> source_string = | 
 |  1035       gin::StringToV8(isolate(), kRegisterHook); | 
 |  1036   v8::Local<v8::String> source_name = | 
 |  1037       gin::StringToV8(isolate(), "custom_hook"); | 
 |  1038   hooks->RegisterJsSource( | 
 |  1039       v8::Global<v8::String>(isolate(), source_string), | 
 |  1040       v8::Global<v8::String>(isolate(), source_name)); | 
 |  1041  | 
 |  1042   std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 
 |  1043   ASSERT_TRUE(functions); | 
 |  1044   ArgumentSpec::RefMap refs; | 
 |  1045  | 
 |  1046   APIBinding binding( | 
 |  1047       "test", functions.get(), nullptr, nullptr, | 
 |  1048       base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 
 |  1049       std::move(hooks), &refs); | 
 |  1050   EXPECT_TRUE(refs.empty()); | 
 |  1051  | 
 |  1052   APIEventHandler event_handler( | 
 |  1053       base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 
 |  1054   v8::Local<v8::Object> binding_object = binding.CreateInstance( | 
 |  1055       context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 
 |  1056  | 
 |  1057   // Try calling the method with an invalid signature. Since it's invalid, we | 
 |  1058   // should never enter the hook. | 
 |  1059   ExpectFailure(binding_object, "obj.oneString(false);", kError); | 
 |  1060   EXPECT_EQ("undefined", GetStringPropertyFromObject( | 
 |  1061                              context->Global(), context, "requestArguments")); | 
 |  1062  | 
 |  1063   // Call the method with a valid signature. The hook should be entered and | 
 |  1064   // manipulate the arguments. | 
 |  1065   ExpectPass(binding_object, "obj.oneString('ping');", "['pong']", false); | 
 |  1066   EXPECT_EQ("[\"ping\"]", GetStringPropertyFromObject( | 
 |  1067                               context->Global(), context, "requestArguments")); | 
 |  1068  | 
 |  1069   // Other methods, like stringAndInt(), should behave normally. | 
 |  1070   ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", | 
 |  1071              "['foo',42]", false); | 
 |  1072 } | 
 |  1073  | 
|  1019 }  // namespace extensions |  1074 }  // namespace extensions | 
| OLD | NEW |