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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
(...skipping 836 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 const char kGetUrl[] = "chrome.runtime.getURL"; | 847 const char kGetUrl[] = "chrome.runtime.getURL"; |
848 const char kOnMessage[] = "chrome.runtime.onMessage"; | 848 const char kOnMessage[] = "chrome.runtime.onMessage"; |
849 EXPECT_TRUE(property_exists(blessed_context, kSendMessage)); | 849 EXPECT_TRUE(property_exists(blessed_context, kSendMessage)); |
850 EXPECT_TRUE(property_exists(blessed_context, kGetUrl)); | 850 EXPECT_TRUE(property_exists(blessed_context, kGetUrl)); |
851 EXPECT_TRUE(property_exists(blessed_context, kOnMessage)); | 851 EXPECT_TRUE(property_exists(blessed_context, kOnMessage)); |
852 EXPECT_TRUE(property_exists(webpage_context, kSendMessage)); | 852 EXPECT_TRUE(property_exists(webpage_context, kSendMessage)); |
853 EXPECT_FALSE(property_exists(webpage_context, kGetUrl)); | 853 EXPECT_FALSE(property_exists(webpage_context, kGetUrl)); |
854 EXPECT_FALSE(property_exists(webpage_context, kOnMessage)); | 854 EXPECT_FALSE(property_exists(webpage_context, kOnMessage)); |
855 } | 855 } |
856 | 856 |
| 857 // Tests updating a context's bindings after adding or removing permissions. |
| 858 TEST_F(NativeExtensionBindingsSystemUnittest, TestUpdatingPermissions) { |
| 859 scoped_refptr<Extension> extension = |
| 860 CreateExtension("extension", ItemType::EXTENSION, {"idle"}); |
| 861 RegisterExtension(extension->id()); |
| 862 |
| 863 v8::HandleScope handle_scope(isolate()); |
| 864 v8::Local<v8::Context> context = MainContext(); |
| 865 ScriptContext* script_context = CreateScriptContext( |
| 866 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 867 script_context->set_url(extension->url()); |
| 868 bindings_system()->UpdateBindingsForContext(script_context); |
| 869 |
| 870 // To start, chrome.idle should be available. |
| 871 v8::Local<v8::Value> initial_idle = |
| 872 V8ValueFromScriptSource(context, "chrome.idle"); |
| 873 ASSERT_FALSE(initial_idle.IsEmpty()); |
| 874 EXPECT_TRUE(initial_idle->IsObject()); |
| 875 |
| 876 { |
| 877 // chrome.power should not be defined. |
| 878 v8::Local<v8::Value> power = |
| 879 V8ValueFromScriptSource(context, "chrome.power"); |
| 880 ASSERT_FALSE(power.IsEmpty()); |
| 881 EXPECT_TRUE(power->IsUndefined()); |
| 882 } |
| 883 |
| 884 // Remove all permissions (`idle`). |
| 885 extension->permissions_data()->SetPermissions( |
| 886 base::MakeUnique<PermissionSet>(), base::MakeUnique<PermissionSet>()); |
| 887 |
| 888 bindings_system()->UpdateBindingsForContext(script_context); |
| 889 { |
| 890 // TODO(devlin): Neither the native nor JS bindings systems clear the |
| 891 // property on the chrome object when an API is no longer available. This |
| 892 // seems unexpected, but warrants further investigation before changing |
| 893 // behavior. It can be complicated by the fact that chrome.idle may not be |
| 894 // the same chrome.idle the system instantiated, or may have additional |
| 895 // properties. |
| 896 // v8::Local<v8::Value> idle = |
| 897 // V8ValueFromScriptSource(context, "chrome.idle"); |
| 898 // ASSERT_FALSE(idle.IsEmpty()); |
| 899 // EXPECT_TRUE(idle->IsUndefined()); |
| 900 |
| 901 // chrome.power should still be undefined. |
| 902 v8::Local<v8::Value> power = |
| 903 V8ValueFromScriptSource(context, "chrome.power"); |
| 904 ASSERT_FALSE(power.IsEmpty()); |
| 905 EXPECT_TRUE(power->IsUndefined()); |
| 906 } |
| 907 |
| 908 v8::Local<v8::Function> run_idle = FunctionFromString( |
| 909 context, "(function(idle) { idle.queryState(30, function() {}); })"); |
| 910 { |
| 911 // Trying to run a chrome.idle function should fail. |
| 912 v8::Local<v8::Value> args[] = {initial_idle}; |
| 913 RunFunctionAndExpectError( |
| 914 run_idle, context, arraysize(args), args, |
| 915 "Uncaught Error: 'idle.queryState' is not available in this context."); |
| 916 EXPECT_TRUE(last_params().name.empty()); |
| 917 } |
| 918 |
| 919 { |
| 920 // Add back the `idle` permission, and also add `power`. |
| 921 APIPermissionSet apis; |
| 922 apis.insert(APIPermission::kPower); |
| 923 apis.insert(APIPermission::kIdle); |
| 924 extension->permissions_data()->SetPermissions( |
| 925 base::MakeUnique<PermissionSet>(apis, ManifestPermissionSet(), |
| 926 URLPatternSet(), URLPatternSet()), |
| 927 base::MakeUnique<PermissionSet>()); |
| 928 bindings_system()->UpdateBindingsForContext(script_context); |
| 929 } |
| 930 |
| 931 { |
| 932 // Both chrome.idle and chrome.power should be defined. |
| 933 v8::Local<v8::Value> idle = V8ValueFromScriptSource(context, "chrome.idle"); |
| 934 ASSERT_FALSE(idle.IsEmpty()); |
| 935 EXPECT_TRUE(idle->IsObject()); |
| 936 |
| 937 v8::Local<v8::Value> power = |
| 938 V8ValueFromScriptSource(context, "chrome.power"); |
| 939 ASSERT_FALSE(power.IsEmpty()); |
| 940 EXPECT_TRUE(power->IsObject()); |
| 941 } |
| 942 |
| 943 { |
| 944 // Trying to run a chrome.idle function should now succeed. |
| 945 v8::Local<v8::Value> args[] = {initial_idle}; |
| 946 RunFunction(run_idle, context, arraysize(args), args); |
| 947 EXPECT_EQ("idle.queryState", last_params().name); |
| 948 } |
| 949 } |
| 950 |
857 } // namespace extensions | 951 } // namespace extensions |
OLD | NEW |