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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 ASSERT_FALSE(last_request_); | 91 ASSERT_FALSE(last_request_); |
92 last_request_ = std::move(request); | 92 last_request_ = std::move(request); |
93 } | 93 } |
94 | 94 |
95 protected: | 95 protected: |
96 APIBindingsSystemTestBase() {} | 96 APIBindingsSystemTestBase() {} |
97 void SetUp() override { | 97 void SetUp() override { |
98 APIBindingTest::SetUp(); | 98 APIBindingTest::SetUp(); |
99 bindings_system_ = base::MakeUnique<APIBindingsSystem>( | 99 bindings_system_ = base::MakeUnique<APIBindingsSystem>( |
100 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 100 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 101 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
101 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, | 102 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, |
102 base::Unretained(this)), | 103 base::Unretained(this)), |
103 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, | 104 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, |
104 base::Unretained(this))); | 105 base::Unretained(this))); |
105 } | 106 } |
106 | 107 |
107 void TearDown() override { | 108 void TearDown() override { |
108 bindings_system_.reset(); | 109 bindings_system_.reset(); |
109 APIBindingTest::TearDown(); | 110 APIBindingTest::TearDown(); |
110 } | 111 } |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 reset_last_request(); | 292 reset_last_request(); |
292 } | 293 } |
293 } | 294 } |
294 | 295 |
295 // Tests adding a custom hook to an API. | 296 // Tests adding a custom hook to an API. |
296 TEST_F(APIBindingsSystemTest, TestCustomHooks) { | 297 TEST_F(APIBindingsSystemTest, TestCustomHooks) { |
297 v8::HandleScope handle_scope(isolate()); | 298 v8::HandleScope handle_scope(isolate()); |
298 v8::Local<v8::Context> context = ContextLocal(); | 299 v8::Local<v8::Context> context = ContextLocal(); |
299 | 300 |
300 bool did_call = false; | 301 bool did_call = false; |
301 auto hook = [](bool* did_call, const APISignature* signature, | 302 auto hook = [](bool* did_call, |
302 gin::Arguments* arguments, | 303 const APISignature* signature, v8::Local<v8::Context> context, |
| 304 std::vector<v8::Local<v8::Value>>* arguments, |
303 const ArgumentSpec::RefMap& type_refs) { | 305 const ArgumentSpec::RefMap& type_refs) { |
304 *did_call = true; | 306 *did_call = true; |
| 307 if (arguments->size() != 2) { // ASSERT* messes with the return type. |
| 308 EXPECT_EQ(2u, arguments->size()); |
| 309 return APIBindingHooks::RequestResult::HANDLED; |
| 310 } |
305 std::string argument; | 311 std::string argument; |
306 EXPECT_EQ(2, arguments->Length()); | 312 EXPECT_EQ("foo", gin::V8ToString(arguments->at(0))); |
307 EXPECT_TRUE(arguments->GetNext(&argument)); | 313 if (!arguments->at(1)->IsFunction()) { |
308 EXPECT_EQ("foo", argument); | 314 EXPECT_TRUE(arguments->at(1)->IsFunction()); |
309 v8::Local<v8::Function> 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; | 315 return APIBindingHooks::RequestResult::HANDLED; |
| 316 } |
315 v8::Local<v8::String> response = | 317 v8::Local<v8::String> response = |
316 gin::StringToV8(arguments->isolate(), "bar"); | 318 gin::StringToV8(context->GetIsolate(), "bar"); |
317 v8::Local<v8::Value> response_args[] = {response}; | 319 v8::Local<v8::Value> response_args[] = {response}; |
318 RunFunctionOnGlobal(function, arguments->isolate()->GetCurrentContext(), | 320 RunFunctionOnGlobal(arguments->at(1).As<v8::Function>(), |
319 1, response_args); | 321 context, 1, response_args); |
320 return APIBindingHooks::RequestResult::HANDLED; | 322 return APIBindingHooks::RequestResult::HANDLED; |
321 }; | 323 }; |
322 | 324 |
323 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); | 325 APIBindingHooks* hooks = bindings_system()->GetHooksForAPI(kAlphaAPIName); |
324 ASSERT_TRUE(hooks); | 326 ASSERT_TRUE(hooks); |
325 hooks->RegisterHandleRequest( | 327 hooks->RegisterHandleRequest( |
326 "alpha.functionWithCallback", | 328 "alpha.functionWithCallback", |
327 base::Bind(hook, &did_call)); | 329 base::Bind(hook, &did_call)); |
328 | 330 |
329 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 331 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 *ListValueFromString("['active']")); | 494 *ListValueFromString("['active']")); |
493 | 495 |
494 std::unique_ptr<base::Value> result = | 496 std::unique_ptr<base::Value> result = |
495 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); | 497 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
496 ASSERT_TRUE(result); | 498 ASSERT_TRUE(result); |
497 EXPECT_EQ("\"active\"", ValueToString(*result)); | 499 EXPECT_EQ("\"active\"", ValueToString(*result)); |
498 } | 500 } |
499 } | 501 } |
500 | 502 |
501 } // namespace extensions | 503 } // namespace extensions |
OLD | NEW |