Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_management_test_util.cc
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 <string>
8
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/policy_constants.h"
17
18 namespace extensions {
19
20 namespace schema = schema_constants;
21
22 namespace {
23
24 const char kInstallSourcesPath[] = "*.install_sources";
25 const char kAllowedTypesPath[] = "*.allowed_types";
26
27 std::string make_path(std::string a, std::string b) {
28   return a + "." + b;
29 }
30
31 }  // namespace
32
33 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
34 }
35
36 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
37 }
38
39 // Helper functions for per extension settings ---------------------------------
40
41 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
42     const ExtensionId& id) {
43   DCHECK(crx_file::id_util::IdIsValid(id));
44   pref_->RemoveWithoutPathExpansion(id, NULL);
45 }
46
47 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
48     const ExtensionId& id) {
49   DCHECK(crx_file::id_util::IdIsValid(id));
50   pref_->SetWithoutPathExpansion(id, new base::DictionaryValue());
51 }
52
53 // Helper functions for 'installation_mode' manipulation -----------------------
54
55 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
56   pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
57                    value ? schema::kBlocked : schema::kAllowed);
58 }
59
60 void 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()));
67       pref_->Remove(make_path(it.key(), schema::kInstallationMode), NULL);
68       pref_->Remove(make_path(it.key(), schema::kUpdateUrl), NULL);
69     }
70   }
71 }
72
73 void
74 ExtensionManagementPrefUpdaterBase::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);
80   pref_->Remove(make_path(id, schema::kUpdateUrl), NULL);
81 }
82
83 void 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
93 // Helper functions for 'install_sources' manipulation -------------------------
94
95 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
96   pref_->Remove(kInstallSourcesPath, NULL);
97 }
98
99 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
100   ClearList(kInstallSourcesPath);
101 }
102
103 void ExtensionManagementPrefUpdaterBase::AddInstallSource(
104     const std::string& install_source) {
105   AddStringToList(kInstallSourcesPath, install_source);
106 }
107
108 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
109     const std::string& install_source) {
110   RemoveStringFromList(kInstallSourcesPath, install_source);
111 }
112
113 // Helper functions for 'allowed_types' manipulation ---------------------------
114
115 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
116   pref_->Remove(kAllowedTypesPath, NULL);
117 }
118
119 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
120   ClearList(kAllowedTypesPath);
121 }
122
123 void ExtensionManagementPrefUpdaterBase::AddAllowedType(
124     const std::string& allowed_type) {
125   AddStringToList(kAllowedTypesPath, allowed_type);
126 }
127
128 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
129     const std::string& allowed_type) {
130   RemoveStringFromList(kAllowedTypesPath, allowed_type);
131 }
132
133 // Helper functions for 'blocked_permissions' manipulation ---------------------
134
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), NULL);
139 }
140
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));
145 }
146
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);
152 }
153
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),
159                        permission);
160 }
161
162 // Helper functions for 'allowed_permissions' manipulation ---------------------
163
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), NULL);
168 }
169
170 void ExtensionManagementPrefUpdaterBase::ClearAllowedPermissions(
171     const std::string& id) {
172   DCHECK(crx_file::id_util::IdIsValid(id));
173   ClearList(make_path(id, schema::kAllowedPermissions));
174 }
175
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);
181 }
182
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);
188 }
189
190 // Expose a read-only preference to user ---------------------------------------
191
192 const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
193   return pref_.get();
194 }
195
196 // Private section functions ---------------------------------------------------
197
198 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
199   pref_.reset(pref);
200 }
201
202 scoped_ptr<base::DictionaryValue>
203 ExtensionManagementPrefUpdaterBase::TakePref() {
204   return pref_.Pass();
205 }
206
207 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
208   pref_->Set(path, new base::ListValue());
209 }
210
211 void ExtensionManagementPrefUpdaterBase::AddStringToList(
212     const std::string& path,
213     const std::string& str) {
214   base::ListValue* list_value = NULL;
215   if (!pref_->GetList(path, &list_value)) {
216     list_value = new base::ListValue();
217     pref_->Set(path, list_value);
218   }
219   CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
220 }
221
222 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
223     const std::string& path,
224     const std::string& str) {
225   base::ListValue* list_value = NULL;
226   if (pref_->GetList(path, &list_value))
227     CHECK(list_value->Remove(base::StringValue(str), NULL));
228 }
229
230 // ExtensionManagementPolicyUpdater --------------------------------------------
231
232 ExtensionManagementPolicyUpdater::ExtensionManagementPolicyUpdater(
233     policy::MockConfigurationPolicyProvider* policy_provider)
234     : provider_(policy_provider), policies_(new policy::PolicyBundle) {
235   policies_->CopyFrom(provider_->policies());
236   const base::Value* policy_value =
237       policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
238                                              std::string()))
239           .GetValue(policy::key::kExtensionSettings);
240   const base::DictionaryValue* dict_value = nullptr;
241   if (policy_value && policy_value->GetAsDictionary(&dict_value))
242     SetPref(dict_value->DeepCopy());
243   else
244     SetPref(new base::DictionaryValue);
245 }
246
247 ExtensionManagementPolicyUpdater::~ExtensionManagementPolicyUpdater() {
248   policies_->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
249                                          std::string()))
250       .Set(policy::key::kExtensionSettings, policy::POLICY_LEVEL_MANDATORY,
251            policy::POLICY_SCOPE_USER, TakePref().release(), nullptr);
252   provider_->UpdatePolicy(policies_.Pass());
253 }
254
255 }  // namespace extensions