OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 5 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
6 | 6 |
7 #if defined(OS_LINUX) | 7 #if defined(OS_LINUX) |
8 #include <locale.h> | 8 #include <locale.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // facilitates changes like adding more specific per-country data in the | 55 // facilitates changes like adding more specific per-country data in the |
56 // future; in such a case the localized engines will transparently replace the | 56 // future; in such a case the localized engines will transparently replace the |
57 // previous, non-localized versions. For engines where we need two instances | 57 // previous, non-localized versions. For engines where we need two instances |
58 // to appear for one country (e.g. Bing Search U.S. English and Spanish), we | 58 // to appear for one country (e.g. Bing Search U.S. English and Spanish), we |
59 // must use two different unique IDs (and different keywords). | 59 // must use two different unique IDs (and different keywords). |
60 // | 60 // |
61 // The following unique IDs are available: | 61 // The following unique IDs are available: |
62 // 33, 34, 36, 39, 42, 43, 47, 48, 49, 50, 52, 53, 56, 58, 60, 61, 64, 65, | 62 // 33, 34, 36, 39, 42, 43, 47, 48, 49, 50, 52, 53, 56, 58, 60, 61, 64, 65, |
63 // 66, 70, 74, 78, 79, 80, 81, 84, 86, 88, 91, 92, 93, 94, 95, 96, 97, 98, | 63 // 66, 70, 74, 78, 79, 80, 81, 84, 86, 88, 91, 92, 93, 94, 95, 96, 97, 98, |
64 // 102+ | 64 // 102+ |
| 65 // |
| 66 // IDs > 1000 are reserved for distribution custom engines. |
| 67 // |
65 // NOTE: CHANGE THE ABOVE NUMBERS IF YOU ADD A NEW ENGINE; ID conflicts = bad! | 68 // NOTE: CHANGE THE ABOVE NUMBERS IF YOU ADD A NEW ENGINE; ID conflicts = bad! |
66 const int id; | 69 const int id; |
67 }; | 70 }; |
68 | 71 |
69 const PrepopulatedEngine abcsok = { | 72 const PrepopulatedEngine abcsok = { |
70 L"ABC S\x00f8k", | 73 L"ABC S\x00f8k", |
71 L"abcsok.no", | 74 L"abcsok.no", |
72 "http://abcsok.no/favicon.ico", | 75 "http://abcsok.no/favicon.ico", |
73 L"http://abcsok.no/index.html?q={searchTerms}", | 76 L"http://abcsok.no/index.html?q={searchTerms}", |
74 "UTF-8", | 77 "UTF-8", |
(...skipping 2708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2783 END_UNHANDLED_COUNTRIES(def, ault) | 2786 END_UNHANDLED_COUNTRIES(def, ault) |
2784 } | 2787 } |
2785 } | 2788 } |
2786 | 2789 |
2787 } // namespace | 2790 } // namespace |
2788 | 2791 |
2789 namespace TemplateURLPrepopulateData { | 2792 namespace TemplateURLPrepopulateData { |
2790 | 2793 |
2791 void RegisterUserPrefs(PrefService* prefs) { | 2794 void RegisterUserPrefs(PrefService* prefs) { |
2792 prefs->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); | 2795 prefs->RegisterIntegerPref(prefs::kCountryIDAtInstall, kCountryIDUnknown); |
2793 | 2796 prefs->RegisterListPref(prefs::kSearchProviderOverrides); |
| 2797 prefs->RegisterIntegerPref(prefs::kSearchProviderOverridesVersion, -1); |
2794 // Obsolete pref, for migration. | 2798 // Obsolete pref, for migration. |
2795 prefs->RegisterIntegerPref(prefs::kGeoIDAtInstall, -1); | 2799 prefs->RegisterIntegerPref(prefs::kGeoIDAtInstall, -1); |
2796 } | 2800 } |
2797 | 2801 |
2798 int GetDataVersion() { | 2802 int GetDataVersion(PrefService* prefs) { |
2799 return 28; // Increment this if you change the above data in ways that mean | 2803 // Increment this if you change the above data in ways that mean users with |
2800 // users with existing data should get a new version. | 2804 // existing data should get a new version. |
| 2805 const int kCurrentDataVersion = 28; |
| 2806 if (!prefs) |
| 2807 return kCurrentDataVersion; |
| 2808 // If a version number exist in the preferences file, it overrides the |
| 2809 // version of the built-in data. |
| 2810 int version = |
| 2811 prefs->GetInteger(prefs::kSearchProviderOverridesVersion); |
| 2812 return (version >= 0) ? version : kCurrentDataVersion; |
| 2813 } |
| 2814 |
| 2815 TemplateURL* MakePrepopulatedTemplateURL(const wchar_t* name, |
| 2816 const wchar_t* keyword, |
| 2817 const wchar_t* search_url, |
| 2818 const char* favicon_url, |
| 2819 const wchar_t* suggest_url, |
| 2820 const char* encoding, |
| 2821 int id) { |
| 2822 TemplateURL* new_turl = new TemplateURL(); |
| 2823 new_turl->SetURL(WideToUTF8(search_url), 0, 0); |
| 2824 if (favicon_url) |
| 2825 new_turl->SetFavIconURL(GURL(favicon_url)); |
| 2826 if (suggest_url) |
| 2827 new_turl->SetSuggestionsURL(WideToUTF8(suggest_url), 0, 0); |
| 2828 new_turl->set_short_name(name); |
| 2829 if (keyword == NULL) |
| 2830 new_turl->set_autogenerate_keyword(true); |
| 2831 else |
| 2832 new_turl->set_keyword(keyword); |
| 2833 new_turl->set_show_in_default_list(true); |
| 2834 new_turl->set_safe_for_autoreplace(true); |
| 2835 new_turl->set_date_created(Time()); |
| 2836 std::vector<std::string> turl_encodings; |
| 2837 turl_encodings.push_back(encoding); |
| 2838 new_turl->set_input_encodings(turl_encodings); |
| 2839 new_turl->set_prepopulate_id(id); |
| 2840 return new_turl; |
| 2841 } |
| 2842 |
| 2843 void GetPrepopulatedTemplatefromPrefs(PrefService* prefs, |
| 2844 std::vector<TemplateURL*>* t_urls) { |
| 2845 const ListValue* list = |
| 2846 prefs->GetList(prefs::kSearchProviderOverrides); |
| 2847 if (!list) |
| 2848 return; |
| 2849 |
| 2850 std::wstring name; |
| 2851 std::wstring keyword; |
| 2852 std::wstring search_url; |
| 2853 std::wstring suggest_url; |
| 2854 std::string favicon_url; |
| 2855 std::string encoding; |
| 2856 int id; |
| 2857 |
| 2858 size_t num_engines = list->GetSize(); |
| 2859 for (size_t i = 0; i != num_engines; ++i) { |
| 2860 Value* val; |
| 2861 DictionaryValue* engine; |
| 2862 list->GetDictionary(i, &engine); |
| 2863 if (engine->Get(L"name", &val) && val->GetAsString(&name) && |
| 2864 engine->Get(L"keyword", &val) && val->GetAsString(&keyword) && |
| 2865 engine->Get(L"search_url", &val) && val->GetAsString(&search_url) && |
| 2866 engine->Get(L"suggest_url", &val) && val->GetAsString(&suggest_url) && |
| 2867 engine->Get(L"favicon_url", &val) && val->GetAsString(&favicon_url) && |
| 2868 engine->Get(L"encoding", &val) && val->GetAsString(&encoding) && |
| 2869 engine->Get(L"id", &val) && val->GetAsInteger(&id)) { |
| 2870 // These next fields are not allowed to be empty. |
| 2871 if (search_url.empty() || favicon_url.empty() || encoding.empty()) |
| 2872 return; |
| 2873 } else { |
| 2874 // Got a parsing error. No big deal. |
| 2875 continue; |
| 2876 } |
| 2877 t_urls->push_back(MakePrepopulatedTemplateURL(name.c_str(), |
| 2878 keyword.c_str(), |
| 2879 search_url.c_str(), |
| 2880 favicon_url.c_str(), |
| 2881 suggest_url.c_str(), |
| 2882 encoding.c_str(), |
| 2883 id)); |
| 2884 } |
2801 } | 2885 } |
2802 | 2886 |
2803 void GetPrepopulatedEngines(PrefService* prefs, | 2887 void GetPrepopulatedEngines(PrefService* prefs, |
2804 std::vector<TemplateURL*>* t_urls, | 2888 std::vector<TemplateURL*>* t_urls, |
2805 size_t* default_search_provider_index) { | 2889 size_t* default_search_provider_index) { |
| 2890 // If there if there is a set of search engines in the preferences |
| 2891 // file, it overrides the built-in set. |
| 2892 *default_search_provider_index = 0; |
| 2893 GetPrepopulatedTemplatefromPrefs(prefs, t_urls); |
| 2894 if (!t_urls->empty()) |
| 2895 return; |
| 2896 |
2806 const PrepopulatedEngine** engines; | 2897 const PrepopulatedEngine** engines; |
2807 size_t num_engines; | 2898 size_t num_engines; |
2808 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 2899 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); |
2809 *default_search_provider_index = 0; | 2900 for (size_t i = 0; i != num_engines; ++i) { |
2810 | 2901 TemplateURL* turl = |
2811 for (size_t i = 0; i < num_engines; ++i) { | 2902 MakePrepopulatedTemplateURL(engines[i]->name, |
2812 TemplateURL* new_turl = new TemplateURL(); | 2903 engines[i]->keyword, |
2813 new_turl->SetURL(WideToUTF8(engines[i]->search_url), 0, 0); | 2904 engines[i]->search_url, |
2814 if (engines[i]->favicon_url) | 2905 engines[i]->favicon_url, |
2815 new_turl->SetFavIconURL(GURL(engines[i]->favicon_url)); | 2906 engines[i]->suggest_url, |
2816 if (engines[i]->suggest_url) | 2907 engines[i]->encoding, |
2817 new_turl->SetSuggestionsURL(WideToUTF8(engines[i]->suggest_url), 0, 0); | 2908 engines[i]->id); |
2818 new_turl->set_short_name(engines[i]->name); | 2909 t_urls->push_back(turl); |
2819 if (engines[i]->keyword == NULL) | |
2820 new_turl->set_autogenerate_keyword(true); | |
2821 else | |
2822 new_turl->set_keyword(engines[i]->keyword); | |
2823 new_turl->set_show_in_default_list(true); | |
2824 new_turl->set_safe_for_autoreplace(true); | |
2825 new_turl->set_date_created(Time()); | |
2826 std::vector<std::string> turl_encodings; | |
2827 turl_encodings.push_back(engines[i]->encoding); | |
2828 new_turl->set_input_encodings(turl_encodings); | |
2829 new_turl->set_prepopulate_id(engines[i]->id); | |
2830 t_urls->push_back(new_turl); | |
2831 } | 2910 } |
2832 } | 2911 } |
2833 | 2912 |
2834 SearchEngineType GetSearchEngineType(const TemplateURL* search_engine) { | 2913 SearchEngineType GetSearchEngineType(const TemplateURL* search_engine) { |
2835 switch (search_engine->prepopulate_id()) { | 2914 switch (search_engine->prepopulate_id()) { |
2836 case 1: | 2915 case 1: |
2837 return SEARCH_ENGINE_GOOGLE; | 2916 return SEARCH_ENGINE_GOOGLE; |
2838 case 2: | 2917 case 2: |
2839 // Construction of country id = 'J' << 8 | 'P' = 19024 | 2918 // Construction of country id = 'J' << 8 | 'P' = 19024 |
2840 return (GetCountryIDFromPrefs(NULL) == 19024) ? SEARCH_ENGINE_YAHOOJP : | 2919 return (GetCountryIDFromPrefs(NULL) == 19024) ? SEARCH_ENGINE_YAHOOJP : |
(...skipping 12 matching lines...) Expand all Loading... |
2853 return SEARCH_ENGINE_NETSPRINT; | 2932 return SEARCH_ENGINE_NETSPRINT; |
2854 case 62: | 2933 case 62: |
2855 return SEARCH_ENGINE_VIRGILIO; | 2934 return SEARCH_ENGINE_VIRGILIO; |
2856 case 83: | 2935 case 83: |
2857 return SEARCH_ENGINE_MAILRU; | 2936 return SEARCH_ENGINE_MAILRU; |
2858 default: | 2937 default: |
2859 return SEARCH_ENGINE_OTHER; | 2938 return SEARCH_ENGINE_OTHER; |
2860 } | 2939 } |
2861 } | 2940 } |
2862 | 2941 |
2863 | |
2864 | |
2865 } // namespace TemplateURLPrepopulateData | 2942 } // namespace TemplateURLPrepopulateData |
OLD | NEW |