blob: 18763169d16130afebc6d227e0d88548928f2fb2 [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>
8
binjinb2454382014-09-22 15:17:439#include "components/crx_file/id_util.h"
binjine6b58b52014-10-31 01:55:5710#include "components/policy/core/common/configuration_policy_provider.h"
11#include "components/policy/core/common/mock_configuration_policy_provider.h"
12#include "components/policy/core/common/policy_bundle.h"
13#include "components/policy/core/common/policy_map.h"
14#include "components/policy/core/common/policy_namespace.h"
15#include "components/policy/core/common/policy_types.h"
16#include "policy/policy/policy_constants.h"
binjinb2454382014-09-22 15:17:4317
18namespace extensions {
19
20namespace schema = schema_constants;
21
22namespace {
23
binjine6b58b52014-10-31 01:55:5724const char kInstallSourcesPath[] = "*.install_sources";
25const char kAllowedTypesPath[] = "*.allowed_types";
26
binjinb2454382014-09-22 15:17:4327std::string make_path(std::string a, std::string b) {
28 return a + "." + b;
29}
30
binjinb2454382014-09-22 15:17:4331} // namespace
32
33ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
34}
35
36ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
37}
38
binjine6b58b52014-10-31 01:55:5739// Helper functions for per extension settings ---------------------------------
40
binjin6a6eb152014-09-24 18:36:3441void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
42 const ExtensionId& id) {
43 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:2844 pref_->RemoveWithoutPathExpansion(id, nullptr);
binjin6a6eb152014-09-24 18:36:3445}
46
47void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
48 const ExtensionId& id) {
49 DCHECK(crx_file::id_util::IdIsValid(id));
50 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue());
51}
52
binjine6b58b52014-10-31 01:55:5753// Helper functions for 'installation_mode' manipulation -----------------------
54
binjinb2454382014-09-22 15:17:4355void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
56 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
57 value ? schema::kBlocked : schema::kAllowed);
58}
59
60void ExtensionManagementPrefUpdaterBase::
61 ClearInstallationModesForIndividualExtensions() {
62 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd();
63 it.Advance()) {
64 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
65 if (it.key() != schema::kWildcard) {
66 DCHECK(crx_file::id_util::IdIsValid(it.key()));
binjin8e3d0182014-12-04 16:44:2867 pref_->Remove(make_path(it.key(), schema::kInstallationMode), nullptr);
68 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4369 }
70 }
71}
72
73void
74ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
75 const ExtensionId& id,
76 bool allowed) {
77 DCHECK(crx_file::id_util::IdIsValid(id));
78 pref_->SetString(make_path(id, schema::kInstallationMode),
79 allowed ? schema::kAllowed : schema::kBlocked);
binjin8e3d0182014-12-04 16:44:2880 pref_->Remove(make_path(id, schema::kUpdateUrl), nullptr);
binjinb2454382014-09-22 15:17:4381}
82
83void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
84 const ExtensionId& id,
85 const std::string& update_url,
86 bool forced) {
87 DCHECK(crx_file::id_util::IdIsValid(id));
88 pref_->SetString(make_path(id, schema::kInstallationMode),
89 forced ? schema::kForceInstalled : schema::kNormalInstalled);
90 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
91}
92
binjine6b58b52014-10-31 01:55:5793// Helper functions for 'install_sources' manipulation -------------------------
94
binjinb2454382014-09-22 15:17:4395void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
binjin8e3d0182014-12-04 16:44:2896 pref_->Remove(kInstallSourcesPath, nullptr);
binjinb2454382014-09-22 15:17:4397}
98
99void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
100 ClearList(kInstallSourcesPath);
101}
102
103void ExtensionManagementPrefUpdaterBase::AddInstallSource(
104 const std::string& install_source) {
105 AddStringToList(kInstallSourcesPath, install_source);
106}
107
108void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
109 const std::string& install_source) {
110 RemoveStringFromList(kInstallSourcesPath, install_source);
111}
112
binjine6b58b52014-10-31 01:55:57113// Helper functions for 'allowed_types' manipulation ---------------------------
114
binjinb2454382014-09-22 15:17:43115void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
binjin8e3d0182014-12-04 16:44:28116 pref_->Remove(kAllowedTypesPath, nullptr);
binjinb2454382014-09-22 15:17:43117}
118
119void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
120 ClearList(kAllowedTypesPath);
121}
122
123void ExtensionManagementPrefUpdaterBase::AddAllowedType(
124 const std::string& allowed_type) {
125 AddStringToList(kAllowedTypesPath, allowed_type);
126}
127
binjine6b58b52014-10-31 01:55:57128void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
129 const std::string& allowed_type) {
130 RemoveStringFromList(kAllowedTypesPath, allowed_type);
131}
132
133// Helper functions for 'blocked_permissions' manipulation ---------------------
134
135void ExtensionManagementPrefUpdaterBase::UnsetBlockedPermissions(
136 const std::string& prefix) {
137 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
binjin8e3d0182014-12-04 16:44:28138 pref_->Remove(make_path(prefix, schema::kBlockedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57139}
140
141void ExtensionManagementPrefUpdaterBase::ClearBlockedPermissions(
142 const std::string& prefix) {
143 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
144 ClearList(make_path(prefix, schema::kBlockedPermissions));
145}
146
147void ExtensionManagementPrefUpdaterBase::AddBlockedPermission(
148 const std::string& prefix,
149 const std::string& permission) {
150 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
151 AddStringToList(make_path(prefix, schema::kBlockedPermissions), permission);
152}
153
154void ExtensionManagementPrefUpdaterBase::RemoveBlockedPermission(
155 const std::string& prefix,
156 const std::string& permission) {
157 DCHECK(prefix == schema::kWildcard || crx_file::id_util::IdIsValid(prefix));
158 RemoveStringFromList(make_path(prefix, schema::kBlockedPermissions),
159 permission);
160}
161
162// Helper functions for 'allowed_permissions' manipulation ---------------------
163
164void ExtensionManagementPrefUpdaterBase::UnsetAllowedPermissions(
165 const std::string& id) {
166 DCHECK(crx_file::id_util::IdIsValid(id));
binjin8e3d0182014-12-04 16:44:28167 pref_->Remove(make_path(id, schema::kAllowedPermissions), nullptr);
binjine6b58b52014-10-31 01:55:57168}
169
170void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
171 const std::string& id) {
172 DCHECK(crx_file::id_util::IdIsValid(id));
173 ClearList(make_path(id, schema::kAllowedPermissions));
174}
175
176void ExtensionManagementPrefUpdaterBase::AddAllowedPermission(
177 const std::string& id,
178 const std::string& permission) {
179 DCHECK(crx_file::id_util::IdIsValid(id));
180 AddStringToList(make_path(id, schema::kAllowedPermissions), permission);
181}
182
183void ExtensionManagementPrefUpdaterBase::RemoveAllowedPermission(
184 const std::string& id,
185 const std::string& permission) {
186 DCHECK(crx_file::id_util::IdIsValid(id));
187 RemoveStringFromList(make_path(id, schema::kAllowedPermissions), permission);
188}
189
binjin8e3d0182014-12-04 16:44:28190// Helper functions for 'minimum_version_required' manipulation ----------------
191
192void ExtensionManagementPrefUpdaterBase::SetMinimumVersionRequired(
193 const std::string& id,
194 const std::string& version) {
195 DCHECK(crx_file::id_util::IdIsValid(id));
196 pref_->SetString(make_path(id, schema::kMinimumVersionRequired), version);
197}
198
199void ExtensionManagementPrefUpdaterBase::UnsetMinimumVersionRequired(
200 const std::string& id) {
201 DCHECK(crx_file::id_util::IdIsValid(id));
202 pref_->Remove(make_path(id, schema::kMinimumVersionRequired), nullptr);
203}
204
binjine6b58b52014-10-31 01:55:57205// Expose a read-only preference to user ---------------------------------------
206
binjinb2454382014-09-22 15:17:43207const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
208 return pref_.get();
209}
210
binjine6b58b52014-10-31 01:55:57211// Private section functions ---------------------------------------------------
212
binjinb2454382014-09-22 15:17:43213void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
214 pref_.reset(pref);
215}
216
217scoped_ptr<base::DictionaryValue>
218ExtensionManagementPrefUpdaterBase::TakePref() {
219 return pref_.Pass();
220}
221
binjinb2454382014-09-22 15:17:43222void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
223 pref_->Set(path, new base::ListValue());
224}
225
226void ExtensionManagementPrefUpdaterBase::AddStringToList(
227 const std::string& path,
228 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28229 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43230 if (!pref_->GetList(path, &list_value)) {
231 list_value = new base::ListValue();
232 pref_->Set(path, list_value);
233 }
234 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
235}
236
237void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
238 const std::string& path,
239 const std::string& str) {
binjin8e3d0182014-12-04 16:44:28240 base::ListValue* list_value = nullptr;
binjinb2454382014-09-22 15:17:43241 if (pref_->GetList(path, &list_value))
binjin8e3d0182014-12-04 16:44:28242 CHECK(list_value->Remove(base::StringValue(str), nullptr));
binjinb2454382014-09-22 15:17:43243}
244
binjine6b58b52014-10-31 01:55:57245// ExtensionManagementPolicyUpdater --------------------------------------------
246
247ExtensionManagementPolicyUpdater::ExtensionManagementPolicyUpdater(
248 policy::MockConfigurationPolicyProvider* policy_provider)
249 : provider_(policy_provider), policies_(new policy::PolicyBundle) {
250 policies_->CopyFrom(provider_->policies());
251 const base::Value* policy_value =
252 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
253 std::string()))
254 .GetValue(policy::key::kExtensionSettings);
255 const base::DictionaryValue* dict_value = nullptr;
256 if (policy_value && policy_value->GetAsDictionary(&dict_value))
257 SetPref(dict_value->DeepCopy());
258 else
259 SetPref(new base::DictionaryValue);
260}
261
262ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
263 policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
264 std::string()))
265 .Set(policy::key::kExtensionSettings, policy::POLICY_LEVEL_MANDATORY,
266 policy::POLICY_SCOPE_USER, TakePref().release(), nullptr);
267 provider_->UpdatePolicy(policies_.Pass());
268}
269
binjinb2454382014-09-22 15:17:43270} // namespace extensions