blob: cda5537d51210484f1bb16cded2db3bbdb95a59f [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();
28 virtual ~ExtensionManagementPrefUpdaterBase();
29
binjin6a6eb152014-09-24 18:36:3430 // Helper functions for per extension settings.
31 void UnsetPerExtensionSettings(const ExtensionId& id);
32 void ClearPerExtensionSettings(const ExtensionId& id);
33
binjinb2454382014-09-22 15:17:4334 // 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);
binjine6b58b52014-10-31 01:55:5753 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
nrpeter2362e7e2017-05-10 17:21:2664 // 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
nrpeter40e16382017-04-13 17:34:5869 // Helper functions for 'runtime_blocked_hosts' manipulation. |prefix| can be
70 // kWildCard or a valid extension ID.
Devlin Cronin7e0f41ff2018-05-16 17:19:3671 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);
nrpeter40e16382017-04-13 17:34:5876
Nick Peterson87ecb102018-10-16 04:55:0177 // 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
binjine6b58b52014-10-31 01:55:5785 // Helper functions for 'allowed_permissions' manipulation. |id| must be a
binjin8e3d0182014-12-04 16:44:2886 // valid extension ID.
binjine6b58b52014-10-31 01:55:5787 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);
binjinb2454382014-09-22 15:17:4393
binjin8e3d0182014-12-04 16:44:2894 // 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
binjinb2454382014-09-22 15:17:43100 // 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.
dchengc963c7142016-04-08 03:55:22112 std::unique_ptr<base::DictionaryValue> TakePref();
binjinb2454382014-09-22 15:17:43113
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
dchengc963c7142016-04-08 03:55:22120 std::unique_ptr<base::DictionaryValue> pref_;
binjinb2454382014-09-22 15:17:43121
122 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase);
123};
124
125// A helper class to manipulate the extension management preference in unit
126// tests.
127template <class TestingPrefService>
128class 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);
binjine6b58b52014-10-31 01:55:57135 const base::DictionaryValue* dict_value = nullptr;
136 if (pref_value && pref_value->GetAsDictionary(&dict_value))
binjinb2454382014-09-22 15:17:43137 SetPref(dict_value->DeepCopy());
binjine6b58b52014-10-31 01:55:57138 else
binjinb2454382014-09-22 15:17:43139 SetPref(new base::DictionaryValue);
binjinb2454382014-09-22 15:17:43140 }
141
142 virtual ~ExtensionManagementPrefUpdater() {
vabr8684c9a2017-03-29 13:14:57143 service_->SetManagedPref(pref_names::kExtensionManagement, TakePref());
binjinb2454382014-09-22 15:17:43144 }
145
146 private:
147 TestingPrefService* service_;
148
149 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater);
150};
151
binjine6b58b52014-10-31 01:55:57152// A helper class to manipulate the extension management policy in browser
153// tests.
154class ExtensionManagementPolicyUpdater
155 : public ExtensionManagementPrefUpdaterBase {
156 public:
157 explicit ExtensionManagementPolicyUpdater(
158 policy::MockConfigurationPolicyProvider* provider);
159 ~ExtensionManagementPolicyUpdater() override;
160
161 private:
162 policy::MockConfigurationPolicyProvider* provider_;
dchengc963c7142016-04-08 03:55:22163 std::unique_ptr<policy::PolicyBundle> policies_;
binjine6b58b52014-10-31 01:55:57164
165 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPolicyUpdater);
166};
167
binjinb2454382014-09-22 15:17:43168} // namespace extensions
169
170#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_