| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/permissions_updater.h" | 5 #include "chrome/browser/extensions/permissions_updater.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_service_test_base.h" | 18 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 18 #include "chrome/browser/extensions/extension_util.h" | 19 #include "chrome/browser/extensions/extension_util.h" |
| 19 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/extensions/extension_test_util.h" | 21 #include "chrome/common/extensions/extension_test_util.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 32 #include "extensions/common/value_builder.h" | 33 #include "extensions/common/value_builder.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 35 |
| 35 using extension_test_util::LoadManifest; | 36 using extension_test_util::LoadManifest; |
| 36 | 37 |
| 37 namespace extensions { | 38 namespace extensions { |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 scoped_refptr<const Extension> CreateExtensionWithOptionalPermissions( | 42 scoped_refptr<const Extension> CreateExtensionWithOptionalPermissions( |
| 42 scoped_ptr<base::Value> optional_permissions, | 43 std::unique_ptr<base::Value> optional_permissions, |
| 43 scoped_ptr<base::Value> permissions, | 44 std::unique_ptr<base::Value> permissions, |
| 44 const std::string& name) { | 45 const std::string& name) { |
| 45 return ExtensionBuilder() | 46 return ExtensionBuilder() |
| 46 .SetLocation(Manifest::INTERNAL) | 47 .SetLocation(Manifest::INTERNAL) |
| 47 .SetManifest( | 48 .SetManifest( |
| 48 DictionaryBuilder() | 49 DictionaryBuilder() |
| 49 .Set("name", name) | 50 .Set("name", name) |
| 50 .Set("description", "foo") | 51 .Set("description", "foo") |
| 51 .Set("manifest_version", 2) | 52 .Set("manifest_version", 2) |
| 52 .Set("version", "0.1.2.3") | 53 .Set("version", "0.1.2.3") |
| 53 .Set("permissions", std::move(permissions)) | 54 .Set("permissions", std::move(permissions)) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (waiting_) { | 104 if (waiting_) { |
| 104 waiting_ = false; | 105 waiting_ = false; |
| 105 base::MessageLoopForUI::current()->QuitWhenIdle(); | 106 base::MessageLoopForUI::current()->QuitWhenIdle(); |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 | 109 |
| 109 bool received_notification_; | 110 bool received_notification_; |
| 110 bool waiting_; | 111 bool waiting_; |
| 111 content::NotificationRegistrar registrar_; | 112 content::NotificationRegistrar registrar_; |
| 112 scoped_refptr<const Extension> extension_; | 113 scoped_refptr<const Extension> extension_; |
| 113 scoped_ptr<const PermissionSet> permissions_; | 114 std::unique_ptr<const PermissionSet> permissions_; |
| 114 UpdatedExtensionPermissionsInfo::Reason reason_; | 115 UpdatedExtensionPermissionsInfo::Reason reason_; |
| 115 }; | 116 }; |
| 116 | 117 |
| 117 class PermissionsUpdaterTest : public ExtensionServiceTestBase { | 118 class PermissionsUpdaterTest : public ExtensionServiceTestBase { |
| 118 }; | 119 }; |
| 119 | 120 |
| 120 scoped_refptr<Extension> LoadOurManifest() { | 121 scoped_refptr<Extension> LoadOurManifest() { |
| 121 base::FilePath path; | 122 base::FilePath path; |
| 122 path = path.AppendASCII("api_test") | 123 path = path.AppendASCII("api_test") |
| 123 .AppendASCII("permissions") | 124 .AppendASCII("permissions") |
| (...skipping 28 matching lines...) Expand all Loading... |
| 152 URLPatternSet default_hosts; | 153 URLPatternSet default_hosts; |
| 153 AddPattern(&default_hosts, "http://a.com/*"); | 154 AddPattern(&default_hosts, "http://a.com/*"); |
| 154 PermissionSet default_permissions(default_apis, empty_manifest_permissions, | 155 PermissionSet default_permissions(default_apis, empty_manifest_permissions, |
| 155 default_hosts, URLPatternSet()); | 156 default_hosts, URLPatternSet()); |
| 156 | 157 |
| 157 // Make sure it loaded properly. | 158 // Make sure it loaded properly. |
| 158 ASSERT_EQ(default_permissions, | 159 ASSERT_EQ(default_permissions, |
| 159 extension->permissions_data()->active_permissions()); | 160 extension->permissions_data()->active_permissions()); |
| 160 | 161 |
| 161 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); | 162 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); |
| 162 scoped_ptr<const PermissionSet> active_permissions; | 163 std::unique_ptr<const PermissionSet> active_permissions; |
| 163 scoped_ptr<const PermissionSet> granted_permissions; | 164 std::unique_ptr<const PermissionSet> granted_permissions; |
| 164 | 165 |
| 165 // Add a few permissions. | 166 // Add a few permissions. |
| 166 APIPermissionSet apis; | 167 APIPermissionSet apis; |
| 167 apis.insert(APIPermission::kNotifications); | 168 apis.insert(APIPermission::kNotifications); |
| 168 URLPatternSet hosts; | 169 URLPatternSet hosts; |
| 169 AddPattern(&hosts, "http://*.c.com/*"); | 170 AddPattern(&hosts, "http://*.c.com/*"); |
| 170 | 171 |
| 171 { | 172 { |
| 172 PermissionSet delta(apis, empty_manifest_permissions, hosts, | 173 PermissionSet delta(apis, empty_manifest_permissions, hosts, |
| 173 URLPatternSet()); | 174 URLPatternSet()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 233 } |
| 233 | 234 |
| 234 TEST_F(PermissionsUpdaterTest, RevokingPermissions) { | 235 TEST_F(PermissionsUpdaterTest, RevokingPermissions) { |
| 235 InitializeEmptyExtensionService(); | 236 InitializeEmptyExtensionService(); |
| 236 | 237 |
| 237 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); | 238 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
| 238 | 239 |
| 239 auto api_permission_set = [](APIPermission::ID id) { | 240 auto api_permission_set = [](APIPermission::ID id) { |
| 240 APIPermissionSet apis; | 241 APIPermissionSet apis; |
| 241 apis.insert(id); | 242 apis.insert(id); |
| 242 return make_scoped_ptr(new PermissionSet(apis, ManifestPermissionSet(), | 243 return base::WrapUnique(new PermissionSet( |
| 243 URLPatternSet(), URLPatternSet())); | 244 apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet())); |
| 244 }; | 245 }; |
| 245 | 246 |
| 246 auto url_permission_set = [](const GURL& url) { | 247 auto url_permission_set = [](const GURL& url) { |
| 247 URLPatternSet set; | 248 URLPatternSet set; |
| 248 URLPattern pattern(URLPattern::SCHEME_ALL, url.spec()); | 249 URLPattern pattern(URLPattern::SCHEME_ALL, url.spec()); |
| 249 set.AddPattern(pattern); | 250 set.AddPattern(pattern); |
| 250 return make_scoped_ptr(new PermissionSet( | 251 return base::WrapUnique(new PermissionSet( |
| 251 APIPermissionSet(), ManifestPermissionSet(), set, URLPatternSet())); | 252 APIPermissionSet(), ManifestPermissionSet(), set, URLPatternSet())); |
| 252 }; | 253 }; |
| 253 | 254 |
| 254 { | 255 { |
| 255 // Test revoking optional permissions. | 256 // Test revoking optional permissions. |
| 256 ListBuilder optional_permissions; | 257 ListBuilder optional_permissions; |
| 257 optional_permissions.Append("tabs").Append("cookies").Append("management"); | 258 optional_permissions.Append("tabs").Append("cookies").Append("management"); |
| 258 ListBuilder required_permissions; | 259 ListBuilder required_permissions; |
| 259 required_permissions.Append("topSites"); | 260 required_permissions.Append("topSites"); |
| 260 scoped_refptr<const Extension> extension = | 261 scoped_refptr<const Extension> extension = |
| 261 CreateExtensionWithOptionalPermissions(optional_permissions.Build(), | 262 CreateExtensionWithOptionalPermissions(optional_permissions.Build(), |
| 262 required_permissions.Build(), | 263 required_permissions.Build(), |
| 263 "My Extension"); | 264 "My Extension"); |
| 264 | 265 |
| 265 PermissionsUpdater updater(profile()); | 266 PermissionsUpdater updater(profile()); |
| 266 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); | 267 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); |
| 267 | 268 |
| 268 // Add the optional "cookies" permission. | 269 // Add the optional "cookies" permission. |
| 269 updater.AddPermissions(extension.get(), | 270 updater.AddPermissions(extension.get(), |
| 270 *api_permission_set(APIPermission::kCookie)); | 271 *api_permission_set(APIPermission::kCookie)); |
| 271 const PermissionsData* permissions = extension->permissions_data(); | 272 const PermissionsData* permissions = extension->permissions_data(); |
| 272 // The extension should have the permission in its active permissions and | 273 // The extension should have the permission in its active permissions and |
| 273 // its granted permissions (stored in prefs). And, the permission should | 274 // its granted permissions (stored in prefs). And, the permission should |
| 274 // be revokable. | 275 // be revokable. |
| 275 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kCookie)); | 276 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kCookie)); |
| 276 scoped_ptr<const PermissionSet> granted_permissions = | 277 std::unique_ptr<const PermissionSet> granted_permissions = |
| 277 prefs->GetGrantedPermissions(extension->id()); | 278 prefs->GetGrantedPermissions(extension->id()); |
| 278 EXPECT_TRUE(granted_permissions->HasAPIPermission(APIPermission::kCookie)); | 279 EXPECT_TRUE(granted_permissions->HasAPIPermission(APIPermission::kCookie)); |
| 279 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get()) | 280 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get()) |
| 280 ->HasAPIPermission(APIPermission::kCookie)); | 281 ->HasAPIPermission(APIPermission::kCookie)); |
| 281 | 282 |
| 282 // Repeat with "tabs". | 283 // Repeat with "tabs". |
| 283 updater.AddPermissions(extension.get(), | 284 updater.AddPermissions(extension.get(), |
| 284 *api_permission_set(APIPermission::kTab)); | 285 *api_permission_set(APIPermission::kTab)); |
| 285 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kTab)); | 286 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kTab)); |
| 286 granted_permissions = prefs->GetGrantedPermissions(extension->id()); | 287 granted_permissions = prefs->GetGrantedPermissions(extension->id()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 ->active_permissions() | 358 ->active_permissions() |
| 358 .HasExplicitAccessToOrigin(kOrigin)); | 359 .HasExplicitAccessToOrigin(kOrigin)); |
| 359 EXPECT_TRUE(extension->permissions_data() | 360 EXPECT_TRUE(extension->permissions_data() |
| 360 ->withheld_permissions() | 361 ->withheld_permissions() |
| 361 .HasExplicitAccessToOrigin(kOrigin)); | 362 .HasExplicitAccessToOrigin(kOrigin)); |
| 362 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); | 363 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); |
| 363 } | 364 } |
| 364 } | 365 } |
| 365 | 366 |
| 366 } // namespace extensions | 367 } // namespace extensions |
| OLD | NEW |