Commit 68aafc3c authored by Scott Chen's avatar Scott Chen Committed by Commit Bot

Search Engines: moved country-code functions to standalone component

This CL moves all functions related to figuring out user's country code
from component/search_engines/ to a stand-alone component, so that they
can be reused by other features that also want to know the user's country
code.

Bug: 894499
Change-Id: I1a79a4db6a5a8abcc32101db3fb7dce8013a1f16
Reviewed-on: https://chromium-review.googlesource.com/c/1334859Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Commit-Queue: Scott Chen <scottchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609788}
parent 3c29ec7e
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/features.gni")
static_library("country_codes") {
sources = [
"country_codes.cc",
"country_codes.h",
]
deps = [
"//base",
"//components/pref_registry",
"//components/prefs",
"//components/sync",
]
}
include_rules = [
"+components/pref_registry",
"+components/prefs",
]
jdonnelly@chromium.org
scottchen@chromium.org
pkasting@chromium.org
# COMPONENT: Internals>Core
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/country_codes/country_codes.h"
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#include <locale.h>
#endif
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#if defined(OS_WIN)
#include <windows.h>
#undef IN // On Windows, windef.h defines this, which screws up "India" cases.
#elif defined(OS_MACOSX)
#include "base/mac/scoped_cftyperef.h"
#endif
#if defined(OS_ANDROID)
#include "base/android/locale_utils.h"
#endif
namespace country_codes {
namespace {
int CountryCharsToCountryID(char c1, char c2) {
return c1 << 8 | c2;
}
// TODO(scottchen): remove this function after confirming if it only pertains
// to obsolete OSes.
int CountryCharsToCountryIDWithUpdate(char c1, char c2) {
// SPECIAL CASE: In 2003, Yugoslavia renamed itself to Serbia and Montenegro.
// Serbia and Montenegro dissolved their union in June 2006. Yugoslavia was
// ISO 'YU' and Serbia and Montenegro were ISO 'CS'. Serbia was subsequently
// issued 'RS' and Montenegro 'ME'. Windows XP and Mac OS X Leopard still use
// the value 'YU'. If we get a value of 'YU' or 'CS' we will map it to 'RS'.
if ((c1 == 'Y' && c2 == 'U') || (c1 == 'C' && c2 == 'S')) {
c1 = 'R';
c2 = 'S';
}
// SPECIAL CASE: Timor-Leste changed from 'TP' to 'TL' in 2002. Windows XP
// predates this; we therefore map this value.
if (c1 == 'T' && c2 == 'P')
c2 = 'L';
return CountryCharsToCountryID(c1, c2);
}
#if defined(OS_WIN)
// For reference, a list of GeoIDs can be found at
// http://msdn.microsoft.com/en-us/library/dd374073.aspx .
int GeoIDToCountryID(GEOID geo_id) {
const int kISOBufferSize = 3; // Two plus one for the terminator.
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]));
}
// Various locations have ISO codes that Windows does not return.
switch (geo_id) {
case 0x144: // Guernsey
return CountryCharsToCountryID('G', 'G');
case 0x148: // Jersey
return CountryCharsToCountryID('J', 'E');
case 0x3B16: // Isle of Man
return CountryCharsToCountryID('I', 'M');
// 'UM' (U.S. Minor Outlying Islands)
case 0x7F: // Johnston Atoll
case 0x102: // Wake Island
case 0x131: // Baker Island
case 0x146: // Howland Island
case 0x147: // Jarvis Island
case 0x149: // Kingman Reef
case 0x152: // Palmyra Atoll
case 0x52FA: // Midway Islands
return CountryCharsToCountryID('U', 'M');
// 'SH' (Saint Helena)
case 0x12F: // Ascension Island
case 0x15C: // Tristan da Cunha
return CountryCharsToCountryID('S', 'H');
// 'IO' (British Indian Ocean Territory)
case 0x13A: // Diego Garcia
return CountryCharsToCountryID('I', 'O');
// Other cases where there is no ISO country code; we assign countries that
// can serve as reasonable defaults.
case 0x154: // Rota Island
case 0x155: // Saipan
case 0x15A: // Tinian Island
return CountryCharsToCountryID('U', 'S');
case 0x134: // Channel Islands
return CountryCharsToCountryID('G', 'B');
case 0x143: // Guantanamo Bay
default:
return kCountryIDUnknown;
}
}
#endif // defined(OS_WIN)
} // namespace
const char kCountryIDAtInstall[] = "countryid_at_install";
#if !defined(OS_WIN) && !defined(OS_MACOSX)
int CountryStringToCountryID(const std::string& country) {
return (country.length() == 2)
? CountryCharsToCountryIDWithUpdate(country[0], country[1])
: kCountryIDUnknown;
}
#endif
int GetCountryIDFromPrefs(PrefService* prefs) {
if (!prefs)
return GetCurrentCountryID();
// Cache first run Country ID value in prefs, and use it afterwards. This
// ensures that just because the user moves around, we won't automatically
// make major changes to their available search providers, which would feel
// surprising.
if (!prefs->HasPrefPath(country_codes::kCountryIDAtInstall)) {
prefs->SetInteger(country_codes::kCountryIDAtInstall,
GetCurrentCountryID());
}
return prefs->GetInteger(country_codes::kCountryIDAtInstall);
}
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(country_codes::kCountryIDAtInstall,
kCountryIDUnknown);
}
#if defined(OS_WIN)
int GetCurrentCountryID() {
return GeoIDToCountryID(GetUserGeoID(GEOCLASS_NATION));
}
#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() {
return CountryStringToCountryID(base::android::GetDefaultCountryCode());
}
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
int GetCurrentCountryID() {
const char* locale = setlocale(LC_MESSAGES, nullptr);
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.
// First remove the language portion.
std::string locale_str(locale);
size_t territory_delim = locale_str.find('_');
if (territory_delim == std::string::npos)
return kCountryIDUnknown;
locale_str.erase(0, territory_delim + 1);
// Next remove any codeset/modifier portion and uppercase.
return CountryStringToCountryID(
base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@"))));
}
#endif // OS_*
} // namespace country_codes
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Please refer to ISO 3166-1 for information about the two-character country
// codes; http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 is useful. In the
// following (C++) code, we pack the two letters of the country code into an int
// value we call the CountryID.
#ifndef COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_
#define COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_
#include <string>
class PrefService;
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace country_codes {
// Integer containing the system Country ID the first time we checked the
// template URL prepopulate data. This is used to avoid adding a whole bunch of
// new search engine choices if prepopulation runs when the user's Country ID
// differs from their previous Country ID. This pref does not exist until
// prepopulation has been run at least once.
extern const char kCountryIDAtInstall[];
const int kCountryIDUnknown = -1;
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
// 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();
// Converts a two-letter country code to an integer-based country identifier.
int CountryStringToCountryID(const std::string& country);
// Returns the country identifier that was stored at install. If no such pref
// is available, it will return identifier of the current country instead.
int GetCountryIDFromPrefs(PrefService* prefs);
} // namespace country_codes
#endif // COMPONENTS_COUNTRY_CODES_COUNTRY_CODES_H_
...@@ -64,6 +64,7 @@ static_library("search_engines") { ...@@ -64,6 +64,7 @@ static_library("search_engines") {
deps = [ deps = [
":prepopulated_engines", ":prepopulated_engines",
"//base:i18n", "//base:i18n",
"//components/country_codes",
"//components/history/core/browser", "//components/history/core/browser",
"//components/infobars/core", "//components/infobars/core",
"//components/pref_registry", "//components/pref_registry",
...@@ -130,6 +131,7 @@ source_set("unit_tests") { ...@@ -130,6 +131,7 @@ source_set("unit_tests") {
":test_support", ":test_support",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//components/country_codes",
"//components/google/core/browser", "//components/google/core/browser",
"//components/pref_registry:pref_registry", "//components/pref_registry:pref_registry",
"//components/prefs", "//components/prefs",
......
include_rules = [ include_rules = [
"+components/country_codes",
"+components/google/core", "+components/google/core",
"+components/history/core", "+components/history/core",
"+components/infobars/core", "+components/infobars/core",
......
...@@ -26,11 +26,4 @@ const char kSearchProviderOverrides[] = "search_provider_overrides"; ...@@ -26,11 +26,4 @@ const char kSearchProviderOverrides[] = "search_provider_overrides";
const char kSearchProviderOverridesVersion[] = const char kSearchProviderOverridesVersion[] =
"search_provider_overrides_version"; "search_provider_overrides_version";
// Integer containing the system Country ID the first time we checked the
// template URL prepopulate data. This is used to avoid adding a whole bunch of
// new search engine choices if prepopulation runs when the user's Country ID
// differs from their previous Country ID. This pref does not exist until
// prepopulation has been run at least once.
const char kCountryIDAtInstall[] = "countryid_at_install";
} // namespace prefs } // namespace prefs
...@@ -11,7 +11,6 @@ extern const char kSyncedDefaultSearchProviderGUID[]; ...@@ -11,7 +11,6 @@ extern const char kSyncedDefaultSearchProviderGUID[];
extern const char kDefaultSearchProviderEnabled[]; extern const char kDefaultSearchProviderEnabled[];
extern const char kSearchProviderOverrides[]; extern const char kSearchProviderOverrides[];
extern const char kSearchProviderOverridesVersion[]; extern const char kSearchProviderOverridesVersion[];
extern const char kCountryIDAtInstall[];
} // namespace prefs } // namespace prefs
......
...@@ -4,17 +4,10 @@ ...@@ -4,17 +4,10 @@
#include "components/search_engines/template_url_prepopulate_data.h" #include "components/search_engines/template_url_prepopulate_data.h"
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#include <locale.h>
#endif
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/country_codes/country_codes.h"
#include "components/google/core/common/google_util.h" #include "components/google/core/common/google_util.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -25,17 +18,6 @@ ...@@ -25,17 +18,6 @@
#include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h" #include "url/gurl.h"
#if defined(OS_WIN)
#include <windows.h>
#undef IN // On Windows, windef.h defines this, which screws up "India" cases.
#elif defined(OS_MACOSX)
#include "base/mac/scoped_cftyperef.h"
#endif
#if defined(OS_ANDROID)
#include "base/android/locale_utils.h"
#endif
namespace TemplateURLPrepopulateData { namespace TemplateURLPrepopulateData {
// Helpers -------------------------------------------------------------------- // Helpers --------------------------------------------------------------------
...@@ -609,120 +591,6 @@ const PrepopulatedEngine* const kAllEngines[] = { ...@@ -609,120 +591,6 @@ const PrepopulatedEngine* const kAllEngines[] = {
&softonic, &sweetim, &terra_ar, &terra_es, &tut, &walla, &wp, &zoznam, &softonic, &sweetim, &terra_ar, &terra_es, &tut, &walla, &wp, &zoznam,
}; };
// Please refer to ISO 3166-1 for information about the two-character country
// codes; http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 is useful. In the
// following (C++) code, we pack the two letters of the country code into an int
// value we call the CountryID.
const int kCountryIDUnknown = -1;
inline int CountryCharsToCountryID(char c1, char c2) {
return c1 << 8 | c2;
}
int CountryCharsToCountryIDWithUpdate(char c1, char c2) {
// SPECIAL CASE: In 2003, Yugoslavia renamed itself to Serbia and Montenegro.
// Serbia and Montenegro dissolved their union in June 2006. Yugoslavia was
// ISO 'YU' and Serbia and Montenegro were ISO 'CS'. Serbia was subsequently
// issued 'RS' and Montenegro 'ME'. Windows XP and Mac OS X Leopard still use
// the value 'YU'. If we get a value of 'YU' or 'CS' we will map it to 'RS'.
if ((c1 == 'Y' && c2 == 'U') ||
(c1 == 'C' && c2 == 'S')) {
c1 = 'R';
c2 = 'S';
}
// SPECIAL CASE: Timor-Leste changed from 'TP' to 'TL' in 2002. Windows XP
// predates this; we therefore map this value.
if (c1 == 'T' && c2 == 'P')
c2 = 'L';
return CountryCharsToCountryID(c1, c2);
}
#if !defined(OS_WIN) && !defined(OS_MACOSX)
int CountryStringToCountryID(const std::string& country) {
return (country.length() == 2)
? CountryCharsToCountryIDWithUpdate(country[0], country[1])
: kCountryIDUnknown;
}
#endif
#if defined(OS_WIN)
// For reference, a list of GeoIDs can be found at
// http://msdn.microsoft.com/en-us/library/dd374073.aspx .
int GeoIDToCountryID(GEOID geo_id) {
const int kISOBufferSize = 3; // Two plus one for the terminator.
wchar_t isobuf[kISOBufferSize] = { 0 };
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]));
// Various locations have ISO codes that Windows does not return.
switch (geo_id) {
case 0x144: // Guernsey
return CountryCharsToCountryID('G', 'G');
case 0x148: // Jersey
return CountryCharsToCountryID('J', 'E');
case 0x3B16: // Isle of Man
return CountryCharsToCountryID('I', 'M');
// 'UM' (U.S. Minor Outlying Islands)
case 0x7F: // Johnston Atoll
case 0x102: // Wake Island
case 0x131: // Baker Island
case 0x146: // Howland Island
case 0x147: // Jarvis Island
case 0x149: // Kingman Reef
case 0x152: // Palmyra Atoll
case 0x52FA: // Midway Islands
return CountryCharsToCountryID('U', 'M');
// 'SH' (Saint Helena)
case 0x12F: // Ascension Island
case 0x15C: // Tristan da Cunha
return CountryCharsToCountryID('S', 'H');
// 'IO' (British Indian Ocean Territory)
case 0x13A: // Diego Garcia
return CountryCharsToCountryID('I', 'O');
// Other cases where there is no ISO country code; we assign countries that
// can serve as reasonable defaults.
case 0x154: // Rota Island
case 0x155: // Saipan
case 0x15A: // Tinian Island
return CountryCharsToCountryID('U', 'S');
case 0x134: // Channel Islands
return CountryCharsToCountryID('G', 'B');
case 0x143: // Guantanamo Bay
default:
return kCountryIDUnknown;
}
}
#endif // defined(OS_WIN)
int GetCountryIDFromPrefs(PrefService* prefs) {
if (!prefs)
return GetCurrentCountryID();
// Cache first run Country ID value in prefs, and use it afterwards. This
// ensures that just because the user moves around, we won't automatically
// make major changes to their available search providers, which would feel
// surprising.
if (!prefs->HasPrefPath(prefs::kCountryIDAtInstall)) {
prefs->SetInteger(prefs::kCountryIDAtInstall, GetCurrentCountryID());
}
return prefs->GetInteger(prefs::kCountryIDAtInstall);
}
std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulationSetFromCountryID( std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulationSetFromCountryID(
int country_id) { int country_id) {
const PrepopulatedEngine* const* engines; const PrepopulatedEngine* const* engines;
...@@ -1073,7 +941,7 @@ std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulationSetFromCountryID( ...@@ -1073,7 +941,7 @@ std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulationSetFromCountryID(
UNHANDLED_COUNTRY(V, U) // Vanuatu UNHANDLED_COUNTRY(V, U) // Vanuatu
UNHANDLED_COUNTRY(W, S) // Samoa UNHANDLED_COUNTRY(W, S) // Samoa
UNHANDLED_COUNTRY(Z, M) // Zambia UNHANDLED_COUNTRY(Z, M) // Zambia
case kCountryIDUnknown: case country_codes::kCountryIDUnknown:
default: // Unhandled location default: // Unhandled location
END_UNHANDLED_COUNTRIES(def, ault) END_UNHANDLED_COUNTRIES(def, ault)
} }
...@@ -1118,7 +986,7 @@ bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { ...@@ -1118,7 +986,7 @@ bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
// Global functions ----------------------------------------------------------- // Global functions -----------------------------------------------------------
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); country_codes::RegisterProfilePrefs(registry);
registry->RegisterListPref(prefs::kSearchProviderOverrides); registry->RegisterListPref(prefs::kSearchProviderOverrides);
registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); registry->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1);
} }
...@@ -1142,7 +1010,8 @@ std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines( ...@@ -1142,7 +1010,8 @@ std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedEngines(
if (!t_urls.empty()) if (!t_urls.empty())
return t_urls; return t_urls;
return GetPrepopulationSetFromCountryID(GetCountryIDFromPrefs(prefs)); return GetPrepopulationSetFromCountryID(
country_codes::GetCountryIDFromPrefs(prefs));
} }
std::unique_ptr<TemplateURLData> GetPrepopulatedEngine(PrefService* prefs, std::unique_ptr<TemplateURLData> GetPrepopulatedEngine(PrefService* prefs,
...@@ -1161,8 +1030,8 @@ std::unique_ptr<TemplateURLData> GetPrepopulatedEngine(PrefService* prefs, ...@@ -1161,8 +1030,8 @@ std::unique_ptr<TemplateURLData> GetPrepopulatedEngine(PrefService* prefs,
std::vector<std::unique_ptr<TemplateURLData>> GetLocalPrepopulatedEngines( std::vector<std::unique_ptr<TemplateURLData>> GetLocalPrepopulatedEngines(
const std::string& locale) { const std::string& locale) {
int country_id = CountryStringToCountryID(locale); int country_id = country_codes::CountryStringToCountryID(locale);
if (country_id == kCountryIDUnknown) { if (country_id == country_codes::kCountryIDUnknown) {
LOG(ERROR) << "Unknown country code specified: " << locale; LOG(ERROR) << "Unknown country code specified: " << locale;
return std::vector<std::unique_ptr<TemplateURLData>>(); return std::vector<std::unique_ptr<TemplateURLData>>();
} }
...@@ -1227,58 +1096,4 @@ SearchEngineType GetEngineType(const GURL& url) { ...@@ -1227,58 +1096,4 @@ SearchEngineType GetEngineType(const GURL& url) {
return SEARCH_ENGINE_OTHER; return SEARCH_ENGINE_OTHER;
} }
#if defined(OS_WIN)
int GetCurrentCountryID() {
return GeoIDToCountryID(GetUserGeoID(GEOCLASS_NATION));
}
#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() {
return CountryStringToCountryID(base::android::GetDefaultCountryCode());
}
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
int GetCurrentCountryID() {
const char* locale = setlocale(LC_MESSAGES, nullptr);
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.
// First remove the language portion.
std::string locale_str(locale);
size_t territory_delim = locale_str.find('_');
if (territory_delim == std::string::npos)
return kCountryIDUnknown;
locale_str.erase(0, territory_delim + 1);
// Next remove any codeset/modifier portion and uppercase.
return CountryStringToCountryID(
base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@"))));
}
#endif // OS_*
} // namespace TemplateURLPrepopulateData } // namespace TemplateURLPrepopulateData
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/strings/string16.h"
#include "components/search_engines/search_engine_type.h" #include "components/search_engines/search_engine_type.h"
class GURL; class GURL;
...@@ -70,14 +69,6 @@ std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( ...@@ -70,14 +69,6 @@ std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch(
// This may be called on any thread. // This may be called on any thread.
SearchEngineType GetEngineType(const GURL& url); 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 } // namespace TemplateURLPrepopulateData
#endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PREPOPULATE_DATA_H_ #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PREPOPULATE_DATA_H_
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h" #include "base/values.h"
#include "components/country_codes/country_codes.h"
#include "components/google/core/common/google_switches.h" #include "components/google/core/common/google_switches.h"
#include "components/search_engines/prepopulated_engines.h" #include "components/search_engines/prepopulated_engines.h"
#include "components/search_engines/search_engines_pref_names.h" #include "components/search_engines/search_engines_pref_names.h"
...@@ -106,7 +107,7 @@ TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) { ...@@ -106,7 +107,7 @@ TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 };
for (size_t i = 0; i < arraysize(kCountryIds); ++i) { for (size_t i = 0; i < arraysize(kCountryIds); ++i) {
prefs_.SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); prefs_.SetInteger(country_codes::kCountryIDAtInstall, kCountryIds[i]);
std::vector<std::unique_ptr<TemplateURLData>> urls = std::vector<std::unique_ptr<TemplateURLData>> urls =
TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, nullptr); TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, nullptr);
std::set<int> unique_ids; std::set<int> unique_ids;
...@@ -253,7 +254,7 @@ TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { ...@@ -253,7 +254,7 @@ TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) {
// Verifies that built-in search providers are processed correctly. // Verifies that built-in search providers are processed correctly.
TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) {
// Use United States. // Use United States.
prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S'); prefs_.SetInteger(country_codes::kCountryIDAtInstall, 'U' << 8 | 'S');
size_t default_index; size_t default_index;
std::vector<std::unique_ptr<TemplateURLData>> t_urls = std::vector<std::unique_ptr<TemplateURLData>> t_urls =
TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
......
...@@ -11,6 +11,7 @@ include_rules = [ ...@@ -11,6 +11,7 @@ include_rules = [
"+components/component_updater", "+components/component_updater",
"+components/content_settings/core", "+components/content_settings/core",
"+components/cookie_config", "+components/cookie_config",
"+components/country_codes",
"+components/crash/core/browser", "+components/crash/core/browser",
"+components/crash/core/common", "+components/crash/core/common",
"+components/dom_distiller/core", "+components/dom_distiller/core",
......
...@@ -36,6 +36,7 @@ source_set("search_engines") { ...@@ -36,6 +36,7 @@ source_set("search_engines") {
] ]
deps = [ deps = [
"//base", "//base",
"//components/country_codes",
"//components/favicon/ios", "//components/favicon/ios",
"//components/google/core/browser", "//components/google/core/browser",
"//components/history/core/browser", "//components/history/core/browser",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/location.h" #include "base/location.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/country_codes/country_codes.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/search_engines/search_engines_pref_names.h" #include "components/search_engines/search_engines_pref_names.h"
#include "components/search_engines/template_url_prepopulate_data.h" #include "components/search_engines/template_url_prepopulate_data.h"
...@@ -86,17 +87,18 @@ namespace search_engines { ...@@ -86,17 +87,18 @@ namespace search_engines {
void UpdateSearchEnginesIfNeeded(PrefService* preferences, void UpdateSearchEnginesIfNeeded(PrefService* preferences,
TemplateURLService* service) { TemplateURLService* service) {
if (!preferences->HasPrefPath(prefs::kCountryIDAtInstall)) { if (!preferences->HasPrefPath(country_codes::kCountryIDAtInstall)) {
// No search engines were ever installed, just return. // No search engines were ever installed, just return.
return; return;
} }
int old_country_id = preferences->GetInteger(prefs::kCountryIDAtInstall); int old_country_id =
int country_id = TemplateURLPrepopulateData::GetCurrentCountryID(); preferences->GetInteger(country_codes::kCountryIDAtInstall);
int country_id = country_codes::GetCurrentCountryID();
if (country_id == old_country_id) { if (country_id == old_country_id) {
// User's locale did not change, just return. // User's locale did not change, just return.
return; return;
} }
preferences->SetInteger(prefs::kCountryIDAtInstall, country_id); preferences->SetInteger(country_codes::kCountryIDAtInstall, country_id);
// If the current search engine is managed by policy then we can't set the // If the current search engine is managed by policy then we can't set the
// default search engine, which is required by UpdateSearchEngine(). This // default search engine, which is required by UpdateSearchEngine(). This
// isn't a problem as long as the default search engine is enforced via // isn't a problem as long as the default search engine is enforced via
......
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