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"
7 #include "components/crx_file/id_util.h"
11 namespace schema
= schema_constants
;
15 std::string
make_path(std::string a
, std::string b
) {
19 const char kInstallSourcesPath
[] = "*.install_sources";
20 const char kAllowedTypesPath
[] = "*.allowed_types";
24 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
27 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
30 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
31 const ExtensionId
& id
) {
32 DCHECK(crx_file::id_util::IdIsValid(id
));
33 pref_
->RemoveWithoutPathExpansion(id
, NULL
);
36 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
37 const ExtensionId
& id
) {
38 DCHECK(crx_file::id_util::IdIsValid(id
));
39 pref_
->SetWithoutPathExpansion(id
, new base::DictionaryValue());
42 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value
) {
43 pref_
->SetString(make_path(schema::kWildcard
, schema::kInstallationMode
),
44 value
? schema::kBlocked
: schema::kAllowed
);
47 void ExtensionManagementPrefUpdaterBase::
48 ClearInstallationModesForIndividualExtensions() {
49 for (base::DictionaryValue::Iterator
it(*pref_
.get()); !it
.IsAtEnd();
51 DCHECK(it
.value().IsType(base::Value::TYPE_DICTIONARY
));
52 if (it
.key() != schema::kWildcard
) {
53 DCHECK(crx_file::id_util::IdIsValid(it
.key()));
54 pref_
->Remove(make_path(it
.key(), schema::kInstallationMode
), NULL
);
55 pref_
->Remove(make_path(it
.key(), schema::kUpdateUrl
), NULL
);
61 ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
62 const ExtensionId
& id
,
64 DCHECK(crx_file::id_util::IdIsValid(id
));
65 pref_
->SetString(make_path(id
, schema::kInstallationMode
),
66 allowed
? schema::kAllowed
: schema::kBlocked
);
67 pref_
->Remove(make_path(id
, schema::kUpdateUrl
), NULL
);
70 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
71 const ExtensionId
& id
,
72 const std::string
& update_url
,
74 DCHECK(crx_file::id_util::IdIsValid(id
));
75 pref_
->SetString(make_path(id
, schema::kInstallationMode
),
76 forced
? schema::kForceInstalled
: schema::kNormalInstalled
);
77 pref_
->SetString(make_path(id
, schema::kUpdateUrl
), update_url
);
80 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
81 pref_
->Remove(kInstallSourcesPath
, NULL
);
84 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
85 ClearList(kInstallSourcesPath
);
88 void ExtensionManagementPrefUpdaterBase::AddInstallSource(
89 const std::string
& install_source
) {
90 AddStringToList(kInstallSourcesPath
, install_source
);
93 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
94 const std::string
& install_source
) {
95 RemoveStringFromList(kInstallSourcesPath
, install_source
);
98 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
99 pref_
->Remove(kAllowedTypesPath
, NULL
);
102 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
103 ClearList(kAllowedTypesPath
);
106 void ExtensionManagementPrefUpdaterBase::AddAllowedType(
107 const std::string
& allowed_type
) {
108 AddStringToList(kAllowedTypesPath
, allowed_type
);
111 const base::DictionaryValue
* ExtensionManagementPrefUpdaterBase::GetPref() {
115 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue
* pref
) {
119 scoped_ptr
<base::DictionaryValue
>
120 ExtensionManagementPrefUpdaterBase::TakePref() {
124 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
125 const std::string
& allowd_type
) {
126 RemoveStringFromList(kAllowedTypesPath
, allowd_type
);
129 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string
& path
) {
130 pref_
->Set(path
, new base::ListValue());
133 void ExtensionManagementPrefUpdaterBase::AddStringToList(
134 const std::string
& path
,
135 const std::string
& str
) {
136 base::ListValue
* list_value
= NULL
;
137 if (!pref_
->GetList(path
, &list_value
)) {
138 list_value
= new base::ListValue();
139 pref_
->Set(path
, list_value
);
141 CHECK(list_value
->AppendIfNotPresent(new base::StringValue(str
)));
144 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
145 const std::string
& path
,
146 const std::string
& str
) {
147 base::ListValue
* list_value
= NULL
;
148 if (pref_
->GetList(path
, &list_value
))
149 CHECK(list_value
->Remove(base::StringValue(str
), NULL
));
152 } // namespace extensions