Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/search_engines/template_url_prepopulate_data.h" | 5 #include "components/search_engines/template_url_prepopulate_data.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 8 #include <locale.h> | 8 #include <locale.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "components/google/core/browser/google_util.h" | 18 #include "components/google/core/browser/google_util.h" |
| 19 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
| 20 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 21 #include "components/search_engines/prepopulated_engines.h" | 21 #include "components/search_engines/prepopulated_engines.h" |
| 22 #include "components/search_engines/search_engines_pref_names.h" | 22 #include "components/search_engines/search_engines_pref_names.h" |
| 23 #include "components/search_engines/template_url_data.h" | 23 #include "components/search_engines/template_url_data.h" |
| 24 #include "components/search_engines/template_url_data_util.h" | |
| 24 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 25 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 27 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
| 28 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. | 29 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. |
| 29 #elif defined(OS_MACOSX) | 30 #elif defined(OS_MACOSX) |
| 30 #include "base/mac/scoped_cftyperef.h" | 31 #include "base/mac/scoped_cftyperef.h" |
| 31 #endif | 32 #endif |
| 32 | 33 |
| 33 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| (...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 965 UNHANDLED_COUNTRY(V, U) // Vanuatu | 966 UNHANDLED_COUNTRY(V, U) // Vanuatu |
| 966 UNHANDLED_COUNTRY(W, S) // Samoa | 967 UNHANDLED_COUNTRY(W, S) // Samoa |
| 967 UNHANDLED_COUNTRY(Z, M) // Zambia | 968 UNHANDLED_COUNTRY(Z, M) // Zambia |
| 968 case kCountryIDUnknown: | 969 case kCountryIDUnknown: |
| 969 default: // Unhandled location | 970 default: // Unhandled location |
| 970 END_UNHANDLED_COUNTRIES(def, ault) | 971 END_UNHANDLED_COUNTRIES(def, ault) |
| 971 } | 972 } |
| 972 | 973 |
| 973 std::vector<std::unique_ptr<TemplateURLData>> t_urls; | 974 std::vector<std::unique_ptr<TemplateURLData>> t_urls; |
| 974 for (size_t i = 0; i < num_engines; ++i) | 975 for (size_t i = 0; i < num_engines; ++i) |
| 975 t_urls.push_back(MakeTemplateURLDataFromPrepopulatedEngine(*engines[i])); | 976 t_urls.push_back(TemplateURLDataFromPrepopulatedEngine(*engines[i])); |
| 976 return t_urls; | 977 return t_urls; |
| 977 } | 978 } |
| 978 | 979 |
| 979 std::unique_ptr<TemplateURLData> MakePrepopulatedTemplateURLData( | |
| 980 const base::string16& name, | |
| 981 const base::string16& keyword, | |
| 982 const base::StringPiece& search_url, | |
| 983 const base::StringPiece& suggest_url, | |
| 984 const base::StringPiece& instant_url, | |
| 985 const base::StringPiece& image_url, | |
| 986 const base::StringPiece& new_tab_url, | |
| 987 const base::StringPiece& contextual_search_url, | |
| 988 const base::StringPiece& search_url_post_params, | |
| 989 const base::StringPiece& suggest_url_post_params, | |
| 990 const base::StringPiece& instant_url_post_params, | |
| 991 const base::StringPiece& image_url_post_params, | |
| 992 const base::StringPiece& favicon_url, | |
| 993 const base::StringPiece& encoding, | |
| 994 const base::ListValue& alternate_urls, | |
| 995 const base::StringPiece& search_terms_replacement_key, | |
| 996 int id) { | |
| 997 std::unique_ptr<TemplateURLData> data(new TemplateURLData); | |
| 998 | |
| 999 data->SetShortName(name); | |
| 1000 data->SetKeyword(keyword); | |
| 1001 data->SetURL(search_url.as_string()); | |
| 1002 data->suggestions_url = suggest_url.as_string(); | |
| 1003 data->instant_url = instant_url.as_string(); | |
| 1004 data->image_url = image_url.as_string(); | |
| 1005 data->new_tab_url = new_tab_url.as_string(); | |
| 1006 data->contextual_search_url = contextual_search_url.as_string(); | |
| 1007 data->search_url_post_params = search_url_post_params.as_string(); | |
| 1008 data->suggestions_url_post_params = suggest_url_post_params.as_string(); | |
| 1009 data->instant_url_post_params = instant_url_post_params.as_string(); | |
| 1010 data->image_url_post_params = image_url_post_params.as_string(); | |
| 1011 data->favicon_url = GURL(favicon_url); | |
| 1012 data->show_in_default_list = true; | |
| 1013 data->safe_for_autoreplace = true; | |
| 1014 data->input_encodings.push_back(encoding.as_string()); | |
| 1015 data->date_created = base::Time(); | |
| 1016 data->last_modified = base::Time(); | |
| 1017 data->prepopulate_id = id; | |
| 1018 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { | |
| 1019 std::string alternate_url; | |
| 1020 alternate_urls.GetString(i, &alternate_url); | |
| 1021 DCHECK(!alternate_url.empty()); | |
| 1022 data->alternate_urls.push_back(alternate_url); | |
| 1023 } | |
| 1024 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); | |
| 1025 return data; | |
| 1026 } | |
| 1027 | |
| 1028 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedTemplateURLData( | 980 std::vector<std::unique_ptr<TemplateURLData>> GetPrepopulatedTemplateURLData( |
| 1029 PrefService* prefs) { | 981 PrefService* prefs) { |
| 1030 std::vector<std::unique_ptr<TemplateURLData>> t_urls; | 982 std::vector<std::unique_ptr<TemplateURLData>> t_urls; |
| 1031 if (!prefs) | 983 if (!prefs) |
| 1032 return t_urls; | 984 return t_urls; |
| 1033 | 985 |
| 1034 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); | 986 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); |
| 1035 if (!list) | 987 if (!list) |
| 1036 return t_urls; | 988 return t_urls; |
| 1037 | 989 |
| 1038 size_t num_engines = list->GetSize(); | 990 size_t num_engines = list->GetSize(); |
| 1039 for (size_t i = 0; i != num_engines; ++i) { | 991 for (size_t i = 0; i != num_engines; ++i) { |
| 1040 const base::DictionaryValue* engine; | 992 const base::DictionaryValue* engine; |
| 1041 base::string16 name; | 993 std::unique_ptr<TemplateURLData> t_url; |
| 1042 base::string16 keyword; | |
| 1043 std::string search_url; | |
| 1044 std::string favicon_url; | |
| 1045 std::string encoding; | |
| 1046 int id = -1; | |
| 1047 // The following fields are required for each search engine configuration. | |
| 1048 if (list->GetDictionary(i, &engine) && | 994 if (list->GetDictionary(i, &engine) && |
| 1049 engine->GetString("name", &name) && !name.empty() && | 995 (t_url = TemplateURLDataFromOverrideDictionary(*engine))) |
|
Peter Kasting
2016/11/20 07:57:50
Nit: Don't use statements with side effects in con
Alexander Yashkin
2016/11/20 20:24:05
Thanks, fixed.
| |
| 1050 engine->GetString("keyword", &keyword) && !keyword.empty() && | 996 t_urls.push_back(std::move(t_url)); |
| 1051 engine->GetString("search_url", &search_url) && !search_url.empty() && | |
| 1052 engine->GetString("favicon_url", &favicon_url) && | |
| 1053 !favicon_url.empty() && | |
| 1054 engine->GetString("encoding", &encoding) && !encoding.empty() && | |
| 1055 engine->GetInteger("id", &id)) { | |
| 1056 // These fields are optional. | |
| 1057 std::string suggest_url; | |
| 1058 std::string instant_url; | |
| 1059 std::string image_url; | |
| 1060 std::string new_tab_url; | |
| 1061 std::string contextual_search_url; | |
| 1062 std::string search_url_post_params; | |
| 1063 std::string suggest_url_post_params; | |
| 1064 std::string instant_url_post_params; | |
| 1065 std::string image_url_post_params; | |
| 1066 base::ListValue empty_list; | |
| 1067 const base::ListValue* alternate_urls = &empty_list; | |
| 1068 std::string search_terms_replacement_key; | |
| 1069 engine->GetString("suggest_url", &suggest_url); | |
| 1070 engine->GetString("instant_url", &instant_url); | |
| 1071 engine->GetString("image_url", &image_url); | |
| 1072 engine->GetString("new_tab_url", &new_tab_url); | |
| 1073 engine->GetString("contextual_search_url", &contextual_search_url); | |
| 1074 engine->GetString("search_url_post_params", &search_url_post_params); | |
| 1075 engine->GetString("suggest_url_post_params", &suggest_url_post_params); | |
| 1076 engine->GetString("instant_url_post_params", &instant_url_post_params); | |
| 1077 engine->GetString("image_url_post_params", &image_url_post_params); | |
| 1078 engine->GetList("alternate_urls", &alternate_urls); | |
| 1079 engine->GetString("search_terms_replacement_key", | |
| 1080 &search_terms_replacement_key); | |
| 1081 t_urls.push_back(MakePrepopulatedTemplateURLData( | |
| 1082 name, keyword, search_url, suggest_url, instant_url, image_url, | |
| 1083 new_tab_url, contextual_search_url, search_url_post_params, | |
| 1084 suggest_url_post_params, instant_url_post_params, | |
| 1085 image_url_post_params, favicon_url, encoding, *alternate_urls, | |
| 1086 search_terms_replacement_key, id)); | |
| 1087 } | |
| 1088 } | 997 } |
| 1089 return t_urls; | 998 return t_urls; |
| 1090 } | 999 } |
| 1091 | 1000 |
| 1092 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { | 1001 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { |
| 1093 return prepopulated_url.is_valid() && | 1002 return prepopulated_url.is_valid() && |
| 1094 net::registry_controlled_domains::SameDomainOrHost( | 1003 net::registry_controlled_domains::SameDomainOrHost( |
| 1095 given_url, prepopulated_url, | 1004 given_url, prepopulated_url, |
| 1096 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 1005 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 1097 } | 1006 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1139 | 1048 |
| 1140 return GetPrepopulationSetFromCountryID(country_id); | 1049 return GetPrepopulationSetFromCountryID(country_id); |
| 1141 } | 1050 } |
| 1142 #endif | 1051 #endif |
| 1143 | 1052 |
| 1144 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { | 1053 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() { |
| 1145 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), | 1054 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines), |
| 1146 std::end(kAllEngines)); | 1055 std::end(kAllEngines)); |
| 1147 } | 1056 } |
| 1148 | 1057 |
| 1149 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( | |
| 1150 const PrepopulatedEngine& engine) { | |
| 1151 base::ListValue alternate_urls; | |
| 1152 if (engine.alternate_urls) { | |
| 1153 for (size_t i = 0; i < engine.alternate_urls_size; ++i) | |
| 1154 alternate_urls.AppendString(std::string(engine.alternate_urls[i])); | |
| 1155 } | |
| 1156 | |
| 1157 return MakePrepopulatedTemplateURLData( | |
| 1158 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword), | |
| 1159 engine.search_url, engine.suggest_url, engine.instant_url, | |
| 1160 engine.image_url, engine.new_tab_url, engine.contextual_search_url, | |
| 1161 engine.search_url_post_params, engine.suggest_url_post_params, | |
| 1162 engine.instant_url_post_params, engine.image_url_post_params, | |
| 1163 engine.favicon_url, engine.encoding, alternate_urls, | |
| 1164 engine.search_terms_replacement_key, engine.id); | |
| 1165 } | |
| 1166 | |
| 1167 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { | 1058 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { |
| 1168 if (!prefs) | 1059 if (!prefs) |
| 1169 return; | 1060 return; |
| 1170 | 1061 |
| 1171 prefs->ClearPref(prefs::kSearchProviderOverrides); | 1062 prefs->ClearPref(prefs::kSearchProviderOverrides); |
| 1172 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); | 1063 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); |
| 1173 } | 1064 } |
| 1174 | 1065 |
| 1175 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( | 1066 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( |
| 1176 PrefService* prefs) { | 1067 PrefService* prefs) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1262 locale_str.erase(0, territory_delim + 1); | 1153 locale_str.erase(0, territory_delim + 1); |
| 1263 | 1154 |
| 1264 // Next remove any codeset/modifier portion and uppercase. | 1155 // Next remove any codeset/modifier portion and uppercase. |
| 1265 return CountryStringToCountryID( | 1156 return CountryStringToCountryID( |
| 1266 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); | 1157 base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@")))); |
| 1267 } | 1158 } |
| 1268 | 1159 |
| 1269 #endif // OS_* | 1160 #endif // OS_* |
| 1270 | 1161 |
| 1271 } // namespace TemplateURLPrepopulateData | 1162 } // namespace TemplateURLPrepopulateData |
| OLD | NEW |