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 907 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 v8::Context::Scope scope(context_b); | 918 v8::Context::Scope scope(context_b); |
919 v8::Local<v8::Boolean> fake_chrome = v8::Boolean::New(isolate(), true); | 919 v8::Local<v8::Boolean> fake_chrome = v8::Boolean::New(isolate(), true); |
920 context_b->Global() | 920 context_b->Global() |
921 ->Set(context_b, gin::StringToSymbol(isolate(), "chrome"), fake_chrome) | 921 ->Set(context_b, gin::StringToSymbol(isolate(), "chrome"), fake_chrome) |
922 .ToChecked(); | 922 .ToChecked(); |
923 } | 923 } |
924 // A non-object chrome shouldn't be used. | 924 // A non-object chrome shouldn't be used. |
925 check_runtime(false); | 925 check_runtime(false); |
926 } | 926 } |
927 | 927 |
| 928 // Tests updating a context's bindings after adding or removing permissions. |
| 929 TEST_F(NativeExtensionBindingsSystemUnittest, TestUpdatingPermissions) { |
| 930 scoped_refptr<Extension> extension = |
| 931 CreateExtension("extension", ItemType::EXTENSION, {"idle"}); |
| 932 RegisterExtension(extension->id()); |
| 933 |
| 934 v8::HandleScope handle_scope(isolate()); |
| 935 v8::Local<v8::Context> context = MainContext(); |
| 936 ScriptContext* script_context = CreateScriptContext( |
| 937 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 938 script_context->set_url(extension->url()); |
| 939 bindings_system()->UpdateBindingsForContext(script_context); |
| 940 |
| 941 // To start, chrome.idle should be available. |
| 942 v8::Local<v8::Value> initial_idle = |
| 943 V8ValueFromScriptSource(context, "chrome.idle"); |
| 944 ASSERT_FALSE(initial_idle.IsEmpty()); |
| 945 EXPECT_TRUE(initial_idle->IsObject()); |
| 946 |
| 947 { |
| 948 // chrome.power should not be defined. |
| 949 v8::Local<v8::Value> power = |
| 950 V8ValueFromScriptSource(context, "chrome.power"); |
| 951 ASSERT_FALSE(power.IsEmpty()); |
| 952 EXPECT_TRUE(power->IsUndefined()); |
| 953 } |
| 954 |
| 955 // Remove all permissions (`idle`). |
| 956 extension->permissions_data()->SetPermissions( |
| 957 base::MakeUnique<PermissionSet>(), base::MakeUnique<PermissionSet>()); |
| 958 |
| 959 bindings_system()->UpdateBindingsForContext(script_context); |
| 960 { |
| 961 // TODO(devlin): Neither the native nor JS bindings systems clear the |
| 962 // property on the chrome object when an API is no longer available. This |
| 963 // seems unexpected, but warrants further investigation before changing |
| 964 // behavior. It can be complicated by the fact that chrome.idle may not be |
| 965 // the same chrome.idle the system instantiated, or may have additional |
| 966 // properties. |
| 967 // v8::Local<v8::Value> idle = |
| 968 // V8ValueFromScriptSource(context, "chrome.idle"); |
| 969 // ASSERT_FALSE(idle.IsEmpty()); |
| 970 // EXPECT_TRUE(idle->IsUndefined()); |
| 971 |
| 972 // chrome.power should still be undefined. |
| 973 v8::Local<v8::Value> power = |
| 974 V8ValueFromScriptSource(context, "chrome.power"); |
| 975 ASSERT_FALSE(power.IsEmpty()); |
| 976 EXPECT_TRUE(power->IsUndefined()); |
| 977 } |
| 978 |
| 979 v8::Local<v8::Function> run_idle = FunctionFromString( |
| 980 context, "(function(idle) { idle.queryState(30, function() {}); })"); |
| 981 { |
| 982 // Trying to run a chrome.idle function should fail. |
| 983 v8::Local<v8::Value> args[] = {initial_idle}; |
| 984 RunFunctionAndExpectError( |
| 985 run_idle, context, arraysize(args), args, |
| 986 "Uncaught Error: 'idle.queryState' is not available in this context."); |
| 987 EXPECT_TRUE(last_params().name.empty()); |
| 988 } |
| 989 |
| 990 { |
| 991 // Add back the `idle` permission, and also add `power`. |
| 992 APIPermissionSet apis; |
| 993 apis.insert(APIPermission::kPower); |
| 994 apis.insert(APIPermission::kIdle); |
| 995 extension->permissions_data()->SetPermissions( |
| 996 base::MakeUnique<PermissionSet>(apis, ManifestPermissionSet(), |
| 997 URLPatternSet(), URLPatternSet()), |
| 998 base::MakeUnique<PermissionSet>()); |
| 999 bindings_system()->UpdateBindingsForContext(script_context); |
| 1000 } |
| 1001 |
| 1002 { |
| 1003 // Both chrome.idle and chrome.power should be defined. |
| 1004 v8::Local<v8::Value> idle = V8ValueFromScriptSource(context, "chrome.idle"); |
| 1005 ASSERT_FALSE(idle.IsEmpty()); |
| 1006 EXPECT_TRUE(idle->IsObject()); |
| 1007 |
| 1008 v8::Local<v8::Value> power = |
| 1009 V8ValueFromScriptSource(context, "chrome.power"); |
| 1010 ASSERT_FALSE(power.IsEmpty()); |
| 1011 EXPECT_TRUE(power->IsObject()); |
| 1012 } |
| 1013 |
| 1014 { |
| 1015 // Trying to run a chrome.idle function should now succeed. |
| 1016 v8::Local<v8::Value> args[] = {initial_idle}; |
| 1017 RunFunction(run_idle, context, arraysize(args), args); |
| 1018 EXPECT_EQ("idle.queryState", last_params().name); |
| 1019 } |
| 1020 } |
| 1021 |
928 } // namespace extensions | 1022 } // namespace extensions |
OLD | NEW |