Commit 71c91039 authored by sdefresne's avatar sdefresne Committed by Commit bot

[iOS] Expose TemplateURLPrepopulateData::GetCurrentCountryID()

Expose TemplateURLPrepopulateData::GetCurrentCountryID() to detect that the
user changed the device region in order to update the list of search engine
on iOS.

Note that when a region change is detected we remove all default search
engines except the one selected by the user and then insert all the new
engines. This is possible because user cannot customize search engines,
only select one of the default engines.

BUG=None

Review URL: https://codereview.chromium.org/1130183004

Cr-Commit-Position: refs/heads/master@{#330716}
parent 9a5d26bf
......@@ -35,7 +35,6 @@
namespace TemplateURLPrepopulateData {
// Helpers --------------------------------------------------------------------
namespace {
......@@ -593,70 +592,7 @@ int GeoIDToCountryID(GEOID geo_id) {
}
}
int GetCurrentCountryID() {
GEOID geo_id = GetUserGeoID(GEOCLASS_NATION);
return GeoIDToCountryID(geo_id);
}
#elif defined(OS_MACOSX)
int GetCurrentCountryID() {
base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent());
CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(),
kCFLocaleCountryCode);
if (!country)
return kCountryIDUnknown;
UniChar isobuf[2];
CFRange char_range = CFRangeMake(0, 2);
CFStringGetCharacters(country, char_range, isobuf);
return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
static_cast<char>(isobuf[1]));
}
#elif defined(OS_ANDROID)
int GetCurrentCountryID() {
const std::string& country_code = base::android::GetDefaultCountryCode();
return (country_code.size() == 2) ?
CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) :
kCountryIDUnknown;
}
#elif defined(OS_POSIX)
int GetCurrentCountryID() {
const char* locale = setlocale(LC_MESSAGES, NULL);
if (!locale)
return kCountryIDUnknown;
// The format of a locale name is:
// language[_territory][.codeset][@modifier], where territory is an ISO 3166
// country code, which is what we want.
std::string locale_str(locale);
size_t begin = locale_str.find('_');
if (begin == std::string::npos || locale_str.size() - begin < 3)
return kCountryIDUnknown;
++begin;
size_t end = locale_str.find_first_of(".@", begin);
if (end == std::string::npos)
end = locale_str.size();
// The territory part must contain exactly two characters.
if (end - begin == 2) {
return CountryCharsToCountryIDWithUpdate(
base::ToUpperASCII(locale_str[begin]),
base::ToUpperASCII(locale_str[begin + 1]));
}
return kCountryIDUnknown;
}
#endif // OS_*
#endif // defined(OS_WIN)
int GetCountryIDFromPrefs(PrefService* prefs) {
if (!prefs)
......@@ -1180,7 +1116,6 @@ bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
} // namespace
// Global functions -----------------------------------------------------------
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
......@@ -1277,4 +1212,71 @@ SearchEngineType GetEngineType(const GURL& url) {
return SEARCH_ENGINE_OTHER;
}
#if defined(OS_WIN)
int GetCurrentCountryID() {
GEOID geo_id = GetUserGeoID(GEOCLASS_NATION);
return GeoIDToCountryID(geo_id);
}
#elif defined(OS_MACOSX)
int GetCurrentCountryID() {
base::ScopedCFTypeRef<CFLocaleRef> locale(CFLocaleCopyCurrent());
CFStringRef country = (CFStringRef)CFLocaleGetValue(locale.get(),
kCFLocaleCountryCode);
if (!country)
return kCountryIDUnknown;
UniChar isobuf[2];
CFRange char_range = CFRangeMake(0, 2);
CFStringGetCharacters(country, char_range, isobuf);
return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
static_cast<char>(isobuf[1]));
}
#elif defined(OS_ANDROID)
int GetCurrentCountryID() {
const std::string& country_code = base::android::GetDefaultCountryCode();
return (country_code.size() == 2) ?
CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) :
kCountryIDUnknown;
}
#elif defined(OS_POSIX)
int GetCurrentCountryID() {
const char* locale = setlocale(LC_MESSAGES, NULL);
if (!locale)
return kCountryIDUnknown;
// The format of a locale name is:
// language[_territory][.codeset][@modifier], where territory is an ISO 3166
// country code, which is what we want.
std::string locale_str(locale);
size_t begin = locale_str.find('_');
if (begin == std::string::npos || locale_str.size() - begin < 3)
return kCountryIDUnknown;
++begin;
size_t end = locale_str.find_first_of(".@", begin);
if (end == std::string::npos)
end = locale_str.size();
// The territory part must contain exactly two characters.
if (end - begin == 2) {
return CountryCharsToCountryIDWithUpdate(
base::ToUpperASCII(locale_str[begin]),
base::ToUpperASCII(locale_str[begin + 1]));
}
return kCountryIDUnknown;
}
#endif // OS_*
} // namespace TemplateURLPrepopulateData
......@@ -65,6 +65,14 @@ SearchEngineType GetEngineType(const TemplateURL& template_url,
// This may be called on any thread.
SearchEngineType GetEngineType(const GURL& url);
// Returns the identifier for the user current country. Used to update the list
// of search engines when user switches device region settings. For use on iOS
// only.
// TODO(ios): Once user can customize search engines ( http://crbug.com/153047 )
// this declaration should be removed and the definition in the .cc file be
// moved back to the anonymous namespace.
int GetCurrentCountryID();
} // namespace TemplateURLPrepopulateData
#endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PREPOPULATE_DATA_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment