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/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 |
| (...skipping 998 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1009 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { | 1009 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { |
| 1010 std::string alternate_url; | 1010 std::string alternate_url; |
| 1011 alternate_urls.GetString(i, &alternate_url); | 1011 alternate_urls.GetString(i, &alternate_url); |
| 1012 DCHECK(!alternate_url.empty()); | 1012 DCHECK(!alternate_url.empty()); |
| 1013 data->alternate_urls.push_back(alternate_url); | 1013 data->alternate_urls.push_back(alternate_url); |
| 1014 } | 1014 } |
| 1015 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); | 1015 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); |
| 1016 return data; | 1016 return data; |
| 1017 } | 1017 } |
| 1018 | 1018 |
| 1019 ScopedVector<TemplateURLData> GetPrepopulatedTemplateURLData( | 1019 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedTemplateURLData( |
| 1020 PrefService* prefs) { | 1020 PrefService* prefs) { |
| 1021 ScopedVector<TemplateURLData> t_urls; | 1021 std::vector<std::unique_ptr<TemplateURLData>> t_urls; |
| 1022 if (!prefs) | 1022 if (!prefs) |
| 1023 return t_urls; | 1023 return t_urls; |
| 1024 | 1024 |
| 1025 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); | 1025 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); |
| 1026 if (!list) | 1026 if (!list) |
| 1027 return t_urls; | 1027 return t_urls; |
| 1028 | 1028 |
| 1029 size_t num_engines = list->GetSize(); | 1029 size_t num_engines = list->GetSize(); |
| 1030 for (size_t i = 0; i != num_engines; ++i) { | 1030 for (size_t i = 0; i != num_engines; ++i) { |
| 1031 const base::DictionaryValue* engine; | 1031 const base::DictionaryValue* engine; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1062 engine->GetString("image_url", &image_url); | 1062 engine->GetString("image_url", &image_url); |
| 1063 engine->GetString("new_tab_url", &new_tab_url); | 1063 engine->GetString("new_tab_url", &new_tab_url); |
| 1064 engine->GetString("contextual_search_url", &contextual_search_url); | 1064 engine->GetString("contextual_search_url", &contextual_search_url); |
| 1065 engine->GetString("search_url_post_params", &search_url_post_params); | 1065 engine->GetString("search_url_post_params", &search_url_post_params); |
| 1066 engine->GetString("suggest_url_post_params", &suggest_url_post_params); | 1066 engine->GetString("suggest_url_post_params", &suggest_url_post_params); |
| 1067 engine->GetString("instant_url_post_params", &instant_url_post_params); | 1067 engine->GetString("instant_url_post_params", &instant_url_post_params); |
| 1068 engine->GetString("image_url_post_params", &image_url_post_params); | 1068 engine->GetString("image_url_post_params", &image_url_post_params); |
| 1069 engine->GetList("alternate_urls", &alternate_urls); | 1069 engine->GetList("alternate_urls", &alternate_urls); |
| 1070 engine->GetString("search_terms_replacement_key", | 1070 engine->GetString("search_terms_replacement_key", |
| 1071 &search_terms_replacement_key); | 1071 &search_terms_replacement_key); |
| 1072 t_urls.push_back(MakePrepopulatedTemplateURLData(name, keyword, | 1072 t_urls.push_back(MakePrepopulatedTemplateURLData( |
| 1073 search_url, suggest_url, instant_url, image_url, new_tab_url, | 1073 name, keyword, search_url, suggest_url, instant_url, image_url, |
| 1074 contextual_search_url, search_url_post_params, | 1074 new_tab_url, contextual_search_url, search_url_post_params, |
| 1075 suggest_url_post_params, instant_url_post_params, | 1075 suggest_url_post_params, instant_url_post_params, |
| 1076 image_url_post_params, favicon_url, encoding, *alternate_urls, | 1076 image_url_post_params, favicon_url, encoding, *alternate_urls, |
| 1077 search_terms_replacement_key, id).release()); | 1077 search_terms_replacement_key, id)); |
| 1078 } | 1078 } |
| 1079 } | 1079 } |
| 1080 return t_urls; | 1080 return t_urls; |
| 1081 } | 1081 } |
| 1082 | 1082 |
| 1083 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { | 1083 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { |
| 1084 return prepopulated_url.is_valid() && | 1084 return prepopulated_url.is_valid() && |
| 1085 net::registry_controlled_domains::SameDomainOrHost( | 1085 net::registry_controlled_domains::SameDomainOrHost( |
| 1086 given_url, prepopulated_url, | 1086 given_url, prepopulated_url, |
| 1087 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 1087 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 1088 } | 1088 } |
| 1089 | 1089 |
| 1090 } // namespace | 1090 } // namespace |
| 1091 | 1091 |
| 1092 // Global functions ----------------------------------------------------------- | 1092 // Global functions ----------------------------------------------------------- |
| 1093 | 1093 |
| 1094 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | 1094 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| 1095 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); | 1095 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); |
| 1096 registry->RegisterListPref(prefs::kSearchProviderOverrides); | 1096 registry->RegisterListPref(prefs::kSearchProviderOverrides); |
| 1097 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); | 1097 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); |
| 1098 } | 1098 } |
| 1099 | 1099 |
| 1100 int GetDataVersion(PrefService* prefs) { | 1100 int GetDataVersion(PrefService* prefs) { |
| 1101 // Allow tests to override the local version. | 1101 // Allow tests to override the local version. |
| 1102 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? | 1102 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? |
| 1103 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : | 1103 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : |
| 1104 kCurrentDataVersion; | 1104 kCurrentDataVersion; |
| 1105 } | 1105 } |
| 1106 | 1106 |
| 1107 ScopedVector<TemplateURLData> GetPrepopulatedEngines( | 1107 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( |
| 1108 PrefService* prefs, | 1108 PrefService* prefs, |
| 1109 size_t* default_search_provider_index) { | 1109 size_t* default_search_provider_index) { |
| 1110 // If there is a set of search engines in the preferences file, it overrides | 1110 // If there is a set of search engines in the preferences file, it overrides |
| 1111 // the built-in set. | 1111 // the built-in set. |
| 1112 *default_search_provider_index = 0; | 1112 *default_search_provider_index = 0; |
| 1113 ScopedVector<TemplateURLData> t_urls = GetPrepopulatedTemplateURLData(prefs); | 1113 std::vector<std::unique_ptr<TemplateURLData>> t_urls = |
| 1114 GetPrepopulatedTemplateURLData(prefs); | |
| 1114 if (!t_urls.empty()) | 1115 if (!t_urls.empty()) |
| 1115 return t_urls; | 1116 return t_urls; |
| 1116 | 1117 |
| 1117 const PrepopulatedEngine** engines; | 1118 const PrepopulatedEngine** engines; |
| 1118 size_t num_engines; | 1119 size_t num_engines; |
| 1119 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 1120 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); |
| 1120 for (size_t i = 0; i != num_engines; ++i) { | 1121 for (size_t i = 0; i != num_engines; ++i) { |
| 1121 t_urls.push_back( | 1122 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); |
| 1122 MakeTemplateURLDataFromPrepopulatedEngine(*engines[i]).release()); | |
| 1123 } | 1123 } |
| 1124 return t_urls; | 1124 return t_urls; |
| 1125 } | 1125 } |
| 1126 | 1126 |
| 1127 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { | 1127 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { |
| 1128 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), | 1128 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
| 1129 std::end(kAllEngines)); | 1129 std::end(kAllEngines)); |
| 1130 } | 1130 } |
| 1131 | 1131 |
| 1132 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( | 1132 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1150 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { | 1150 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { |
| 1151 if (!prefs) | 1151 if (!prefs) |
| 1152 return; | 1152 return; |
| 1153 | 1153 |
| 1154 prefs->ClearPref(prefs::kSearchProviderOverrides); | 1154 prefs->ClearPref(prefs::kSearchProviderOverrides); |
| 1155 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); | 1155 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); |
| 1156 } | 1156 } |
| 1157 | 1157 |
| 1158 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( | 1158 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( |
| 1159 PrefService* prefs) { | 1159 PrefService* prefs) { |
| 1160 std::unique_ptr<TemplateURLData> default_search_provider; | |
| 1161 size_t default_search_index; | 1160 size_t default_search_index; |
| 1162 // This could be more efficient. We are loading all the URLs to only keep | 1161 // This could be more efficient. We are loading all the URLs to only keep |
| 1163 // the first one. | 1162 // the first one. |
| 1164 ScopedVector<TemplateURLData> loaded_urls = | 1163 std::vector<std::unique_ptr<TemplateURLData>> loaded_urls = |
| 1165 GetPrepopulatedEngines(prefs, &default_search_index); | 1164 GetPrepopulatedEngines(prefs, &default_search_index); |
| 1166 if (default_search_index < loaded_urls.size()) { | 1165 |
| 1167 default_search_provider.reset(loaded_urls[default_search_index]); | 1166 if (default_search_index < loaded_urls.size()) |
| 1168 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); | 1167 return std::move(loaded_urls[default_search_index]); |
|
Peter Kasting
2016/09/01 23:17:32
Nit: Maybe
return (default_search_index < loade
Avi (use Gerrit)
2016/09/01 23:26:43
Done.
| |
| 1169 } | 1168 |
| 1170 return default_search_provider; | 1169 return std::unique_ptr<TemplateURLData>(); |
|
Peter Kasting
2016/09/01 23:17:32
Nit: return nullptr?
Avi (use Gerrit)
2016/09/01 23:26:43
Acknowledged.
| |
| 1171 } | 1170 } |
| 1172 | 1171 |
| 1173 SearchEngineType GetEngineType(const GURL& url) { | 1172 SearchEngineType GetEngineType(const GURL& url) { |
| 1174 DCHECK(url.is_valid()); | 1173 DCHECK(url.is_valid()); |
| 1175 | 1174 |
| 1176 // Check using TLD+1s, in order to more aggressively match search engine types | 1175 // Check using TLD+1s, in order to more aggressively match search engine types |
| 1177 // for data imported from other browsers. | 1176 // for data imported from other browsers. |
| 1178 // | 1177 // |
| 1179 // First special-case Google, because the prepopulate URL for it will not | 1178 // First special-case Google, because the prepopulate URL for it will not |
| 1180 // convert to a GURL and thus won't have an origin. Instead see if the | 1179 // convert to a GURL and thus won't have an origin. Instead see if the |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1260 base::ToUpperASCII(locale_str[begin]), | 1259 base::ToUpperASCII(locale_str[begin]), |
| 1261 base::ToUpperASCII(locale_str[begin + 1])); | 1260 base::ToUpperASCII(locale_str[begin + 1])); |
| 1262 } | 1261 } |
| 1263 | 1262 |
| 1264 return kCountryIDUnknown; | 1263 return kCountryIDUnknown; |
| 1265 } | 1264 } |
| 1266 | 1265 |
| 1267 #endif // OS_* | 1266 #endif // OS_* |
| 1268 | 1267 |
| 1269 } // namespace TemplateURLPrepopulateData | 1268 } // namespace TemplateURLPrepopulateData |
| OLD | NEW |