blob: b33464f6b7c080a7982100a22cb0526e198dd05f [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
binjinb2454382014-09-22 15:17:4310#include "components/crx_file/id_util.h"
binjine6b58b52014-10-31 01:55:5711#include "components/policy/core/common/configuration_policy_provider.h"
12#include "components/policy/core/common/mock_configuration_policy_provider.h"
13#include "components/policy/core/common/policy_bundle.h"
14#include "components/policy/core/common/policy_map.h"
15#include "components/policy/core/common/policy_namespace.h"
16#include "components/policy/core/common/policy_types.h"
brettw7d6ec2462015-01-07 13:00:5117#include "policy/policy_constants.h"
binjinb2454382014-09-22 15:17:4318
19namespace extensions {
20
21namespace schema = schema_constants;
22
23namespace {
24
binjine6b58b52014-10-31 01:55:5725const char kInstallSourcesPath[] = "*.install_sources";
26const char kAllowedTypesPath[] = "*.allowed_types";
27
ki.stfuf38f9312015-09-27 14:44:3728std::string make_path(const std::string& a, const std::string& b) {
binjinb2454382014-09-22 15:17:4329 return a + "." + b;
30}
31
binjinb2454382014-09-22 15:17:4332} // namespace
33
34ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
35}
36
37ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
38}
39
binjine6b58b52014-10-31 01:55:5740// Helper functions for per extension settings ---------------------------------
41
binjin6a6eb152014-09-24 18:36:3442void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
43 const ExtensionId& id) {
44 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:2845 pref_->RemoveWithoutPathExpansion(id, nullptr);
binjin6a6eb152014-09-24 18:36:3446}
47
48void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
49 const ExtensionId& id) {
50 DCHECK(crx_file::id_util::IdIsValid(id));
51 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue());
52}
53
binjine6b58b52014-10-31 01:55:5754// Helper functions for 'installation_mode' manipulation -----------------------
55
binjinb2454382014-09-22 15:17:4356void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
57 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
58 value ? schema::kBlocked : schema::kAllowed);
59}
60
61void ExtensionManagementPrefUpdaterBase::
62 ClearInstallationModesForIndividualExtensions() {
63 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd();
64 it.Advance()) {
65 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
66 if (it.key() != schema::kWildcard) {
67 DCHECK(crx_file::id_util::IdIsValid(it.key()));
binjin8e3d0182014-12-04 16:44:2868 pref_->Remove(make_path(it.key(), schema::kInstallationMode), nullptr);
69 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4370 }
71 }
72}
73
74void
75ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
76 const ExtensionId& id,
77 bool allowed) {
78 DCHECK(crx_file::id_util::IdIsValid(id));
79 pref_->SetString(make_path(id, schema::kInstallationMode),
80 allowed ? schema::kAllowed : schema::kBlocked);
binjin8e3d0182014-12-04 16:44:2881 pref_->Remove(make_path(id, schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4382}
83
84void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
85 const ExtensionId& id,
86 const std::string& update_url,
87 bool forced) {
88 DCHECK(crx_file::id_util::IdIsValid(id));
89 pref_->SetString(make_path(id, schema::kInstallationMode),
90 forced ? schema::kForceInstalled : schema::kNormalInstalled);
91 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
92}
93
binjine6b58b52014-10-31 01:55:5794// Helper functions for 'install_sources' manipulation -------------------------
95
binjinb2454382014-09-22 15:17:4396void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
binjin8e3d0182014-12-04 16:44:2897 pref_->Remove(kInstallSourcesPath, nullptr);
binjinb2454382014-09-22 15:17:4398}
99
100void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
101 ClearList(kInstallSourcesPath);
102}
103
104void ExtensionManagementPrefUpdaterBase::AddInstallSource(
105 const std::string& install_source) {
106 AddStringToList(kInstallSourcesPath, install_source);
107}
108
109void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
110 const std::string& install_source) {
111 RemoveStringFromList(kInstallSourcesPath, install_source);
112}
113
binjine6b58b52014-10-31 01:55:57114// Helper functions for 'allowed_types' manipulation ---------------------------
115
binjinb2454382014-09-22 15:17:43116void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
binjin8e3d0182014-12-04 16:44:28117 pref_->Remove(kAllowedTypesPath, nullptr);
binjinb2454382014-09-22 15:17:43118}
119
120void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
121 ClearList(kAllowedTypesPath);
122}
123
124void ExtensionManagementPrefUpdaterBase::AddAllowedType(
125 const std::string& allowed_type) {
126 AddStringToList(kAllowedTypesPath, allowed_type);
127}
128
binjine6b58b52014-10-31 01:55:57129void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
130 const std::string& allowed_type) {
131 RemoveStringFromList(kAllowedTypesPath, allowed_type);
132}
133
134// Helper functions for 'blocked_permissions' manipulation ---------------------
135
136void ExtensionManagementPrefUpdaterBase::UnsetBlockedPermissions(
137 const std::string& prefix) {
138 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
binjin8e3d0182014-12-04 16:44:28139 pref_->Remove(make_path(prefix, schema::kBlockedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57140}
141
142void ExtensionManagementPrefUpdaterBase::ClearBlockedPermissions(
143 const std::string& prefix) {
144 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
145 ClearList(make_path(prefix, schema::kBlockedPermissions));
146}
147
148void ExtensionManagementPrefUpdaterBase::AddBlockedPermission(
149 const std::string& prefix,
150 const std::string& permission) {
151 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
152 AddStringToList(make_path(prefix, schema::kBlockedPermissions), permission);
153}
154
155void ExtensionManagementPrefUpdaterBase::RemoveBlockedPermission(
156 const std::string& prefix,
157 const std::string& permission) {
158 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
159 RemoveStringFromList(make_path(prefix, schema::kBlockedPermissions),
160 permission);
161}
162
163// Helper functions for 'allowed_permissions' manipulation ---------------------
164
165void ExtensionManagementPrefUpdaterBase::UnsetAllowedPermissions(
166 const std::string& id) {
167 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:28168 pref_->Remove(make_path(id, schema::kAllowedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57169}
170
171void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
172 const std::string& id) {
173 DCHECK(crx_file::id_util::IdIsValid(id));
174 ClearList(make_path(id, schema::kAllowedPermissions));
175}
176
177void ExtensionManagementPrefUpdaterBase::AddAllowedPermission(
178 const std::string& id,
179 const std::string& permission) {
180 DCHECK(crx_file::id_util::IdIsValid(id));
181 AddStringToList(make_path(id, schema::kAllowedPermissions), permission);
182}
183
184void ExtensionManagementPrefUpdaterBase::RemoveAllowedPermission(
185 const std::string& id,
186 const std::string& permission) {
187 DCHECK(crx_file::id_util::IdIsValid(id));
188 RemoveStringFromList(make_path(id, schema::kAllowedPermissions), permission);
189}
190
binjin8e3d0182014-12-04 16:44:28191// Helper functions for 'minimum_version_required' manipulation ----------------
192
193void ExtensionManagementPrefUpdaterBase::SetMinimumVersionRequired(
194 const std::string& id,
195 const std::string& version) {
196 DCHECK(crx_file::id_util::IdIsValid(id));
197 pref_->SetString(make_path(id, schema::kMinimumVersionRequired), version);
198}
199
200void ExtensionManagementPrefUpdaterBase::UnsetMinimumVersionRequired(
201 const std::string& id) {
202 DCHECK(crx_file::id_util::IdIsValid(id));
203 pref_->Remove(make_path(id, schema::kMinimumVersionRequired), nullptr);
204}
205
binjine6b58b52014-10-31 01:55:57206// Expose a read-only preference to user ---------------------------------------
207
binjinb2454382014-09-22 15:17:43208const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
209 return pref_.get();
210}
211
binjine6b58b52014-10-31 01:55:57212// Private section functions ---------------------------------------------------
213
binjinb2454382014-09-22 15:17:43214void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
215 pref_.reset(pref);
216}
217
218scoped_ptr<base::DictionaryValue>
219ExtensionManagementPrefUpdaterBase::TakePref() {
dcheng1fc00f12015-12-26 22:18:03220 return std::move(pref_);
binjinb2454382014-09-22 15:17:43221}
222
binjinb2454382014-09-22 15:17:43223void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
224 pref_->Set(path, new base::ListValue());
225}
226
227void ExtensionManagementPrefUpdaterBase::AddStringToList(
228 const std::string& path,
229 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28230 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43231 if (!pref_->GetList(path, &list_value)) {
232 list_value = new base::ListValue();
233 pref_->Set(path, list_value);
234 }
235 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
236}
237
238void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
239 const std::string& path,
240 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28241 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43242 if (pref_->GetList(path, &list_value))
binjin8e3d0182014-12-04 16:44:28243 CHECK(list_value->Remove(base::StringValue(str), nullptr));
binjinb2454382014-09-22 15:17:43244}
245
binjine6b58b52014-10-31 01:55:57246// ExtensionManagementPolicyUpdater --------------------------------------------
247
248ExtensionManagementPolicyUpdater::ExtensionManagementPolicyUpdater(
249 policy::MockConfigurationPolicyProvider* policy_provider)
250 : provider_(policy_provider), policies_(new policy::PolicyBundle) {
251 policies_->CopyFrom(provider_->policies());
252 const base::Value* policy_value =
253 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
254 std::string()))
255 .GetValue(policy::key::kExtensionSettings);
256 const base::DictionaryValue* dict_value = nullptr;
257 if (policy_value && policy_value->GetAsDictionary(&dict_value))
258 SetPref(dict_value->DeepCopy());
259 else
260 SetPref(new base::DictionaryValue);
261}
262
263ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
264 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
265 std::string()))
266 .Set(policy::key::kExtensionSettings, policy::POLICY_LEVEL_MANDATORY,
fhorschig64834b712015-09-21 14:20:23267 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
268 TakePref().release(), nullptr);
dcheng1fc00f12015-12-26 22:18:03269 provider_->UpdatePolicy(std::move(policies_));
binjine6b58b52014-10-31 01:55:57270}
271
binjinb2454382014-09-22 15:17:43272} // namespace extensions