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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "extensions/renderer/api_binding.h" | 10 #include "extensions/renderer/api_binding.h" |
| 11 #include "extensions/renderer/api_binding_hooks.h" | |
| 11 #include "extensions/renderer/api_binding_test.h" | 12 #include "extensions/renderer/api_binding_test.h" |
| 12 #include "extensions/renderer/api_binding_test_util.h" | 13 #include "extensions/renderer/api_binding_test_util.h" |
| 13 #include "extensions/renderer/api_event_handler.h" | 14 #include "extensions/renderer/api_event_handler.h" |
| 14 #include "extensions/renderer/api_request_handler.h" | 15 #include "extensions/renderer/api_request_handler.h" |
| 16 #include "gin/arguments.h" | |
| 15 #include "gin/converter.h" | 17 #include "gin/converter.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 18 | 20 |
| 19 namespace extensions { | 21 namespace extensions { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // Function spec; we use single quotes for readability and then replace them. | 25 // Function spec; we use single quotes for readability and then replace them. |
| 24 const char kFunctions[] = | 26 const char kFunctions[] = |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 arguments_.reset(); | 189 arguments_.reset(); |
| 188 } | 190 } |
| 189 | 191 |
| 190 TEST_F(APIBindingUnittest, Test) { | 192 TEST_F(APIBindingUnittest, Test) { |
| 191 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 193 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 192 ASSERT_TRUE(functions); | 194 ASSERT_TRUE(functions); |
| 193 ArgumentSpec::RefMap refs; | 195 ArgumentSpec::RefMap refs; |
| 194 APIBinding binding( | 196 APIBinding binding( |
| 195 "test", *functions, nullptr, nullptr, | 197 "test", *functions, nullptr, nullptr, |
| 196 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 198 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 197 &refs); | 199 base::MakeUnique<APIBindingHooks>(), &refs); |
|
jbroman
2016/12/09 15:53:08
nit: might as well use nullptr here, since the cal
Devlin
2016/12/09 16:35:07
Done.
| |
| 198 EXPECT_TRUE(refs.empty()); | 200 EXPECT_TRUE(refs.empty()); |
| 199 | 201 |
| 200 v8::HandleScope handle_scope(isolate()); | 202 v8::HandleScope handle_scope(isolate()); |
| 201 v8::Local<v8::Context> context = ContextLocal(); | 203 v8::Local<v8::Context> context = ContextLocal(); |
| 202 | 204 |
| 203 APIEventHandler event_handler( | 205 APIEventHandler event_handler( |
| 204 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 206 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 205 v8::Local<v8::Object> binding_object = binding.CreateInstance( | 207 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 206 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 208 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 207 | 209 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 | 290 |
| 289 std::unique_ptr<base::ListValue> functions = | 291 std::unique_ptr<base::ListValue> functions = |
| 290 ListValueFromString(kRefFunctions); | 292 ListValueFromString(kRefFunctions); |
| 291 ASSERT_TRUE(functions); | 293 ASSERT_TRUE(functions); |
| 292 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); | 294 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); |
| 293 ASSERT_TRUE(types); | 295 ASSERT_TRUE(types); |
| 294 ArgumentSpec::RefMap refs; | 296 ArgumentSpec::RefMap refs; |
| 295 APIBinding binding( | 297 APIBinding binding( |
| 296 "test", *functions, types.get(), nullptr, | 298 "test", *functions, types.get(), nullptr, |
| 297 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 299 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 298 &refs); | 300 base::MakeUnique<APIBindingHooks>(), &refs); |
| 299 EXPECT_EQ(2u, refs.size()); | 301 EXPECT_EQ(2u, refs.size()); |
| 300 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); | 302 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); |
| 301 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); | 303 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); |
| 302 | 304 |
| 303 v8::HandleScope handle_scope(isolate()); | 305 v8::HandleScope handle_scope(isolate()); |
| 304 v8::Local<v8::Context> context = ContextLocal(); | 306 v8::Local<v8::Context> context = ContextLocal(); |
| 305 | 307 |
| 306 APIEventHandler event_handler( | 308 APIEventHandler event_handler( |
| 307 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 309 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 308 v8::Local<v8::Object> binding_object = binding.CreateInstance( | 310 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 334 " 'name': 'restrictedTwo'," | 336 " 'name': 'restrictedTwo'," |
| 335 " 'parameters': []" | 337 " 'parameters': []" |
| 336 "}]"; | 338 "}]"; |
| 337 std::unique_ptr<base::ListValue> functions = | 339 std::unique_ptr<base::ListValue> functions = |
| 338 ListValueFromString(kRestrictedFunctions); | 340 ListValueFromString(kRestrictedFunctions); |
| 339 ASSERT_TRUE(functions); | 341 ASSERT_TRUE(functions); |
| 340 ArgumentSpec::RefMap refs; | 342 ArgumentSpec::RefMap refs; |
| 341 APIBinding binding( | 343 APIBinding binding( |
| 342 "test", *functions, nullptr, nullptr, | 344 "test", *functions, nullptr, nullptr, |
| 343 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 345 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 344 &refs); | 346 base::MakeUnique<APIBindingHooks>(), &refs); |
| 345 | 347 |
| 346 v8::HandleScope handle_scope(isolate()); | 348 v8::HandleScope handle_scope(isolate()); |
| 347 v8::Local<v8::Context> context = ContextLocal(); | 349 v8::Local<v8::Context> context = ContextLocal(); |
| 348 | 350 |
| 349 auto is_available = [](const std::string& name) { | 351 auto is_available = [](const std::string& name) { |
| 350 std::set<std::string> functions = {"test.allowedOne", "test.allowedTwo", | 352 std::set<std::string> functions = {"test.allowedOne", "test.allowedTwo", |
| 351 "test.restrictedOne", | 353 "test.restrictedOne", |
| 352 "test.restrictedTwo"}; | 354 "test.restrictedTwo"}; |
| 353 EXPECT_TRUE(functions.count(name)); | 355 EXPECT_TRUE(functions.count(name)); |
| 354 return name == "test.allowedOne" || name == "test.allowedTwo"; | 356 return name == "test.allowedOne" || name == "test.allowedTwo"; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 377 TEST_F(APIBindingUnittest, TestEventCreation) { | 379 TEST_F(APIBindingUnittest, TestEventCreation) { |
| 378 const char kEvents[] = "[{'name': 'onFoo'}, {'name': 'onBar'}]"; | 380 const char kEvents[] = "[{'name': 'onFoo'}, {'name': 'onBar'}]"; |
| 379 std::unique_ptr<base::ListValue> events = ListValueFromString(kEvents); | 381 std::unique_ptr<base::ListValue> events = ListValueFromString(kEvents); |
| 380 ASSERT_TRUE(events); | 382 ASSERT_TRUE(events); |
| 381 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 383 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 382 ASSERT_TRUE(functions); | 384 ASSERT_TRUE(functions); |
| 383 ArgumentSpec::RefMap refs; | 385 ArgumentSpec::RefMap refs; |
| 384 APIBinding binding( | 386 APIBinding binding( |
| 385 "test", *functions, nullptr, events.get(), | 387 "test", *functions, nullptr, events.get(), |
| 386 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 388 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 387 &refs); | 389 base::MakeUnique<APIBindingHooks>(), &refs); |
| 388 | 390 |
| 389 v8::HandleScope handle_scope(isolate()); | 391 v8::HandleScope handle_scope(isolate()); |
| 390 v8::Local<v8::Context> context = ContextLocal(); | 392 v8::Local<v8::Context> context = ContextLocal(); |
| 391 | 393 |
| 392 APIEventHandler event_handler( | 394 APIEventHandler event_handler( |
| 393 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 395 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 394 v8::Local<v8::Object> binding_object = binding.CreateInstance( | 396 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 395 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 397 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 396 | 398 |
| 397 // Event behavior is tested in the APIEventHandler unittests as well as the | 399 // Event behavior is tested in the APIEventHandler unittests as well as the |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 413 EXPECT_FALSE(has_on_baz.FromJust()); | 415 EXPECT_FALSE(has_on_baz.FromJust()); |
| 414 } | 416 } |
| 415 | 417 |
| 416 TEST_F(APIBindingUnittest, TestDisposedContext) { | 418 TEST_F(APIBindingUnittest, TestDisposedContext) { |
| 417 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 419 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
| 418 ASSERT_TRUE(functions); | 420 ASSERT_TRUE(functions); |
| 419 ArgumentSpec::RefMap refs; | 421 ArgumentSpec::RefMap refs; |
| 420 APIBinding binding( | 422 APIBinding binding( |
| 421 "test", *functions, nullptr, nullptr, | 423 "test", *functions, nullptr, nullptr, |
| 422 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 424 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 423 &refs); | 425 base::MakeUnique<APIBindingHooks>(), &refs); |
| 424 EXPECT_TRUE(refs.empty()); | 426 EXPECT_TRUE(refs.empty()); |
| 425 | 427 |
| 426 v8::HandleScope handle_scope(isolate()); | 428 v8::HandleScope handle_scope(isolate()); |
| 427 v8::Local<v8::Context> context = ContextLocal(); | 429 v8::Local<v8::Context> context = ContextLocal(); |
| 428 | 430 |
| 429 APIEventHandler event_handler( | 431 APIEventHandler event_handler( |
| 430 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 432 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 431 v8::Local<v8::Object> binding_object = binding.CreateInstance( | 433 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 432 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 434 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 433 | 435 |
| 434 v8::Local<v8::Function> func = | 436 v8::Local<v8::Function> func = |
| 435 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); | 437 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); |
| 436 v8::Local<v8::Value> argv[] = {binding_object}; | 438 v8::Local<v8::Value> argv[] = {binding_object}; |
| 437 DisposeContext(); | 439 DisposeContext(); |
| 438 RunFunction(func, context, arraysize(argv), argv); | 440 RunFunction(func, context, arraysize(argv), argv); |
| 439 EXPECT_FALSE(HandlerWasInvoked()); | 441 EXPECT_FALSE(HandlerWasInvoked()); |
| 440 // This test passes if this does not crash, even under AddressSanitizer | 442 // This test passes if this does not crash, even under AddressSanitizer |
| 441 // builds. | 443 // builds. |
| 442 } | 444 } |
| 443 | 445 |
| 446 // Tests adding custom hooks for an API method. | |
| 447 TEST_F(APIBindingUnittest, TestCustomHooks) { | |
| 448 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | |
| 449 ASSERT_TRUE(functions); | |
| 450 ArgumentSpec::RefMap refs; | |
| 451 | |
| 452 // Register a hook for the test.oneString method. | |
| 453 auto hooks = base::MakeUnique<APIBindingHooks>(); | |
| 454 bool did_call = false; | |
| 455 auto hook = [](bool* did_call, const binding::APISignature* signature, | |
| 456 gin::Arguments* arguments) { | |
| 457 *did_call = true; | |
| 458 EXPECT_EQ(1, arguments->Length()); | |
| 459 std::string argument; | |
| 460 EXPECT_TRUE(arguments->GetNext(&argument)); | |
| 461 EXPECT_EQ("foo", argument); | |
| 462 }; | |
| 463 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); | |
| 464 | |
| 465 APIBinding binding( | |
| 466 "test", *functions, nullptr, nullptr, | |
| 467 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | |
| 468 std::move(hooks), &refs); | |
| 469 EXPECT_TRUE(refs.empty()); | |
| 470 | |
| 471 v8::HandleScope handle_scope(isolate()); | |
| 472 v8::Local<v8::Context> context = ContextLocal(); | |
| 473 | |
| 474 APIEventHandler event_handler( | |
| 475 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | |
| 476 v8::Local<v8::Object> binding_object = binding.CreateInstance( | |
| 477 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | |
| 478 | |
| 479 // First try calling the oneString() method, which has a custom hook | |
| 480 // installed. | |
| 481 v8::Local<v8::Function> func = | |
| 482 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); | |
| 483 v8::Local<v8::Value> args[] = {binding_object}; | |
| 484 RunFunction(func, context, 1, args); | |
| 485 EXPECT_TRUE(did_call); | |
| 486 | |
| 487 // Other methods, like stringAndInt(), should behave normally. | |
| 488 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); | |
| 489 } | |
| 490 | |
| 444 } // namespace extensions | 491 } // namespace extensions |
| OLD | NEW |