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