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