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 17 matching lines...) Expand all Loading... |
28 #elif defined(OS_MACOSX) | 28 #elif defined(OS_MACOSX) |
29 #include "base/mac/scoped_cftyperef.h" | 29 #include "base/mac/scoped_cftyperef.h" |
30 #endif | 30 #endif |
31 | 31 |
32 #if defined(OS_ANDROID) | 32 #if defined(OS_ANDROID) |
33 #include "base/android/locale_utils.h" | 33 #include "base/android/locale_utils.h" |
34 #endif | 34 #endif |
35 | 35 |
36 namespace TemplateURLPrepopulateData { | 36 namespace TemplateURLPrepopulateData { |
37 | 37 |
38 | |
39 // Helpers -------------------------------------------------------------------- | 38 // Helpers -------------------------------------------------------------------- |
40 | 39 |
41 namespace { | 40 namespace { |
42 | 41 |
43 // NOTE: You should probably not change the data in this file without changing | 42 // NOTE: You should probably not change the data in this file without changing |
44 // |kCurrentDataVersion| in prepopulated_engines.json. See comments in | 43 // |kCurrentDataVersion| in prepopulated_engines.json. See comments in |
45 // GetDataVersion() below! | 44 // GetDataVersion() below! |
46 | 45 |
47 // Put the engines within each country in order with most interesting/important | 46 // Put the engines within each country in order with most interesting/important |
48 // first. The default will be the first engine. | 47 // first. The default will be the first engine. |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 case 0x15A: // Tinian Island | 585 case 0x15A: // Tinian Island |
587 return CountryCharsToCountryID('U', 'S'); | 586 return CountryCharsToCountryID('U', 'S'); |
588 case 0x134: // Channel Islands | 587 case 0x134: // Channel Islands |
589 return CountryCharsToCountryID('G', 'B'); | 588 return CountryCharsToCountryID('G', 'B'); |
590 case 0x143: // Guantanamo Bay | 589 case 0x143: // Guantanamo Bay |
591 default: | 590 default: |
592 return kCountryIDUnknown; | 591 return kCountryIDUnknown; |
593 } | 592 } |
594 } | 593 } |
595 | 594 |
596 int GetCurrentCountryID() { | 595 #endif // defined(OS_WIN) |
597 GEOID geo_id = GetUserGeoID(GEOCLASS_NATION); | |
598 | |
599 return GeoIDToCountryID(geo_id); | |
600 } | |
601 | |
602 #elif defined(OS_MACOSX) | |
603 | |
604 int GetCurrentCountryID() { | |
605 base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent()); | |
606 CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(), | |
607 kCFLocaleCountryCode); | |
608 if (!country) | |
609 return kCountryIDUnknown; | |
610 | |
611 UniChar isobuf[2]; | |
612 CFRange char_range = CFRangeMake(0, 2); | |
613 CFStringGetCharacters(country, char_range, isobuf); | |
614 | |
615 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]), | |
616 static_cast<char>(isobuf[1])); | |
617 } | |
618 | |
619 #elif defined(OS_ANDROID) | |
620 | |
621 int GetCurrentCountryID() { | |
622 const std::string& country_code = base::android::GetDefaultCountryCode(); | |
623 return (country_code.size() == 2) ? | |
624 CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) : | |
625 kCountryIDUnknown; | |
626 } | |
627 | |
628 #elif defined(OS_POSIX) | |
629 | |
630 int GetCurrentCountryID() { | |
631 const char* locale = setlocale(LC_MESSAGES, NULL); | |
632 | |
633 if (!locale) | |
634 return kCountryIDUnknown; | |
635 | |
636 // The format of a locale name is: | |
637 // language[_territory][.codeset][@modifier], where territory is an ISO 3166 | |
638 // country code, which is what we want. | |
639 std::string locale_str(locale); | |
640 size_t begin = locale_str.find('_'); | |
641 if (begin == std::string::npos || locale_str.size() - begin < 3) | |
642 return kCountryIDUnknown; | |
643 | |
644 ++begin; | |
645 size_t end = locale_str.find_first_of(".@", begin); | |
646 if (end == std::string::npos) | |
647 end = locale_str.size(); | |
648 | |
649 // The territory part must contain exactly two characters. | |
650 if (end - begin == 2) { | |
651 return CountryCharsToCountryIDWithUpdate( | |
652 base::ToUpperASCII(locale_str[begin]), | |
653 base::ToUpperASCII(locale_str[begin + 1])); | |
654 } | |
655 | |
656 return kCountryIDUnknown; | |
657 } | |
658 | |
659 #endif // OS_* | |
660 | 596 |
661 int GetCountryIDFromPrefs(PrefService* prefs) { | 597 int GetCountryIDFromPrefs(PrefService* prefs) { |
662 if (!prefs) | 598 if (!prefs) |
663 return GetCurrentCountryID(); | 599 return GetCurrentCountryID(); |
664 | 600 |
665 // Cache first run Country ID value in prefs, and use it afterwards. This | 601 // Cache first run Country ID value in prefs, and use it afterwards. This |
666 // ensures that just because the user moves around, we won't automatically | 602 // ensures that just because the user moves around, we won't automatically |
667 // make major changes to their available search providers, which would feel | 603 // make major changes to their available search providers, which would feel |
668 // surprising. | 604 // surprising. |
669 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { | 605 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1173 | 1109 |
1174 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { | 1110 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { |
1175 return prepopulated_url.is_valid() && | 1111 return prepopulated_url.is_valid() && |
1176 net::registry_controlled_domains::SameDomainOrHost( | 1112 net::registry_controlled_domains::SameDomainOrHost( |
1177 given_url, prepopulated_url, | 1113 given_url, prepopulated_url, |
1178 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 1114 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
1179 } | 1115 } |
1180 | 1116 |
1181 } // namespace | 1117 } // namespace |
1182 | 1118 |
1183 | |
1184 // Global functions ----------------------------------------------------------- | 1119 // Global functions ----------------------------------------------------------- |
1185 | 1120 |
1186 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | 1121 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
1187 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); | 1122 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); |
1188 registry->RegisterListPref(prefs::kSearchProviderOverrides); | 1123 registry->RegisterListPref(prefs::kSearchProviderOverrides); |
1189 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); | 1124 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); |
1190 } | 1125 } |
1191 | 1126 |
1192 int GetDataVersion(PrefService* prefs) { | 1127 int GetDataVersion(PrefService* prefs) { |
1193 // Allow tests to override the local version. | 1128 // Allow tests to override the local version. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1270 // Then check the alternate URLs. | 1205 // Then check the alternate URLs. |
1271 for (size_t j = 0; j < kAllEngines[i]->alternate_urls_size; ++j) { | 1206 for (size_t j = 0; j < kAllEngines[i]->alternate_urls_size; ++j) { |
1272 if (SameDomain(url, GURL(kAllEngines[i]->alternate_urls[j]))) | 1207 if (SameDomain(url, GURL(kAllEngines[i]->alternate_urls[j]))) |
1273 return kAllEngines[i]->type; | 1208 return kAllEngines[i]->type; |
1274 } | 1209 } |
1275 } | 1210 } |
1276 | 1211 |
1277 return SEARCH_ENGINE_OTHER; | 1212 return SEARCH_ENGINE_OTHER; |
1278 } | 1213 } |
1279 | 1214 |
| 1215 #if defined(OS_WIN) |
| 1216 |
| 1217 int GetCurrentCountryID() { |
| 1218 GEOID geo_id = GetUserGeoID(GEOCLASS_NATION); |
| 1219 |
| 1220 return GeoIDToCountryID(geo_id); |
| 1221 } |
| 1222 |
| 1223 #elif defined(OS_MACOSX) |
| 1224 |
| 1225 int GetCurrentCountryID() { |
| 1226 base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent()); |
| 1227 CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(), |
| 1228 kCFLocaleCountryCode); |
| 1229 if (!country) |
| 1230 return kCountryIDUnknown; |
| 1231 |
| 1232 UniChar isobuf[2]; |
| 1233 CFRange char_range = CFRangeMake(0, 2); |
| 1234 CFStringGetCharacters(country, char_range, isobuf); |
| 1235 |
| 1236 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]), |
| 1237 static_cast<char>(isobuf[1])); |
| 1238 } |
| 1239 |
| 1240 #elif defined(OS_ANDROID) |
| 1241 |
| 1242 int GetCurrentCountryID() { |
| 1243 const std::string& country_code = base::android::GetDefaultCountryCode(); |
| 1244 return (country_code.size() == 2) ? |
| 1245 CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) : |
| 1246 kCountryIDUnknown; |
| 1247 } |
| 1248 |
| 1249 #elif defined(OS_POSIX) |
| 1250 |
| 1251 int GetCurrentCountryID() { |
| 1252 const char* locale = setlocale(LC_MESSAGES, NULL); |
| 1253 |
| 1254 if (!locale) |
| 1255 return kCountryIDUnknown; |
| 1256 |
| 1257 // The format of a locale name is: |
| 1258 // language[_territory][.codeset][@modifier], where territory is an ISO 3166 |
| 1259 // country code, which is what we want. |
| 1260 std::string locale_str(locale); |
| 1261 size_t begin = locale_str.find('_'); |
| 1262 if (begin == std::string::npos || locale_str.size() - begin < 3) |
| 1263 return kCountryIDUnknown; |
| 1264 |
| 1265 ++begin; |
| 1266 size_t end = locale_str.find_first_of(".@", begin); |
| 1267 if (end == std::string::npos) |
| 1268 end = locale_str.size(); |
| 1269 |
| 1270 // The territory part must contain exactly two characters. |
| 1271 if (end - begin == 2) { |
| 1272 return CountryCharsToCountryIDWithUpdate( |
| 1273 base::ToUpperASCII(locale_str[begin]), |
| 1274 base::ToUpperASCII(locale_str[begin + 1])); |
| 1275 } |
| 1276 |
| 1277 return kCountryIDUnknown; |
| 1278 } |
| 1279 |
| 1280 #endif // OS_* |
| 1281 |
1280 } // namespace TemplateURLPrepopulateData | 1282 } // namespace TemplateURLPrepopulateData |
OLD | NEW |