OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_management_test_util.h" | 5 #include "chrome/browser/extensions/extension_management_test_util.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "components/crx_file/id_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/policy_constants.h" |
8 | 17 |
9 namespace extensions { | 18 namespace extensions { |
10 | 19 |
11 namespace schema = schema_constants; | 20 namespace schema = schema_constants; |
12 | 21 |
13 namespace { | 22 namespace { |
14 | 23 |
| 24 const char kInstallSourcesPath[] = "*.install_sources"; |
| 25 const char kAllowedTypesPath[] = "*.allowed_types"; |
| 26 |
15 std::string make_path(std::string a, std::string b) { | 27 std::string make_path(std::string a, std::string b) { |
16 return a + "." + b; | 28 return a + "." + b; |
17 } | 29 } |
18 | 30 |
19 const char kInstallSourcesPath[] = "*.install_sources"; | |
20 const char kAllowedTypesPath[] = "*.allowed_types"; | |
21 | |
22 } // namespace | 31 } // namespace |
23 | 32 |
24 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() { | 33 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() { |
25 } | 34 } |
26 | 35 |
27 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() { | 36 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() { |
28 } | 37 } |
29 | 38 |
| 39 // Helper functions for per extension settings --------------------------------- |
| 40 |
30 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings( | 41 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings( |
31 const ExtensionId& id) { | 42 const ExtensionId& id) { |
32 DCHECK(crx_file::id_util::IdIsValid(id)); | 43 DCHECK(crx_file::id_util::IdIsValid(id)); |
33 pref_->RemoveWithoutPathExpansion(id, NULL); | 44 pref_->RemoveWithoutPathExpansion(id, NULL); |
34 } | 45 } |
35 | 46 |
36 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings( | 47 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings( |
37 const ExtensionId& id) { | 48 const ExtensionId& id) { |
38 DCHECK(crx_file::id_util::IdIsValid(id)); | 49 DCHECK(crx_file::id_util::IdIsValid(id)); |
39 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue()); | 50 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue()); |
40 } | 51 } |
41 | 52 |
| 53 // Helper functions for 'installation_mode' manipulation ----------------------- |
| 54 |
42 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) { | 55 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) { |
43 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode), | 56 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode), |
44 value ? schema::kBlocked : schema::kAllowed); | 57 value ? schema::kBlocked : schema::kAllowed); |
45 } | 58 } |
46 | 59 |
47 void ExtensionManagementPrefUpdaterBase:: | 60 void ExtensionManagementPrefUpdaterBase:: |
48 ClearInstallationModesForIndividualExtensions() { | 61 ClearInstallationModesForIndividualExtensions() { |
49 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd(); | 62 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd(); |
50 it.Advance()) { | 63 it.Advance()) { |
51 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); | 64 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); |
(...skipping 18 matching lines...) Expand all Loading... |
70 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled( | 83 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled( |
71 const ExtensionId& id, | 84 const ExtensionId& id, |
72 const std::string& update_url, | 85 const std::string& update_url, |
73 bool forced) { | 86 bool forced) { |
74 DCHECK(crx_file::id_util::IdIsValid(id)); | 87 DCHECK(crx_file::id_util::IdIsValid(id)); |
75 pref_->SetString(make_path(id, schema::kInstallationMode), | 88 pref_->SetString(make_path(id, schema::kInstallationMode), |
76 forced ? schema::kForceInstalled : schema::kNormalInstalled); | 89 forced ? schema::kForceInstalled : schema::kNormalInstalled); |
77 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url); | 90 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url); |
78 } | 91 } |
79 | 92 |
| 93 // Helper functions for 'install_sources' manipulation ------------------------- |
| 94 |
80 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() { | 95 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() { |
81 pref_->Remove(kInstallSourcesPath, NULL); | 96 pref_->Remove(kInstallSourcesPath, NULL); |
82 } | 97 } |
83 | 98 |
84 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() { | 99 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() { |
85 ClearList(kInstallSourcesPath); | 100 ClearList(kInstallSourcesPath); |
86 } | 101 } |
87 | 102 |
88 void ExtensionManagementPrefUpdaterBase::AddInstallSource( | 103 void ExtensionManagementPrefUpdaterBase::AddInstallSource( |
89 const std::string& install_source) { | 104 const std::string& install_source) { |
90 AddStringToList(kInstallSourcesPath, install_source); | 105 AddStringToList(kInstallSourcesPath, install_source); |
91 } | 106 } |
92 | 107 |
93 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource( | 108 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource( |
94 const std::string& install_source) { | 109 const std::string& install_source) { |
95 RemoveStringFromList(kInstallSourcesPath, install_source); | 110 RemoveStringFromList(kInstallSourcesPath, install_source); |
96 } | 111 } |
97 | 112 |
| 113 // Helper functions for 'allowed_types' manipulation --------------------------- |
| 114 |
98 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() { | 115 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() { |
99 pref_->Remove(kAllowedTypesPath, NULL); | 116 pref_->Remove(kAllowedTypesPath, NULL); |
100 } | 117 } |
101 | 118 |
102 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() { | 119 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() { |
103 ClearList(kAllowedTypesPath); | 120 ClearList(kAllowedTypesPath); |
104 } | 121 } |
105 | 122 |
106 void ExtensionManagementPrefUpdaterBase::AddAllowedType( | 123 void ExtensionManagementPrefUpdaterBase::AddAllowedType( |
107 const std::string& allowed_type) { | 124 const std::string& allowed_type) { |
108 AddStringToList(kAllowedTypesPath, allowed_type); | 125 AddStringToList(kAllowedTypesPath, allowed_type); |
109 } | 126 } |
110 | 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 |
111 const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() { | 192 const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() { |
112 return pref_.get(); | 193 return pref_.get(); |
113 } | 194 } |
114 | 195 |
| 196 // Private section functions --------------------------------------------------- |
| 197 |
115 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) { | 198 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) { |
116 pref_.reset(pref); | 199 pref_.reset(pref); |
117 } | 200 } |
118 | 201 |
119 scoped_ptr<base::DictionaryValue> | 202 scoped_ptr<base::DictionaryValue> |
120 ExtensionManagementPrefUpdaterBase::TakePref() { | 203 ExtensionManagementPrefUpdaterBase::TakePref() { |
121 return pref_.Pass(); | 204 return pref_.Pass(); |
122 } | 205 } |
123 | 206 |
124 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType( | |
125 const std::string& allowd_type) { | |
126 RemoveStringFromList(kAllowedTypesPath, allowd_type); | |
127 } | |
128 | |
129 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) { | 207 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) { |
130 pref_->Set(path, new base::ListValue()); | 208 pref_->Set(path, new base::ListValue()); |
131 } | 209 } |
132 | 210 |
133 void ExtensionManagementPrefUpdaterBase::AddStringToList( | 211 void ExtensionManagementPrefUpdaterBase::AddStringToList( |
134 const std::string& path, | 212 const std::string& path, |
135 const std::string& str) { | 213 const std::string& str) { |
136 base::ListValue* list_value = NULL; | 214 base::ListValue* list_value = NULL; |
137 if (!pref_->GetList(path, &list_value)) { | 215 if (!pref_->GetList(path, &list_value)) { |
138 list_value = new base::ListValue(); | 216 list_value = new base::ListValue(); |
139 pref_->Set(path, list_value); | 217 pref_->Set(path, list_value); |
140 } | 218 } |
141 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str))); | 219 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str))); |
142 } | 220 } |
143 | 221 |
144 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList( | 222 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList( |
145 const std::string& path, | 223 const std::string& path, |
146 const std::string& str) { | 224 const std::string& str) { |
147 base::ListValue* list_value = NULL; | 225 base::ListValue* list_value = NULL; |
148 if (pref_->GetList(path, &list_value)) | 226 if (pref_->GetList(path, &list_value)) |
149 CHECK(list_value->Remove(base::StringValue(str), NULL)); | 227 CHECK(list_value->Remove(base::StringValue(str), NULL)); |
150 } | 228 } |
151 | 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 |
152 } // namespace extensions | 255 } // namespace extensions |
OLD | NEW |