| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "chrome/browser/browser_thread.h" | 7 #include "chrome/browser/browser_thread.h" |
| 8 #include "chrome/browser/extensions/extension_info_map.h" | 8 #include "chrome/browser/extensions/extension_info_map.h" |
| 9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "chrome/common/extensions/extension.h" |
| 10 #include "chrome/common/json_value_serializer.h" | 11 #include "chrome/common/json_value_serializer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace keys = extension_manifest_keys; | 14 namespace keys = extension_manifest_keys; |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 class ExtensionInfoMapTest : public testing::Test { | 18 class ExtensionInfoMapTest : public testing::Test { |
| 18 public: | 19 public: |
| 19 ExtensionInfoMapTest() | 20 ExtensionInfoMapTest() |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 false, &error); | 67 false, &error); |
| 67 EXPECT_TRUE(extension) << error; | 68 EXPECT_TRUE(extension) << error; |
| 68 | 69 |
| 69 return extension; | 70 return extension; |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Test that the ExtensionInfoMap handles refcounting properly. | 73 // Test that the ExtensionInfoMap handles refcounting properly. |
| 73 TEST_F(ExtensionInfoMapTest, RefCounting) { | 74 TEST_F(ExtensionInfoMapTest, RefCounting) { |
| 74 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); | 75 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 75 | 76 |
| 76 // New extensions should have a single reference holding onto their static | 77 // New extensions should have a single reference holding onto them. |
| 77 // data. | |
| 78 scoped_refptr<Extension> extension1(CreateExtension("extension1")); | 78 scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 79 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | 79 scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 80 scoped_refptr<Extension> extension3(CreateExtension("extension3")); | 80 scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
| 81 EXPECT_TRUE(extension1->static_data()->HasOneRef()); | 81 EXPECT_TRUE(extension1->HasOneRef()); |
| 82 EXPECT_TRUE(extension2->static_data()->HasOneRef()); | 82 EXPECT_TRUE(extension2->HasOneRef()); |
| 83 EXPECT_TRUE(extension3->static_data()->HasOneRef()); | 83 EXPECT_TRUE(extension3->HasOneRef()); |
| 84 | 84 |
| 85 // Add a ref to each extension and give it to the info map. The info map | 85 // Add a ref to each extension and give it to the info map. The info map |
| 86 // expects the caller to add a ref for it, but then assumes ownership of that | 86 // expects the caller to add a ref for it, but then assumes ownership of that |
| 87 // reference. | 87 // reference. |
| 88 extension1->static_data()->AddRef(); | 88 extension1->AddRef(); |
| 89 info_map->AddExtension(extension1->static_data()); | 89 info_map->AddExtension(extension1); |
| 90 extension2->static_data()->AddRef(); | 90 extension2->AddRef(); |
| 91 info_map->AddExtension(extension2->static_data()); | 91 info_map->AddExtension(extension2); |
| 92 extension3->static_data()->AddRef(); | 92 extension3->AddRef(); |
| 93 info_map->AddExtension(extension3->static_data()); | 93 info_map->AddExtension(extension3); |
| 94 | 94 |
| 95 // Delete extension1, and the info map should have the only ref. | 95 // Release extension1, and the info map should have the only ref. |
| 96 const Extension::StaticData* data1 = extension1->static_data(); | 96 const Extension* weak_extension1 = extension1; |
| 97 extension1 = NULL; | 97 extension1 = NULL; |
| 98 EXPECT_TRUE(data1->HasOneRef()); | 98 EXPECT_TRUE(weak_extension1->HasOneRef()); |
| 99 | 99 |
| 100 // Remove extension2, and the extension2 object should have the only ref. | 100 // Remove extension2, and the extension2 object should have the only ref. |
| 101 info_map->RemoveExtension(extension2->id()); | 101 info_map->RemoveExtension(extension2->id()); |
| 102 EXPECT_TRUE(extension2->static_data()->HasOneRef()); | 102 EXPECT_TRUE(extension2->HasOneRef()); |
| 103 | 103 |
| 104 // Delete the info map, and the extension3 object should have the only ref. | 104 // Delete the info map, and the extension3 object should have the only ref. |
| 105 info_map = NULL; | 105 info_map = NULL; |
| 106 EXPECT_TRUE(extension3->static_data()->HasOneRef()); | 106 EXPECT_TRUE(extension3->HasOneRef()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Tests that we can query a few extension properties from the ExtensionInfoMap. | 109 // Tests that we can query a few extension properties from the ExtensionInfoMap. |
| 110 TEST_F(ExtensionInfoMapTest, Properties) { | 110 TEST_F(ExtensionInfoMapTest, Properties) { |
| 111 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); | 111 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 112 | 112 |
| 113 scoped_refptr<Extension> extension1(CreateExtension("extension1")); | 113 scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 114 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | 114 scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 115 | 115 |
| 116 extension1->static_data()->AddRef(); | 116 extension1->AddRef(); |
| 117 info_map->AddExtension(extension1->static_data()); | 117 info_map->AddExtension(extension1); |
| 118 extension2->static_data()->AddRef(); | 118 extension2->AddRef(); |
| 119 info_map->AddExtension(extension2->static_data()); | 119 info_map->AddExtension(extension2); |
| 120 | 120 |
| 121 EXPECT_EQ(extension1->name(), | 121 EXPECT_EQ(extension1->name(), |
| 122 info_map->GetNameForExtension(extension1->id())); | 122 info_map->GetNameForExtension(extension1->id())); |
| 123 EXPECT_EQ(extension2->name(), | 123 EXPECT_EQ(extension2->name(), |
| 124 info_map->GetNameForExtension(extension2->id())); | 124 info_map->GetNameForExtension(extension2->id())); |
| 125 | 125 |
| 126 EXPECT_EQ(extension1->path().value(), | 126 EXPECT_EQ(extension1->path().value(), |
| 127 info_map->GetPathForExtension(extension1->id()).value()); | 127 info_map->GetPathForExtension(extension1->id()).value()); |
| 128 EXPECT_EQ(extension2->path().value(), | 128 EXPECT_EQ(extension2->path().value(), |
| 129 info_map->GetPathForExtension(extension2->id()).value()); | 129 info_map->GetPathForExtension(extension2->id()).value()); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. | 132 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. |
| 133 TEST_F(ExtensionInfoMapTest, CheckPermissions) { | 133 TEST_F(ExtensionInfoMapTest, CheckPermissions) { |
| 134 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); | 134 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 135 | 135 |
| 136 scoped_refptr<Extension> app(LoadManifest("manifest_tests", | 136 scoped_refptr<Extension> app(LoadManifest("manifest_tests", |
| 137 "valid_app.json")); | 137 "valid_app.json")); |
| 138 scoped_refptr<Extension> extension(LoadManifest("manifest_tests", | 138 scoped_refptr<Extension> extension(LoadManifest("manifest_tests", |
| 139 "tabs_extension.json")); | 139 "tabs_extension.json")); |
| 140 | 140 |
| 141 GURL app_url("http://www.google.com/mail/foo.html"); | 141 GURL app_url("http://www.google.com/mail/foo.html"); |
| 142 ASSERT_TRUE(app->is_app()); | 142 ASSERT_TRUE(app->is_app()); |
| 143 ASSERT_TRUE(app->web_extent().ContainsURL(app_url)); | 143 ASSERT_TRUE(app->web_extent().ContainsURL(app_url)); |
| 144 | 144 |
| 145 app->static_data()->AddRef(); | 145 app->AddRef(); |
| 146 info_map->AddExtension(app->static_data()); | 146 info_map->AddExtension(app); |
| 147 extension->static_data()->AddRef(); | 147 extension->AddRef(); |
| 148 info_map->AddExtension(extension->static_data()); | 148 info_map->AddExtension(extension); |
| 149 | 149 |
| 150 // The app should have the notifications permission, either from a | 150 // The app should have the notifications permission, either from a |
| 151 // chrome-extension URL or from its web extent. | 151 // chrome-extension URL or from its web extent. |
| 152 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( | 152 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 153 app->GetResourceURL("a.html"), Extension::kNotificationPermission)); | 153 app->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 154 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( | 154 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 155 app_url, Extension::kNotificationPermission)); | 155 app_url, Extension::kNotificationPermission)); |
| 156 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( | 156 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 157 app_url, Extension::kTabPermission)); | 157 app_url, Extension::kTabPermission)); |
| 158 | 158 |
| 159 // The extension should have the tabs permission. | 159 // The extension should have the tabs permission. |
| 160 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( | 160 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission( |
| 161 extension->GetResourceURL("a.html"), Extension::kTabPermission)); | 161 extension->GetResourceURL("a.html"), Extension::kTabPermission)); |
| 162 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( | 162 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 163 extension->GetResourceURL("a.html"), Extension::kNotificationPermission)); | 163 extension->GetResourceURL("a.html"), Extension::kNotificationPermission)); |
| 164 | 164 |
| 165 // Random URL should not have any permissions. | 165 // Random URL should not have any permissions. |
| 166 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( | 166 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 167 GURL("http://evil.com/a.html"), Extension::kNotificationPermission)); | 167 GURL("http://evil.com/a.html"), Extension::kNotificationPermission)); |
| 168 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( | 168 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission( |
| 169 GURL("http://evil.com/a.html"), Extension::kTabPermission)); | 169 GURL("http://evil.com/a.html"), Extension::kTabPermission)); |
| 170 } | 170 } |
| 171 | 171 |
| 172 } // namespace | 172 } // namespace |
| OLD | NEW |