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 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 // Cache first run Country ID value in prefs, and use it afterwards. This | 602 // Cache first run Country ID value in prefs, and use it afterwards. This |
603 // ensures that just because the user moves around, we won't automatically | 603 // ensures that just because the user moves around, we won't automatically |
604 // make major changes to their available search providers, which would feel | 604 // make major changes to their available search providers, which would feel |
605 // surprising. | 605 // surprising. |
606 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { | 606 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { |
607 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID()); | 607 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID()); |
608 } | 608 } |
609 return prefs->GetInteger(prefs::kCountryIDAtInstall); | 609 return prefs->GetInteger(prefs::kCountryIDAtInstall); |
610 } | 610 } |
611 | 611 |
612 void GetPrepopulationSetFromCountryID(PrefService* prefs, | 612 int CountryStringToCountryID(std::string country) { |
613 if (country.length() != 2) | |
614 return kCountryIDUnknown; | |
615 | |
616 return CountryCharsToCountryIDWithUpdate(country[0], country[1]); | |
617 } | |
Peter Kasting
2016/09/19 21:06:23
I added this in https://codereview.chromium.org/23
Ian Wen
2016/09/20 04:48:03
Done.
| |
618 | |
619 void GetPrepopulationSetFromCountryID(int country_id, | |
613 const PrepopulatedEngine*** engines, | 620 const PrepopulatedEngine*** engines, |
614 size_t* num_engines) { | 621 size_t* num_engines) { |
615 // NOTE: This function should ALWAYS set its outparams. | 622 // NOTE: This function should ALWAYS set its outparams. |
616 | 623 |
617 // If you add a new country make sure to update the unit test for coverage. | 624 // If you add a new country make sure to update the unit test for coverage. |
618 switch (GetCountryIDFromPrefs(prefs)) { | 625 switch (country_id) { |
619 | |
620 #define CHAR_A 'A' | 626 #define CHAR_A 'A' |
621 #define CHAR_B 'B' | 627 #define CHAR_B 'B' |
622 #define CHAR_C 'C' | 628 #define CHAR_C 'C' |
623 #define CHAR_D 'D' | 629 #define CHAR_D 'D' |
624 #define CHAR_E 'E' | 630 #define CHAR_E 'E' |
625 #define CHAR_F 'F' | 631 #define CHAR_F 'F' |
626 #define CHAR_G 'G' | 632 #define CHAR_G 'G' |
627 #define CHAR_H 'H' | 633 #define CHAR_H 'H' |
628 #define CHAR_I 'I' | 634 #define CHAR_I 'I' |
629 #define CHAR_J 'J' | 635 #define CHAR_J 'J' |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
960 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines | 966 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines |
961 UNHANDLED_COUNTRY(V, U) // Vanuatu | 967 UNHANDLED_COUNTRY(V, U) // Vanuatu |
962 UNHANDLED_COUNTRY(W, S) // Samoa | 968 UNHANDLED_COUNTRY(W, S) // Samoa |
963 UNHANDLED_COUNTRY(Z, M) // Zambia | 969 UNHANDLED_COUNTRY(Z, M) // Zambia |
964 case kCountryIDUnknown: | 970 case kCountryIDUnknown: |
965 default: // Unhandled location | 971 default: // Unhandled location |
966 END_UNHANDLED_COUNTRIES(def, ault) | 972 END_UNHANDLED_COUNTRIES(def, ault) |
967 } | 973 } |
968 } | 974 } |
969 | 975 |
976 void GetPrepopulationSetFromPrefs(PrefService* prefs, | |
Peter Kasting
2016/09/19 21:06:23
Nit: You only call this helper once, and it's simp
Ian Wen
2016/09/20 04:48:03
Done.
| |
977 const PrepopulatedEngine*** engines, | |
978 size_t* num_engines) { | |
979 GetPrepopulationSetFromCountryID(GetCountryIDFromPrefs(prefs), engines, | |
980 num_engines); | |
981 } | |
982 | |
970 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( | 983 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( |
971 const base::string16& name, | 984 const base::string16& name, |
972 const base::string16& keyword, | 985 const base::string16& keyword, |
973 const base::StringPiece& search_url, | 986 const base::StringPiece& search_url, |
974 const base::StringPiece& suggest_url, | 987 const base::StringPiece& suggest_url, |
975 const base::StringPiece& instant_url, | 988 const base::StringPiece& instant_url, |
976 const base::StringPiece& image_url, | 989 const base::StringPiece& image_url, |
977 const base::StringPiece& new_tab_url, | 990 const base::StringPiece& new_tab_url, |
978 const base::StringPiece& contextual_search_url, | 991 const base::StringPiece& contextual_search_url, |
979 const base::StringPiece& search_url_post_params, | 992 const base::StringPiece& search_url_post_params, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1097 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); | 1110 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); |
1098 } | 1111 } |
1099 | 1112 |
1100 int GetDataVersion(PrefService* prefs) { | 1113 int GetDataVersion(PrefService* prefs) { |
1101 // Allow tests to override the local version. | 1114 // Allow tests to override the local version. |
1102 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? | 1115 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? |
1103 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : | 1116 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : |
1104 kCurrentDataVersion; | 1117 kCurrentDataVersion; |
1105 } | 1118 } |
1106 | 1119 |
1120 std::vector<std::unique_ptr<TemplateURLData>> GetLocalPrepopulatedEngines( | |
Peter Kasting
2016/09/19 21:06:23
Nit: Function definition order should match .h dec
Ian Wen
2016/09/20 04:48:03
Done.
| |
1121 const std::string& locale, | |
1122 PrefService* prefs, | |
1123 bool* need_to_load) { | |
1124 std::vector<std::unique_ptr<TemplateURLData>> t_urls; | |
1125 | |
1126 int country_id = CountryStringToCountryID(locale); | |
1127 DCHECK(country_id != kCountryIDUnknown); | |
Peter Kasting
2016/09/19 21:06:23
This DCHECK makes me nervous. Are you sure the ca
Ian Wen
2016/09/20 04:48:03
Done.
| |
1128 | |
1129 if (country_id == GetCountryIDFromPrefs(prefs)) { | |
1130 *need_to_load = false; | |
1131 return t_urls; | |
1132 } | |
1133 *need_to_load = true; | |
1134 | |
1135 const PrepopulatedEngine** engines; | |
1136 size_t num_engines; | |
1137 GetPrepopulationSetFromCountryID(country_id, &engines, &num_engines); | |
Peter Kasting
2016/09/19 21:06:23
Nit: I suggest modifying GetPrepopulationSetFromCo
| |
1138 for (size_t i = 0; i != num_engines; ++i) { | |
1139 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | |
1140 } | |
1141 return t_urls; | |
1142 } | |
1143 | |
1107 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( | 1144 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( |
1108 PrefService* prefs, | 1145 PrefService* prefs, |
1109 size_t* default_search_provider_index) { | 1146 size_t* default_search_provider_index) { |
1110 // If there is a set of search engines in the preferences file, it overrides | 1147 // If there is a set of search engines in the preferences file, it overrides |
1111 // the built-in set. | 1148 // the built-in set. |
1112 *default_search_provider_index = 0; | 1149 *default_search_provider_index = 0; |
1113 std::vector<std::unique_ptr<TemplateURLData>> t_urls = | 1150 std::vector<std::unique_ptr<TemplateURLData>> t_urls = |
1114 GetPrepopulatedTemplateURLData(prefs); | 1151 GetPrepopulatedTemplateURLData(prefs); |
1115 if (!t_urls.empty()) | 1152 if (!t_urls.empty()) |
1116 return t_urls; | 1153 return t_urls; |
1117 | 1154 |
1118 const PrepopulatedEngine** engines; | 1155 const PrepopulatedEngine** engines; |
1119 size_t num_engines; | 1156 size_t num_engines; |
1120 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 1157 GetPrepopulationSetFromPrefs(prefs, &engines, &num_engines); |
1121 for (size_t i = 0; i != num_engines; ++i) { | 1158 for (size_t i = 0; i != num_engines; ++i) { |
1122 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | 1159 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); |
1123 } | 1160 } |
1124 return t_urls; | 1161 return t_urls; |
1125 } | 1162 } |
1126 | 1163 |
1127 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { | 1164 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { |
1128 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), | 1165 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
1129 std::end(kAllEngines)); | 1166 std::end(kAllEngines)); |
1130 } | 1167 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1258 base::ToUpperASCII(locale_str[begin]), | 1295 base::ToUpperASCII(locale_str[begin]), |
1259 base::ToUpperASCII(locale_str[begin + 1])); | 1296 base::ToUpperASCII(locale_str[begin + 1])); |
1260 } | 1297 } |
1261 | 1298 |
1262 return kCountryIDUnknown; | 1299 return kCountryIDUnknown; |
1263 } | 1300 } |
1264 | 1301 |
1265 #endif // OS_* | 1302 #endif // OS_* |
1266 | 1303 |
1267 } // namespace TemplateURLPrepopulateData | 1304 } // namespace TemplateURLPrepopulateData |
OLD | NEW |