| 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" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 "manifest.json", | 126 "manifest.json", |
| 127 Manifest::INTERNAL, | 127 Manifest::INTERNAL, |
| 128 Extension::NO_FLAGS); | 128 Extension::NO_FLAGS); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 131 void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 132 int schemes = URLPattern::SCHEME_ALL; | 132 int schemes = URLPattern::SCHEME_ALL; |
| 133 extent->AddPattern(URLPattern(schemes, pattern)); | 133 extent->AddPattern(URLPattern(schemes, pattern)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 class PermissionsUpdaterTestDelegate : public PermissionsUpdater::Delegate { |
| 137 public: |
| 138 PermissionsUpdaterTestDelegate() {} |
| 139 ~PermissionsUpdaterTestDelegate() override {} |
| 140 |
| 141 // PermissionsUpdater::Delegate |
| 142 void InitializePermissions( |
| 143 const Extension* extension, |
| 144 std::unique_ptr<const PermissionSet>* granted_permissions) override { |
| 145 // Remove the cookie permission. |
| 146 APIPermissionSet api_permission_set((*granted_permissions)->apis()); |
| 147 api_permission_set.erase(APIPermission::kCookie); |
| 148 granted_permissions->reset( |
| 149 new PermissionSet(api_permission_set, ManifestPermissionSet(), |
| 150 URLPatternSet(), URLPatternSet())); |
| 151 } |
| 152 |
| 153 private: |
| 154 DISALLOW_COPY_AND_ASSIGN(PermissionsUpdaterTestDelegate); |
| 155 }; |
| 156 |
| 136 } // namespace | 157 } // namespace |
| 137 | 158 |
| 138 // Test that the PermissionUpdater can correctly add and remove active | 159 // Test that the PermissionUpdater can correctly add and remove active |
| 139 // permissions. This tests all of PermissionsUpdater's public methods because | 160 // permissions. This tests all of PermissionsUpdater's public methods because |
| 140 // GrantActivePermissions and SetPermissions are used by AddPermissions. | 161 // GrantActivePermissions and SetPermissions are used by AddPermissions. |
| 141 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { | 162 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { |
| 142 InitializeEmptyExtensionService(); | 163 InitializeEmptyExtensionService(); |
| 143 | 164 |
| 144 // Load the test extension. | 165 // Load the test extension. |
| 145 scoped_refptr<Extension> extension = LoadOurManifest(); | 166 scoped_refptr<Extension> extension = LoadOurManifest(); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 EXPECT_FALSE(extension->permissions_data() | 376 EXPECT_FALSE(extension->permissions_data() |
| 356 ->active_permissions() | 377 ->active_permissions() |
| 357 .HasExplicitAccessToOrigin(kOrigin)); | 378 .HasExplicitAccessToOrigin(kOrigin)); |
| 358 EXPECT_TRUE(extension->permissions_data() | 379 EXPECT_TRUE(extension->permissions_data() |
| 359 ->withheld_permissions() | 380 ->withheld_permissions() |
| 360 .HasExplicitAccessToOrigin(kOrigin)); | 381 .HasExplicitAccessToOrigin(kOrigin)); |
| 361 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); | 382 EXPECT_TRUE(updater.GetRevokablePermissions(extension.get())->IsEmpty()); |
| 362 } | 383 } |
| 363 } | 384 } |
| 364 | 385 |
| 386 // Test that the permissions updater delegate works - in this test it removes |
| 387 // the cookies permission. |
| 388 TEST_F(PermissionsUpdaterTest, Delegate) { |
| 389 InitializeEmptyExtensionService(); |
| 390 |
| 391 ListBuilder required_permissions; |
| 392 required_permissions.Append("tabs").Append("management").Append("cookies"); |
| 393 scoped_refptr<const Extension> extension = |
| 394 CreateExtensionWithOptionalPermissions( |
| 395 base::MakeUnique<base::ListValue>(), |
| 396 required_permissions.Build(), |
| 397 "My Extension"); |
| 398 |
| 399 auto test_delegate = base::MakeUnique<PermissionsUpdaterTestDelegate>(); |
| 400 PermissionsUpdater::SetPlatformDelegate(test_delegate.get()); |
| 401 PermissionsUpdater updater(profile()); |
| 402 updater.InitializePermissions(extension.get()); |
| 403 |
| 404 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 405 APIPermission::kTab)); |
| 406 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 407 APIPermission::kManagement)); |
| 408 EXPECT_FALSE(extension->permissions_data()->HasAPIPermission( |
| 409 APIPermission::kCookie)); |
| 410 |
| 411 // Unset the delegate. |
| 412 PermissionsUpdater::SetPlatformDelegate(nullptr); |
| 413 } |
| 414 |
| 365 } // namespace extensions | 415 } // namespace extensions |
| OLD | NEW |