1 // 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.
5 #include "chrome/browser/extensions/extension_management_test_util.h"
9 #include "components/crx_file/id_util.h"
10 #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_constants.h"
18 namespace extensions
{
20 namespace schema
= schema_constants
;
24 const char kInstallSourcesPath
[] = "*.install_sources";
25 const char kAllowedTypesPath
[] = "*.allowed_types";
27 std::string
make_path(std::string a
, std::string b
) {
33 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
36 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
39 // Helper functions for per extension settings ---------------------------------
41 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
42 const ExtensionId
& id
) {
43 DCHECK(crx_file::id_util::IdIsValid(id
));
44 pref_
->RemoveWithoutPathExpansion(id
, nullptr);
47 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
48 const ExtensionId
& id
) {
49 DCHECK(crx_file::id_util::IdIsValid(id
));
50 pref_
->SetWithoutPathExpansion(id
, new base::DictionaryValue());
53 // Helper functions for 'installation_mode' manipulation -----------------------
55 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value
) {
56 pref_
->SetString(make_path(schema::kWildcard
, schema::kInstallationMode
),
57 value
? schema::kBlocked
: schema::kAllowed
);
60 void ExtensionManagementPrefUpdaterBase::
61 ClearInstallationModesForIndividualExtensions() {
62 for (base::DictionaryValue::Iterator
it(*pref_
.get()); !it
.IsAtEnd();
64 DCHECK(it
.value().IsType(base::Value::TYPE_DICTIONARY
));
65 if (it
.key() != schema::kWildcard
) {
66 DCHECK(crx_file::id_util::IdIsValid(it
.key()));
67 pref_
->Remove(make_path(it
.key(), schema::kInstallationMode
), nullptr);
68 pref_
->Remove(make_path(it
.key(), schema::kUpdateUrl
), nullptr);
74 ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
75 const ExtensionId
& id
,
77 DCHECK(crx_file::id_util::IdIsValid(id
));
78 pref_
->SetString(make_path(id
, schema::kInstallationMode
),
79 allowed
? schema::kAllowed
: schema::kBlocked
);
80 pref_
->Remove(make_path(id
, schema::kUpdateUrl
), nullptr);
83 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
84 const ExtensionId
& id
,
85 const std::string
& update_url
,
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
);
93 // Helper functions for 'install_sources' manipulation -------------------------
95 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
96 pref_
->Remove(kInstallSourcesPath
, nullptr);
99 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
100 ClearList(kInstallSourcesPath
);
103 void ExtensionManagementPrefUpdaterBase::AddInstallSource(
104 const std::string
& install_source
) {
105 AddStringToList(kInstallSourcesPath
, install_source
);
108 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
109 const std::string
& install_source
) {
110 RemoveStringFromList(kInstallSourcesPath
, install_source
);
113 // Helper functions for 'allowed_types' manipulation ---------------------------
115 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
116 pref_
->Remove(kAllowedTypesPath
, nullptr);
119 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
120 ClearList(kAllowedTypesPath
);
123 void ExtensionManagementPrefUpdaterBase::AddAllowedType(
124 const std::string
& allowed_type
) {
125 AddStringToList(kAllowedTypesPath
, allowed_type
);
128 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
129 const std::string
& allowed_type
) {
130 RemoveStringFromList(kAllowedTypesPath
, allowed_type
);
133 // Helper functions for 'blocked_permissions' manipulation ---------------------
135 void ExtensionManagementPrefUpdaterBase::UnsetBlockedPermissions(
136 const std::string
& prefix
) {
137 DCHECK(prefix
== schema::kWildcard
|| crx_file::id_util::IdIsValid(prefix
));
138 pref_
->Remove(make_path(prefix
, schema::kBlockedPermissions
), nullptr);
141 void 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
));
147 void 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
);
154 void 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
),
162 // Helper functions for 'allowed_permissions' manipulation ---------------------
164 void ExtensionManagementPrefUpdaterBase::UnsetAllowedPermissions(
165 const std::string
& id
) {
166 DCHECK(crx_file::id_util::IdIsValid(id
));
167 pref_
->Remove(make_path(id
, schema::kAllowedPermissions
), nullptr);
170 void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
171 const std::string
& id
) {
172 DCHECK(crx_file::id_util::IdIsValid(id
));
173 ClearList(make_path(id
, schema::kAllowedPermissions
));
176 void 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
);
183 void 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
);
190 // Helper functions for 'minimum_version_required' manipulation ----------------
192 void 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
);
199 void ExtensionManagementPrefUpdaterBase::UnsetMinimumVersionRequired(
200 const std::string
& id
) {
201 DCHECK(crx_file::id_util::IdIsValid(id
));
202 pref_
->Remove(make_path(id
, schema::kMinimumVersionRequired
), nullptr);
205 // Expose a read-only preference to user ---------------------------------------
207 const base::DictionaryValue
* ExtensionManagementPrefUpdaterBase::GetPref() {
211 // Private section functions ---------------------------------------------------
213 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue
* pref
) {
217 scoped_ptr
<base::DictionaryValue
>
218 ExtensionManagementPrefUpdaterBase::TakePref() {
222 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string
& path
) {
223 pref_
->Set(path
, new base::ListValue());
226 void ExtensionManagementPrefUpdaterBase::AddStringToList(
227 const std::string
& path
,
228 const std::string
& str
) {
229 base::ListValue
* list_value
= nullptr;
230 if (!pref_
->GetList(path
, &list_value
)) {
231 list_value
= new base::ListValue();
232 pref_
->Set(path
, list_value
);
234 CHECK(list_value
->AppendIfNotPresent(new base::StringValue(str
)));
237 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
238 const std::string
& path
,
239 const std::string
& str
) {
240 base::ListValue
* list_value
= nullptr;
241 if (pref_
->GetList(path
, &list_value
))
242 CHECK(list_value
->Remove(base::StringValue(str
), nullptr));
245 // ExtensionManagementPolicyUpdater --------------------------------------------
247 ExtensionManagementPolicyUpdater::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
,
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());
259 SetPref(new base::DictionaryValue
);
262 ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
263 policies_
->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME
,
265 .Set(policy::key::kExtensionSettings
, policy::POLICY_LEVEL_MANDATORY
,
266 policy::POLICY_SCOPE_USER
, TakePref().release(), nullptr);
267 provider_
->UpdatePolicy(policies_
.Pass());
270 } // namespace extensions