| 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 "components/search_engines/default_search_policy_handler.h" | 5 #include "components/search_engines/default_search_policy_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "components/policy/core/browser/policy_error_map.h" | 14 #include "components/policy/core/browser/policy_error_map.h" |
| 16 #include "components/policy/core/common/policy_map.h" | 15 #include "components/policy/core/common/policy_map.h" |
| 17 #include "components/policy/policy_constants.h" | 16 #include "components/policy/policy_constants.h" |
| 18 #include "components/prefs/pref_value_map.h" | 17 #include "components/prefs/pref_value_map.h" |
| 19 #include "components/search_engines/default_search_manager.h" | 18 #include "components/search_engines/default_search_manager.h" |
| 20 #include "components/search_engines/search_engines_pref_names.h" | 19 #include "components/search_engines/search_engines_pref_names.h" |
| 21 #include "components/search_engines/search_terms_data.h" | 20 #include "components/search_engines/search_terms_data.h" |
| 22 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
| 23 #include "grit/components_strings.h" | 22 #include "grit/components_strings.h" |
| 24 | 23 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 52 bool is_string = policy_value->GetAsString(&str); | 51 bool is_string = policy_value->GetAsString(&str); |
| 53 DCHECK(is_string); | 52 DCHECK(is_string); |
| 54 } | 53 } |
| 55 dict->SetString(key, str); | 54 dict->SetString(key, str); |
| 56 } | 55 } |
| 57 | 56 |
| 58 } // namespace | 57 } // namespace |
| 59 | 58 |
| 60 // List of policy types to preference names, for policies affecting the default | 59 // List of policy types to preference names, for policies affecting the default |
| 61 // search provider. | 60 // search provider. |
| 62 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { | |
| 63 { key::kDefaultSearchProviderEnabled, | |
| 64 prefs::kDefaultSearchProviderEnabled, | |
| 65 base::Value::Type::BOOLEAN }, | |
| 66 { key::kDefaultSearchProviderName, | |
| 67 prefs::kDefaultSearchProviderName, | |
| 68 base::Value::Type::STRING }, | |
| 69 { key::kDefaultSearchProviderKeyword, | |
| 70 prefs::kDefaultSearchProviderKeyword, | |
| 71 base::Value::Type::STRING }, | |
| 72 { key::kDefaultSearchProviderSearchURL, | |
| 73 prefs::kDefaultSearchProviderSearchURL, | |
| 74 base::Value::Type::STRING }, | |
| 75 { key::kDefaultSearchProviderSuggestURL, | |
| 76 prefs::kDefaultSearchProviderSuggestURL, | |
| 77 base::Value::Type::STRING }, | |
| 78 { key::kDefaultSearchProviderInstantURL, | |
| 79 prefs::kDefaultSearchProviderInstantURL, | |
| 80 base::Value::Type::STRING }, | |
| 81 { key::kDefaultSearchProviderIconURL, | |
| 82 prefs::kDefaultSearchProviderIconURL, | |
| 83 base::Value::Type::STRING }, | |
| 84 { key::kDefaultSearchProviderEncodings, | |
| 85 prefs::kDefaultSearchProviderEncodings, | |
| 86 base::Value::Type::LIST }, | |
| 87 { key::kDefaultSearchProviderAlternateURLs, | |
| 88 prefs::kDefaultSearchProviderAlternateURLs, | |
| 89 base::Value::Type::LIST }, | |
| 90 { key::kDefaultSearchProviderSearchTermsReplacementKey, | |
| 91 prefs::kDefaultSearchProviderSearchTermsReplacementKey, | |
| 92 base::Value::Type::STRING }, | |
| 93 { key::kDefaultSearchProviderImageURL, | |
| 94 prefs::kDefaultSearchProviderImageURL, | |
| 95 base::Value::Type::STRING }, | |
| 96 { key::kDefaultSearchProviderNewTabURL, | |
| 97 prefs::kDefaultSearchProviderNewTabURL, | |
| 98 base::Value::Type::STRING }, | |
| 99 { key::kDefaultSearchProviderSearchURLPostParams, | |
| 100 prefs::kDefaultSearchProviderSearchURLPostParams, | |
| 101 base::Value::Type::STRING }, | |
| 102 { key::kDefaultSearchProviderSuggestURLPostParams, | |
| 103 prefs::kDefaultSearchProviderSuggestURLPostParams, | |
| 104 base::Value::Type::STRING }, | |
| 105 { key::kDefaultSearchProviderInstantURLPostParams, | |
| 106 prefs::kDefaultSearchProviderInstantURLPostParams, | |
| 107 base::Value::Type::STRING }, | |
| 108 { key::kDefaultSearchProviderImageURLPostParams, | |
| 109 prefs::kDefaultSearchProviderImageURLPostParams, | |
| 110 base::Value::Type::STRING }, | |
| 111 }; | |
| 112 | |
| 113 // List of policy types to preference names, for policies affecting the default | |
| 114 // search provider. | |
| 115 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { | 61 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { |
| 62 {key::kDefaultSearchProviderEnabled, prefs::kDefaultSearchProviderEnabled, |
| 63 base::Value::Type::BOOLEAN}, |
| 116 {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName, | 64 {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName, |
| 117 base::Value::Type::STRING}, | 65 base::Value::Type::STRING}, |
| 118 {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword, | 66 {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword, |
| 119 base::Value::Type::STRING}, | 67 base::Value::Type::STRING}, |
| 120 {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL, | 68 {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL, |
| 121 base::Value::Type::STRING}, | 69 base::Value::Type::STRING}, |
| 122 {key::kDefaultSearchProviderSuggestURL, | 70 {key::kDefaultSearchProviderSuggestURL, |
| 123 DefaultSearchManager::kSuggestionsURL, base::Value::Type::STRING}, | 71 DefaultSearchManager::kSuggestionsURL, base::Value::Type::STRING}, |
| 124 {key::kDefaultSearchProviderInstantURL, DefaultSearchManager::kInstantURL, | 72 {key::kDefaultSearchProviderInstantURL, DefaultSearchManager::kInstantURL, |
| 125 base::Value::Type::STRING}, | 73 base::Value::Type::STRING}, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 140 DefaultSearchManager::kSearchURLPostParams, base::Value::Type::STRING}, | 88 DefaultSearchManager::kSearchURLPostParams, base::Value::Type::STRING}, |
| 141 {key::kDefaultSearchProviderSuggestURLPostParams, | 89 {key::kDefaultSearchProviderSuggestURLPostParams, |
| 142 DefaultSearchManager::kSuggestionsURLPostParams, | 90 DefaultSearchManager::kSuggestionsURLPostParams, |
| 143 base::Value::Type::STRING}, | 91 base::Value::Type::STRING}, |
| 144 {key::kDefaultSearchProviderInstantURLPostParams, | 92 {key::kDefaultSearchProviderInstantURLPostParams, |
| 145 DefaultSearchManager::kInstantURLPostParams, base::Value::Type::STRING}, | 93 DefaultSearchManager::kInstantURLPostParams, base::Value::Type::STRING}, |
| 146 {key::kDefaultSearchProviderImageURLPostParams, | 94 {key::kDefaultSearchProviderImageURLPostParams, |
| 147 DefaultSearchManager::kImageURLPostParams, base::Value::Type::STRING}, | 95 DefaultSearchManager::kImageURLPostParams, base::Value::Type::STRING}, |
| 148 }; | 96 }; |
| 149 | 97 |
| 150 // DefaultSearchEncodingsPolicyHandler implementation -------------------------- | |
| 151 | |
| 152 DefaultSearchEncodingsPolicyHandler::DefaultSearchEncodingsPolicyHandler() | |
| 153 : TypeCheckingPolicyHandler(key::kDefaultSearchProviderEncodings, | |
| 154 base::Value::Type::LIST) {} | |
| 155 | |
| 156 DefaultSearchEncodingsPolicyHandler::~DefaultSearchEncodingsPolicyHandler() { | |
| 157 } | |
| 158 | |
| 159 void DefaultSearchEncodingsPolicyHandler::ApplyPolicySettings( | |
| 160 const PolicyMap& policies, PrefValueMap* prefs) { | |
| 161 // The DefaultSearchProviderEncodings policy has type list, but the related | |
| 162 // preference has type string. Convert one into the other here, using | |
| 163 // ';' as a separator. | |
| 164 const base::Value* value = policies.GetValue(policy_name()); | |
| 165 const base::ListValue* list; | |
| 166 if (!value || !value->GetAsList(&list)) | |
| 167 return; | |
| 168 | |
| 169 base::ListValue::const_iterator iter(list->begin()); | |
| 170 base::ListValue::const_iterator end(list->end()); | |
| 171 std::vector<std::string> string_parts; | |
| 172 for (; iter != end; ++iter) { | |
| 173 std::string s; | |
| 174 if ((*iter)->GetAsString(&s)) { | |
| 175 string_parts.push_back(s); | |
| 176 } | |
| 177 } | |
| 178 std::string encodings = base::JoinString(string_parts, ";"); | |
| 179 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); | |
| 180 } | |
| 181 | |
| 182 | |
| 183 // DefaultSearchPolicyHandler implementation ----------------------------------- | 98 // DefaultSearchPolicyHandler implementation ----------------------------------- |
| 184 | 99 |
| 185 DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() { | 100 DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() {} |
| 186 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyMap); ++i) { | |
| 187 const char* policy_name = kDefaultSearchPolicyMap[i].policy_name; | |
| 188 if (policy_name == key::kDefaultSearchProviderEncodings) { | |
| 189 handlers_.push_back( | |
| 190 base::MakeUnique<DefaultSearchEncodingsPolicyHandler>()); | |
| 191 } else { | |
| 192 handlers_.push_back(base::MakeUnique<SimplePolicyHandler>( | |
| 193 policy_name, kDefaultSearchPolicyMap[i].preference_path, | |
| 194 kDefaultSearchPolicyMap[i].value_type)); | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 | 101 |
| 199 DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() {} | 102 DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() {} |
| 200 | 103 |
| 201 bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, | 104 bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, |
| 202 PolicyErrorMap* errors) { | 105 PolicyErrorMap* errors) { |
| 203 if (!CheckIndividualPolicies(policies, errors)) | 106 if (!CheckIndividualPolicies(policies, errors)) |
| 204 return false; | 107 return false; |
| 205 | 108 |
| 206 if (DefaultSearchProviderIsDisabled(policies)) { | 109 if (DefaultSearchProviderIsDisabled(policies)) { |
| 207 // Add an error for all specified default search policies except | 110 // Add an error for all specified default search policies except |
| 208 // DefaultSearchProviderEnabled. | 111 // DefaultSearchProviderEnabled. |
| 209 | 112 |
| 210 for (const auto& handler : handlers_) { | 113 for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) { |
| 211 const char* policy_name = handler->policy_name(); | 114 const char* policy_name = policy_map_entry.policy_name; |
| 212 if (policy_name != key::kDefaultSearchProviderEnabled && | 115 if (policy_name != key::kDefaultSearchProviderEnabled && |
| 213 HasDefaultSearchPolicy(policies, policy_name)) { | 116 HasDefaultSearchPolicy(policies, policy_name)) { |
| 214 errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); | 117 errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); |
| 215 } | 118 } |
| 216 } | 119 } |
| 217 return true; | 120 return true; |
| 218 } | 121 } |
| 219 | 122 |
| 220 const base::Value* url; | 123 const base::Value* url; |
| 221 std::string dummy; | 124 std::string dummy; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 240 // sure that they are all specified via policy, so that the regular prefs | 143 // sure that they are all specified via policy, so that the regular prefs |
| 241 // aren't used. | 144 // aren't used. |
| 242 const base::Value* dummy; | 145 const base::Value* dummy; |
| 243 std::string url; | 146 std::string url; |
| 244 if (!DefaultSearchURLIsValid(policies, &dummy, &url)) | 147 if (!DefaultSearchURLIsValid(policies, &dummy, &url)) |
| 245 return; | 148 return; |
| 246 | 149 |
| 247 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 150 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 248 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) { | 151 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyDataMap); ++i) { |
| 249 const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name; | 152 const char* policy_name = kDefaultSearchPolicyDataMap[i].policy_name; |
| 153 // kDefaultSearchProviderEnabled has already been handled. |
| 154 if (policy_name == key::kDefaultSearchProviderEnabled) |
| 155 continue; |
| 156 |
| 250 switch (kDefaultSearchPolicyDataMap[i].value_type) { | 157 switch (kDefaultSearchPolicyDataMap[i].value_type) { |
| 251 case base::Value::Type::STRING: | 158 case base::Value::Type::STRING: |
| 252 SetStringInPref(policies, | 159 SetStringInPref(policies, |
| 253 policy_name, | 160 policy_name, |
| 254 kDefaultSearchPolicyDataMap[i].preference_path, | 161 kDefaultSearchPolicyDataMap[i].preference_path, |
| 255 dict.get()); | 162 dict.get()); |
| 256 break; | 163 break; |
| 257 case base::Value::Type::LIST: | 164 case base::Value::Type::LIST: |
| 258 SetListInPref(policies, | 165 SetListInPref(policies, |
| 259 policy_name, | 166 policy_name, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 if (keyword.empty()) | 203 if (keyword.empty()) |
| 297 dict->SetString(DefaultSearchManager::kKeyword, host); | 204 dict->SetString(DefaultSearchManager::kKeyword, host); |
| 298 | 205 |
| 299 DefaultSearchManager::AddPrefValueToMap(std::move(dict), prefs); | 206 DefaultSearchManager::AddPrefValueToMap(std::move(dict), prefs); |
| 300 } | 207 } |
| 301 | 208 |
| 302 bool DefaultSearchPolicyHandler::CheckIndividualPolicies( | 209 bool DefaultSearchPolicyHandler::CheckIndividualPolicies( |
| 303 const PolicyMap& policies, | 210 const PolicyMap& policies, |
| 304 PolicyErrorMap* errors) { | 211 PolicyErrorMap* errors) { |
| 305 bool all_ok = true; | 212 bool all_ok = true; |
| 306 for (const auto& handler : handlers_) { | 213 for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) { |
| 307 // It's important to call CheckPolicySettings() on all handlers and not just | 214 // It's important to check policy type for all policies and not just exit on |
| 308 // exit on the first error, so we report all policy errors. | 215 // the first error, so we report all policy errors. |
| 309 all_ok &= handler->CheckPolicySettings(policies, errors); | 216 const base::Value* value = policies.GetValue(policy_map_entry.policy_name); |
| 217 if (value && !value->IsType(policy_map_entry.value_type)) { |
| 218 errors->AddError(policy_map_entry.policy_name, IDS_POLICY_TYPE_ERROR, |
| 219 base::Value::GetTypeName(policy_map_entry.value_type)); |
| 220 all_ok = false; |
| 221 } |
| 310 } | 222 } |
| 311 return all_ok; | 223 return all_ok; |
| 312 } | 224 } |
| 313 | 225 |
| 314 bool DefaultSearchPolicyHandler::HasDefaultSearchPolicy( | 226 bool DefaultSearchPolicyHandler::HasDefaultSearchPolicy( |
| 315 const PolicyMap& policies, | 227 const PolicyMap& policies, |
| 316 const char* policy_name) { | 228 const char* policy_name) { |
| 317 return policies.Get(policy_name) != NULL; | 229 return policies.Get(policy_name) != NULL; |
| 318 } | 230 } |
| 319 | 231 |
| 320 bool DefaultSearchPolicyHandler::AnyDefaultSearchPoliciesSpecified( | 232 bool DefaultSearchPolicyHandler::AnyDefaultSearchPoliciesSpecified( |
| 321 const PolicyMap& policies) { | 233 const PolicyMap& policies) { |
| 322 for (const auto& handler : handlers_) { | 234 for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) { |
| 323 if (policies.Get(handler->policy_name())) | 235 if (policies.Get(policy_map_entry.policy_name)) |
| 324 return true; | 236 return true; |
| 325 } | 237 } |
| 326 return false; | 238 return false; |
| 327 } | 239 } |
| 328 | 240 |
| 329 bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled( | 241 bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled( |
| 330 const PolicyMap& policies) { | 242 const PolicyMap& policies) { |
| 331 const base::Value* provider_enabled = | 243 const base::Value* provider_enabled = |
| 332 policies.GetValue(key::kDefaultSearchProviderEnabled); | 244 policies.GetValue(key::kDefaultSearchProviderEnabled); |
| 333 bool enabled = true; | 245 bool enabled = true; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 360 void DefaultSearchPolicyHandler::EnsureListPrefExists( | 272 void DefaultSearchPolicyHandler::EnsureListPrefExists( |
| 361 PrefValueMap* prefs, | 273 PrefValueMap* prefs, |
| 362 const std::string& path) { | 274 const std::string& path) { |
| 363 base::Value* value; | 275 base::Value* value; |
| 364 base::ListValue* list_value; | 276 base::ListValue* list_value; |
| 365 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) | 277 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) |
| 366 prefs->SetValue(path, base::MakeUnique<base::ListValue>()); | 278 prefs->SetValue(path, base::MakeUnique<base::ListValue>()); |
| 367 } | 279 } |
| 368 | 280 |
| 369 } // namespace policy | 281 } // namespace policy |
| OLD | NEW |