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 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
529 } | 529 } |
530 | 530 |
531 // SPECIAL CASE: Timor-Leste changed from 'TP' to 'TL' in 2002. Windows XP | 531 // SPECIAL CASE: Timor-Leste changed from 'TP' to 'TL' in 2002. Windows XP |
532 // predates this; we therefore map this value. | 532 // predates this; we therefore map this value. |
533 if (c1 == 'T' && c2 == 'P') | 533 if (c1 == 'T' && c2 == 'P') |
534 c2 = 'L'; | 534 c2 = 'L'; |
535 | 535 |
536 return CountryCharsToCountryID(c1, c2); | 536 return CountryCharsToCountryID(c1, c2); |
537 } | 537 } |
538 | 538 |
539 int CountryStringToCountryID(const std::string& country) { | |
540 return (country.length() == 2) | |
541 ? CountryCharsToCountryIDWithUpdate(country[0], country[1]) | |
542 : kCountryIDUnknown; | |
543 } | |
544 | |
539 #if defined(OS_WIN) | 545 #if defined(OS_WIN) |
540 | 546 |
541 // For reference, a list of GeoIDs can be found at | 547 // For reference, a list of GeoIDs can be found at |
542 // http://msdn.microsoft.com/en-us/library/dd374073.aspx . | 548 // http://msdn.microsoft.com/en-us/library/dd374073.aspx . |
543 int GeoIDToCountryID(GEOID geo_id) { | 549 int GeoIDToCountryID(GEOID geo_id) { |
544 const int kISOBufferSize = 3; // Two plus one for the terminator. | 550 const int kISOBufferSize = 3; // Two plus one for the terminator. |
545 wchar_t isobuf[kISOBufferSize] = { 0 }; | 551 wchar_t isobuf[kISOBufferSize] = { 0 }; |
546 int retval = GetGeoInfo(geo_id, GEO_ISO2, isobuf, kISOBufferSize, 0); | 552 int retval = GetGeoInfo(geo_id, GEO_ISO2, isobuf, kISOBufferSize, 0); |
547 | 553 |
548 if (retval == kISOBufferSize && | 554 if (retval == kISOBufferSize && |
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1193 return kAllEngines[i]->type; | 1199 return kAllEngines[i]->type; |
1194 } | 1200 } |
1195 } | 1201 } |
1196 | 1202 |
1197 return SEARCH_ENGINE_OTHER; | 1203 return SEARCH_ENGINE_OTHER; |
1198 } | 1204 } |
1199 | 1205 |
1200 #if defined(OS_WIN) | 1206 #if defined(OS_WIN) |
1201 | 1207 |
1202 int GetCurrentCountryID() { | 1208 int GetCurrentCountryID() { |
1203 GEOID geo_id = GetUserGeoID(GEOCLASS_NATION); | 1209 return GeoIDToCountryID(GetUserGeoID(GEOCLASS_NATION)); |
1204 | |
1205 return GeoIDToCountryID(geo_id); | |
1206 } | 1210 } |
1207 | 1211 |
1208 #elif defined(OS_MACOSX) | 1212 #elif defined(OS_MACOSX) |
1209 | 1213 |
1210 int GetCurrentCountryID() { | 1214 int GetCurrentCountryID() { |
1211 base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent()); | 1215 base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent()); |
1212 CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(), | 1216 CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(), |
1213 kCFLocaleCountryCode); | 1217 kCFLocaleCountryCode); |
1214 if (!country) | 1218 if (!country) |
1215 return kCountryIDUnknown; | 1219 return kCountryIDUnknown; |
1216 | 1220 |
1217 UniChar isobuf[2]; | 1221 UniChar isobuf[2]; |
1218 CFRange char_range = CFRangeMake(0, 2); | 1222 CFRange char_range = CFRangeMake(0, 2); |
1219 CFStringGetCharacters(country, char_range, isobuf); | 1223 CFStringGetCharacters(country, char_range, isobuf); |
1220 | 1224 |
1221 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]), | 1225 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]), |
1222 static_cast<char>(isobuf[1])); | 1226 static_cast<char>(isobuf[1])); |
1223 } | 1227 } |
1224 | 1228 |
1225 #elif defined(OS_ANDROID) | 1229 #elif defined(OS_ANDROID) |
1226 | 1230 |
1227 int GetCurrentCountryID() { | 1231 int GetCurrentCountryID() { |
1228 const std::string& country_code = base::android::GetDefaultCountryCode(); | 1232 return CountryStringToCountryID(base::android::GetDefaultCountryCode()); |
1229 return (country_code.size() == 2) ? | |
1230 CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) : | |
1231 kCountryIDUnknown; | |
1232 } | 1233 } |
1233 | 1234 |
1234 #elif defined(OS_POSIX) | 1235 #elif defined(OS_POSIX) |
1235 | 1236 |
1236 int GetCurrentCountryID() { | 1237 int GetCurrentCountryID() { |
1237 const char* locale = setlocale(LC_MESSAGES, NULL); | 1238 const char* locale = setlocale(LC_MESSAGES, NULL); |
1238 | |
1239 if (!locale) | 1239 if (!locale) |
1240 return kCountryIDUnknown; | 1240 return kCountryIDUnknown; |
1241 | 1241 |
1242 // The format of a locale name is: | 1242 // The format of a locale name is: |
1243 // language[_territory][.codeset][@modifier], where territory is an ISO 3166 | 1243 // language[_territory][.codeset][@modifier], where territory is an ISO 3166 |
1244 // country code, which is what we want. | 1244 // country code, which is what we want. |
1245 | |
1246 // First remove the language portion. | |
1245 std::string locale_str(locale); | 1247 std::string locale_str(locale); |
1246 size_t begin = locale_str.find('_'); | 1248 size_t territory_delim = locale_str.find('_'); |
1247 if (begin == std::string::npos || locale_str.size() - begin < 3) | 1249 if (begin == std::string::npos) |
Ian Wen
2016/09/19 20:51:31
Do you mean territory_delim here?
| |
1248 return kCountryIDUnknown; | 1250 return kCountryIDUnknown; |
1249 | 1251 |
1250 ++begin; | 1252 // Next remove any codeset/modifier portion and uppercase. |
1251 size_t end = locale_str.find_first_of(".@", begin); | 1253 return CountryStringToCountryID( |
1252 if (end == std::string::npos) | 1254 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); |
1253 end = locale_str.size(); | |
1254 | |
1255 // The territory part must contain exactly two characters. | |
1256 if (end - begin == 2) { | |
1257 return CountryCharsToCountryIDWithUpdate( | |
1258 base::ToUpperASCII(locale_str[begin]), | |
1259 base::ToUpperASCII(locale_str[begin + 1])); | |
1260 } | |
1261 | |
1262 return kCountryIDUnknown; | |
1263 } | 1255 } |
1264 | 1256 |
1265 #endif // OS_* | 1257 #endif // OS_* |
1266 | 1258 |
1267 } // namespace TemplateURLPrepopulateData | 1259 } // namespace TemplateURLPrepopulateData |
OLD | NEW |