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.
4
5 #include "chrome/browser/extensions/extension_management_test_util.h"
6
7 #include "components/crx_file/id_util.h"
8
9 namespace extensions {
10
11 namespace schema = schema_constants;
12
13 namespace {
14
make_path(std::string a,std::string b)15 std::string make_path(std::string a, std::string b) {
16 return a + "." + b;
17 }
18
19 const char kInstallSourcesPath[] = "*.install_sources";
20 const char kAllowedTypesPath[] = "*.allowed_types";
21
22 } // namespace
23
ExtensionManagementPrefUpdaterBase()24 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
25 }
26
~ExtensionManagementPrefUpdaterBase()27 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
28 }
29
UnsetPerExtensionSettings(const ExtensionId & id)30 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
31 const ExtensionId& id) {
32 DCHECK(crx_file::id_util::IdIsValid(id));
33 pref_->RemoveWithoutPathExpansion(id, NULL);
34 }
35
ClearPerExtensionSettings(const ExtensionId & id)36 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
37 const ExtensionId& id) {
38 DCHECK(crx_file::id_util::IdIsValid(id));
39 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue());
40 }
41
SetBlacklistedByDefault(bool value)42 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
43 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
44 value ? schema::kBlocked : schema::kAllowed);
45 }
46
47 void ExtensionManagementPrefUpdaterBase::
ClearInstallationModesForIndividualExtensions()48 ClearInstallationModesForIndividualExtensions() {
49 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd();
50 it.Advance()) {
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);
56 }
57 }
58 }
59
60 void
SetIndividualExtensionInstallationAllowed(const ExtensionId & id,bool allowed)61 ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
62 const ExtensionId& id,
63 bool allowed) {
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);
68 }
69
SetIndividualExtensionAutoInstalled(const ExtensionId & id,const std::string & update_url,bool forced)70 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
71 const ExtensionId& id,
72 const std::string& update_url,
73 bool forced) {
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);
78 }
79
UnsetInstallSources()80 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
81 pref_->Remove(kInstallSourcesPath, NULL);
82 }
83
ClearInstallSources()84 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
85 ClearList(kInstallSourcesPath);
86 }
87
AddInstallSource(const std::string & install_source)88 void ExtensionManagementPrefUpdaterBase::AddInstallSource(
89 const std::string& install_source) {
90 AddStringToList(kInstallSourcesPath, install_source);
91 }
92
RemoveInstallSource(const std::string & install_source)93 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
94 const std::string& install_source) {
95 RemoveStringFromList(kInstallSourcesPath, install_source);
96 }
97
UnsetAllowedTypes()98 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
99 pref_->Remove(kAllowedTypesPath, NULL);
100 }
101
ClearAllowedTypes()102 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
103 ClearList(kAllowedTypesPath);
104 }
105
AddAllowedType(const std::string & allowed_type)106 void ExtensionManagementPrefUpdaterBase::AddAllowedType(
107 const std::string& allowed_type) {
108 AddStringToList(kAllowedTypesPath, allowed_type);
109 }
110
GetPref()111 const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
112 return pref_.get();
113 }
114
SetPref(base::DictionaryValue * pref)115 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
116 pref_.reset(pref);
117 }
118
119 scoped_ptr<base::DictionaryValue>
TakePref()120 ExtensionManagementPrefUpdaterBase::TakePref() {
121 return pref_.Pass();
122 }
123
RemoveAllowedType(const std::string & allowd_type)124 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
125 const std::string& allowd_type) {
126 RemoveStringFromList(kAllowedTypesPath, allowd_type);
127 }
128
ClearList(const std::string & path)129 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
130 pref_->Set(path, new base::ListValue());
131 }
132
AddStringToList(const std::string & path,const std::string & str)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);
140 }
141 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
142 }
143
RemoveStringFromList(const std::string & path,const std::string & 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));
150 }
151
152 } // namespace extensions
153