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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 // Tests adding a custom hook to an API. | 295 // Tests adding a custom hook to an API. |
296 TEST_F(APIBindingsSystemTest, TestCustomHooks) { | 296 TEST_F(APIBindingsSystemTest, TestCustomHooks) { |
297 v8::HandleScope handle_scope(isolate()); | 297 v8::HandleScope handle_scope(isolate()); |
298 v8::Local<v8::Context> context = ContextLocal(); | 298 v8::Local<v8::Context> context = ContextLocal(); |
299 | 299 |
300 bool did_call = false; | 300 bool did_call = false; |
301 auto hook = [](bool* did_call, const APISignature* signature, | 301 auto hook = [](bool* did_call, const APISignature* signature, |
302 gin::Arguments* arguments) { | 302 gin::Arguments* arguments, |
| 303 const ArgumentSpec::RefMap& type_refs) { |
303 *did_call = true; | 304 *did_call = true; |
304 std::string argument; | 305 std::string argument; |
305 EXPECT_EQ(2, arguments->Length()); | 306 EXPECT_EQ(2, arguments->Length()); |
306 EXPECT_TRUE(arguments->GetNext(&argument)); | 307 EXPECT_TRUE(arguments->GetNext(&argument)); |
307 EXPECT_EQ("foo", argument); | 308 EXPECT_EQ("foo", argument); |
308 v8::Local<v8::Function> function; | 309 v8::Local<v8::Function> function; |
309 ASSERT_TRUE(arguments->GetNext(&function)); | 310 EXPECT_TRUE(arguments->GetNext(&function)); |
| 311 // The above EXPECT_TRUE should really be an ASSERT, but that messes with |
| 312 // the return type. |
| 313 if (function.IsEmpty()) |
| 314 return APIBindingHooks::RequestResult::HANDLED; |
310 v8::Local<v8::String> response = | 315 v8::Local<v8::String> response = |
311 gin::StringToV8(arguments->isolate(), "bar"); | 316 gin::StringToV8(arguments->isolate(), "bar"); |
312 v8::Local<v8::Value> response_args[] = {response}; | 317 v8::Local<v8::Value> response_args[] = {response}; |
313 RunFunctionOnGlobal(function, arguments->isolate()->GetCurrentContext(), | 318 RunFunctionOnGlobal(function, arguments->isolate()->GetCurrentContext(), |
314 1, response_args); | 319 1, response_args); |
| 320 return APIBindingHooks::RequestResult::HANDLED; |
315 }; | 321 }; |
316 | 322 |
317 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); | 323 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); |
318 ASSERT_TRUE(hooks); | 324 ASSERT_TRUE(hooks); |
319 hooks->RegisterHandleRequest( | 325 hooks->RegisterHandleRequest( |
320 "alpha.functionWithCallback", | 326 "alpha.functionWithCallback", |
321 base::Bind(hook, &did_call)); | 327 base::Bind(hook, &did_call)); |
322 | 328 |
323 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 329 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
324 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 330 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 *ListValueFromString("['active']")); | 492 *ListValueFromString("['active']")); |
487 | 493 |
488 std::unique_ptr<base::Value> result = | 494 std::unique_ptr<base::Value> result = |
489 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); | 495 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
490 ASSERT_TRUE(result); | 496 ASSERT_TRUE(result); |
491 EXPECT_EQ("\"active\"", ValueToString(*result)); | 497 EXPECT_EQ("\"active\"", ValueToString(*result)); |
492 } | 498 } |
493 } | 499 } |
494 | 500 |
495 } // namespace extensions | 501 } // namespace extensions |
OLD | NEW |