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