blob: 7733efe66cbd162a0b2c8823ca5f232bb38ab5bc [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
dcheng66c7a4c2016-09-14 05:49:5810#include "base/memory/ptr_util.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() {
39}
40
binjine6b58b52014-10-31 01:55:5741// Helper functions for per extension settings ---------------------------------
42
binjin6a6eb152014-09-24 18:36:3443void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
44 const ExtensionId& id) {
45 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:2846 pref_->RemoveWithoutPathExpansion(id, nullptr);
binjin6a6eb152014-09-24 18:36:3447}
48
49void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
50 const ExtensionId& id) {
51 DCHECK(crx_file::id_util::IdIsValid(id));
vabr9984ea62017-04-10 11:33:4952 pref_->SetWithoutPathExpansion(id, base::MakeUnique<base::DictionaryValue>());
binjin6a6eb152014-09-24 18:36:3453}
54
binjine6b58b52014-10-31 01:55:5755// Helper functions for 'installation_mode' manipulation -----------------------
56
binjinb2454382014-09-22 15:17:4357void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
58 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
59 value ? schema::kBlocked : schema::kAllowed);
60}
61
62void ExtensionManagementPrefUpdaterBase::
63 ClearInstallationModesForIndividualExtensions() {
rdevlin.cronin165732a42016-07-18 22:25:0864 for (base::DictionaryValue::Iterator it(*pref_); !it.IsAtEnd();
binjinb2454382014-09-22 15:17:4365 it.Advance()) {
jdoerriedc72ee942016-12-07 15:43:2866 DCHECK(it.value().IsType(base::Value::Type::DICTIONARY));
binjinb2454382014-09-22 15:17:4367 if (it.key() != schema::kWildcard) {
68 DCHECK(crx_file::id_util::IdIsValid(it.key()));
binjin8e3d0182014-12-04 16:44:2869 pref_->Remove(make_path(it.key(), schema::kInstallationMode), nullptr);
70 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4371 }
72 }
73}
74
75void
76ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
77 const ExtensionId& id,
78 bool allowed) {
79 DCHECK(crx_file::id_util::IdIsValid(id));
80 pref_->SetString(make_path(id, schema::kInstallationMode),
81 allowed ? schema::kAllowed : schema::kBlocked);
binjin8e3d0182014-12-04 16:44:2882 pref_->Remove(make_path(id, schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4383}
84
85void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
86 const ExtensionId& id,
87 const std::string& update_url,
88 bool forced) {
89 DCHECK(crx_file::id_util::IdIsValid(id));
90 pref_->SetString(make_path(id, schema::kInstallationMode),
91 forced ? schema::kForceInstalled : schema::kNormalInstalled);
92 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
93}
94
binjine6b58b52014-10-31 01:55:5795// Helper functions for 'install_sources' manipulation -------------------------
96
binjinb2454382014-09-22 15:17:4397void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
binjin8e3d0182014-12-04 16:44:2898 pref_->Remove(kInstallSourcesPath, nullptr);
binjinb2454382014-09-22 15:17:4399}
100
101void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
102 ClearList(kInstallSourcesPath);
103}
104
105void ExtensionManagementPrefUpdaterBase::AddInstallSource(
106 const std::string& install_source) {
107 AddStringToList(kInstallSourcesPath, install_source);
108}
109
110void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
111 const std::string& install_source) {
112 RemoveStringFromList(kInstallSourcesPath, install_source);
113}
114
binjine6b58b52014-10-31 01:55:57115// Helper functions for 'allowed_types' manipulation ---------------------------
116
binjinb2454382014-09-22 15:17:43117void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
binjin8e3d0182014-12-04 16:44:28118 pref_->Remove(kAllowedTypesPath, nullptr);
binjinb2454382014-09-22 15:17:43119}
120
121void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
122 ClearList(kAllowedTypesPath);
123}
124
125void ExtensionManagementPrefUpdaterBase::AddAllowedType(
126 const std::string& allowed_type) {
127 AddStringToList(kAllowedTypesPath, allowed_type);
128}
129
binjine6b58b52014-10-31 01:55:57130void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
131 const std::string& allowed_type) {
132 RemoveStringFromList(kAllowedTypesPath, allowed_type);
133}
134
135// Helper functions for 'blocked_permissions' manipulation ---------------------
136
137void ExtensionManagementPrefUpdaterBase::UnsetBlockedPermissions(
138 const std::string& prefix) {
139 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
binjin8e3d0182014-12-04 16:44:28140 pref_->Remove(make_path(prefix, schema::kBlockedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57141}
142
143void ExtensionManagementPrefUpdaterBase::ClearBlockedPermissions(
144 const std::string& prefix) {
145 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
146 ClearList(make_path(prefix, schema::kBlockedPermissions));
147}
148
149void ExtensionManagementPrefUpdaterBase::AddBlockedPermission(
150 const std::string& prefix,
151 const std::string& permission) {
152 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
153 AddStringToList(make_path(prefix, schema::kBlockedPermissions), permission);
154}
155
156void ExtensionManagementPrefUpdaterBase::RemoveBlockedPermission(
157 const std::string& prefix,
158 const std::string& permission) {
159 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
160 RemoveStringFromList(make_path(prefix, schema::kBlockedPermissions),
161 permission);
162}
163
164// Helper functions for 'allowed_permissions' manipulation ---------------------
165
166void ExtensionManagementPrefUpdaterBase::UnsetAllowedPermissions(
167 const std::string& id) {
168 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:28169 pref_->Remove(make_path(id, schema::kAllowedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57170}
171
172void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
173 const std::string& id) {
174 DCHECK(crx_file::id_util::IdIsValid(id));
175 ClearList(make_path(id, schema::kAllowedPermissions));
176}
177
178void ExtensionManagementPrefUpdaterBase::AddAllowedPermission(
179 const std::string& id,
180 const std::string& permission) {
181 DCHECK(crx_file::id_util::IdIsValid(id));
182 AddStringToList(make_path(id, schema::kAllowedPermissions), permission);
183}
184
185void ExtensionManagementPrefUpdaterBase::RemoveAllowedPermission(
186 const std::string& id,
187 const std::string& permission) {
188 DCHECK(crx_file::id_util::IdIsValid(id));
189 RemoveStringFromList(make_path(id, schema::kAllowedPermissions), permission);
190}
191
binjin8e3d0182014-12-04 16:44:28192// Helper functions for 'minimum_version_required' manipulation ----------------
193
194void ExtensionManagementPrefUpdaterBase::SetMinimumVersionRequired(
195 const std::string& id,
196 const std::string& version) {
197 DCHECK(crx_file::id_util::IdIsValid(id));
198 pref_->SetString(make_path(id, schema::kMinimumVersionRequired), version);
199}
200
201void ExtensionManagementPrefUpdaterBase::UnsetMinimumVersionRequired(
202 const std::string& id) {
203 DCHECK(crx_file::id_util::IdIsValid(id));
204 pref_->Remove(make_path(id, schema::kMinimumVersionRequired), nullptr);
205}
206
binjine6b58b52014-10-31 01:55:57207// Expose a read-only preference to user ---------------------------------------
208
binjinb2454382014-09-22 15:17:43209const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
210 return pref_.get();
211}
212
binjine6b58b52014-10-31 01:55:57213// Private section functions ---------------------------------------------------
214
binjinb2454382014-09-22 15:17:43215void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
216 pref_.reset(pref);
217}
218
dchengc963c7142016-04-08 03:55:22219std::unique_ptr<base::DictionaryValue>
binjinb2454382014-09-22 15:17:43220ExtensionManagementPrefUpdaterBase::TakePref() {
dcheng1fc00f12015-12-26 22:18:03221 return std::move(pref_);
binjinb2454382014-09-22 15:17:43222}
223
binjinb2454382014-09-22 15:17:43224void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
vabr9984ea62017-04-10 11:33:49225 pref_->Set(path, base::MakeUnique<base::ListValue>());
binjinb2454382014-09-22 15:17:43226}
227
228void ExtensionManagementPrefUpdaterBase::AddStringToList(
229 const std::string& path,
230 const std::string& str) {
vabr9984ea62017-04-10 11:33:49231 base::ListValue* list_value_weak = nullptr;
232 if (!pref_->GetList(path, &list_value_weak)) {
233 auto list_value = base::MakeUnique<base::ListValue>();
234 list_value_weak = list_value.get();
235 pref_->Set(path, std::move(list_value));
binjinb2454382014-09-22 15:17:43236 }
vabr9984ea62017-04-10 11:33:49237 CHECK(
238 list_value_weak->AppendIfNotPresent(base::MakeUnique<base::Value>(str)));
binjinb2454382014-09-22 15:17:43239}
240
241void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
242 const std::string& path,
243 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28244 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43245 if (pref_->GetList(path, &list_value))
jdoerrie122c4da2017-03-06 11:12:04246 CHECK(list_value->Remove(base::Value(str), nullptr));
binjinb2454382014-09-22 15:17:43247}
248
binjine6b58b52014-10-31 01:55:57249// ExtensionManagementPolicyUpdater --------------------------------------------
250
251ExtensionManagementPolicyUpdater::ExtensionManagementPolicyUpdater(
252 policy::MockConfigurationPolicyProvider* policy_provider)
253 : provider_(policy_provider), policies_(new policy::PolicyBundle) {
254 policies_->CopyFrom(provider_->policies());
255 const base::Value* policy_value =
256 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
257 std::string()))
258 .GetValue(policy::key::kExtensionSettings);
259 const base::DictionaryValue* dict_value = nullptr;
260 if (policy_value && policy_value->GetAsDictionary(&dict_value))
261 SetPref(dict_value->DeepCopy());
262 else
263 SetPref(new base::DictionaryValue);
264}
265
266ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
dcheng3b344bc22016-05-10 02:26:09267 policies_
268 ->Get(
269 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()))
binjine6b58b52014-10-31 01:55:57270 .Set(policy::key::kExtensionSettings, policy::POLICY_LEVEL_MANDATORY,
dcheng3b344bc22016-05-10 02:26:09271 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, TakePref(),
272 nullptr);
dcheng1fc00f12015-12-26 22:18:03273 provider_->UpdatePolicy(std::move(policies_));
binjine6b58b52014-10-31 01:55:57274}
275
binjinb2454382014-09-22 15:17:43276} // namespace extensions