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 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
608 // Cache first run Country ID value in prefs, and use it afterwards. This | 608 // Cache first run Country ID value in prefs, and use it afterwards. This |
609 // ensures that just because the user moves around, we won't automatically | 609 // ensures that just because the user moves around, we won't automatically |
610 // make major changes to their available search providers, which would feel | 610 // make major changes to their available search providers, which would feel |
611 // surprising. | 611 // surprising. |
612 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { | 612 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { |
613 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID()); | 613 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID()); |
614 } | 614 } |
615 return prefs->GetInteger(prefs::kCountryIDAtInstall); | 615 return prefs->GetInteger(prefs::kCountryIDAtInstall); |
616 } | 616 } |
617 | 617 |
618 void GetPrepopulationSetFromCountryID(PrefService* prefs, | 618 void GetPrepopulationSetFromCountryID( |
619 const PrepopulatedEngine*** engines, | 619 int country_id, |
620 size_t* num_engines) { | 620 std::vector<std::unique_ptr<TemplateURLData>>& t_urls) { |
Peter Kasting
2016/09/21 21:45:08
Nit: Don't take an outparam by non-const ref. If
Ian Wen
2016/09/21 23:12:38
Done.
| |
621 // NOTE: This function should ALWAYS set its outparams. | 621 const PrepopulatedEngine** engines; |
622 | 622 size_t num_engines; |
623 // If you add a new country make sure to update the unit test for coverage. | 623 // If you add a new country make sure to update the unit test for coverage. |
624 switch (GetCountryIDFromPrefs(prefs)) { | 624 switch (country_id) { |
625 | |
626 #define CHAR_A 'A' | 625 #define CHAR_A 'A' |
627 #define CHAR_B 'B' | 626 #define CHAR_B 'B' |
628 #define CHAR_C 'C' | 627 #define CHAR_C 'C' |
629 #define CHAR_D 'D' | 628 #define CHAR_D 'D' |
630 #define CHAR_E 'E' | 629 #define CHAR_E 'E' |
631 #define CHAR_F 'F' | 630 #define CHAR_F 'F' |
632 #define CHAR_G 'G' | 631 #define CHAR_G 'G' |
633 #define CHAR_H 'H' | 632 #define CHAR_H 'H' |
634 #define CHAR_I 'I' | 633 #define CHAR_I 'I' |
635 #define CHAR_J 'J' | 634 #define CHAR_J 'J' |
(...skipping 13 matching lines...) Expand all Loading... | |
649 #define CHAR_X 'X' | 648 #define CHAR_X 'X' |
650 #define CHAR_Y 'Y' | 649 #define CHAR_Y 'Y' |
651 #define CHAR_Z 'Z' | 650 #define CHAR_Z 'Z' |
652 #define CHAR(ch) CHAR_##ch | 651 #define CHAR(ch) CHAR_##ch |
653 #define CODE_TO_ID(code1, code2)\ | 652 #define CODE_TO_ID(code1, code2)\ |
654 (CHAR(code1) << 8 | CHAR(code2)) | 653 (CHAR(code1) << 8 | CHAR(code2)) |
655 | 654 |
656 #define UNHANDLED_COUNTRY(code1, code2)\ | 655 #define UNHANDLED_COUNTRY(code1, code2)\ |
657 case CODE_TO_ID(code1, code2): | 656 case CODE_TO_ID(code1, code2): |
658 #define END_UNHANDLED_COUNTRIES(code1, code2)\ | 657 #define END_UNHANDLED_COUNTRIES(code1, code2)\ |
659 *engines = engines_##code1##code2;\ | 658 engines = engines_##code1##code2;\ |
660 *num_engines = arraysize(engines_##code1##code2);\ | 659 num_engines = arraysize(engines_##code1##code2);\ |
661 return; | 660 break; |
662 #define DECLARE_COUNTRY(code1, code2)\ | 661 #define DECLARE_COUNTRY(code1, code2)\ |
663 UNHANDLED_COUNTRY(code1, code2)\ | 662 UNHANDLED_COUNTRY(code1, code2)\ |
664 END_UNHANDLED_COUNTRIES(code1, code2) | 663 END_UNHANDLED_COUNTRIES(code1, code2) |
665 | 664 |
666 // Countries with their own, dedicated engine set. | 665 // Countries with their own, dedicated engine set. |
667 DECLARE_COUNTRY(A, E) // United Arab Emirates | 666 DECLARE_COUNTRY(A, E) // United Arab Emirates |
668 DECLARE_COUNTRY(A, L) // Albania | 667 DECLARE_COUNTRY(A, L) // Albania |
669 DECLARE_COUNTRY(A, R) // Argentina | 668 DECLARE_COUNTRY(A, R) // Argentina |
670 DECLARE_COUNTRY(A, T) // Austria | 669 DECLARE_COUNTRY(A, T) // Austria |
671 DECLARE_COUNTRY(A, U) // Australia | 670 DECLARE_COUNTRY(A, U) // Australia |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
964 UNHANDLED_COUNTRY(T, V) // Tuvalu | 963 UNHANDLED_COUNTRY(T, V) // Tuvalu |
965 UNHANDLED_COUNTRY(U, G) // Uganda | 964 UNHANDLED_COUNTRY(U, G) // Uganda |
966 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines | 965 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines |
967 UNHANDLED_COUNTRY(V, U) // Vanuatu | 966 UNHANDLED_COUNTRY(V, U) // Vanuatu |
968 UNHANDLED_COUNTRY(W, S) // Samoa | 967 UNHANDLED_COUNTRY(W, S) // Samoa |
969 UNHANDLED_COUNTRY(Z, M) // Zambia | 968 UNHANDLED_COUNTRY(Z, M) // Zambia |
970 case kCountryIDUnknown: | 969 case kCountryIDUnknown: |
971 default: // Unhandled location | 970 default: // Unhandled location |
972 END_UNHANDLED_COUNTRIES(def, ault) | 971 END_UNHANDLED_COUNTRIES(def, ault) |
973 } | 972 } |
973 for (size_t i = 0; i < num_engines; ++i) { | |
Peter Kasting
2016/09/21 21:45:08
Nit: Blank line above this.
No {}.
Ian Wen
2016/09/21 23:12:38
Done.
| |
974 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | |
975 } | |
974 } | 976 } |
975 | 977 |
976 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( | 978 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( |
977 const base::string16& name, | 979 const base::string16& name, |
978 const base::string16& keyword, | 980 const base::string16& keyword, |
979 const base::StringPiece& search_url, | 981 const base::StringPiece& search_url, |
980 const base::StringPiece& suggest_url, | 982 const base::StringPiece& suggest_url, |
981 const base::StringPiece& instant_url, | 983 const base::StringPiece& instant_url, |
982 const base::StringPiece& image_url, | 984 const base::StringPiece& image_url, |
983 const base::StringPiece& new_tab_url, | 985 const base::StringPiece& new_tab_url, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1114 PrefService* prefs, | 1116 PrefService* prefs, |
1115 size_t* default_search_provider_index) { | 1117 size_t* default_search_provider_index) { |
1116 // If there is a set of search engines in the preferences file, it overrides | 1118 // If there is a set of search engines in the preferences file, it overrides |
1117 // the built-in set. | 1119 // the built-in set. |
1118 *default_search_provider_index = 0; | 1120 *default_search_provider_index = 0; |
1119 std::vector<std::unique_ptr<TemplateURLData>> t_urls = | 1121 std::vector<std::unique_ptr<TemplateURLData>> t_urls = |
1120 GetPrepopulatedTemplateURLData(prefs); | 1122 GetPrepopulatedTemplateURLData(prefs); |
1121 if (!t_urls.empty()) | 1123 if (!t_urls.empty()) |
1122 return t_urls; | 1124 return t_urls; |
1123 | 1125 |
1124 const PrepopulatedEngine** engines; | 1126 GetPrepopulationSetFromCountryID(GetCountryIDFromPrefs(prefs), t_urls); |
1125 size_t num_engines; | |
1126 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | |
1127 for (size_t i = 0; i != num_engines; ++i) { | |
1128 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | |
1129 } | |
1130 return t_urls; | 1127 return t_urls; |
1131 } | 1128 } |
1132 | 1129 |
1130 #if defined(OS_ANDROID) | |
1131 std::vector<std::unique_ptr<TemplateURLData>> GetLocalPrepopulatedEngines( | |
1132 const std::string& locale, | |
1133 PrefService* prefs) { | |
1134 std::vector<std::unique_ptr<TemplateURLData>> t_urls; | |
1135 | |
1136 int country_id = CountryStringToCountryID(locale); | |
1137 if (country_id == kCountryIDUnknown || | |
1138 country_id == GetCountryIDFromPrefs(prefs)) { | |
1139 return t_urls; | |
1140 } | |
1141 | |
1142 GetPrepopulationSetFromCountryID(country_id, t_urls); | |
1143 return t_urls; | |
1144 } | |
1145 #endif | |
1146 | |
1133 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { | 1147 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { |
1134 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), | 1148 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
1135 std::end(kAllEngines)); | 1149 std::end(kAllEngines)); |
1136 } | 1150 } |
1137 | 1151 |
1138 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( | 1152 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( |
1139 const PrepopulatedEngine& engine) { | 1153 const PrepopulatedEngine& engine) { |
1140 base::ListValue alternate_urls; | 1154 base::ListValue alternate_urls; |
1141 if (engine.alternate_urls) { | 1155 if (engine.alternate_urls) { |
1142 for (size_t i = 0; i < engine.alternate_urls_size; ++i) | 1156 for (size_t i = 0; i < engine.alternate_urls_size; ++i) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1251 locale_str.erase(0, territory_delim + 1); | 1265 locale_str.erase(0, territory_delim + 1); |
1252 | 1266 |
1253 // Next remove any codeset/modifier portion and uppercase. | 1267 // Next remove any codeset/modifier portion and uppercase. |
1254 return CountryStringToCountryID( | 1268 return CountryStringToCountryID( |
1255 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); | 1269 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); |
1256 } | 1270 } |
1257 | 1271 |
1258 #endif // OS_* | 1272 #endif // OS_* |
1259 | 1273 |
1260 } // namespace TemplateURLPrepopulateData | 1274 } // namespace TemplateURLPrepopulateData |
OLD | NEW |