1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
11 #include "base/logging.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/google/google_util.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/search_engines/prepopulated_engines.h"
21 #include "chrome/browser/search_engines/search_terms_data.h"
22 #include "chrome/browser/search_engines/template_url.h"
23 #include "chrome/browser/search_engines/template_url_service.h"
24 #include "chrome/common/pref_names.h"
25 #include "components/user_prefs/pref_registry_syncable.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h"
29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
30 #include "ui/base/l10n/l10n_util.h"
34 #undef IN // On Windows, windef.h defines this, which screws up "India" cases.
35 #elif defined(OS_MACOSX)
36 #include "base/mac/scoped_cftyperef.h"
39 namespace TemplateURLPrepopulateData
{
42 // Helpers --------------------------------------------------------------------
46 // NOTE: You should probably not change the data in this file without changing
47 // |kCurrentDataVersion| in prepopulated_engines.json. See comments in
48 // GetDataVersion() below!
50 // Put the engines within each country in order with most interesting/important
51 // first. The default will be the first engine.
53 // Default (for countries with no better engine set)
54 const PrepopulatedEngine
* engines_default
[] =
55 { &google
, &bing
, &yahoo
, };
57 // United Arab Emirates
58 const PrepopulatedEngine
* engines_AE
[] =
59 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
62 const PrepopulatedEngine
* engines_AL
[] =
63 { &google
, &yahoo
, &bing_en_XA
, &bing_ar_XA
, };
66 const PrepopulatedEngine
* engines_AR
[] =
67 { &google
, &bing_es_AR
, &yahoo_ar
, };
70 const PrepopulatedEngine
* engines_AT
[] =
71 { &google
, &bing_de_AT
, &yahoo_at
, };
74 const PrepopulatedEngine
* engines_AU
[] =
75 { &google
, &bing_en_AU
, &yahoo_au
, };
77 // Bosnia and Herzegovina
78 const PrepopulatedEngine
* engines_BA
[] =
79 { &google
, &yahoo
, &bing
, };
82 const PrepopulatedEngine
* engines_BE
[] =
83 { &google
, &bing_nl_BE
, &bing_fr_BE
, &yahoo_be
, &yahoo_fr_be
, };
86 const PrepopulatedEngine
* engines_BG
[] =
87 { &google
, &bing
, &ask
, };
90 const PrepopulatedEngine
* engines_BH
[] =
91 { &google
, &yahoo_maktoob
, &bing_en_XA
, &bing_ar_XA
, };
94 const PrepopulatedEngine
* engines_BI
[] =
95 { &google
, &yahoo
, &bing
, };
98 const PrepopulatedEngine
* engines_BN
[] =
99 { &google
, &yahoo_my
, &bing
, };
102 const PrepopulatedEngine
* engines_BO
[] =
103 { &google
, &bing_es_XL
, &yahoo
, };
106 const PrepopulatedEngine
* engines_BR
[] =
107 { &google
, &ask_br
, &bing_pt_BR
, &yahoo_br
, };
110 const PrepopulatedEngine
* engines_BY
[] =
111 { &google
, &yahoo_ru
, &bing_ru_RU
, };
114 const PrepopulatedEngine
* engines_BZ
[] =
115 { &google
, &yahoo
, &bing
, };
118 const PrepopulatedEngine
* engines_CA
[] =
119 { &google
, &bing_en_CA
, &bing_fr_CA
, &ask
, &yahoo_ca
, &yahoo_qc
, };
122 const PrepopulatedEngine
* engines_CH
[] =
123 { &google
, &bing_de_CH
, &bing_fr_CH
, &yahoo_ch
, };
126 const PrepopulatedEngine
* engines_CL
[] =
127 { &google
, &bing_es_CL
, &yahoo_cl
, };
130 const PrepopulatedEngine
* engines_CN
[] =
131 { &google
, &baidu
, &sogou
, &soso
, };
134 const PrepopulatedEngine
* engines_CO
[] =
135 { &google
, &bing_es_XL
, &yahoo_co
, };
138 const PrepopulatedEngine
* engines_CR
[] =
139 { &google
, &yahoo
, &bing_es_XL
, };
142 const PrepopulatedEngine
* engines_CZ
[] =
143 { &google
, &seznam
, &bing
, };
146 const PrepopulatedEngine
* engines_DE
[] =
147 { &google
, &bing_de_DE
, &yahoo_de
};
150 const PrepopulatedEngine
* engines_DK
[] =
151 { &google
, &bing_da_DK
, &yahoo_dk
, };
153 // Dominican Republic
154 const PrepopulatedEngine
* engines_DO
[] =
155 { &google
, &yahoo
, &bing_es_XL
, };
158 const PrepopulatedEngine
* engines_DZ
[] =
159 { &google
, &bing_ar_XA
, &bing_en_XA
, &yahoo_maktoob
, };
162 const PrepopulatedEngine
* engines_EC
[] =
163 { &google
, &bing_es_XL
, &yahoo
, };
166 const PrepopulatedEngine
* engines_EE
[] =
167 { &google
, &bing
, &yahoo
, };
170 const PrepopulatedEngine
* engines_EG
[] =
171 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
174 const PrepopulatedEngine
* engines_ES
[] =
175 { &google
, &bing_es_ES
, &yahoo_es
, };
178 const PrepopulatedEngine
* engines_FO
[] =
179 { &google
, &bing_da_DK
, &ask
, };
182 const PrepopulatedEngine
* engines_FI
[] =
183 { &google
, &bing_fi_FI
, &yahoo_fi
, };
186 const PrepopulatedEngine
* engines_FR
[] =
187 { &google
, &bing_fr_FR
, &yahoo_fr
, };
190 const PrepopulatedEngine
* engines_GB
[] =
191 { &google
, &bing_en_GB
, &yahoo_uk
, &ask_uk
, };
194 const PrepopulatedEngine
* engines_GR
[] =
195 { &google
, &bing
, &yahoo_gr
, };
198 const PrepopulatedEngine
* engines_GT
[] =
199 { &google
, &yahoo
, &bing_es_XL
, };
202 const PrepopulatedEngine
* engines_HK
[] =
203 { &google
, &yahoo_hk
, &baidu
, &bing_zh_HK
, };
206 const PrepopulatedEngine
* engines_HN
[] =
207 { &google
, &yahoo
, &bing_es_XL
, };
210 const PrepopulatedEngine
* engines_HR
[] =
211 { &google
, &bing
, &yahoo
, };
214 const PrepopulatedEngine
* engines_HU
[] =
215 { &google
, &bing
, &yahoo
, };
218 const PrepopulatedEngine
* engines_ID
[] =
219 { &google
, &yahoo_id
, &bing
, };
222 const PrepopulatedEngine
* engines_IE
[] =
223 { &google
, &bing_en_IE
, &yahoo_uk
, };
226 const PrepopulatedEngine
* engines_IL
[] =
227 { &google
, &yahoo
, &bing
, };
230 const PrepopulatedEngine
* engines_IN
[] =
231 { &google
, &bing_en_IN
, &yahoo_in
, };
234 const PrepopulatedEngine
* engines_IQ
[] =
235 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
238 const PrepopulatedEngine
* engines_IR
[] =
239 { &google
, &yahoo
, &bing
, };
242 const PrepopulatedEngine
* engines_IS
[] =
243 { &google
, &bing
, &yahoo
, };
246 const PrepopulatedEngine
* engines_IT
[] =
247 { &google
, &virgilio
, &bing_it_IT
, };
250 const PrepopulatedEngine
* engines_JM
[] =
251 { &google
, &yahoo
, &bing
, };
254 const PrepopulatedEngine
* engines_JO
[] =
255 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
258 const PrepopulatedEngine
* engines_JP
[] =
259 { &google
, &yahoo_jp
, &bing_ja_JP
, };
262 const PrepopulatedEngine
* engines_KE
[] =
263 { &google
, &yahoo
, &bing
, };
266 const PrepopulatedEngine
* engines_KW
[] =
267 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
270 const PrepopulatedEngine
* engines_KR
[] =
271 { &google
, &naver
, &daum
, &nate
, };
274 const PrepopulatedEngine
* engines_KZ
[] =
275 { &google
, &bing
, &yahoo
, };
278 const PrepopulatedEngine
* engines_LB
[] =
279 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
282 const PrepopulatedEngine
* engines_LI
[] =
283 { &google
, &bing_de_DE
, &yahoo_de
, };
286 const PrepopulatedEngine
* engines_LT
[] =
287 { &google
, &bing
, &yandex_ru
, };
290 const PrepopulatedEngine
* engines_LU
[] =
291 { &google
, &bing_fr_FR
, &yahoo_fr
, };
294 const PrepopulatedEngine
* engines_LV
[] =
295 { &google
, &yandex_ru
, &bing
, };
298 const PrepopulatedEngine
* engines_LY
[] =
299 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
302 const PrepopulatedEngine
* engines_MA
[] =
303 { &google
, &bing_ar_XA
, &bing_en_XA
, &yahoo_maktoob
, };
306 const PrepopulatedEngine
* engines_MC
[] =
307 { &google
, &yahoo_fr
, &bing_fr_FR
, };
310 const PrepopulatedEngine
* engines_MD
[] =
311 { &google
, &bing
, &yahoo
, };
314 const PrepopulatedEngine
* engines_ME
[] =
315 { &google
, &bing
, &yahoo
, };
318 const PrepopulatedEngine
* engines_MK
[] =
319 { &google
, &yahoo
, &bing
, };
322 const PrepopulatedEngine
* engines_MX
[] =
323 { &google
, &bing_es_MX
, &yahoo_mx
, };
326 const PrepopulatedEngine
* engines_MY
[] =
327 { &google
, &yahoo_my
, &bing
, };
330 const PrepopulatedEngine
* engines_NI
[] =
331 { &google
, &yahoo
, &bing_es_XL
, };
334 const PrepopulatedEngine
* engines_NL
[] =
335 { &google
, &yahoo_nl
, &vinden
, };
338 const PrepopulatedEngine
* engines_NO
[] =
339 { &google
, &bing_nb_NO
, &kvasir
, };
342 const PrepopulatedEngine
* engines_NZ
[] =
343 { &google
, &bing_en_NZ
, &yahoo_nz
, };
346 const PrepopulatedEngine
* engines_OM
[] =
347 { &google
, &bing_ar_XA
, &yahoo_maktoob
, &bing_en_XA
, };
350 const PrepopulatedEngine
* engines_PA
[] =
351 { &google
, &yahoo
, &bing_es_XL
, };
354 const PrepopulatedEngine
* engines_PE
[] =
355 { &google
, &bing_es_XL
, &yahoo_pe
, };
358 const PrepopulatedEngine
* engines_PH
[] =
359 { &google
, &yahoo_ph
, &bing_en_PH
, };
362 const PrepopulatedEngine
* engines_PK
[] =
363 { &google
, &yahoo
, &bing
, };
366 const PrepopulatedEngine
* engines_PR
[] =
367 { &google
, &yahoo
, &bing_es_XL
, };
370 const PrepopulatedEngine
* engines_PL
[] =
371 { &google
, &onet
, &bing_pl_PL
, };
374 const PrepopulatedEngine
* engines_PT
[] =
375 { &google
, &bing_pt_PT
, &yahoo
, };
378 const PrepopulatedEngine
* engines_PY
[] =
379 { &google
, &bing_es_XL
, &yahoo
, };
382 const PrepopulatedEngine
* engines_QA
[] =
383 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
386 const PrepopulatedEngine
* engines_RO
[] =
387 { &google
, &yahoo_ro
, &bing
, };
390 const PrepopulatedEngine
* engines_RS
[] =
391 { &google
, &bing
, &yahoo
, };
394 const PrepopulatedEngine
* engines_RU
[] =
395 { &google
, &yandex_ru
, &mail_ru
, };
398 const PrepopulatedEngine
* engines_RW
[] =
399 { &google
, &bing
, &yahoo
, };
402 const PrepopulatedEngine
* engines_SA
[] =
403 { &google
, &yahoo_maktoob
, &bing_ar_XA
, &bing_en_XA
, };
406 const PrepopulatedEngine
* engines_SE
[] =
407 { &google
, &bing_sv_SE
, &yahoo_se
, };
410 const PrepopulatedEngine
* engines_SG
[] =
411 { &google
, &yahoo_sg
, &bing_en_SG
, };
414 const PrepopulatedEngine
* engines_SI
[] =
415 { &google
, &najdi
, &ask
, };
418 const PrepopulatedEngine
* engines_SK
[] =
419 { &google
, &bing
, &yahoo
, };
422 const PrepopulatedEngine
* engines_SV
[] =
423 { &google
, &yahoo
, &bing_es_XL
, };
426 const PrepopulatedEngine
* engines_SY
[] =
427 { &google
, &bing_ar_XA
, &bing_en_XA
, &yahoo_maktoob
, };
430 const PrepopulatedEngine
* engines_TH
[] =
431 { &google
, &yahoo_th
, &bing
, };
434 const PrepopulatedEngine
* engines_TN
[] =
435 { &google
, &bing_ar_XA
, &bing_en_XA
, &yahoo_maktoob
, };
438 const PrepopulatedEngine
* engines_TR
[] =
439 { &google
, &bing_tr_TR
, &yahoo_tr
, };
441 // Trinidad and Tobago
442 const PrepopulatedEngine
* engines_TT
[] =
443 { &google
, &bing
, &yahoo
, };
446 const PrepopulatedEngine
* engines_TW
[] =
447 { &google
, &yahoo_tw
, &bing_zh_TW
, };
450 const PrepopulatedEngine
* engines_TZ
[] =
451 { &google
, &yahoo
, &bing
, };
454 const PrepopulatedEngine
* engines_UA
[] =
455 { &google
, &yandex_ua
, &bing_ru_RU
, };
458 const PrepopulatedEngine
* engines_US
[] =
459 { &google
, &bing_en_US
, &yahoo
, &aol
, &ask
, };
462 const PrepopulatedEngine
* engines_UY
[] =
463 { &google
, &bing_es_XL
, &yahoo
, };
466 const PrepopulatedEngine
* engines_VE
[] =
467 { &google
, &bing_es_XL
, &yahoo_ve
, };
470 const PrepopulatedEngine
* engines_VN
[] =
471 { &google
, &yahoo_vn
, &bing
, };
474 const PrepopulatedEngine
* engines_YE
[] =
475 { &google
, &bing_ar_XA
, &bing_en_XA
, &yahoo_maktoob
, };
478 const PrepopulatedEngine
* engines_ZA
[] =
479 { &google
, &bing
, &yahoo_za
, };
482 const PrepopulatedEngine
* engines_ZW
[] =
483 { &google
, &bing
, &yahoo
, &ask
, };
485 // A list of all the engines that we know about.
486 const PrepopulatedEngine
* kAllEngines
[] = {
487 // Prepopulated engines:
488 &aol
, &ask
, &ask_br
, &ask_uk
, &baidu
,
489 &bing
, &bing_ar_XA
, &bing_da_DK
, &bing_de_AT
, &bing_de_CH
,
490 &bing_de_DE
, &bing_en_AU
, &bing_en_CA
, &bing_en_GB
, &bing_en_IE
,
491 &bing_en_IN
, &bing_en_NZ
, &bing_en_PH
, &bing_en_SG
, &bing_en_US
,
492 &bing_en_XA
, &bing_es_AR
, &bing_es_CL
, &bing_es_ES
, &bing_es_MX
,
493 &bing_es_XL
, &bing_fi_FI
, &bing_fr_BE
, &bing_fr_CA
, &bing_fr_CH
,
494 &bing_fr_FR
, &bing_it_IT
, &bing_ja_JP
, &bing_lv_LV
, &bing_nb_NO
,
495 &bing_nl_BE
, &bing_pl_PL
, &bing_pt_BR
, &bing_pt_PT
, &bing_ru_RU
,
496 &bing_sv_SE
, &bing_tr_TR
, &bing_zh_HK
, &bing_zh_TW
, &daum
,
497 &google
, &kvasir
, &mail_ru
, &najdi
, &nate
,
498 &naver
, &onet
, &seznam
, &sogou
, &soso
,
499 &vinden
, &virgilio
, &yahoo
, &yahoo_ar
, &yahoo_at
,
500 &yahoo_au
, &yahoo_be
, &yahoo_br
, &yahoo_ca
, &yahoo_ch
,
501 &yahoo_cl
, &yahoo_co
, &yahoo_de
, &yahoo_dk
, &yahoo_es
,
502 &yahoo_fi
, &yahoo_fr
, &yahoo_fr_be
, &yahoo_gr
, &yahoo_hk
,
503 &yahoo_id
, &yahoo_in
, &yahoo_jp
, &yahoo_maktoob
, &yahoo_mx
,
504 &yahoo_my
, &yahoo_nl
, &yahoo_nz
, &yahoo_pe
, &yahoo_ph
,
505 &yahoo_qc
, &yahoo_ro
, &yahoo_ru
, &yahoo_se
, &yahoo_sg
,
506 &yahoo_th
, &yahoo_tr
, &yahoo_tw
, &yahoo_uk
, &yahoo_ve
,
507 &yahoo_vn
, &yahoo_za
, &yandex_ru
, &yandex_ua
,
510 &atlas_cz
, &atlas_sk
, &avg
, &babylon
, &conduit
,
511 &delfi_lt
, &delfi_lv
, &delta
, &funmoods
, &goo
,
512 &imesh
, &iminent
, &in
, &incredibar
, &libero
,
513 &neti
, &nigma
, &ok
, &rambler
, &sapo
,
514 &search_results
, &searchnu
, &snapdo
, &softonic
, &sweetim
,
515 &terra_ar
, &terra_es
, &tut
, &walla
, &wp
,
519 const struct LogoURLs
{
520 const char* const logo_100_percent_url
;
521 const char* const logo_200_percent_url
;
523 "https://www.google.com/images/chrome_search/google_logo.png",
524 "https://www.google.com/images/chrome_search/google_logo_2x.png",
527 // Please refer to ISO 3166-1 for information about the two-character country
528 // codes; http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 is useful. In the
529 // following (C++) code, we pack the two letters of the country code into an int
530 // value we call the CountryID.
532 const int kCountryIDUnknown
= -1;
534 inline int CountryCharsToCountryID(char c1
, char c2
) {
538 int CountryCharsToCountryIDWithUpdate(char c1
, char c2
) {
539 // SPECIAL CASE: In 2003, Yugoslavia renamed itself to Serbia and Montenegro.
540 // Serbia and Montenegro dissolved their union in June 2006. Yugoslavia was
541 // ISO 'YU' and Serbia and Montenegro were ISO 'CS'. Serbia was subsequently
542 // issued 'RS' and Montenegro 'ME'. Windows XP and Mac OS X Leopard still use
543 // the value 'YU'. If we get a value of 'YU' or 'CS' we will map it to 'RS'.
544 if ((c1
== 'Y' && c2
== 'U') ||
545 (c1
== 'C' && c2
== 'S')) {
550 // SPECIAL CASE: Timor-Leste changed from 'TP' to 'TL' in 2002. Windows XP
551 // predates this; we therefore map this value.
552 if (c1
== 'T' && c2
== 'P')
555 return CountryCharsToCountryID(c1
, c2
);
560 // For reference, a list of GeoIDs can be found at
561 // http://msdn.microsoft.com/en-us/library/dd374073.aspx .
562 int GeoIDToCountryID(GEOID geo_id
) {
563 const int kISOBufferSize
= 3; // Two plus one for the terminator.
564 wchar_t isobuf
[kISOBufferSize
] = { 0 };
565 int retval
= GetGeoInfo(geo_id
, GEO_ISO2
, isobuf
, kISOBufferSize
, 0);
567 if (retval
== kISOBufferSize
&&
568 !(isobuf
[0] == L
'X' && isobuf
[1] == L
'X'))
569 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf
[0]),
570 static_cast<char>(isobuf
[1]));
572 // Various locations have ISO codes that Windows does not return.
574 case 0x144: // Guernsey
575 return CountryCharsToCountryID('G', 'G');
576 case 0x148: // Jersey
577 return CountryCharsToCountryID('J', 'E');
578 case 0x3B16: // Isle of Man
579 return CountryCharsToCountryID('I', 'M');
581 // 'UM' (U.S. Minor Outlying Islands)
582 case 0x7F: // Johnston Atoll
583 case 0x102: // Wake Island
584 case 0x131: // Baker Island
585 case 0x146: // Howland Island
586 case 0x147: // Jarvis Island
587 case 0x149: // Kingman Reef
588 case 0x152: // Palmyra Atoll
589 case 0x52FA: // Midway Islands
590 return CountryCharsToCountryID('U', 'M');
592 // 'SH' (Saint Helena)
593 case 0x12F: // Ascension Island
594 case 0x15C: // Tristan da Cunha
595 return CountryCharsToCountryID('S', 'H');
597 // 'IO' (British Indian Ocean Territory)
598 case 0x13A: // Diego Garcia
599 return CountryCharsToCountryID('I', 'O');
601 // Other cases where there is no ISO country code; we assign countries that
602 // can serve as reasonable defaults.
603 case 0x154: // Rota Island
604 case 0x155: // Saipan
605 case 0x15A: // Tinian Island
606 return CountryCharsToCountryID('U', 'S');
607 case 0x134: // Channel Islands
608 return CountryCharsToCountryID('G', 'B');
609 case 0x143: // Guantanamo Bay
611 return kCountryIDUnknown
;
615 int GetCurrentCountryID() {
616 GEOID geo_id
= GetUserGeoID(GEOCLASS_NATION
);
618 return GeoIDToCountryID(geo_id
);
621 #elif defined(OS_MACOSX)
623 int GetCurrentCountryID() {
624 base::ScopedCFTypeRef
<CFLocaleRef
> locale(CFLocaleCopyCurrent());
625 CFStringRef country
= (CFStringRef
)CFLocaleGetValue(locale
.get(),
626 kCFLocaleCountryCode
);
628 return kCountryIDUnknown
;
631 CFRange char_range
= CFRangeMake(0, 2);
632 CFStringGetCharacters(country
, char_range
, isobuf
);
634 return CountryCharsToCountryIDWithUpdate(static_cast<char>(isobuf
[0]),
635 static_cast<char>(isobuf
[1]));
638 #elif defined(OS_ANDROID)
640 // Initialized by InitCountryCode().
641 int g_country_code_at_install
= kCountryIDUnknown
;
643 int GetCurrentCountryID() {
644 return g_country_code_at_install
;
647 #elif defined(OS_POSIX)
649 int GetCurrentCountryID() {
650 const char* locale
= setlocale(LC_MESSAGES
, NULL
);
653 return kCountryIDUnknown
;
655 // The format of a locale name is:
656 // language[_territory][.codeset][@modifier], where territory is an ISO 3166
657 // country code, which is what we want.
658 std::string
locale_str(locale
);
659 size_t begin
= locale_str
.find('_');
660 if (begin
== std::string::npos
|| locale_str
.size() - begin
< 3)
661 return kCountryIDUnknown
;
664 size_t end
= locale_str
.find_first_of(".@", begin
);
665 if (end
== std::string::npos
)
666 end
= locale_str
.size();
668 // The territory part must contain exactly two characters.
669 if (end
- begin
== 2) {
670 return CountryCharsToCountryIDWithUpdate(
671 base::ToUpperASCII(locale_str
[begin
]),
672 base::ToUpperASCII(locale_str
[begin
+ 1]));
675 return kCountryIDUnknown
;
680 int GetCountryIDFromPrefs(PrefService
* prefs
) {
682 return GetCurrentCountryID();
684 // Cache first run Country ID value in prefs, and use it afterwards. This
685 // ensures that just because the user moves around, we won't automatically
686 // make major changes to their available search providers, which would feel
688 if (!prefs
->HasPrefPath(prefs::kCountryIDAtInstall
)) {
689 int new_country_id
= GetCurrentCountryID();
691 // Migrate the old platform-specific value if it's present.
692 if (prefs
->HasPrefPath(prefs::kGeoIDAtInstall
)) {
693 int geo_id
= prefs
->GetInteger(prefs::kGeoIDAtInstall
);
694 prefs
->ClearPref(prefs::kGeoIDAtInstall
);
695 new_country_id
= GeoIDToCountryID(geo_id
);
698 prefs
->SetInteger(prefs::kCountryIDAtInstall
, new_country_id
);
700 return prefs
->GetInteger(prefs::kCountryIDAtInstall
);
703 void GetPrepopulationSetFromCountryID(PrefService
* prefs
,
704 const PrepopulatedEngine
*** engines
,
705 size_t* num_engines
) {
706 // NOTE: This function should ALWAYS set its outparams.
708 // If you add a new country make sure to update the unit test for coverage.
709 switch (GetCountryIDFromPrefs(prefs
)) {
737 #define CHAR(ch) CHAR_##ch
738 #define CODE_TO_ID(code1, code2)\
739 (CHAR(code1) << 8 | CHAR(code2))
741 #define UNHANDLED_COUNTRY(code1, code2)\
742 case CODE_TO_ID(code1, code2):
743 #define END_UNHANDLED_COUNTRIES(code1, code2)\
744 *engines = engines_##code1##code2;\
745 *num_engines = arraysize(engines_##code1##code2);\
747 #define DECLARE_COUNTRY(code1, code2)\
748 UNHANDLED_COUNTRY(code1, code2)\
749 END_UNHANDLED_COUNTRIES(code1, code2)
751 // Countries with their own, dedicated engine set.
752 DECLARE_COUNTRY(A
, E
) // United Arab Emirates
753 DECLARE_COUNTRY(A
, L
) // Albania
754 DECLARE_COUNTRY(A
, R
) // Argentina
755 DECLARE_COUNTRY(A
, T
) // Austria
756 DECLARE_COUNTRY(A
, U
) // Australia
757 DECLARE_COUNTRY(B
, A
) // Bosnia and Herzegovina
758 DECLARE_COUNTRY(B
, E
) // Belgium
759 DECLARE_COUNTRY(B
, G
) // Bulgaria
760 DECLARE_COUNTRY(B
, H
) // Bahrain
761 DECLARE_COUNTRY(B
, I
) // Burundi
762 DECLARE_COUNTRY(B
, N
) // Brunei
763 DECLARE_COUNTRY(B
, O
) // Bolivia
764 DECLARE_COUNTRY(B
, R
) // Brazil
765 DECLARE_COUNTRY(B
, Y
) // Belarus
766 DECLARE_COUNTRY(B
, Z
) // Belize
767 DECLARE_COUNTRY(C
, A
) // Canada
768 DECLARE_COUNTRY(C
, H
) // Switzerland
769 DECLARE_COUNTRY(C
, L
) // Chile
770 DECLARE_COUNTRY(C
, N
) // China
771 DECLARE_COUNTRY(C
, O
) // Colombia
772 DECLARE_COUNTRY(C
, R
) // Costa Rica
773 DECLARE_COUNTRY(C
, Z
) // Czech Republic
774 DECLARE_COUNTRY(D
, E
) // Germany
775 DECLARE_COUNTRY(D
, K
) // Denmark
776 DECLARE_COUNTRY(D
, O
) // Dominican Republic
777 DECLARE_COUNTRY(D
, Z
) // Algeria
778 DECLARE_COUNTRY(E
, C
) // Ecuador
779 DECLARE_COUNTRY(E
, E
) // Estonia
780 DECLARE_COUNTRY(E
, G
) // Egypt
781 DECLARE_COUNTRY(E
, S
) // Spain
782 DECLARE_COUNTRY(F
, I
) // Finland
783 DECLARE_COUNTRY(F
, O
) // Faroe Islands
784 DECLARE_COUNTRY(F
, R
) // France
785 DECLARE_COUNTRY(G
, B
) // United Kingdom
786 DECLARE_COUNTRY(G
, R
) // Greece
787 DECLARE_COUNTRY(G
, T
) // Guatemala
788 DECLARE_COUNTRY(H
, K
) // Hong Kong
789 DECLARE_COUNTRY(H
, N
) // Honduras
790 DECLARE_COUNTRY(H
, R
) // Croatia
791 DECLARE_COUNTRY(H
, U
) // Hungary
792 DECLARE_COUNTRY(I
, D
) // Indonesia
793 DECLARE_COUNTRY(I
, E
) // Ireland
794 DECLARE_COUNTRY(I
, L
) // Israel
795 DECLARE_COUNTRY(I
, N
) // India
796 DECLARE_COUNTRY(I
, Q
) // Iraq
797 DECLARE_COUNTRY(I
, R
) // Iran
798 DECLARE_COUNTRY(I
, S
) // Iceland
799 DECLARE_COUNTRY(I
, T
) // Italy
800 DECLARE_COUNTRY(J
, M
) // Jamaica
801 DECLARE_COUNTRY(J
, O
) // Jordan
802 DECLARE_COUNTRY(J
, P
) // Japan
803 DECLARE_COUNTRY(K
, E
) // Kenya
804 DECLARE_COUNTRY(K
, R
) // South Korea
805 DECLARE_COUNTRY(K
, W
) // Kuwait
806 DECLARE_COUNTRY(K
, Z
) // Kazakhstan
807 DECLARE_COUNTRY(L
, B
) // Lebanon
808 DECLARE_COUNTRY(L
, I
) // Liechtenstein
809 DECLARE_COUNTRY(L
, T
) // Lithuania
810 DECLARE_COUNTRY(L
, U
) // Luxembourg
811 DECLARE_COUNTRY(L
, V
) // Latvia
812 DECLARE_COUNTRY(L
, Y
) // Libya
813 DECLARE_COUNTRY(M
, A
) // Morocco
814 DECLARE_COUNTRY(M
, C
) // Monaco
815 DECLARE_COUNTRY(M
, D
) // Moldova
816 DECLARE_COUNTRY(M
, E
) // Montenegro
817 DECLARE_COUNTRY(M
, K
) // Macedonia
818 DECLARE_COUNTRY(M
, X
) // Mexico
819 DECLARE_COUNTRY(M
, Y
) // Malaysia
820 DECLARE_COUNTRY(N
, I
) // Nicaragua
821 DECLARE_COUNTRY(N
, L
) // Netherlands
822 DECLARE_COUNTRY(N
, O
) // Norway
823 DECLARE_COUNTRY(N
, Z
) // New Zealand
824 DECLARE_COUNTRY(O
, M
) // Oman
825 DECLARE_COUNTRY(P
, A
) // Panama
826 DECLARE_COUNTRY(P
, E
) // Peru
827 DECLARE_COUNTRY(P
, H
) // Philippines
828 DECLARE_COUNTRY(P
, K
) // Pakistan
829 DECLARE_COUNTRY(P
, L
) // Poland
830 DECLARE_COUNTRY(P
, R
) // Puerto Rico
831 DECLARE_COUNTRY(P
, T
) // Portugal
832 DECLARE_COUNTRY(P
, Y
) // Paraguay
833 DECLARE_COUNTRY(Q
, A
) // Qatar
834 DECLARE_COUNTRY(R
, O
) // Romania
835 DECLARE_COUNTRY(R
, S
) // Serbia
836 DECLARE_COUNTRY(R
, U
) // Russia
837 DECLARE_COUNTRY(R
, W
) // Rwanda
838 DECLARE_COUNTRY(S
, A
) // Saudi Arabia
839 DECLARE_COUNTRY(S
, E
) // Sweden
840 DECLARE_COUNTRY(S
, G
) // Singapore
841 DECLARE_COUNTRY(S
, I
) // Slovenia
842 DECLARE_COUNTRY(S
, K
) // Slovakia
843 DECLARE_COUNTRY(S
, V
) // El Salvador
844 DECLARE_COUNTRY(S
, Y
) // Syria
845 DECLARE_COUNTRY(T
, H
) // Thailand
846 DECLARE_COUNTRY(T
, N
) // Tunisia
847 DECLARE_COUNTRY(T
, R
) // Turkey
848 DECLARE_COUNTRY(T
, T
) // Trinidad and Tobago
849 DECLARE_COUNTRY(T
, W
) // Taiwan
850 DECLARE_COUNTRY(T
, Z
) // Tanzania
851 DECLARE_COUNTRY(U
, A
) // Ukraine
852 DECLARE_COUNTRY(U
, S
) // United States
853 DECLARE_COUNTRY(U
, Y
) // Uruguay
854 DECLARE_COUNTRY(V
, E
) // Venezuela
855 DECLARE_COUNTRY(V
, N
) // Vietnam
856 DECLARE_COUNTRY(Y
, E
) // Yemen
857 DECLARE_COUNTRY(Z
, A
) // South Africa
858 DECLARE_COUNTRY(Z
, W
) // Zimbabwe
860 // Countries using the "Australia" engine set.
861 UNHANDLED_COUNTRY(C
, C
) // Cocos Islands
862 UNHANDLED_COUNTRY(C
, X
) // Christmas Island
863 UNHANDLED_COUNTRY(H
, M
) // Heard Island and McDonald Islands
864 UNHANDLED_COUNTRY(N
, F
) // Norfolk Island
865 END_UNHANDLED_COUNTRIES(A
, U
)
867 // Countries using the "China" engine set.
868 UNHANDLED_COUNTRY(M
, O
) // Macao
869 END_UNHANDLED_COUNTRIES(C
, N
)
871 // Countries using the "Denmark" engine set.
872 UNHANDLED_COUNTRY(G
, L
) // Greenland
873 END_UNHANDLED_COUNTRIES(D
, K
)
875 // Countries using the "Spain" engine set.
876 UNHANDLED_COUNTRY(A
, D
) // Andorra
877 END_UNHANDLED_COUNTRIES(E
, S
)
879 // Countries using the "Finland" engine set.
880 UNHANDLED_COUNTRY(A
, X
) // Aland Islands
881 END_UNHANDLED_COUNTRIES(F
, I
)
883 // Countries using the "France" engine set.
884 UNHANDLED_COUNTRY(B
, F
) // Burkina Faso
885 UNHANDLED_COUNTRY(B
, J
) // Benin
886 UNHANDLED_COUNTRY(C
, D
) // Congo - Kinshasa
887 UNHANDLED_COUNTRY(C
, F
) // Central African Republic
888 UNHANDLED_COUNTRY(C
, G
) // Congo - Brazzaville
889 UNHANDLED_COUNTRY(C
, I
) // Ivory Coast
890 UNHANDLED_COUNTRY(C
, M
) // Cameroon
891 UNHANDLED_COUNTRY(D
, J
) // Djibouti
892 UNHANDLED_COUNTRY(G
, A
) // Gabon
893 UNHANDLED_COUNTRY(G
, F
) // French Guiana
894 UNHANDLED_COUNTRY(G
, N
) // Guinea
895 UNHANDLED_COUNTRY(G
, P
) // Guadeloupe
896 UNHANDLED_COUNTRY(H
, T
) // Haiti
898 UNHANDLED_COUNTRY(I
, P
) // Clipperton Island ('IP' is an WinXP-ism; ISO
899 // includes it with France)
901 UNHANDLED_COUNTRY(M
, L
) // Mali
902 UNHANDLED_COUNTRY(M
, Q
) // Martinique
903 UNHANDLED_COUNTRY(N
, C
) // New Caledonia
904 UNHANDLED_COUNTRY(N
, E
) // Niger
905 UNHANDLED_COUNTRY(P
, F
) // French Polynesia
906 UNHANDLED_COUNTRY(P
, M
) // Saint Pierre and Miquelon
907 UNHANDLED_COUNTRY(R
, E
) // Reunion
908 UNHANDLED_COUNTRY(S
, N
) // Senegal
909 UNHANDLED_COUNTRY(T
, D
) // Chad
910 UNHANDLED_COUNTRY(T
, F
) // French Southern Territories
911 UNHANDLED_COUNTRY(T
, G
) // Togo
912 UNHANDLED_COUNTRY(W
, F
) // Wallis and Futuna
913 UNHANDLED_COUNTRY(Y
, T
) // Mayotte
914 END_UNHANDLED_COUNTRIES(F
, R
)
916 // Countries using the "Greece" engine set.
917 UNHANDLED_COUNTRY(C
, Y
) // Cyprus
918 END_UNHANDLED_COUNTRIES(G
, R
)
920 // Countries using the "Italy" engine set.
921 UNHANDLED_COUNTRY(S
, M
) // San Marino
922 UNHANDLED_COUNTRY(V
, A
) // Vatican
923 END_UNHANDLED_COUNTRIES(I
, T
)
925 // Countries using the "Morocco" engine set.
926 UNHANDLED_COUNTRY(E
, H
) // Western Sahara
927 END_UNHANDLED_COUNTRIES(M
, A
)
929 // Countries using the "Netherlands" engine set.
930 UNHANDLED_COUNTRY(A
, N
) // Netherlands Antilles
931 UNHANDLED_COUNTRY(A
, W
) // Aruba
932 END_UNHANDLED_COUNTRIES(N
, L
)
934 // Countries using the "Norway" engine set.
935 UNHANDLED_COUNTRY(B
, V
) // Bouvet Island
936 UNHANDLED_COUNTRY(S
, J
) // Svalbard and Jan Mayen
937 END_UNHANDLED_COUNTRIES(N
, O
)
939 // Countries using the "New Zealand" engine set.
940 UNHANDLED_COUNTRY(C
, K
) // Cook Islands
941 UNHANDLED_COUNTRY(N
, U
) // Niue
942 UNHANDLED_COUNTRY(T
, K
) // Tokelau
943 END_UNHANDLED_COUNTRIES(N
, Z
)
945 // Countries using the "Portugal" engine set.
946 UNHANDLED_COUNTRY(C
, V
) // Cape Verde
947 UNHANDLED_COUNTRY(G
, W
) // Guinea-Bissau
948 UNHANDLED_COUNTRY(M
, Z
) // Mozambique
949 UNHANDLED_COUNTRY(S
, T
) // Sao Tome and Principe
950 UNHANDLED_COUNTRY(T
, L
) // Timor-Leste
951 END_UNHANDLED_COUNTRIES(P
, T
)
953 // Countries using the "Russia" engine set.
954 UNHANDLED_COUNTRY(A
, M
) // Armenia
955 UNHANDLED_COUNTRY(A
, Z
) // Azerbaijan
956 UNHANDLED_COUNTRY(K
, G
) // Kyrgyzstan
957 UNHANDLED_COUNTRY(T
, J
) // Tajikistan
958 UNHANDLED_COUNTRY(T
, M
) // Turkmenistan
959 UNHANDLED_COUNTRY(U
, Z
) // Uzbekistan
960 END_UNHANDLED_COUNTRIES(R
, U
)
962 // Countries using the "Saudi Arabia" engine set.
963 UNHANDLED_COUNTRY(M
, R
) // Mauritania
964 UNHANDLED_COUNTRY(P
, S
) // Palestinian Territory
965 UNHANDLED_COUNTRY(S
, D
) // Sudan
966 END_UNHANDLED_COUNTRIES(S
, A
)
968 // Countries using the "United Kingdom" engine set.
969 UNHANDLED_COUNTRY(B
, M
) // Bermuda
970 UNHANDLED_COUNTRY(F
, K
) // Falkland Islands
971 UNHANDLED_COUNTRY(G
, G
) // Guernsey
972 UNHANDLED_COUNTRY(G
, I
) // Gibraltar
973 UNHANDLED_COUNTRY(G
, S
) // South Georgia and the South Sandwich
975 UNHANDLED_COUNTRY(I
, M
) // Isle of Man
976 UNHANDLED_COUNTRY(I
, O
) // British Indian Ocean Territory
977 UNHANDLED_COUNTRY(J
, E
) // Jersey
978 UNHANDLED_COUNTRY(K
, Y
) // Cayman Islands
979 UNHANDLED_COUNTRY(M
, S
) // Montserrat
980 UNHANDLED_COUNTRY(M
, T
) // Malta
981 UNHANDLED_COUNTRY(P
, N
) // Pitcairn Islands
982 UNHANDLED_COUNTRY(S
, H
) // Saint Helena, Ascension Island, and Tristan da
984 UNHANDLED_COUNTRY(T
, C
) // Turks and Caicos Islands
985 UNHANDLED_COUNTRY(V
, G
) // British Virgin Islands
986 END_UNHANDLED_COUNTRIES(G
, B
)
988 // Countries using the "United States" engine set.
989 UNHANDLED_COUNTRY(A
, S
) // American Samoa
990 UNHANDLED_COUNTRY(G
, U
) // Guam
991 UNHANDLED_COUNTRY(M
, P
) // Northern Mariana Islands
992 UNHANDLED_COUNTRY(U
, M
) // U.S. Minor Outlying Islands
993 UNHANDLED_COUNTRY(V
, I
) // U.S. Virgin Islands
994 END_UNHANDLED_COUNTRIES(U
, S
)
996 // Countries using the "default" engine set.
997 UNHANDLED_COUNTRY(A
, F
) // Afghanistan
998 UNHANDLED_COUNTRY(A
, G
) // Antigua and Barbuda
999 UNHANDLED_COUNTRY(A
, I
) // Anguilla
1000 UNHANDLED_COUNTRY(A
, O
) // Angola
1001 UNHANDLED_COUNTRY(A
, Q
) // Antarctica
1002 UNHANDLED_COUNTRY(B
, B
) // Barbados
1003 UNHANDLED_COUNTRY(B
, D
) // Bangladesh
1004 UNHANDLED_COUNTRY(B
, S
) // Bahamas
1005 UNHANDLED_COUNTRY(B
, T
) // Bhutan
1006 UNHANDLED_COUNTRY(B
, W
) // Botswana
1007 UNHANDLED_COUNTRY(C
, U
) // Cuba
1008 UNHANDLED_COUNTRY(D
, M
) // Dominica
1009 UNHANDLED_COUNTRY(E
, R
) // Eritrea
1010 UNHANDLED_COUNTRY(E
, T
) // Ethiopia
1011 UNHANDLED_COUNTRY(F
, J
) // Fiji
1012 UNHANDLED_COUNTRY(F
, M
) // Micronesia
1013 UNHANDLED_COUNTRY(G
, D
) // Grenada
1014 UNHANDLED_COUNTRY(G
, E
) // Georgia
1015 UNHANDLED_COUNTRY(G
, H
) // Ghana
1016 UNHANDLED_COUNTRY(G
, M
) // Gambia
1017 UNHANDLED_COUNTRY(G
, Q
) // Equatorial Guinea
1018 UNHANDLED_COUNTRY(G
, Y
) // Guyana
1019 UNHANDLED_COUNTRY(K
, H
) // Cambodia
1020 UNHANDLED_COUNTRY(K
, I
) // Kiribati
1021 UNHANDLED_COUNTRY(K
, M
) // Comoros
1022 UNHANDLED_COUNTRY(K
, N
) // Saint Kitts and Nevis
1023 UNHANDLED_COUNTRY(K
, P
) // North Korea
1024 UNHANDLED_COUNTRY(L
, A
) // Laos
1025 UNHANDLED_COUNTRY(L
, C
) // Saint Lucia
1026 UNHANDLED_COUNTRY(L
, K
) // Sri Lanka
1027 UNHANDLED_COUNTRY(L
, R
) // Liberia
1028 UNHANDLED_COUNTRY(L
, S
) // Lesotho
1029 UNHANDLED_COUNTRY(M
, G
) // Madagascar
1030 UNHANDLED_COUNTRY(M
, H
) // Marshall Islands
1031 UNHANDLED_COUNTRY(M
, M
) // Myanmar
1032 UNHANDLED_COUNTRY(M
, N
) // Mongolia
1033 UNHANDLED_COUNTRY(M
, U
) // Mauritius
1034 UNHANDLED_COUNTRY(M
, V
) // Maldives
1035 UNHANDLED_COUNTRY(M
, W
) // Malawi
1036 UNHANDLED_COUNTRY(N
, A
) // Namibia
1037 UNHANDLED_COUNTRY(N
, G
) // Nigeria
1038 UNHANDLED_COUNTRY(N
, P
) // Nepal
1039 UNHANDLED_COUNTRY(N
, R
) // Nauru
1040 UNHANDLED_COUNTRY(P
, G
) // Papua New Guinea
1041 UNHANDLED_COUNTRY(P
, W
) // Palau
1042 UNHANDLED_COUNTRY(S
, B
) // Solomon Islands
1043 UNHANDLED_COUNTRY(S
, C
) // Seychelles
1044 UNHANDLED_COUNTRY(S
, L
) // Sierra Leone
1045 UNHANDLED_COUNTRY(S
, O
) // Somalia
1046 UNHANDLED_COUNTRY(S
, R
) // Suriname
1047 UNHANDLED_COUNTRY(S
, Z
) // Swaziland
1048 UNHANDLED_COUNTRY(T
, O
) // Tonga
1049 UNHANDLED_COUNTRY(T
, V
) // Tuvalu
1050 UNHANDLED_COUNTRY(U
, G
) // Uganda
1051 UNHANDLED_COUNTRY(V
, C
) // Saint Vincent and the Grenadines
1052 UNHANDLED_COUNTRY(V
, U
) // Vanuatu
1053 UNHANDLED_COUNTRY(W
, S
) // Samoa
1054 UNHANDLED_COUNTRY(Z
, M
) // Zambia
1055 case kCountryIDUnknown
:
1056 default: // Unhandled location
1057 END_UNHANDLED_COUNTRIES(def
, ault
)
1061 TemplateURL
* MakePrepopulatedTemplateURL(
1063 const base::string16
& name
,
1064 const base::string16
& keyword
,
1065 const base::StringPiece
& search_url
,
1066 const base::StringPiece
& suggest_url
,
1067 const base::StringPiece
& instant_url
,
1068 const base::StringPiece
& image_url
,
1069 const base::StringPiece
& new_tab_url
,
1070 const base::StringPiece
& search_url_post_params
,
1071 const base::StringPiece
& suggest_url_post_params
,
1072 const base::StringPiece
& instant_url_post_params
,
1073 const base::StringPiece
& image_url_post_params
,
1074 const base::StringPiece
& favicon_url
,
1075 const base::StringPiece
& encoding
,
1076 const base::ListValue
& alternate_urls
,
1077 const base::StringPiece
& search_terms_replacement_key
,
1080 TemplateURLData data
;
1082 data
.short_name
= name
;
1083 data
.SetKeyword(keyword
);
1084 data
.SetURL(search_url
.as_string());
1085 data
.suggestions_url
= suggest_url
.as_string();
1086 data
.instant_url
= instant_url
.as_string();
1087 data
.image_url
= image_url
.as_string();
1088 data
.new_tab_url
= new_tab_url
.as_string();
1089 data
.search_url_post_params
= search_url_post_params
.as_string();
1090 data
.suggestions_url_post_params
= suggest_url_post_params
.as_string();
1091 data
.instant_url_post_params
= instant_url_post_params
.as_string();
1092 data
.image_url_post_params
= image_url_post_params
.as_string();
1093 data
.favicon_url
= GURL(favicon_url
.as_string());
1094 data
.show_in_default_list
= true;
1095 data
.safe_for_autoreplace
= true;
1096 data
.input_encodings
.push_back(encoding
.as_string());
1097 data
.date_created
= base::Time();
1098 data
.last_modified
= base::Time();
1099 data
.prepopulate_id
= id
;
1100 for (size_t i
= 0; i
< alternate_urls
.GetSize(); ++i
) {
1101 std::string alternate_url
;
1102 alternate_urls
.GetString(i
, &alternate_url
);
1103 DCHECK(!alternate_url
.empty());
1104 data
.alternate_urls
.push_back(alternate_url
);
1106 data
.search_terms_replacement_key
= search_terms_replacement_key
.as_string();
1107 return new TemplateURL(profile
, data
);
1110 ScopedVector
<TemplateURL
> GetPrepopulatedTemplateFromPrefs(Profile
* profile
) {
1111 ScopedVector
<TemplateURL
> t_urls
;
1113 return t_urls
.Pass();
1115 const base::ListValue
* list
=
1116 profile
->GetPrefs()->GetList(prefs::kSearchProviderOverrides
);
1118 return t_urls
.Pass();
1120 size_t num_engines
= list
->GetSize();
1121 for (size_t i
= 0; i
!= num_engines
; ++i
) {
1122 const base::DictionaryValue
* engine
;
1123 base::string16 name
;
1124 base::string16 keyword
;
1125 std::string search_url
;
1126 std::string favicon_url
;
1127 std::string encoding
;
1129 // The following fields are required for each search engine configuration.
1130 if (list
->GetDictionary(i
, &engine
) &&
1131 engine
->GetString("name", &name
) && !name
.empty() &&
1132 engine
->GetString("keyword", &keyword
) && !keyword
.empty() &&
1133 engine
->GetString("search_url", &search_url
) && !search_url
.empty() &&
1134 engine
->GetString("favicon_url", &favicon_url
) &&
1135 !favicon_url
.empty() &&
1136 engine
->GetString("encoding", &encoding
) && !encoding
.empty() &&
1137 engine
->GetInteger("id", &id
)) {
1138 // These fields are optional.
1139 std::string suggest_url
;
1140 std::string instant_url
;
1141 std::string image_url
;
1142 std::string new_tab_url
;
1143 std::string search_url_post_params
;
1144 std::string suggest_url_post_params
;
1145 std::string instant_url_post_params
;
1146 std::string image_url_post_params
;
1147 base::ListValue empty_list
;
1148 const base::ListValue
* alternate_urls
= &empty_list
;
1149 std::string search_terms_replacement_key
;
1150 engine
->GetString("suggest_url", &suggest_url
);
1151 engine
->GetString("instant_url", &instant_url
);
1152 engine
->GetString("image_url", &image_url
);
1153 engine
->GetString("new_tab_url", &new_tab_url
);
1154 engine
->GetString("search_url_post_params", &search_url_post_params
);
1155 engine
->GetString("suggest_url_post_params", &suggest_url_post_params
);
1156 engine
->GetString("instant_url_post_params", &instant_url_post_params
);
1157 engine
->GetString("image_url_post_params", &image_url_post_params
);
1158 engine
->GetList("alternate_urls", &alternate_urls
);
1159 engine
->GetString("search_terms_replacement_key",
1160 &search_terms_replacement_key
);
1161 t_urls
.push_back(MakePrepopulatedTemplateURL(profile
, name
, keyword
,
1162 search_url
, suggest_url
, instant_url
, image_url
, new_tab_url
,
1163 search_url_post_params
, suggest_url_post_params
,
1164 instant_url_post_params
, image_url_post_params
,
1165 favicon_url
, encoding
, *alternate_urls
, search_terms_replacement_key
,
1169 return t_urls
.Pass();
1172 // The caller owns the returned TemplateURL.
1173 TemplateURL
* MakePrepopulatedTemplateURLFromPrepopulateEngine(
1175 const PrepopulatedEngine
& engine
) {
1177 base::ListValue alternate_urls
;
1178 if (engine
.alternate_urls
) {
1179 for (size_t i
= 0; i
< engine
.alternate_urls_size
; ++i
)
1180 alternate_urls
.AppendString(std::string(engine
.alternate_urls
[i
]));
1183 return MakePrepopulatedTemplateURL(profile
, base::WideToUTF16(engine
.name
),
1184 base::WideToUTF16(engine
.keyword
), engine
.search_url
, engine
.suggest_url
,
1185 engine
.instant_url
, engine
.image_url
, engine
.new_tab_url
,
1186 engine
.search_url_post_params
, engine
.suggest_url_post_params
,
1187 engine
.instant_url_post_params
, engine
.image_url_post_params
,
1188 engine
.favicon_url
, engine
.encoding
, alternate_urls
,
1189 engine
.search_terms_replacement_key
, engine
.id
);
1192 bool SameDomain(const GURL
& given_url
, const GURL
& prepopulated_url
) {
1193 return prepopulated_url
.is_valid() &&
1194 net::registry_controlled_domains::SameDomainOrHost(
1195 given_url
, prepopulated_url
,
1196 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
1202 // Global functions -----------------------------------------------------------
1204 #if defined(OS_ANDROID)
1205 void InitCountryCode(const std::string
& country_code
) {
1206 if (country_code
.size() != 2) {
1207 DLOG(ERROR
) << "Invalid country code: " << country_code
;
1208 g_country_code_at_install
= kCountryIDUnknown
;
1210 g_country_code_at_install
=
1211 CountryCharsToCountryIDWithUpdate(country_code
[0], country_code
[1]);
1216 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
1217 registry
->RegisterIntegerPref(
1218 prefs::kCountryIDAtInstall
,
1220 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
1221 registry
->RegisterListPref(prefs::kSearchProviderOverrides
,
1222 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
1223 registry
->RegisterIntegerPref(
1224 prefs::kSearchProviderOverridesVersion
,
1226 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
1227 // Obsolete pref, for migration.
1228 registry
->RegisterIntegerPref(
1229 prefs::kGeoIDAtInstall
,
1231 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
1234 int GetDataVersion(PrefService
* prefs
) {
1235 // Allow tests to override the local version.
1236 return (prefs
&& prefs
->HasPrefPath(prefs::kSearchProviderOverridesVersion
)) ?
1237 prefs
->GetInteger(prefs::kSearchProviderOverridesVersion
) :
1238 kCurrentDataVersion
;
1241 ScopedVector
<TemplateURL
> GetPrepopulatedEngines(
1242 Profile
* profile
, size_t* default_search_provider_index
) {
1243 // If there is a set of search engines in the preferences file, it overrides
1244 // the built-in set.
1245 *default_search_provider_index
= 0;
1246 ScopedVector
<TemplateURL
> t_urls
= GetPrepopulatedTemplateFromPrefs(profile
);
1247 if (!t_urls
.empty())
1248 return t_urls
.Pass();
1250 const PrepopulatedEngine
** engines
;
1252 GetPrepopulationSetFromCountryID(profile
? profile
->GetPrefs() : NULL
,
1253 &engines
, &num_engines
);
1254 for (size_t i
= 0; i
!= num_engines
; ++i
) {
1256 MakePrepopulatedTemplateURLFromPrepopulateEngine(profile
, *engines
[i
]));
1258 return t_urls
.Pass();
1261 void ClearPrepopulatedEnginesInPrefs(Profile
* profile
) {
1265 PrefService
* prefs
= profile
->GetPrefs();
1267 prefs
->ClearPref(prefs::kSearchProviderOverrides
);
1268 prefs
->ClearPref(prefs::kSearchProviderOverridesVersion
);
1271 TemplateURL
* GetPrepopulatedDefaultSearch(Profile
* profile
) {
1272 TemplateURL
* default_search_provider
= NULL
;
1273 size_t default_search_index
;
1274 // This could be more efficient. We are loading all the URLs to only keep
1276 ScopedVector
<TemplateURL
> loaded_urls
= GetPrepopulatedEngines(
1277 profile
, &default_search_index
);
1278 if (default_search_index
< loaded_urls
.size()) {
1279 default_search_provider
= loaded_urls
[default_search_index
];
1280 loaded_urls
.weak_erase(loaded_urls
.begin() + default_search_index
);
1282 return default_search_provider
;
1285 SearchEngineType
GetEngineType(const TemplateURL
& url
) {
1286 // Restricted to UI thread because ReplaceSearchTerms() is so restricted.
1287 using content::BrowserThread
;
1288 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
1289 BrowserThread::CurrentlyOn(BrowserThread::UI
));
1291 // By calling ReplaceSearchTerms, we ensure that even TemplateURLs whose URLs
1292 // can't be directly inspected (e.g. due to containing {google:baseURL}) can
1293 // be converted to GURLs we can look at.
1294 GURL
gurl(url
.url_ref().ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(
1295 base::ASCIIToUTF16("x"))));
1296 return gurl
.is_valid() ? GetEngineType(gurl
) : SEARCH_ENGINE_OTHER
;
1299 SearchEngineType
GetEngineType(const GURL
& url
) {
1300 DCHECK(url
.is_valid());
1302 // Check using TLD+1s, in order to more aggressively match search engine types
1303 // for data imported from other browsers.
1305 // First special-case Google, because the prepopulate URL for it will not
1306 // convert to a GURL and thus won't have an origin. Instead see if the
1307 // incoming URL's host is "[*.]google.<TLD>".
1308 if (google_util::IsGoogleHostname(url
.host(),
1309 google_util::DISALLOW_SUBDOMAIN
))
1312 // Now check the rest of the prepopulate data.
1313 for (size_t i
= 0; i
< arraysize(kAllEngines
); ++i
) {
1314 // First check the main search URL.
1315 if (SameDomain(url
, GURL(kAllEngines
[i
]->search_url
)))
1316 return kAllEngines
[i
]->type
;
1318 // Then check the alternate URLs.
1319 for (size_t j
= 0; j
< kAllEngines
[i
]->alternate_urls_size
; ++j
) {
1320 if (SameDomain(url
, GURL(kAllEngines
[i
]->alternate_urls
[j
])))
1321 return kAllEngines
[i
]->type
;
1325 return SEARCH_ENGINE_OTHER
;
1328 GURL
GetLogoURL(const TemplateURL
& template_url
, LogoSize size
) {
1329 if (GetEngineType(template_url
) != SEARCH_ENGINE_GOOGLE
)
1331 return GURL((size
== LOGO_200_PERCENT
) ?
1332 google_logos
.logo_200_percent_url
: google_logos
.logo_100_percent_url
);
1335 } // namespace TemplateURLPrepopulateData