#include "components/country_codes/country_codes.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)
#include <locale.h>
#endif
#include "base/strings/string_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#undef IN
#elif BUILDFLAG(IS_APPLE)
#include "base/apple/scoped_cftyperef.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/locale_utils.h"
#endif
namespace country_codes {
namespace {
int CountryCharsToCountryIDWithUpdate(char c1, char c2) { … }
#if BUILDFLAG(IS_WIN)
int GeoIDToCountryID(GEOID geo_id) {
const int kISOBufferSize = 3;
wchar_t isobuf[kISOBufferSize] = {};
int retval = GetGeoInfo(geo_id, GEO_ISO2, isobuf, kISOBufferSize, 0);
if (retval == kISOBufferSize && !(isobuf[0] == L'X' && isobuf[1] == L'X')) {
return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf[0]),
static_cast<char>(isobuf[1]));
}
switch (geo_id) {
case 0x144:
return CountryCharsToCountryID('G', 'G');
case 0x148:
return CountryCharsToCountryID('J', 'E');
case 0x3B16:
return CountryCharsToCountryID('I', 'M');
case 0x7F:
case 0x102:
case 0x131:
case 0x146:
case 0x147:
case 0x149:
case 0x152:
case 0x52FA:
return CountryCharsToCountryID('U', 'M');
case 0x12F:
case 0x15C:
return CountryCharsToCountryID('S', 'H');
case 0x13A:
return CountryCharsToCountryID('I', 'O');
case 0x154:
case 0x155:
case 0x15A:
return CountryCharsToCountryID('U', 'S');
case 0x134:
return CountryCharsToCountryID('G', 'B');
case 0x143:
default:
return kCountryIDUnknown;
}
}
#endif
}
const char kCountryIDAtInstall[] = …;
int CountryStringToCountryID(const std::string& country) { … }
int GetCountryIDFromPrefs(PrefService* prefs) { … }
void RegisterProfilePrefs(PrefRegistrySimple* registry) { … }
#if BUILDFLAG(IS_WIN)
int GetCurrentCountryID() {
static int id = GeoIDToCountryID(GetUserGeoID(GEOCLASS_NATION));
return id;
}
#elif BUILDFLAG(IS_APPLE)
int GetCurrentCountryID() {
base::apple::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 BUILDFLAG(IS_ANDROID)
int GetCurrentCountryID() {
return CountryStringToCountryID(base::android::GetDefaultCountryCode());
}
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int GetCurrentCountryID() { … }
#endif
std::string CountryIDToCountryString(int country_id) { … }
std::string GetCurrentCountryCode() { … }
}