binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/macros.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "base/values.h" |
| 13 | #include "chrome/browser/extensions/extension_management_constants.h" |
| 14 | #include "extensions/browser/pref_names.h" |
| 15 | #include "extensions/common/extension.h" |
| 16 | |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 17 | namespace policy { |
| 18 | class MockConfigurationPolicyProvider; |
| 19 | class PolicyBundle; |
| 20 | } // namespace policy |
| 21 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 22 | namespace extensions { |
| 23 | |
| 24 | // Base class for essential routines on preference manipulation. |
| 25 | class ExtensionManagementPrefUpdaterBase { |
| 26 | public: |
| 27 | ExtensionManagementPrefUpdaterBase(); |
| 28 | virtual ~ExtensionManagementPrefUpdaterBase(); |
| 29 | |
binjin | 6a6eb15 | 2014-09-24 18:36:34 | [diff] [blame] | 30 | // Helper functions for per extension settings. |
| 31 | void UnsetPerExtensionSettings(const ExtensionId& id); |
| 32 | void ClearPerExtensionSettings(const ExtensionId& id); |
| 33 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 34 | // Helper functions for 'installation_mode' manipulation. |
| 35 | void SetBlacklistedByDefault(bool value); |
| 36 | void ClearInstallationModesForIndividualExtensions(); |
| 37 | void SetIndividualExtensionInstallationAllowed(const ExtensionId& id, |
| 38 | bool allowed); |
| 39 | void SetIndividualExtensionAutoInstalled(const ExtensionId& id, |
| 40 | const std::string& update_url, |
| 41 | bool forced); |
| 42 | |
| 43 | // Helper functions for 'install_sources' manipulation. |
| 44 | void UnsetInstallSources(); |
| 45 | void ClearInstallSources(); |
| 46 | void AddInstallSource(const std::string& install_source); |
| 47 | void RemoveInstallSource(const std::string& install_source); |
| 48 | |
| 49 | // Helper functions for 'allowed_types' manipulation. |
| 50 | void UnsetAllowedTypes(); |
| 51 | void ClearAllowedTypes(); |
| 52 | void AddAllowedType(const std::string& allowed_type); |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 53 | void RemoveAllowedType(const std::string& allowed_type); |
| 54 | |
| 55 | // Helper functions for 'blocked_permissions' manipulation. |prefix| can be |
| 56 | // kWildCard or a valid extension ID. |
| 57 | void UnsetBlockedPermissions(const std::string& prefix); |
| 58 | void ClearBlockedPermissions(const std::string& prefix); |
| 59 | void AddBlockedPermission(const std::string& prefix, |
| 60 | const std::string& permission); |
| 61 | void RemoveBlockedPermission(const std::string& prefix, |
| 62 | const std::string& permission); |
| 63 | |
| 64 | // Helper functions for 'allowed_permissions' manipulation. |id| must be a |
binjin | 8e3d018 | 2014-12-04 16:44:28 | [diff] [blame^] | 65 | // valid extension ID. |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 66 | void UnsetAllowedPermissions(const std::string& id); |
| 67 | void ClearAllowedPermissions(const std::string& id); |
| 68 | void AddAllowedPermission(const std::string& id, |
| 69 | const std::string& permission); |
| 70 | void RemoveAllowedPermission(const std::string& id, |
| 71 | const std::string& permission); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 72 | |
binjin | 8e3d018 | 2014-12-04 16:44:28 | [diff] [blame^] | 73 | // Helper functions for 'minimum_version_required' manipulation. |id| must be |
| 74 | // a valid extension ID. |
| 75 | void SetMinimumVersionRequired(const std::string& id, |
| 76 | const std::string& version); |
| 77 | void UnsetMinimumVersionRequired(const std::string& id); |
| 78 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 79 | // Expose a read-only preference to user. |
| 80 | const base::DictionaryValue* GetPref(); |
| 81 | |
| 82 | protected: |
| 83 | // Set the preference with |pref|, pass the ownership of it as well. |
| 84 | // This function must be called before accessing publicly exposed functions, |
| 85 | // for example in constructor of subclass. |
| 86 | void SetPref(base::DictionaryValue* pref); |
| 87 | |
| 88 | // Take the preference. Caller takes ownership of it as well. |
| 89 | // This function must be called after accessing publicly exposed functions, |
| 90 | // for example in destructor of subclass. |
| 91 | scoped_ptr<base::DictionaryValue> TakePref(); |
| 92 | |
| 93 | private: |
| 94 | // Helper functions for manipulating sub properties like list of strings. |
| 95 | void ClearList(const std::string& path); |
| 96 | void AddStringToList(const std::string& path, const std::string& str); |
| 97 | void RemoveStringFromList(const std::string& path, const std::string& str); |
| 98 | |
| 99 | scoped_ptr<base::DictionaryValue> pref_; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase); |
| 102 | }; |
| 103 | |
| 104 | // A helper class to manipulate the extension management preference in unit |
| 105 | // tests. |
| 106 | template <class TestingPrefService> |
| 107 | class ExtensionManagementPrefUpdater |
| 108 | : public ExtensionManagementPrefUpdaterBase { |
| 109 | public: |
| 110 | explicit ExtensionManagementPrefUpdater(TestingPrefService* service) |
| 111 | : service_(service) { |
| 112 | const base::Value* pref_value = |
| 113 | service_->GetManagedPref(pref_names::kExtensionManagement); |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 114 | const base::DictionaryValue* dict_value = nullptr; |
| 115 | if (pref_value && pref_value->GetAsDictionary(&dict_value)) |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 116 | SetPref(dict_value->DeepCopy()); |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 117 | else |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 118 | SetPref(new base::DictionaryValue); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | virtual ~ExtensionManagementPrefUpdater() { |
| 122 | service_->SetManagedPref(pref_names::kExtensionManagement, |
| 123 | TakePref().release()); |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | TestingPrefService* service_; |
| 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater); |
| 130 | }; |
| 131 | |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 132 | // A helper class to manipulate the extension management policy in browser |
| 133 | // tests. |
| 134 | class ExtensionManagementPolicyUpdater |
| 135 | : public ExtensionManagementPrefUpdaterBase { |
| 136 | public: |
| 137 | explicit ExtensionManagementPolicyUpdater( |
| 138 | policy::MockConfigurationPolicyProvider* provider); |
| 139 | ~ExtensionManagementPolicyUpdater() override; |
| 140 | |
| 141 | private: |
| 142 | policy::MockConfigurationPolicyProvider* provider_; |
| 143 | scoped_ptr<policy::PolicyBundle> policies_; |
| 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPolicyUpdater); |
| 146 | }; |
| 147 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 148 | } // namespace extensions |
| 149 | |
| 150 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |