blob: 2f6743b7965a088b9dad8ef58e2ec64aa3d12545 [file] [log] [blame]
binjinb2454382014-09-22 15:17:431// 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
dchengc963c7142016-04-08 03:55:228#include <memory>
binjinb2454382014-09-22 15:17:439#include <string>
10
11#include "base/macros.h"
binjinb2454382014-09-22 15:17:4312#include "base/values.h"
13#include "chrome/browser/extensions/extension_management_constants.h"
14#include "extensions/browser/pref_names.h"
rdevlin.cronin0670b562016-07-02 02:05:4315#include "extensions/common/extension_id.h"
binjinb2454382014-09-22 15:17:4316
binjine6b58b52014-10-31 01:55:5717namespace policy {
18class MockConfigurationPolicyProvider;
19class PolicyBundle;
20} // namespace policy
21
binjinb2454382014-09-22 15:17:4322namespace extensions {
23
24// Base class for essential routines on preference manipulation.
25class ExtensionManagementPrefUpdaterBase {
26 public:
27 ExtensionManagementPrefUpdaterBase();
Peter Boström53c6c5952021-09-17 09:41:2628
29 ExtensionManagementPrefUpdaterBase(
30 const ExtensionManagementPrefUpdaterBase&) = delete;
31 ExtensionManagementPrefUpdaterBase& operator=(
32 const ExtensionManagementPrefUpdaterBase&) = delete;
33
binjinb2454382014-09-22 15:17:4334 virtual ~ExtensionManagementPrefUpdaterBase();
35
binjin6a6eb152014-09-24 18:36:3436 // Helper functions for per extension settings.
37 void UnsetPerExtensionSettings(const ExtensionId& id);
38 void ClearPerExtensionSettings(const ExtensionId& id);
39
binjinb2454382014-09-22 15:17:4340 // Helper functions for 'installation_mode' manipulation.
[email protected]1bae029f2020-06-30 14:22:3341 void SetBlocklistedByDefault(bool value);
binjinb2454382014-09-22 15:17:4342 void ClearInstallationModesForIndividualExtensions();
43 void SetIndividualExtensionInstallationAllowed(const ExtensionId& id,
44 bool allowed);
45 void SetIndividualExtensionAutoInstalled(const ExtensionId& id,
46 const std::string& update_url,
47 bool forced);
48
49 // Helper functions for 'install_sources' manipulation.
50 void UnsetInstallSources();
51 void ClearInstallSources();
52 void AddInstallSource(const std::string& install_source);
53 void RemoveInstallSource(const std::string& install_source);
54
55 // Helper functions for 'allowed_types' manipulation.
56 void UnsetAllowedTypes();
57 void ClearAllowedTypes();
58 void AddAllowedType(const std::string& allowed_type);
binjine6b58b52014-10-31 01:55:5759 void RemoveAllowedType(const std::string& allowed_type);
60
61 // Helper functions for 'blocked_permissions' manipulation. |prefix| can be
62 // kWildCard or a valid extension ID.
63 void UnsetBlockedPermissions(const std::string& prefix);
64 void ClearBlockedPermissions(const std::string& prefix);
65 void AddBlockedPermission(const std::string& prefix,
66 const std::string& permission);
67 void RemoveBlockedPermission(const std::string& prefix,
68 const std::string& permission);
69
nrpeter2362e7e2017-05-10 17:21:2670 // Helper function for 'blocked_install_message' manipulation.
71 // |id| is extension ID.
72 void SetBlockedInstallMessage(const ExtensionId& id,
73 const std::string& custom_error);
74
nrpeter40e16382017-04-13 17:34:5875 // Helper functions for 'runtime_blocked_hosts' manipulation. |prefix| can be
76 // kWildCard or a valid extension ID.
Devlin Cronin7e0f41ff2018-05-16 17:19:3677 void UnsetPolicyBlockedHosts(const std::string& prefix);
78 void ClearPolicyBlockedHosts(const std::string& prefix);
79 void AddPolicyBlockedHost(const std::string& prefix, const std::string& host);
80 void RemovePolicyBlockedHost(const std::string& prefix,
81 const std::string& host);
nrpeter40e16382017-04-13 17:34:5882
Nick Peterson87ecb102018-10-16 04:55:0183 // Helper functions for 'runtime_allowed_hosts' manipulation. |prefix| can be
84 // kWildCard or a valid extension ID.
85 void UnsetPolicyAllowedHosts(const std::string& prefix);
86 void ClearPolicyAllowedHosts(const std::string& prefix);
87 void AddPolicyAllowedHost(const std::string& prefix, const std::string& host);
88 void RemovePolicyAllowedHost(const std::string& prefix,
89 const std::string& host);
90
binjine6b58b52014-10-31 01:55:5791 // Helper functions for 'allowed_permissions' manipulation. |id| must be a
binjin8e3d0182014-12-04 16:44:2892 // valid extension ID.
binjine6b58b52014-10-31 01:55:5793 void UnsetAllowedPermissions(const std::string& id);
94 void ClearAllowedPermissions(const std::string& id);
95 void AddAllowedPermission(const std::string& id,
96 const std::string& permission);
97 void RemoveAllowedPermission(const std::string& id,
98 const std::string& permission);
binjinb2454382014-09-22 15:17:4399
binjin8e3d0182014-12-04 16:44:28100 // Helper functions for 'minimum_version_required' manipulation. |id| must be
101 // a valid extension ID.
102 void SetMinimumVersionRequired(const std::string& id,
103 const std::string& version);
104 void UnsetMinimumVersionRequired(const std::string& id);
105
binjinb2454382014-09-22 15:17:43106 // Expose a read-only preference to user.
107 const base::DictionaryValue* GetPref();
108
109 protected:
110 // Set the preference with |pref|, pass the ownership of it as well.
111 // This function must be called before accessing publicly exposed functions,
112 // for example in constructor of subclass.
113 void SetPref(base::DictionaryValue* pref);
114
115 // Take the preference. Caller takes ownership of it as well.
116 // This function must be called after accessing publicly exposed functions,
117 // for example in destructor of subclass.
dchengc963c7142016-04-08 03:55:22118 std::unique_ptr<base::DictionaryValue> TakePref();
binjinb2454382014-09-22 15:17:43119
120 private:
121 // Helper functions for manipulating sub properties like list of strings.
122 void ClearList(const std::string& path);
123 void AddStringToList(const std::string& path, const std::string& str);
124 void RemoveStringFromList(const std::string& path, const std::string& str);
125
dchengc963c7142016-04-08 03:55:22126 std::unique_ptr<base::DictionaryValue> pref_;
binjinb2454382014-09-22 15:17:43127};
128
129// A helper class to manipulate the extension management preference in unit
130// tests.
131template <class TestingPrefService>
132class ExtensionManagementPrefUpdater
133 : public ExtensionManagementPrefUpdaterBase {
134 public:
135 explicit ExtensionManagementPrefUpdater(TestingPrefService* service)
136 : service_(service) {
137 const base::Value* pref_value =
138 service_->GetManagedPref(pref_names::kExtensionManagement);
binjine6b58b52014-10-31 01:55:57139 const base::DictionaryValue* dict_value = nullptr;
140 if (pref_value && pref_value->GetAsDictionary(&dict_value))
binjinb2454382014-09-22 15:17:43141 SetPref(dict_value->DeepCopy());
binjine6b58b52014-10-31 01:55:57142 else
binjinb2454382014-09-22 15:17:43143 SetPref(new base::DictionaryValue);
binjinb2454382014-09-22 15:17:43144 }
145
Peter Boström53c6c5952021-09-17 09:41:26146 ExtensionManagementPrefUpdater(const ExtensionManagementPrefUpdater&) =
147 delete;
148 ExtensionManagementPrefUpdater& operator=(
149 const ExtensionManagementPrefUpdater&) = delete;
150
binjinb2454382014-09-22 15:17:43151 virtual ~ExtensionManagementPrefUpdater() {
vabr8684c9a2017-03-29 13:14:57152 service_->SetManagedPref(pref_names::kExtensionManagement, TakePref());
binjinb2454382014-09-22 15:17:43153 }
154
155 private:
156 TestingPrefService* service_;
binjinb2454382014-09-22 15:17:43157};
158
binjine6b58b52014-10-31 01:55:57159// A helper class to manipulate the extension management policy in browser
160// tests.
161class ExtensionManagementPolicyUpdater
162 : public ExtensionManagementPrefUpdaterBase {
163 public:
164 explicit ExtensionManagementPolicyUpdater(
165 policy::MockConfigurationPolicyProvider* provider);
Peter Boström53c6c5952021-09-17 09:41:26166
167 ExtensionManagementPolicyUpdater(const ExtensionManagementPolicyUpdater&) =
168 delete;
169 ExtensionManagementPolicyUpdater& operator=(
170 const ExtensionManagementPolicyUpdater&) = delete;
171
binjine6b58b52014-10-31 01:55:57172 ~ExtensionManagementPolicyUpdater() override;
173
174 private:
175 policy::MockConfigurationPolicyProvider* provider_;
dchengc963c7142016-04-08 03:55:22176 std::unique_ptr<policy::PolicyBundle> policies_;
binjine6b58b52014-10-31 01:55:57177};
178
binjinb2454382014-09-22 15:17:43179} // namespace extensions
180
181#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_