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