Chromium Code Reviews| 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" | 14 #include "base/strings/string_util.h" |
|
Peter Kasting
2017/01/06 00:09:56
Can this #include or any others now be removed?
Alexander Yashkin
2017/01/09 06:11:38
Removed string_util.h.
| |
| 15 #include "components/policy/core/browser/policy_error_map.h" | 15 #include "components/policy/core/browser/policy_error_map.h" |
| 16 #include "components/policy/core/common/policy_map.h" | 16 #include "components/policy/core/common/policy_map.h" |
| 17 #include "components/policy/policy_constants.h" | 17 #include "components/policy/policy_constants.h" |
| 18 #include "components/prefs/pref_value_map.h" | 18 #include "components/prefs/pref_value_map.h" |
| 19 #include "components/search_engines/default_search_manager.h" | 19 #include "components/search_engines/default_search_manager.h" |
| 20 #include "components/search_engines/search_engines_pref_names.h" | 20 #include "components/search_engines/search_engines_pref_names.h" |
| 21 #include "components/search_engines/search_terms_data.h" | 21 #include "components/search_engines/search_terms_data.h" |
| 22 #include "components/search_engines/template_url.h" | 22 #include "components/search_engines/template_url.h" |
| 23 #include "grit/components_strings.h" | 23 #include "grit/components_strings.h" |
| 24 | 24 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 52 bool is_string = policy_value->GetAsString(&str); | 52 bool is_string = policy_value->GetAsString(&str); |
| 53 DCHECK(is_string); | 53 DCHECK(is_string); |
| 54 } | 54 } |
| 55 dict->SetString(key, str); | 55 dict->SetString(key, str); |
| 56 } | 56 } |
| 57 | 57 |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 // List of policy types to preference names, for policies affecting the default | 60 // List of policy types to preference names, for policies affecting the default |
| 61 // search provider. | 61 // search provider. |
| 62 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { | 62 const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[] = { |
|
Peter Kasting
2017/01/06 00:09:56
Will we have more entries in this map in the futur
Alexander Yashkin
2017/01/09 06:11:38
Removed kDefaultSearchPolicyMap and handlers_ from
| |
| 63 { key::kDefaultSearchProviderEnabled, | 63 { key::kDefaultSearchProviderEnabled, |
| 64 prefs::kDefaultSearchProviderEnabled, | 64 prefs::kDefaultSearchProviderEnabled, |
| 65 base::Value::Type::BOOLEAN }, | 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 }; | 66 }; |
| 112 | 67 |
| 113 // List of policy types to preference names, for policies affecting the default | 68 // List of policy types to preference names, for policies affecting the default |
| 114 // search provider. | 69 // search provider. |
| 115 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { | 70 const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = { |
| 116 {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName, | 71 {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName, |
| 117 base::Value::Type::STRING}, | 72 base::Value::Type::STRING}, |
| 118 {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword, | 73 {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword, |
| 119 base::Value::Type::STRING}, | 74 base::Value::Type::STRING}, |
| 120 {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL, | 75 {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 140 DefaultSearchManager::kSearchURLPostParams, base::Value::Type::STRING}, | 95 DefaultSearchManager::kSearchURLPostParams, base::Value::Type::STRING}, |
| 141 {key::kDefaultSearchProviderSuggestURLPostParams, | 96 {key::kDefaultSearchProviderSuggestURLPostParams, |
| 142 DefaultSearchManager::kSuggestionsURLPostParams, | 97 DefaultSearchManager::kSuggestionsURLPostParams, |
| 143 base::Value::Type::STRING}, | 98 base::Value::Type::STRING}, |
| 144 {key::kDefaultSearchProviderInstantURLPostParams, | 99 {key::kDefaultSearchProviderInstantURLPostParams, |
| 145 DefaultSearchManager::kInstantURLPostParams, base::Value::Type::STRING}, | 100 DefaultSearchManager::kInstantURLPostParams, base::Value::Type::STRING}, |
| 146 {key::kDefaultSearchProviderImageURLPostParams, | 101 {key::kDefaultSearchProviderImageURLPostParams, |
| 147 DefaultSearchManager::kImageURLPostParams, base::Value::Type::STRING}, | 102 DefaultSearchManager::kImageURLPostParams, base::Value::Type::STRING}, |
| 148 }; | 103 }; |
| 149 | 104 |
| 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 ----------------------------------- | 105 // DefaultSearchPolicyHandler implementation ----------------------------------- |
| 184 | 106 |
| 185 DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() { | 107 DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() { |
| 186 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyMap); ++i) { | 108 for (size_t i = 0; i < arraysize(kDefaultSearchPolicyMap); ++i) { |
| 187 const char* policy_name = kDefaultSearchPolicyMap[i].policy_name; | 109 const char* policy_name = kDefaultSearchPolicyMap[i].policy_name; |
| 188 if (policy_name == key::kDefaultSearchProviderEncodings) { | 110 handlers_.push_back(base::MakeUnique<SimplePolicyHandler>( |
| 189 handlers_.push_back( | 111 policy_name, kDefaultSearchPolicyMap[i].preference_path, |
| 190 base::MakeUnique<DefaultSearchEncodingsPolicyHandler>()); | 112 kDefaultSearchPolicyMap[i].value_type)); |
| 191 } else { | |
| 192 handlers_.push_back(base::MakeUnique<SimplePolicyHandler>( | |
| 193 policy_name, kDefaultSearchPolicyMap[i].preference_path, | |
| 194 kDefaultSearchPolicyMap[i].value_type)); | |
| 195 } | |
| 196 } | 113 } |
| 197 } | 114 } |
| 198 | 115 |
| 199 DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() {} | 116 DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() {} |
| 200 | 117 |
| 201 bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, | 118 bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies, |
| 202 PolicyErrorMap* errors) { | 119 PolicyErrorMap* errors) { |
| 203 if (!CheckIndividualPolicies(policies, errors)) | 120 if (!CheckIndividualPolicies(policies, errors)) |
| 204 return false; | 121 return false; |
| 205 | 122 |
| 206 if (DefaultSearchProviderIsDisabled(policies)) { | 123 if (DefaultSearchProviderIsDisabled(policies)) { |
| 207 // Add an error for all specified default search policies except | 124 // Add an error for all specified default search policies except |
| 208 // DefaultSearchProviderEnabled. | 125 // DefaultSearchProviderEnabled. |
| 209 | 126 |
| 210 for (const auto& handler : handlers_) { | 127 for (const auto& handler : handlers_) { |
| 211 const char* policy_name = handler->policy_name(); | 128 const char* policy_name = handler->policy_name(); |
| 212 if (policy_name != key::kDefaultSearchProviderEnabled && | 129 if (policy_name != key::kDefaultSearchProviderEnabled && |
|
Peter Kasting
2017/01/06 00:09:56
Can this ever succeed anymore?
Alexander Yashkin
2017/01/09 06:11:38
I think after removing entries from kDefaultSearch
| |
| 213 HasDefaultSearchPolicy(policies, policy_name)) { | 130 HasDefaultSearchPolicy(policies, policy_name)) { |
| 214 errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); | 131 errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED); |
| 215 } | 132 } |
| 216 } | 133 } |
| 217 return true; | 134 return true; |
| 218 } | 135 } |
| 219 | 136 |
| 220 const base::Value* url; | 137 const base::Value* url; |
| 221 std::string dummy; | 138 std::string dummy; |
| 222 if (DefaultSearchURLIsValid(policies, &url, &dummy) || | 139 if (DefaultSearchURLIsValid(policies, &url, &dummy) || |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 void DefaultSearchPolicyHandler::EnsureListPrefExists( | 277 void DefaultSearchPolicyHandler::EnsureListPrefExists( |
| 361 PrefValueMap* prefs, | 278 PrefValueMap* prefs, |
| 362 const std::string& path) { | 279 const std::string& path) { |
| 363 base::Value* value; | 280 base::Value* value; |
| 364 base::ListValue* list_value; | 281 base::ListValue* list_value; |
| 365 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) | 282 if (!prefs->GetValue(path, &value) || !value->GetAsList(&list_value)) |
| 366 prefs->SetValue(path, base::MakeUnique<base::ListValue>()); | 283 prefs->SetValue(path, base::MakeUnique<base::ListValue>()); |
| 367 } | 284 } |
| 368 | 285 |
| 369 } // namespace policy | 286 } // namespace policy |
| OLD | NEW |