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 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulationSetFromCountryID( |
619 const PrepopulatedEngine*** engines, | 619 int country_id) { |
620 size_t* num_engines) { | 620 const PrepopulatedEngine** engines; |
621 // NOTE: This function should ALWAYS set its outparams. | 621 size_t num_engines; |
622 | |
623 // If you add a new country make sure to update the unit test for coverage. | 622 // If you add a new country make sure to update the unit test for coverage. |
624 switch (GetCountryIDFromPrefs(prefs)) { | 623 switch (country_id) { |
625 | |
626 #define CHAR_A 'A' | 624 #define CHAR_A 'A' |
627 #define CHAR_B 'B' | 625 #define CHAR_B 'B' |
628 #define CHAR_C 'C' | 626 #define CHAR_C 'C' |
629 #define CHAR_D 'D' | 627 #define CHAR_D 'D' |
630 #define CHAR_E 'E' | 628 #define CHAR_E 'E' |
631 #define CHAR_F 'F' | 629 #define CHAR_F 'F' |
632 #define CHAR_G 'G' | 630 #define CHAR_G 'G' |
633 #define CHAR_H 'H' | 631 #define CHAR_H 'H' |
634 #define CHAR_I 'I' | 632 #define CHAR_I 'I' |
635 #define CHAR_J 'J' | 633 #define CHAR_J 'J' |
(...skipping 13 matching lines...) Expand all Loading... | |
649 #define CHAR_X 'X' | 647 #define CHAR_X 'X' |
650 #define CHAR_Y 'Y' | 648 #define CHAR_Y 'Y' |
651 #define CHAR_Z 'Z' | 649 #define CHAR_Z 'Z' |
652 #define CHAR(ch) CHAR_##ch | 650 #define CHAR(ch) CHAR_##ch |
653 #define CODE_TO_ID(code1, code2)\ | 651 #define CODE_TO_ID(code1, code2)\ |
654 (CHAR(code1) << 8 | CHAR(code2)) | 652 (CHAR(code1) << 8 | CHAR(code2)) |
655 | 653 |
656 #define UNHANDLED_COUNTRY(code1, code2)\ | 654 #define UNHANDLED_COUNTRY(code1, code2)\ |
657 case CODE_TO_ID(code1, code2): | 655 case CODE_TO_ID(code1, code2): |
658 #define END_UNHANDLED_COUNTRIES(code1, code2)\ | 656 #define END_UNHANDLED_COUNTRIES(code1, code2)\ |
659 *engines = engines_##code1##code2;\ | 657 engines = engines_##code1##code2;\ |
660 *num_engines = arraysize(engines_##code1##code2);\ | 658 num_engines = arraysize(engines_##code1##code2);\ |
661 return; | 659 break; |
662 #define DECLARE_COUNTRY(code1, code2)\ | 660 #define DECLARE_COUNTRY(code1, code2)\ |
663 UNHANDLED_COUNTRY(code1, code2)\ | 661 UNHANDLED_COUNTRY(code1, code2)\ |
664 END_UNHANDLED_COUNTRIES(code1, code2) | 662 END_UNHANDLED_COUNTRIES(code1, code2) |
665 | 663 |
666 // Countries with their own, dedicated engine set. | 664 // Countries with their own, dedicated engine set. |
667 DECLARE_COUNTRY(A, E) // United Arab Emirates | 665 DECLARE_COUNTRY(A, E) // United Arab Emirates |
668 DECLARE_COUNTRY(A, L) // Albania | 666 DECLARE_COUNTRY(A, L) // Albania |
669 DECLARE_COUNTRY(A, R) // Argentina | 667 DECLARE_COUNTRY(A, R) // Argentina |
670 DECLARE_COUNTRY(A, T) // Austria | 668 DECLARE_COUNTRY(A, T) // Austria |
671 DECLARE_COUNTRY(A, U) // Australia | 669 DECLARE_COUNTRY(A, U) // Australia |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
964 UNHANDLED_COUNTRY(T, V) // Tuvalu | 962 UNHANDLED_COUNTRY(T, V) // Tuvalu |
965 UNHANDLED_COUNTRY(U, G) // Uganda | 963 UNHANDLED_COUNTRY(U, G) // Uganda |
966 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines | 964 UNHANDLED_COUNTRY(V, C) // Saint Vincent and the Grenadines |
967 UNHANDLED_COUNTRY(V, U) // Vanuatu | 965 UNHANDLED_COUNTRY(V, U) // Vanuatu |
968 UNHANDLED_COUNTRY(W, S) // Samoa | 966 UNHANDLED_COUNTRY(W, S) // Samoa |
969 UNHANDLED_COUNTRY(Z, M) // Zambia | 967 UNHANDLED_COUNTRY(Z, M) // Zambia |
970 case kCountryIDUnknown: | 968 case kCountryIDUnknown: |
971 default: // Unhandled location | 969 default: // Unhandled location |
972 END_UNHANDLED_COUNTRIES(def, ault) | 970 END_UNHANDLED_COUNTRIES(def, ault) |
973 } | 971 } |
972 | |
973 std::vector<std::unique_ptr<TemplateURLData>> t_urls; | |
974 for (size_t i = 0; i < num_engines; ++i) | |
975 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | |
976 return t_urls; | |
974 } | 977 } |
975 | 978 |
976 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( | 979 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( |
977 const base::string16& name, | 980 const base::string16& name, |
978 const base::string16& keyword, | 981 const base::string16& keyword, |
979 const base::StringPiece& search_url, | 982 const base::StringPiece& search_url, |
980 const base::StringPiece& suggest_url, | 983 const base::StringPiece& suggest_url, |
981 const base::StringPiece& instant_url, | 984 const base::StringPiece& instant_url, |
982 const base::StringPiece& image_url, | 985 const base::StringPiece& image_url, |
983 const base::StringPiece& new_tab_url, | 986 const base::StringPiece& new_tab_url, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1108 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? | 1111 return (prefs && prefs->HasPrefPath(prefs::kSearchProviderOverridesVersion)) ? |
1109 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : | 1112 prefs->GetInteger(prefs::kSearchProviderOverridesVersion) : |
1110 kCurrentDataVersion; | 1113 kCurrentDataVersion; |
1111 } | 1114 } |
1112 | 1115 |
1113 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( | 1116 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( |
1114 PrefService* prefs, | 1117 PrefService* prefs, |
1115 size_t* default_search_provider_index) { | 1118 size_t* default_search_provider_index) { |
1116 // If there is a set of search engines in the preferences file, it overrides | 1119 // If there is a set of search engines in the preferences file, it overrides |
1117 // the built-in set. | 1120 // the built-in set. |
1118 *default_search_provider_index = 0; | 1121 *default_search_provider_index = 0; |
Peter Kasting
2016/09/23 01:28:36
Nit: Not for this CL, but wanna write a separate c
Ian Wen
2016/09/23 22:01:57
Sure. Let me do it next week.
| |
1119 std::vector<std::unique_ptr<TemplateURLData>> t_urls = | 1122 std::vector<std::unique_ptr<TemplateURLData>> t_urls = |
1120 GetPrepopulatedTemplateURLData(prefs); | 1123 GetPrepopulatedTemplateURLData(prefs); |
1121 if (!t_urls.empty()) | 1124 if (!t_urls.empty()) |
1122 return t_urls; | 1125 return t_urls; |
1123 | 1126 |
1124 const PrepopulatedEngine** engines; | 1127 return GetPrepopulationSetFromCountryID(GetCountryIDFromPrefs(prefs)); |
1125 size_t num_engines; | 1128 } |
1126 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 1129 |
1127 for (size_t i = 0; i != num_engines; ++i) { | 1130 #if defined(OS_ANDROID) |
1128 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | 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; | |
Peter Kasting
2016/09/23 01:28:36
Nit: This is only used to return the empty object
Ian Wen
2016/09/23 22:01:56
Done.
| |
1135 | |
1136 int country_id = CountryStringToCountryID(locale); | |
1137 if (country_id == kCountryIDUnknown || | |
1138 country_id == GetCountryIDFromPrefs(prefs)) { | |
1139 return t_urls; | |
1129 } | 1140 } |
1130 return t_urls; | 1141 |
1142 return GetPrepopulationSetFromCountryID(country_id); | |
1131 } | 1143 } |
1144 #endif | |
1132 | 1145 |
1133 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { | 1146 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { |
1134 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), | 1147 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
1135 std::end(kAllEngines)); | 1148 std::end(kAllEngines)); |
1136 } | 1149 } |
1137 | 1150 |
1138 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( | 1151 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( |
1139 const PrepopulatedEngine& engine) { | 1152 const PrepopulatedEngine& engine) { |
1140 base::ListValue alternate_urls; | 1153 base::ListValue alternate_urls; |
1141 if (engine.alternate_urls) { | 1154 if (engine.alternate_urls) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1251 locale_str.erase(0, territory_delim + 1); | 1264 locale_str.erase(0, territory_delim + 1); |
1252 | 1265 |
1253 // Next remove any codeset/modifier portion and uppercase. | 1266 // Next remove any codeset/modifier portion and uppercase. |
1254 return CountryStringToCountryID( | 1267 return CountryStringToCountryID( |
1255 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); | 1268 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); |
1256 } | 1269 } |
1257 | 1270 |
1258 #endif // OS_* | 1271 #endif // OS_* |
1259 | 1272 |
1260 } // namespace TemplateURLPrepopulateData | 1273 } // namespace TemplateURLPrepopulateData |
OLD | NEW |