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" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 APIBindingsSystemTestBase::SetUp(); | 203 APIBindingsSystemTestBase::SetUp(); |
204 } | 204 } |
205 | 205 |
206 // Tests API object initialization, calling a method on the supplied APIs, and | 206 // Tests API object initialization, calling a method on the supplied APIs, and |
207 // triggering the callback for the request. | 207 // triggering the callback for the request. |
208 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { | 208 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { |
209 v8::HandleScope handle_scope(isolate()); | 209 v8::HandleScope handle_scope(isolate()); |
210 v8::Local<v8::Context> context = ContextLocal(); | 210 v8::Local<v8::Context> context = ContextLocal(); |
211 | 211 |
212 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 212 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
213 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs)); | 213 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
214 ASSERT_FALSE(alpha_api.IsEmpty()); | 214 ASSERT_FALSE(alpha_api.IsEmpty()); |
215 v8::Local<v8::Object> beta_api = bindings_system()->CreateAPIInstance( | 215 v8::Local<v8::Object> beta_api = bindings_system()->CreateAPIInstance( |
216 kBetaAPIName, context, isolate(), base::Bind(&AllowAllAPIs)); | 216 kBetaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
217 ASSERT_FALSE(beta_api.IsEmpty()); | 217 ASSERT_FALSE(beta_api.IsEmpty()); |
218 | 218 |
219 { | 219 { |
220 // Test a simple call -> response. | 220 // Test a simple call -> response. |
221 const char kTestCall[] = | 221 const char kTestCall[] = |
222 "obj.functionWithCallback('foo', function() {\n" | 222 "obj.functionWithCallback('foo', function() {\n" |
223 " this.callbackArguments = Array.from(arguments);\n" | 223 " this.callbackArguments = Array.from(arguments);\n" |
224 "});"; | 224 "});"; |
225 CallFunctionOnObject(context, alpha_api, kTestCall); | 225 CallFunctionOnObject(context, alpha_api, kTestCall); |
226 | 226 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 1, response_args); | 314 1, response_args); |
315 }; | 315 }; |
316 | 316 |
317 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); | 317 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); |
318 ASSERT_TRUE(hooks); | 318 ASSERT_TRUE(hooks); |
319 hooks->RegisterHandleRequest( | 319 hooks->RegisterHandleRequest( |
320 "alpha.functionWithCallback", | 320 "alpha.functionWithCallback", |
321 base::Bind(hook, &did_call)); | 321 base::Bind(hook, &did_call)); |
322 | 322 |
323 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 323 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
324 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs)); | 324 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
325 ASSERT_FALSE(alpha_api.IsEmpty()); | 325 ASSERT_FALSE(alpha_api.IsEmpty()); |
326 | 326 |
327 { | 327 { |
328 // Test a simple call -> response. | 328 // Test a simple call -> response. |
329 const char kTestCall[] = | 329 const char kTestCall[] = |
330 "obj.functionWithCallback('foo', function() {\n" | 330 "obj.functionWithCallback('foo', function() {\n" |
331 " this.callbackArguments = Array.from(arguments);\n" | 331 " this.callbackArguments = Array.from(arguments);\n" |
332 "});"; | 332 "});"; |
333 CallFunctionOnObject(context, alpha_api, kTestCall); | 333 CallFunctionOnObject(context, alpha_api, kTestCall); |
334 EXPECT_TRUE(did_call); | 334 EXPECT_TRUE(did_call); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 { | 401 { |
402 v8::Maybe<bool> res = context->Global()->Set( | 402 v8::Maybe<bool> res = context->Global()->Set( |
403 context, gin::StringToV8(isolate(), "chrome"), chrome); | 403 context, gin::StringToV8(isolate(), "chrome"), chrome); |
404 ASSERT_TRUE(res.IsJust()); | 404 ASSERT_TRUE(res.IsJust()); |
405 ASSERT_TRUE(res.FromJust()); | 405 ASSERT_TRUE(res.FromJust()); |
406 } | 406 } |
407 | 407 |
408 auto add_api_to_chrome = [this, &chrome, | 408 auto add_api_to_chrome = [this, &chrome, |
409 &context](const std::string& api_name) { | 409 &context](const std::string& api_name) { |
410 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( | 410 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( |
411 api_name, context, context->GetIsolate(), base::Bind(&AllowAllAPIs)); | 411 api_name, context, context->GetIsolate(), base::Bind(&AllowAllAPIs), |
| 412 nullptr); |
412 ASSERT_FALSE(api.IsEmpty()) << api_name; | 413 ASSERT_FALSE(api.IsEmpty()) << api_name; |
413 v8::Maybe<bool> res = chrome->Set( | 414 v8::Maybe<bool> res = chrome->Set( |
414 context, gin::StringToV8(context->GetIsolate(), api_name), api); | 415 context, gin::StringToV8(context->GetIsolate(), api_name), api); |
415 ASSERT_TRUE(res.IsJust()); | 416 ASSERT_TRUE(res.IsJust()); |
416 ASSERT_TRUE(res.FromJust()); | 417 ASSERT_TRUE(res.FromJust()); |
417 }; | 418 }; |
418 | 419 |
419 // Pick two relatively small APIs that don't have any custom bindings (which | 420 // Pick two relatively small APIs that don't have any custom bindings (which |
420 // aren't supported yet). | 421 // aren't supported yet). |
421 add_api_to_chrome("idle"); | 422 add_api_to_chrome("idle"); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 *ListValueFromString("['active']")); | 486 *ListValueFromString("['active']")); |
486 | 487 |
487 std::unique_ptr<base::Value> result = | 488 std::unique_ptr<base::Value> result = |
488 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); | 489 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
489 ASSERT_TRUE(result); | 490 ASSERT_TRUE(result); |
490 EXPECT_EQ("\"active\"", ValueToString(*result)); | 491 EXPECT_EQ("\"active\"", ValueToString(*result)); |
491 } | 492 } |
492 } | 493 } |
493 | 494 |
494 } // namespace extensions | 495 } // namespace extensions |
OLD | NEW |