blob: 580e891323eaf37ecb0ab5eeebba7bfff3f382ba [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#include "chrome/browser/extensions/extension_management_test_util.h"
6
binjine6b58b52014-10-31 01:55:577#include <string>
dcheng1fc00f12015-12-26 22:18:038#include <utility>
binjine6b58b52014-10-31 01:55:579
Takashi Toyoshima52f24672019-03-05 05:36:3610#include "base/run_loop.h"
binjinb2454382014-09-22 15:17:4311#include "components/crx_file/id_util.h"
binjine6b58b52014-10-31 01:55:5712#include "components/policy/core/common/configuration_policy_provider.h"
13#include "components/policy/core/common/mock_configuration_policy_provider.h"
14#include "components/policy/core/common/policy_bundle.h"
15#include "components/policy/core/common/policy_map.h"
16#include "components/policy/core/common/policy_namespace.h"
17#include "components/policy/core/common/policy_types.h"
brettw39d6ba42016-08-24 16:56:3818#include "components/policy/policy_constants.h"
binjinb2454382014-09-22 15:17:4319
20namespace extensions {
21
22namespace schema = schema_constants;
23
24namespace {
25
binjine6b58b52014-10-31 01:55:5726const char kInstallSourcesPath[] = "*.install_sources";
27const char kAllowedTypesPath[] = "*.allowed_types";
28
ki.stfuf38f9312015-09-27 14:44:3729std::string make_path(const std::string& a, const std::string& b) {
binjinb2454382014-09-22 15:17:4330 return a + "." + b;
31}
32
binjinb2454382014-09-22 15:17:4333} // namespace
34
35ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
36}
37
38ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
Takashi Toyoshima52f24672019-03-05 05:36:3639 // Make asynchronous calls finished to deliver all preference changes to the
40 // NetworkService and extension processes.
41 base::RunLoop().RunUntilIdle();
binjinb2454382014-09-22 15:17:4342}
43
binjine6b58b52014-10-31 01:55:5744// Helper functions for per extension settings ---------------------------------
45
binjin6a6eb152014-09-24 18:36:3446void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
47 const ExtensionId& id) {
48 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:2849 pref_->RemoveWithoutPathExpansion(id, nullptr);
binjin6a6eb152014-09-24 18:36:3450}
51
52void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
53 const ExtensionId& id) {
54 DCHECK(crx_file::id_util::IdIsValid(id));
Jinho Bangb5216cec2018-01-17 19:43:1155 pref_->SetWithoutPathExpansion(id, std::make_unique<base::DictionaryValue>());
binjin6a6eb152014-09-24 18:36:3456}
57
binjine6b58b52014-10-31 01:55:5758// Helper functions for 'installation_mode' manipulation -----------------------
59
binjinb2454382014-09-22 15:17:4360void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
61 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
62 value ? schema::kBlocked : schema::kAllowed);
63}
64
65void ExtensionManagementPrefUpdaterBase::
66 ClearInstallationModesForIndividualExtensions() {
rdevlin.cronin165732a42016-07-18 22:25:0867 for (base::DictionaryValue::Iterator it(*pref_); !it.IsAtEnd();
binjinb2454382014-09-22 15:17:4368 it.Advance()) {
jdoerrie1f536b22017-10-23 17:15:1169 DCHECK(it.value().is_dict());
binjinb2454382014-09-22 15:17:4370 if (it.key() != schema::kWildcard) {
71 DCHECK(crx_file::id_util::IdIsValid(it.key()));
binjin8e3d0182014-12-04 16:44:2872 pref_->Remove(make_path(it.key(), schema::kInstallationMode), nullptr);
73 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4374 }
75 }
76}
77
78void
79ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
80 const ExtensionId& id,
81 bool allowed) {
82 DCHECK(crx_file::id_util::IdIsValid(id));
83 pref_->SetString(make_path(id, schema::kInstallationMode),
84 allowed ? schema::kAllowed : schema::kBlocked);
binjin8e3d0182014-12-04 16:44:2885 pref_->Remove(make_path(id, schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4386}
87
88void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
89 const ExtensionId& id,
90 const std::string& update_url,
91 bool forced) {
92 DCHECK(crx_file::id_util::IdIsValid(id));
93 pref_->SetString(make_path(id, schema::kInstallationMode),
94 forced ? schema::kForceInstalled : schema::kNormalInstalled);
95 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
96}
97
binjine6b58b52014-10-31 01:55:5798// Helper functions for 'install_sources' manipulation -------------------------
99
binjinb2454382014-09-22 15:17:43100void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
binjin8e3d0182014-12-04 16:44:28101 pref_->Remove(kInstallSourcesPath, nullptr);
binjinb2454382014-09-22 15:17:43102}
103
104void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
105 ClearList(kInstallSourcesPath);
106}
107
108void ExtensionManagementPrefUpdaterBase::AddInstallSource(
109 const std::string& install_source) {
110 AddStringToList(kInstallSourcesPath, install_source);
111}
112
113void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
114 const std::string& install_source) {
115 RemoveStringFromList(kInstallSourcesPath, install_source);
116}
117
binjine6b58b52014-10-31 01:55:57118// Helper functions for 'allowed_types' manipulation ---------------------------
119
binjinb2454382014-09-22 15:17:43120void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
binjin8e3d0182014-12-04 16:44:28121 pref_->Remove(kAllowedTypesPath, nullptr);
binjinb2454382014-09-22 15:17:43122}
123
124void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
125 ClearList(kAllowedTypesPath);
126}
127
128void ExtensionManagementPrefUpdaterBase::AddAllowedType(
129 const std::string& allowed_type) {
130 AddStringToList(kAllowedTypesPath, allowed_type);
131}
132
binjine6b58b52014-10-31 01:55:57133void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
134 const std::string& allowed_type) {
135 RemoveStringFromList(kAllowedTypesPath, allowed_type);
136}
137
138// Helper functions for 'blocked_permissions' manipulation ---------------------
139
140void ExtensionManagementPrefUpdaterBase::UnsetBlockedPermissions(
141 const std::string& prefix) {
142 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
binjin8e3d0182014-12-04 16:44:28143 pref_->Remove(make_path(prefix, schema::kBlockedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57144}
145
146void ExtensionManagementPrefUpdaterBase::ClearBlockedPermissions(
147 const std::string& prefix) {
148 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
149 ClearList(make_path(prefix, schema::kBlockedPermissions));
150}
151
152void ExtensionManagementPrefUpdaterBase::AddBlockedPermission(
153 const std::string& prefix,
154 const std::string& permission) {
155 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
156 AddStringToList(make_path(prefix, schema::kBlockedPermissions), permission);
157}
158
159void ExtensionManagementPrefUpdaterBase::RemoveBlockedPermission(
160 const std::string& prefix,
161 const std::string& permission) {
162 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
163 RemoveStringFromList(make_path(prefix, schema::kBlockedPermissions),
164 permission);
165}
166
nrpeter2362e7e2017-05-10 17:21:26167// Helper function for 'blocked_install_message' manipulation -----------------
168
169void ExtensionManagementPrefUpdaterBase::SetBlockedInstallMessage(
170 const ExtensionId& id,
171 const std::string& blocked_install_message) {
172 DCHECK(id == schema::kWildcard || crx_file::id_util::IdIsValid(id));
173 pref_->SetString(make_path(id, schema::kBlockedInstallMessage),
174 blocked_install_message);
175}
176
nrpeter40e16382017-04-13 17:34:58177// Helper functions for 'runtime_blocked_hosts' manipulation ------------------
178
Devlin Cronin7e0f41ff2018-05-16 17:19:36179void ExtensionManagementPrefUpdaterBase::UnsetPolicyBlockedHosts(
nrpeter40e16382017-04-13 17:34:58180 const std::string& prefix) {
181 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
Devlin Cronin7e0f41ff2018-05-16 17:19:36182 pref_->Remove(make_path(prefix, schema::kPolicyBlockedHosts), nullptr);
nrpeter40e16382017-04-13 17:34:58183}
184
Devlin Cronin7e0f41ff2018-05-16 17:19:36185void ExtensionManagementPrefUpdaterBase::ClearPolicyBlockedHosts(
nrpeter40e16382017-04-13 17:34:58186 const std::string& prefix) {
187 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
Devlin Cronin7e0f41ff2018-05-16 17:19:36188 ClearList(make_path(prefix, schema::kPolicyBlockedHosts));
nrpeter40e16382017-04-13 17:34:58189}
190
Devlin Cronin7e0f41ff2018-05-16 17:19:36191void ExtensionManagementPrefUpdaterBase::AddPolicyBlockedHost(
nrpeter40e16382017-04-13 17:34:58192 const std::string& prefix,
193 const std::string& host) {
194 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
Devlin Cronin7e0f41ff2018-05-16 17:19:36195 AddStringToList(make_path(prefix, schema::kPolicyBlockedHosts), host);
nrpeter40e16382017-04-13 17:34:58196}
197
Devlin Cronin7e0f41ff2018-05-16 17:19:36198void ExtensionManagementPrefUpdaterBase::RemovePolicyBlockedHost(
nrpeter40e16382017-04-13 17:34:58199 const std::string& prefix,
200 const std::string& host) {
201 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
Devlin Cronin7e0f41ff2018-05-16 17:19:36202 RemoveStringFromList(make_path(prefix, schema::kPolicyBlockedHosts), host);
nrpeter40e16382017-04-13 17:34:58203}
204
Nick Peterson87ecb102018-10-16 04:55:01205// Helper functions for 'runtime_allowed_hosts' manipulation ------------------
206
207void ExtensionManagementPrefUpdaterBase::UnsetPolicyAllowedHosts(
208 const std::string& prefix) {
209 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
210 pref_->Remove(make_path(prefix, schema::kPolicyAllowedHosts), nullptr);
211}
212
213void ExtensionManagementPrefUpdaterBase::ClearPolicyAllowedHosts(
214 const std::string& prefix) {
215 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
216 ClearList(make_path(prefix, schema::kPolicyAllowedHosts));
217}
218
219void ExtensionManagementPrefUpdaterBase::AddPolicyAllowedHost(
220 const std::string& prefix,
221 const std::string& host) {
222 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
223 AddStringToList(make_path(prefix, schema::kPolicyAllowedHosts), host);
224}
225
226void ExtensionManagementPrefUpdaterBase::RemovePolicyAllowedHost(
227 const std::string& prefix,
228 const std::string& host) {
229 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
230 RemoveStringFromList(make_path(prefix, schema::kPolicyAllowedHosts), host);
231}
232
binjine6b58b52014-10-31 01:55:57233// Helper functions for 'allowed_permissions' manipulation ---------------------
234
235void ExtensionManagementPrefUpdaterBase::UnsetAllowedPermissions(
236 const std::string& id) {
237 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:28238 pref_->Remove(make_path(id, schema::kAllowedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57239}
240
241void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
242 const std::string& id) {
243 DCHECK(crx_file::id_util::IdIsValid(id));
244 ClearList(make_path(id, schema::kAllowedPermissions));
245}
246
247void ExtensionManagementPrefUpdaterBase::AddAllowedPermission(
248 const std::string& id,
249 const std::string& permission) {
250 DCHECK(crx_file::id_util::IdIsValid(id));
251 AddStringToList(make_path(id, schema::kAllowedPermissions), permission);
252}
253
254void ExtensionManagementPrefUpdaterBase::RemoveAllowedPermission(
255 const std::string& id,
256 const std::string& permission) {
257 DCHECK(crx_file::id_util::IdIsValid(id));
258 RemoveStringFromList(make_path(id, schema::kAllowedPermissions), permission);
259}
260
binjin8e3d0182014-12-04 16:44:28261// Helper functions for 'minimum_version_required' manipulation ----------------
262
263void ExtensionManagementPrefUpdaterBase::SetMinimumVersionRequired(
264 const std::string& id,
265 const std::string& version) {
266 DCHECK(crx_file::id_util::IdIsValid(id));
267 pref_->SetString(make_path(id, schema::kMinimumVersionRequired), version);
268}
269
270void ExtensionManagementPrefUpdaterBase::UnsetMinimumVersionRequired(
271 const std::string& id) {
272 DCHECK(crx_file::id_util::IdIsValid(id));
273 pref_->Remove(make_path(id, schema::kMinimumVersionRequired), nullptr);
274}
275
binjine6b58b52014-10-31 01:55:57276// Expose a read-only preference to user ---------------------------------------
277
binjinb2454382014-09-22 15:17:43278const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
279 return pref_.get();
280}
281
binjine6b58b52014-10-31 01:55:57282// Private section functions ---------------------------------------------------
283
binjinb2454382014-09-22 15:17:43284void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
285 pref_.reset(pref);
286}
287
dchengc963c7142016-04-08 03:55:22288std::unique_ptr<base::DictionaryValue>
binjinb2454382014-09-22 15:17:43289ExtensionManagementPrefUpdaterBase::TakePref() {
dcheng1fc00f12015-12-26 22:18:03290 return std::move(pref_);
binjinb2454382014-09-22 15:17:43291}
292
binjinb2454382014-09-22 15:17:43293void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
Jinho Bangb5216cec2018-01-17 19:43:11294 pref_->Set(path, std::make_unique<base::ListValue>());
binjinb2454382014-09-22 15:17:43295}
296
297void ExtensionManagementPrefUpdaterBase::AddStringToList(
298 const std::string& path,
299 const std::string& str) {
vabr9984ea62017-04-10 11:33:49300 base::ListValue* list_value_weak = nullptr;
301 if (!pref_->GetList(path, &list_value_weak)) {
Jinho Bangb5216cec2018-01-17 19:43:11302 auto list_value = std::make_unique<base::ListValue>();
vabr9984ea62017-04-10 11:33:49303 list_value_weak = list_value.get();
304 pref_->Set(path, std::move(list_value));
binjinb2454382014-09-22 15:17:43305 }
vabr9984ea62017-04-10 11:33:49306 CHECK(
Jinho Bangb5216cec2018-01-17 19:43:11307 list_value_weak->AppendIfNotPresent(std::make_unique<base::Value>(str)));
binjinb2454382014-09-22 15:17:43308}
309
310void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
311 const std::string& path,
312 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28313 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43314 if (pref_->GetList(path, &list_value))
jdoerrie122c4da2017-03-06 11:12:04315 CHECK(list_value->Remove(base::Value(str), nullptr));
binjinb2454382014-09-22 15:17:43316}
317
binjine6b58b52014-10-31 01:55:57318// ExtensionManagementPolicyUpdater --------------------------------------------
319
320ExtensionManagementPolicyUpdater::ExtensionManagementPolicyUpdater(
321 policy::MockConfigurationPolicyProvider* policy_provider)
322 : provider_(policy_provider), policies_(new policy::PolicyBundle) {
323 policies_->CopyFrom(provider_->policies());
324 const base::Value* policy_value =
325 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
326 std::string()))
327 .GetValue(policy::key::kExtensionSettings);
328 const base::DictionaryValue* dict_value = nullptr;
329 if (policy_value && policy_value->GetAsDictionary(&dict_value))
330 SetPref(dict_value->DeepCopy());
331 else
332 SetPref(new base::DictionaryValue);
333}
334
335ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
dcheng3b344bc22016-05-10 02:26:09336 policies_
337 ->Get(
338 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()))
binjine6b58b52014-10-31 01:55:57339 .Set(policy::key::kExtensionSettings, policy::POLICY_LEVEL_MANDATORY,
dcheng3b344bc22016-05-10 02:26:09340 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, TakePref(),
341 nullptr);
dcheng1fc00f12015-12-26 22:18:03342 provider_->UpdatePolicy(std::move(policies_));
binjine6b58b52014-10-31 01:55:57343}
344
binjinb2454382014-09-22 15:17:43345} // namespace extensions