Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: components/search_engines/template_url_prepopulate_data.cc

Issue 1130183004: [iOS] Expose TemplateURLPrepopulateData::GetCurrentCountryID() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address second round of comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 #if defined(OS_WIN)
540
541 // For reference, a list of GeoIDs can be found at
542 // http://msdn.microsoft.com/en-us/library/dd374073.aspx .
543 int GeoIDToCountryID(GEOID geo_id) {
544 const int kISOBufferSize = 3; // Two plus one for the terminator.
545 wchar_t isobuf[kISOBufferSize] = { 0 };
546 int retval = GetGeoInfo(geo_id, GEO_ISO2, isobuf, kISOBufferSize, 0);
547
548 if (retval == kISOBufferSize &&
549 !(isobuf[0] == L'X' && isobuf[1] == L'X'))
550 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
551 static_cast<char>(isobuf[1]));
552
553 // Various locations have ISO codes that Windows does not return.
554 switch (geo_id) {
555 case 0x144: // Guernsey
556 return CountryCharsToCountryID('G', 'G');
557 case 0x148: // Jersey
558 return CountryCharsToCountryID('J', 'E');
559 case 0x3B16: // Isle of Man
560 return CountryCharsToCountryID('I', 'M');
561
562 // 'UM' (U.S. Minor Outlying Islands)
563 case 0x7F: // Johnston Atoll
564 case 0x102: // Wake Island
565 case 0x131: // Baker Island
566 case 0x146: // Howland Island
567 case 0x147: // Jarvis Island
568 case 0x149: // Kingman Reef
569 case 0x152: // Palmyra Atoll
570 case 0x52FA: // Midway Islands
571 return CountryCharsToCountryID('U', 'M');
572
573 // 'SH' (Saint Helena)
574 case 0x12F: // Ascension Island
575 case 0x15C: // Tristan da Cunha
576 return CountryCharsToCountryID('S', 'H');
577
578 // 'IO' (British Indian Ocean Territory)
579 case 0x13A: // Diego Garcia
580 return CountryCharsToCountryID('I', 'O');
581
582 // Other cases where there is no ISO country code; we assign countries that
583 // can serve as reasonable defaults.
584 case 0x154: // Rota Island
585 case 0x155: // Saipan
586 case 0x15A: // Tinian Island
587 return CountryCharsToCountryID('U', 'S');
588 case 0x134: // Channel Islands
589 return CountryCharsToCountryID('G', 'B');
590 case 0x143: // Guantanamo Bay
591 default:
592 return kCountryIDUnknown;
593 }
594 }
595
596 int GetCurrentCountryID() {
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
661 int GetCountryIDFromPrefs(PrefService* prefs) { 539 int GetCountryIDFromPrefs(PrefService* prefs) {
662 if (!prefs) 540 if (!prefs)
663 return GetCurrentCountryID(); 541 return GetCurrentCountryID();
664 542
665 // Cache first run Country ID value in prefs, and use it afterwards. This 543 // 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 544 // ensures that just because the user moves around, we won't automatically
667 // make major changes to their available search providers, which would feel 545 // make major changes to their available search providers, which would feel
668 // surprising. 546 // surprising.
669 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) { 547 if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) {
670 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID()); 548 prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID());
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 1051
1174 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { 1052 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
1175 return prepopulated_url.is_valid() && 1053 return prepopulated_url.is_valid() &&
1176 net::registry_controlled_domains::SameDomainOrHost( 1054 net::registry_controlled_domains::SameDomainOrHost(
1177 given_url, prepopulated_url, 1055 given_url, prepopulated_url,
1178 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); 1056 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
1179 } 1057 }
1180 1058
1181 } // namespace 1059 } // namespace
1182 1060
1061 #if defined(OS_WIN)
1062
1063 namespace {
1064
1065 // For reference, a list of GeoIDs can be found at
1066 // http://msdn.microsoft.com/en-us/library/dd374073.aspx .
1067 int GeoIDToCountryID(GEOID geo_id) {
Peter Kasting 2015/05/18 18:31:34 Nit: Leave this function (still #if'd) where it us
sdefresne 2015/05/19 09:04:22 Done.
1068 const int kISOBufferSize = 3; // Two plus one for the terminator.
1069 wchar_t isobuf[kISOBufferSize] = { 0 };
1070 int retval = GetGeoInfo(geo_id, GEO_ISO2, isobuf, kISOBufferSize, 0);
1071
1072 if (retval == kISOBufferSize &&
1073 !(isobuf[0] == L'X' && isobuf[1] == L'X'))
1074 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
1075 static_cast<char>(isobuf[1]));
1076
1077 // Various locations have ISO codes that Windows does not return.
1078 switch (geo_id) {
1079 case 0x144: // Guernsey
1080 return CountryCharsToCountryID('G', 'G');
1081 case 0x148: // Jersey
1082 return CountryCharsToCountryID('J', 'E');
1083 case 0x3B16: // Isle of Man
1084 return CountryCharsToCountryID('I', 'M');
1085
1086 // 'UM' (U.S. Minor Outlying Islands)
1087 case 0x7F: // Johnston Atoll
1088 case 0x102: // Wake Island
1089 case 0x131: // Baker Island
1090 case 0x146: // Howland Island
1091 case 0x147: // Jarvis Island
1092 case 0x149: // Kingman Reef
1093 case 0x152: // Palmyra Atoll
1094 case 0x52FA: // Midway Islands
1095 return CountryCharsToCountryID('U', 'M');
1096
1097 // 'SH' (Saint Helena)
1098 case 0x12F: // Ascension Island
1099 case 0x15C: // Tristan da Cunha
1100 return CountryCharsToCountryID('S', 'H');
1101
1102 // 'IO' (British Indian Ocean Territory)
1103 case 0x13A: // Diego Garcia
1104 return CountryCharsToCountryID('I', 'O');
1105
1106 // Other cases where there is no ISO country code; we assign countries that
1107 // can serve as reasonable defaults.
1108 case 0x154: // Rota Island
1109 case 0x155: // Saipan
1110 case 0x15A: // Tinian Island
1111 return CountryCharsToCountryID('U', 'S');
1112 case 0x134: // Channel Islands
1113 return CountryCharsToCountryID('G', 'B');
1114 case 0x143: // Guantanamo Bay
1115 default:
1116 return kCountryIDUnknown;
1117 }
1118 }
1119
1120 } // namespace
1121
1122 int GetCurrentCountryID() {
1123 GEOID geo_id = GetUserGeoID(GEOCLASS_NATION);
1124
1125 return GeoIDToCountryID(geo_id);
1126 }
1127
1128 #elif defined(OS_MACOSX)
1129
1130 int GetCurrentCountryID() {
1131 base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent());
1132 CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(),
1133 kCFLocaleCountryCode);
1134 if (!country)
1135 return kCountryIDUnknown;
1136
1137 UniChar isobuf[2];
1138 CFRange char_range = CFRangeMake(0, 2);
1139 CFStringGetCharacters(country, char_range, isobuf);
1140
1141 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
1142 static_cast<char>(isobuf[1]));
1143 }
1144
1145 #elif defined(OS_ANDROID)
1146
1147 int GetCurrentCountryID() {
1148 const std::string& country_code = base::android::GetDefaultCountryCode();
1149 return (country_code.size() == 2) ?
1150 CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) :
1151 kCountryIDUnknown;
1152 }
1153
1154 #elif defined(OS_POSIX)
1155
1156 int GetCurrentCountryID() {
1157 const char* locale = setlocale(LC_MESSAGES, NULL);
1158
1159 if (!locale)
1160 return kCountryIDUnknown;
1161
1162 // The format of a locale name is:
1163 // language[_territory][.codeset][@modifier], where territory is an ISO 3166
1164 // country code, which is what we want.
1165 std::string locale_str(locale);
1166 size_t begin = locale_str.find('_');
1167 if (begin == std::string::npos || locale_str.size() - begin < 3)
1168 return kCountryIDUnknown;
1169
1170 ++begin;
1171 size_t end = locale_str.find_first_of(".@", begin);
1172 if (end == std::string::npos)
1173 end = locale_str.size();
1174
1175 // The territory part must contain exactly two characters.
1176 if (end - begin == 2) {
1177 return CountryCharsToCountryIDWithUpdate(
1178 base::ToUpperASCII(locale_str[begin]),
1179 base::ToUpperASCII(locale_str[begin + 1]));
1180 }
1181
1182 return kCountryIDUnknown;
1183 }
1184
1185 #endif // OS_*
1183 1186
1184 // Global functions ----------------------------------------------------------- 1187 // Global functions -----------------------------------------------------------
1185 1188
1186 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { 1189 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
1187 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); 1190 registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown);
1188 registry->RegisterListPref(prefs::kSearchProviderOverrides); 1191 registry->RegisterListPref(prefs::kSearchProviderOverrides);
1189 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); 1192 registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1);
1190 } 1193 }
1191 1194
1192 int GetDataVersion(PrefService* prefs) { 1195 int GetDataVersion(PrefService* prefs) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1271 for (size_t j = 0; j < kAllEngines[i]->alternate_urls_size; ++j) { 1274 for (size_t j = 0; j < kAllEngines[i]->alternate_urls_size; ++j) {
1272 if (SameDomain(url, GURL(kAllEngines[i]->alternate_urls[j]))) 1275 if (SameDomain(url, GURL(kAllEngines[i]->alternate_urls[j])))
1273 return kAllEngines[i]->type; 1276 return kAllEngines[i]->type;
1274 } 1277 }
1275 } 1278 }
1276 1279
1277 return SEARCH_ENGINE_OTHER; 1280 return SEARCH_ENGINE_OTHER;
1278 } 1281 }
1279 1282
1280 } // namespace TemplateURLPrepopulateData 1283 } // namespace TemplateURLPrepopulateData
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698