Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: extensions/renderer/bindings/api_binding_unittest.cc

Issue 2950353004: [Extensions Bindings] Check availability of custom API properties (Closed)
Patch Set: . Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/bindings/api_binding.h"
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
7 #include "base/stl_util.h" 9 #include "base/stl_util.h"
8 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
9 #include "base/values.h" 11 #include "base/values.h"
10 #include "extensions/renderer/bindings/api_binding.h"
11 #include "extensions/renderer/bindings/api_binding_hooks.h" 12 #include "extensions/renderer/bindings/api_binding_hooks.h"
12 #include "extensions/renderer/bindings/api_binding_hooks_test_delegate.h" 13 #include "extensions/renderer/bindings/api_binding_hooks_test_delegate.h"
13 #include "extensions/renderer/bindings/api_binding_test.h" 14 #include "extensions/renderer/bindings/api_binding_test.h"
14 #include "extensions/renderer/bindings/api_binding_test_util.h" 15 #include "extensions/renderer/bindings/api_binding_test_util.h"
15 #include "extensions/renderer/bindings/api_event_handler.h" 16 #include "extensions/renderer/bindings/api_event_handler.h"
16 #include "extensions/renderer/bindings/api_invocation_errors.h" 17 #include "extensions/renderer/bindings/api_invocation_errors.h"
17 #include "extensions/renderer/bindings/api_request_handler.h" 18 #include "extensions/renderer/bindings/api_request_handler.h"
18 #include "extensions/renderer/bindings/api_type_reference_map.h" 19 #include "extensions/renderer/bindings/api_type_reference_map.h"
19 #include "extensions/renderer/bindings/binding_access_checker.h" 20 #include "extensions/renderer/bindings/binding_access_checker.h"
20 #include "gin/arguments.h" 21 #include "gin/arguments.h"
21 #include "gin/converter.h" 22 #include "gin/converter.h"
23 #include "gin/data_object_builder.h"
22 #include "gin/public/context_holder.h" 24 #include "gin/public/context_holder.h"
23 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" 26 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
25 #include "v8/include/v8.h" 27 #include "v8/include/v8.h"
26 28
27 namespace extensions { 29 namespace extensions {
28 30
29 using namespace api_errors; 31 using namespace api_errors;
30 32
31 namespace { 33 namespace {
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 TEST_F(APIBindingUnittest, TestRefProperties) { 624 TEST_F(APIBindingUnittest, TestRefProperties) {
623 SetProperties( 625 SetProperties(
624 "{" 626 "{"
625 " 'alpha': {" 627 " 'alpha': {"
626 " '$ref': 'AlphaRef'," 628 " '$ref': 'AlphaRef',"
627 " 'value': ['a']" 629 " 'value': ['a']"
628 " }," 630 " },"
629 " 'beta': {" 631 " 'beta': {"
630 " '$ref': 'BetaRef'," 632 " '$ref': 'BetaRef',"
631 " 'value': ['b']" 633 " 'value': ['b']"
634 " },"
635 " 'restricted': {"
636 " '$ref': 'RestrictedRef',"
637 " 'value': ['r']"
632 " }" 638 " }"
633 "}"); 639 "}");
634 auto create_custom_type = [](v8::Isolate* isolate, 640 auto create_custom_type = [](v8::Isolate* isolate,
635 const std::string& type_name, 641 const std::string& type_name,
636 const std::string& property_name, 642 const std::string& property_name,
637 const base::ListValue* property_values) { 643 const base::ListValue* property_values) {
638 v8::Local<v8::Context> context = isolate->GetCurrentContext(); 644 v8::Local<v8::Context> context = isolate->GetCurrentContext();
639 v8::Local<v8::Object> result = v8::Object::New(isolate); 645 v8::Local<v8::Object> result = v8::Object::New(isolate);
640 if (type_name == "AlphaRef") { 646 if (type_name == "AlphaRef") {
641 EXPECT_EQ("alpha", property_name); 647 EXPECT_EQ("alpha", property_name);
(...skipping 10 matching lines...) Expand all
652 gin::StringToV8(isolate, "betaVal")) 658 gin::StringToV8(isolate, "betaVal"))
653 .ToChecked(); 659 .ToChecked();
654 } else { 660 } else {
655 EXPECT_TRUE(false) << type_name; 661 EXPECT_TRUE(false) << type_name;
656 } 662 }
657 return result; 663 return result;
658 }; 664 };
659 665
660 SetCreateCustomType(base::Bind(create_custom_type)); 666 SetCreateCustomType(base::Bind(create_custom_type));
661 667
668 auto is_available = [](v8::Local<v8::Context> context,
669 const std::string& name) {
670 return name != "test.restricted";
671 };
672 SetAvailabilityCallback(base::Bind(is_available));
673
662 InitializeBinding(); 674 InitializeBinding();
663 675
664 v8::HandleScope handle_scope(isolate()); 676 v8::HandleScope handle_scope(isolate());
665 v8::Local<v8::Context> context = MainContext(); 677 v8::Local<v8::Context> context = MainContext();
666 v8::Local<v8::Object> binding_object = binding()->CreateInstance(context); 678 v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
667 EXPECT_EQ(R"({"alphaProp":"alphaVal"})", 679 EXPECT_EQ(R"({"alphaProp":"alphaVal"})",
668 GetStringPropertyFromObject(binding_object, context, "alpha")); 680 GetStringPropertyFromObject(binding_object, context, "alpha"));
669 EXPECT_EQ( 681 EXPECT_EQ(
670 R"({"betaProp":"betaVal"})", 682 R"({"betaProp":"betaVal"})",
671 GetStringPropertyFromObject(binding_object, context, "beta")); 683 GetStringPropertyFromObject(binding_object, context, "beta"));
684 EXPECT_EQ("undefined",
685 GetStringPropertyFromObject(binding_object, context, "restricted"));
686 }
687
688 // Test that created subproperties are only exposed if they are available to
689 // the context.
690 TEST_F(APIBindingUnittest, RestrictedReferenceProperties) {
691 SetProperties(
692 "{"
693 " 'alpha': {"
694 " 'type': 'object',"
695 " 'properties': {"
696 " 'allowed': {"
697 " '$ref': 'PropType',"
698 " 'value': []"
699 " },"
700 " 'disallowed': {"
701 " '$ref': 'PropType',"
702 " 'value': []"
703 " }"
704 " }"
705 " }"
706 "}");
707
708 auto create_custom_type = [](v8::Isolate* isolate,
709 const std::string& type_name,
710 const std::string& property_name,
711 const base::ListValue* property_values) {
712 EXPECT_EQ("allowed", property_name);
713 return gin::DataObjectBuilder(isolate)
714 .Set("prop", std::string("val"))
715 .Build();
716 };
717 SetCreateCustomType(base::Bind(create_custom_type));
718
719 auto is_available = [](v8::Local<v8::Context> context,
720 const std::string& name) {
721 if (name == "test.alpha.allowed")
722 return true;
723 if (name == "test.alpha.disallowed")
724 return false;
725 ADD_FAILURE() << name;
726 return false;
727 };
728 SetAvailabilityCallback(base::Bind(is_available));
729
730 InitializeBinding();
731
732 v8::HandleScope handle_scope(isolate());
733 v8::Local<v8::Context> context = MainContext();
734 v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
735 v8::Local<v8::Value> alpha =
736 GetPropertyFromObject(binding_object, context, "alpha");
737 ASSERT_FALSE(alpha.IsEmpty());
738 ASSERT_TRUE(alpha->IsObject());
739 v8::Local<v8::Object> alpha_obj = alpha.As<v8::Object>();
740 EXPECT_EQ(R"({"prop":"val"})",
741 GetStringPropertyFromObject(alpha_obj, context, "allowed"));
742 EXPECT_EQ("undefined",
743 GetStringPropertyFromObject(alpha_obj, context, "disallowed"));
672 } 744 }
673 745
674 TEST_F(APIBindingUnittest, TestDisposedContext) { 746 TEST_F(APIBindingUnittest, TestDisposedContext) {
675 SetFunctions(kFunctions); 747 SetFunctions(kFunctions);
676 InitializeBinding(); 748 InitializeBinding();
677 749
678 v8::HandleScope handle_scope(isolate()); 750 v8::HandleScope handle_scope(isolate());
679 v8::Local<v8::Context> context = MainContext(); 751 v8::Local<v8::Context> context = MainContext();
680 752
681 v8::Local<v8::Object> binding_object = binding()->CreateInstance(context); 753 v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after
1273 1345
1274 // The extra property should be present on the binding object. 1346 // The extra property should be present on the binding object.
1275 EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context, 1347 EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context,
1276 "hookedProperty")); 1348 "hookedProperty"));
1277 // Sanity check: other values should still be there. 1349 // Sanity check: other values should still be there.
1278 EXPECT_EQ("function", 1350 EXPECT_EQ("function",
1279 GetStringPropertyFromObject(binding_object, context, "oneString")); 1351 GetStringPropertyFromObject(binding_object, context, "oneString"));
1280 } 1352 }
1281 1353
1282 } // namespace extensions 1354 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698