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 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 EXPECT_FALSE(value_a.IsEmpty()) << property; | 786 EXPECT_FALSE(value_a.IsEmpty()) << property; |
787 EXPECT_FALSE(value_b.IsEmpty()) << property; | 787 EXPECT_FALSE(value_b.IsEmpty()) << property; |
788 EXPECT_NE(value_a, value_b) << property; | 788 EXPECT_NE(value_a, value_b) << property; |
789 }; | 789 }; |
790 | 790 |
791 check_properties_inequal(context_a, context_b, "chrome"); | 791 check_properties_inequal(context_a, context_b, "chrome"); |
792 check_properties_inequal(context_a, context_b, "chrome.idle"); | 792 check_properties_inequal(context_a, context_b, "chrome.idle"); |
793 check_properties_inequal(context_a, context_b, "chrome.idle.onStateChanged"); | 793 check_properties_inequal(context_a, context_b, "chrome.idle.onStateChanged"); |
794 } | 794 } |
795 | 795 |
| 796 // Tests that API methods and events that are conditionally available based on |
| 797 // context are properly present or absent from the API object. |
| 798 TEST_F(NativeExtensionBindingsSystemUnittest, |
| 799 CheckRestrictedFeaturesBasedOnContext) { |
| 800 scoped_refptr<Extension> extension = |
| 801 CreateExtension("extension", ItemType::EXTENSION, {"idle"}); |
| 802 RegisterExtension(extension->id()); |
| 803 |
| 804 v8::HandleScope handle_scope(isolate()); |
| 805 v8::Local<v8::Context> blessed_context = MainContext(); |
| 806 v8::Local<v8::Context> webpage_context = AddContext(); |
| 807 |
| 808 // Create two contexts - a blessed extension context and a normal web page |
| 809 // context. |
| 810 ScriptContext* blessed_script_context = CreateScriptContext( |
| 811 blessed_context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 812 blessed_script_context->set_url(extension->url()); |
| 813 bindings_system()->UpdateBindingsForContext(blessed_script_context); |
| 814 |
| 815 ScriptContext* webpage_script_context = |
| 816 CreateScriptContext(webpage_context, nullptr, Feature::WEB_PAGE_CONTEXT); |
| 817 webpage_script_context->set_url(GURL("http://example.com")); |
| 818 bindings_system()->UpdateBindingsForContext(webpage_script_context); |
| 819 |
| 820 auto property_exists = [](v8::Local<v8::Context> context, |
| 821 base::StringPiece property) { |
| 822 v8::Local<v8::Value> value = V8ValueFromScriptSource(context, property); |
| 823 EXPECT_FALSE(value.IsEmpty()); |
| 824 return !value->IsUndefined(); |
| 825 }; |
| 826 |
| 827 // Check that properties are correctly restricted. The blessed context should |
| 828 // have access to the whole runtime API, but the webpage should only have |
| 829 // access to sendMessage. |
| 830 const char kSendMessage[] = "chrome.runtime.sendMessage"; |
| 831 const char kGetUrl[] = "chrome.runtime.getURL"; |
| 832 const char kOnMessage[] = "chrome.runtime.onMessage"; |
| 833 EXPECT_TRUE(property_exists(blessed_context, kSendMessage)); |
| 834 EXPECT_TRUE(property_exists(blessed_context, kGetUrl)); |
| 835 EXPECT_TRUE(property_exists(blessed_context, kOnMessage)); |
| 836 EXPECT_TRUE(property_exists(webpage_context, kSendMessage)); |
| 837 EXPECT_FALSE(property_exists(webpage_context, kGetUrl)); |
| 838 EXPECT_FALSE(property_exists(webpage_context, kOnMessage)); |
| 839 } |
| 840 |
796 } // namespace extensions | 841 } // namespace extensions |
OLD | NEW |