| 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/template_url_prepopulate_data.h" | 5 #include "components/search_engines/template_url_prepopulate_data.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 8 #include <locale.h> | 8 #include <locale.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "components/google/core/browser/google_util.h" | 19 #include "components/google/core/browser/google_util.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 22 #include "components/search_engines/prepopulated_engines.h" | 22 #include "components/search_engines/prepopulated_engines.h" |
| 23 #include "components/search_engines/search_engines_pref_names.h" | 23 #include "components/search_engines/search_engines_pref_names.h" |
| 24 #include "components/search_engines/template_url.h" | 24 #include "components/search_engines/template_url_data.h" |
| 25 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 25 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
| 29 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. | 29 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. |
| 30 #elif defined(OS_MACOSX) | 30 #elif defined(OS_MACOSX) |
| 31 #include "base/mac/scoped_cftyperef.h" | 31 #include "base/mac/scoped_cftyperef.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| (...skipping 1101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1136 return MakePrepopulatedTemplateURLData( | 1136 return MakePrepopulatedTemplateURLData( |
| 1137 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword), | 1137 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword), |
| 1138 engine.search_url, engine.suggest_url, engine.instant_url, | 1138 engine.search_url, engine.suggest_url, engine.instant_url, |
| 1139 engine.image_url, engine.new_tab_url, engine.contextual_search_url, | 1139 engine.image_url, engine.new_tab_url, engine.contextual_search_url, |
| 1140 engine.search_url_post_params, engine.suggest_url_post_params, | 1140 engine.search_url_post_params, engine.suggest_url_post_params, |
| 1141 engine.instant_url_post_params, engine.image_url_post_params, | 1141 engine.instant_url_post_params, engine.image_url_post_params, |
| 1142 engine.favicon_url, engine.encoding, alternate_urls, | 1142 engine.favicon_url, engine.encoding, alternate_urls, |
| 1143 engine.search_terms_replacement_key, engine.id); | 1143 engine.search_terms_replacement_key, engine.id); |
| 1144 } | 1144 } |
| 1145 | 1145 |
| 1146 std::vector<const PrepopulatedEngine*> GetAllKnownPrepopulatedEngines() { |
| 1147 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
| 1148 std::end(kAllEngines)); |
| 1149 } |
| 1150 |
| 1146 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { | 1151 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { |
| 1147 if (!prefs) | 1152 if (!prefs) |
| 1148 return; | 1153 return; |
| 1149 | 1154 |
| 1150 prefs->ClearPref(prefs::kSearchProviderOverrides); | 1155 prefs->ClearPref(prefs::kSearchProviderOverrides); |
| 1151 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); | 1156 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); |
| 1152 } | 1157 } |
| 1153 | 1158 |
| 1154 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( | 1159 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( |
| 1155 PrefService* prefs) { | 1160 PrefService* prefs) { |
| 1156 std::unique_ptr<TemplateURLData> default_search_provider; | 1161 std::unique_ptr<TemplateURLData> default_search_provider; |
| 1157 size_t default_search_index; | 1162 size_t default_search_index; |
| 1158 // This could be more efficient. We are loading all the URLs to only keep | 1163 // This could be more efficient. We are loading all the URLs to only keep |
| 1159 // the first one. | 1164 // the first one. |
| 1160 ScopedVector<TemplateURLData> loaded_urls = | 1165 ScopedVector<TemplateURLData> loaded_urls = |
| 1161 GetPrepopulatedEngines(prefs, &default_search_index); | 1166 GetPrepopulatedEngines(prefs, &default_search_index); |
| 1162 if (default_search_index < loaded_urls.size()) { | 1167 if (default_search_index < loaded_urls.size()) { |
| 1163 default_search_provider.reset(loaded_urls[default_search_index]); | 1168 default_search_provider.reset(loaded_urls[default_search_index]); |
| 1164 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); | 1169 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); |
| 1165 } | 1170 } |
| 1166 return default_search_provider; | 1171 return default_search_provider; |
| 1167 } | 1172 } |
| 1168 | 1173 |
| 1169 SearchEngineType GetEngineType(const TemplateURL& url, | |
| 1170 const SearchTermsData& search_terms_data) { | |
| 1171 // By calling ReplaceSearchTerms, we ensure that even TemplateURLs whose URLs | |
| 1172 // can't be directly inspected (e.g. due to containing {google:baseURL}) can | |
| 1173 // be converted to GURLs we can look at. | |
| 1174 GURL gurl(url.url_ref().ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs( | |
| 1175 base::ASCIIToUTF16("x")), search_terms_data)); | |
| 1176 return gurl.is_valid() ? GetEngineType(gurl) : SEARCH_ENGINE_OTHER; | |
| 1177 } | |
| 1178 | |
| 1179 SearchEngineType GetEngineType(const GURL& url) { | 1174 SearchEngineType GetEngineType(const GURL& url) { |
| 1180 DCHECK(url.is_valid()); | 1175 DCHECK(url.is_valid()); |
| 1181 | 1176 |
| 1182 // Check using TLD+1s, in order to more aggressively match search engine types | 1177 // Check using TLD+1s, in order to more aggressively match search engine types |
| 1183 // for data imported from other browsers. | 1178 // for data imported from other browsers. |
| 1184 // | 1179 // |
| 1185 // First special-case Google, because the prepopulate URL for it will not | 1180 // First special-case Google, because the prepopulate URL for it will not |
| 1186 // convert to a GURL and thus won't have an origin. Instead see if the | 1181 // convert to a GURL and thus won't have an origin. Instead see if the |
| 1187 // incoming URL's host is "[*.]google.<TLD>". | 1182 // incoming URL's host is "[*.]google.<TLD>". |
| 1188 if (google_util::IsGoogleHostname(url.host(), | 1183 if (google_util::IsGoogleHostname(url.host(), |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1266 base::ToUpperASCII(locale_str[begin]), | 1261 base::ToUpperASCII(locale_str[begin]), |
| 1267 base::ToUpperASCII(locale_str[begin + 1])); | 1262 base::ToUpperASCII(locale_str[begin + 1])); |
| 1268 } | 1263 } |
| 1269 | 1264 |
| 1270 return kCountryIDUnknown; | 1265 return kCountryIDUnknown; |
| 1271 } | 1266 } |
| 1272 | 1267 |
| 1273 #endif // OS_* | 1268 #endif // OS_* |
| 1274 | 1269 |
| 1275 } // namespace TemplateURLPrepopulateData | 1270 } // namespace TemplateURLPrepopulateData |
| OLD | NEW |