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 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/macros.h" |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 12 | #include "base/values.h" |
| 13 | #include "chrome/browser/extensions/extension_management_constants.h" |
| 14 | #include "extensions/browser/pref_names.h" |
rdevlin.cronin | 0670b56 | 2016-07-02 02:05:43 | [diff] [blame] | 15 | #include "extensions/common/extension_id.h" |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 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 | |
nrpeter | 2362e7e | 2017-05-10 17:21:26 | [diff] [blame] | 64 | // Helper function for 'blocked_install_message' manipulation. |
| 65 | // |id| is extension ID. |
| 66 | void SetBlockedInstallMessage(const ExtensionId& id, |
| 67 | const std::string& custom_error); |
| 68 | |
nrpeter | 40e1638 | 2017-04-13 17:34:58 | [diff] [blame] | 69 | // Helper functions for 'runtime_blocked_hosts' manipulation. |prefix| can be |
| 70 | // kWildCard or a valid extension ID. |
Devlin Cronin | 7e0f41ff | 2018-05-16 17:19:36 | [diff] [blame] | 71 | void UnsetPolicyBlockedHosts(const std::string& prefix); |
| 72 | void ClearPolicyBlockedHosts(const std::string& prefix); |
| 73 | void AddPolicyBlockedHost(const std::string& prefix, const std::string& host); |
| 74 | void RemovePolicyBlockedHost(const std::string& prefix, |
| 75 | const std::string& host); |
nrpeter | 40e1638 | 2017-04-13 17:34:58 | [diff] [blame] | 76 | |
Nick Peterson | 87ecb10 | 2018-10-16 04:55:01 | [diff] [blame] | 77 | // Helper functions for 'runtime_allowed_hosts' manipulation. |prefix| can be |
| 78 | // kWildCard or a valid extension ID. |
| 79 | void UnsetPolicyAllowedHosts(const std::string& prefix); |
| 80 | void ClearPolicyAllowedHosts(const std::string& prefix); |
| 81 | void AddPolicyAllowedHost(const std::string& prefix, const std::string& host); |
| 82 | void RemovePolicyAllowedHost(const std::string& prefix, |
| 83 | const std::string& host); |
| 84 | |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 85 | // Helper functions for 'allowed_permissions' manipulation. |id| must be a |
binjin | 8e3d018 | 2014-12-04 16:44:28 | [diff] [blame] | 86 | // valid extension ID. |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 87 | void UnsetAllowedPermissions(const std::string& id); |
| 88 | void ClearAllowedPermissions(const std::string& id); |
| 89 | void AddAllowedPermission(const std::string& id, |
| 90 | const std::string& permission); |
| 91 | void RemoveAllowedPermission(const std::string& id, |
| 92 | const std::string& permission); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 93 | |
binjin | 8e3d018 | 2014-12-04 16:44:28 | [diff] [blame] | 94 | // Helper functions for 'minimum_version_required' manipulation. |id| must be |
| 95 | // a valid extension ID. |
| 96 | void SetMinimumVersionRequired(const std::string& id, |
| 97 | const std::string& version); |
| 98 | void UnsetMinimumVersionRequired(const std::string& id); |
| 99 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 100 | // Expose a read-only preference to user. |
| 101 | const base::DictionaryValue* GetPref(); |
| 102 | |
| 103 | protected: |
| 104 | // Set the preference with |pref|, pass the ownership of it as well. |
| 105 | // This function must be called before accessing publicly exposed functions, |
| 106 | // for example in constructor of subclass. |
| 107 | void SetPref(base::DictionaryValue* pref); |
| 108 | |
| 109 | // Take the preference. Caller takes ownership of it as well. |
| 110 | // This function must be called after accessing publicly exposed functions, |
| 111 | // for example in destructor of subclass. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 112 | std::unique_ptr<base::DictionaryValue> TakePref(); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | // Helper functions for manipulating sub properties like list of strings. |
| 116 | void ClearList(const std::string& path); |
| 117 | void AddStringToList(const std::string& path, const std::string& str); |
| 118 | void RemoveStringFromList(const std::string& path, const std::string& str); |
| 119 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 120 | std::unique_ptr<base::DictionaryValue> pref_; |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase); |
| 123 | }; |
| 124 | |
| 125 | // A helper class to manipulate the extension management preference in unit |
| 126 | // tests. |
| 127 | template <class TestingPrefService> |
| 128 | class ExtensionManagementPrefUpdater |
| 129 | : public ExtensionManagementPrefUpdaterBase { |
| 130 | public: |
| 131 | explicit ExtensionManagementPrefUpdater(TestingPrefService* service) |
| 132 | : service_(service) { |
| 133 | const base::Value* pref_value = |
| 134 | service_->GetManagedPref(pref_names::kExtensionManagement); |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 135 | const base::DictionaryValue* dict_value = nullptr; |
| 136 | if (pref_value && pref_value->GetAsDictionary(&dict_value)) |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 137 | SetPref(dict_value->DeepCopy()); |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 138 | else |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 139 | SetPref(new base::DictionaryValue); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | virtual ~ExtensionManagementPrefUpdater() { |
vabr | 8684c9a | 2017-03-29 13:14:57 | [diff] [blame] | 143 | service_->SetManagedPref(pref_names::kExtensionManagement, TakePref()); |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
| 147 | TestingPrefService* service_; |
| 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater); |
| 150 | }; |
| 151 | |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 152 | // A helper class to manipulate the extension management policy in browser |
| 153 | // tests. |
| 154 | class ExtensionManagementPolicyUpdater |
| 155 | : public ExtensionManagementPrefUpdaterBase { |
| 156 | public: |
| 157 | explicit ExtensionManagementPolicyUpdater( |
| 158 | policy::MockConfigurationPolicyProvider* provider); |
| 159 | ~ExtensionManagementPolicyUpdater() override; |
| 160 | |
| 161 | private: |
| 162 | policy::MockConfigurationPolicyProvider* provider_; |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 163 | std::unique_ptr<policy::PolicyBundle> policies_; |
binjin | e6b58b5 | 2014-10-31 01:55:57 | [diff] [blame] | 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPolicyUpdater); |
| 166 | }; |
| 167 | |
binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame] | 168 | } // namespace extensions |
| 169 | |
| 170 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_ |