Chromium Code Reviews| 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/extension_api.h" | 13 #include "extensions/common/extension_api.h" |
| 14 #include "extensions/renderer/api_binding.h" | 14 #include "extensions/renderer/api_binding.h" |
| 15 #include "extensions/renderer/api_binding_hooks.h" | |
| 15 #include "extensions/renderer/api_binding_test.h" | 16 #include "extensions/renderer/api_binding_test.h" |
| 16 #include "extensions/renderer/api_binding_test_util.h" | 17 #include "extensions/renderer/api_binding_test_util.h" |
| 18 #include "extensions/renderer/api_binding_types.h" | |
| 19 #include "gin/arguments.h" | |
| 17 #include "gin/converter.h" | 20 #include "gin/converter.h" |
| 18 #include "gin/try_catch.h" | 21 #include "gin/try_catch.h" |
| 19 | 22 |
| 20 namespace extensions { | 23 namespace extensions { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 // Fake API for testing. | 27 // Fake API for testing. |
| 25 const char kAlphaAPIName[] = "alpha"; | 28 const char kAlphaAPIName[] = "alpha"; |
| 26 const char kAlphaAPISpec[] = | 29 const char kAlphaAPISpec[] = |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 { | 285 { |
| 283 // Test a call -> response on the second API. | 286 // Test a call -> response on the second API. |
| 284 const char kTestCall[] = "obj.simpleFunc(2)"; | 287 const char kTestCall[] = "obj.simpleFunc(2)"; |
| 285 CallFunctionOnObject(context, beta_api, kTestCall); | 288 CallFunctionOnObject(context, beta_api, kTestCall); |
| 286 ValidateLastRequest("beta.simpleFunc", "[2]"); | 289 ValidateLastRequest("beta.simpleFunc", "[2]"); |
| 287 EXPECT_EQ(-1, last_request()->request_id); | 290 EXPECT_EQ(-1, last_request()->request_id); |
| 288 reset_last_request(); | 291 reset_last_request(); |
| 289 } | 292 } |
| 290 } | 293 } |
| 291 | 294 |
| 295 // Tests adding a custom hook to an API. | |
| 296 TEST_F(APIBindingsSystemTest, TestCustomHooks) { | |
| 297 v8::HandleScope handle_scope(isolate()); | |
| 298 v8::Local<v8::Context> context = ContextLocal(); | |
| 299 | |
| 300 bool did_call = false; | |
| 301 auto hook = [](bool* did_call, const binding::APISignature* signature, | |
| 302 gin::Arguments* arguments) { | |
| 303 *did_call = true; | |
| 304 std::string argument; | |
| 305 EXPECT_EQ(2, arguments->Length()); | |
| 306 EXPECT_TRUE(arguments->GetNext(&argument)); | |
| 307 EXPECT_EQ("foo", argument); | |
| 308 v8::Local<v8::Function> function; | |
| 309 ASSERT_TRUE(arguments->GetNext(&function)); | |
| 310 v8::Local<v8::String> response = | |
| 311 gin::StringToV8(arguments->isolate(), "bar"); | |
| 312 v8::Local<v8::Value> response_args[] = {response}; | |
| 313 RunFunctionOnGlobal(function, arguments->isolate()->GetCurrentContext(), | |
| 314 1, response_args); | |
| 315 }; | |
| 316 | |
| 317 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); | |
|
lazyboy
2016/12/13 01:52:32
I'm finding this pattern to add a hook harder to r
Devlin
2016/12/13 18:41:30
Hmm... depending on how complex the "add hooks" me
lazyboy
2016/12/13 18:58:25
Keeping this as is + TODO SG.
Calling APIBindingHo
Devlin
2016/12/13 19:05:39
Yeah, that's reasonable. Added a DCHECK.
| |
| 318 ASSERT_TRUE(hooks); | |
| 319 hooks->RegisterHandleRequest( | |
| 320 "alpha.functionWithCallback", | |
| 321 base::Bind(hook, &did_call)); | |
| 322 | |
| 323 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | |
| 324 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs)); | |
| 325 ASSERT_FALSE(alpha_api.IsEmpty()); | |
| 326 | |
| 327 { | |
| 328 // Test a simple call -> response. | |
| 329 const char kTestCall[] = | |
| 330 "obj.functionWithCallback('foo', function() {\n" | |
| 331 " this.callbackArguments = Array.from(arguments);\n" | |
| 332 "});"; | |
| 333 CallFunctionOnObject(context, alpha_api, kTestCall); | |
| 334 EXPECT_TRUE(did_call); | |
| 335 | |
| 336 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | |
| 337 context->Global(), context, "callbackArguments"); | |
| 338 ASSERT_TRUE(result); | |
| 339 EXPECT_EQ("[\"bar\"]", ValueToString(*result)); | |
| 340 } | |
| 341 } | |
| 342 | |
| 292 // An implementation using real API schemas. | 343 // An implementation using real API schemas. |
| 293 class APIBindingsSystemTestWithRealAPI : public APIBindingsSystemTestBase { | 344 class APIBindingsSystemTestWithRealAPI : public APIBindingsSystemTestBase { |
| 294 protected: | 345 protected: |
| 295 APIBindingsSystemTestWithRealAPI() {} | 346 APIBindingsSystemTestWithRealAPI() {} |
| 296 | 347 |
| 297 // Executes the given |script_source| in |context|, expecting no exceptions. | 348 // Executes the given |script_source| in |context|, expecting no exceptions. |
| 298 void ExecuteScript(v8::Local<v8::Context> context, | 349 void ExecuteScript(v8::Local<v8::Context> context, |
| 299 const std::string& script_source); | 350 const std::string& script_source); |
| 300 | 351 |
| 301 // Executes the given |script_source| in |context| and compares a caught | 352 // Executes the given |script_source| in |context| and compares a caught |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 *ListValueFromString("['active']")); | 485 *ListValueFromString("['active']")); |
| 435 | 486 |
| 436 std::unique_ptr<base::Value> result = | 487 std::unique_ptr<base::Value> result = |
| 437 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); | 488 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
| 438 ASSERT_TRUE(result); | 489 ASSERT_TRUE(result); |
| 439 EXPECT_EQ("\"active\"", ValueToString(*result)); | 490 EXPECT_EQ("\"active\"", ValueToString(*result)); |
| 440 } | 491 } |
| 441 } | 492 } |
| 442 | 493 |
| 443 } // namespace extensions | 494 } // namespace extensions |
| OLD | NEW |