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