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 |
(...skipping 996 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 data->date_created = base::Time(); | 1007 data->date_created = base::Time(); |
1008 data->last_modified = base::Time(); | 1008 data->last_modified = base::Time(); |
1009 data->prepopulate_id = id; | 1009 data->prepopulate_id = id; |
1010 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { | 1010 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { |
1011 std::string alternate_url; | 1011 std::string alternate_url; |
1012 alternate_urls.GetString(i, &alternate_url); | 1012 alternate_urls.GetString(i, &alternate_url); |
1013 DCHECK(!alternate_url.empty()); | 1013 DCHECK(!alternate_url.empty()); |
1014 data->alternate_urls.push_back(alternate_url); | 1014 data->alternate_urls.push_back(alternate_url); |
1015 } | 1015 } |
1016 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); | 1016 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); |
1017 return data.Pass(); | 1017 return data; |
1018 } | 1018 } |
1019 | 1019 |
1020 ScopedVector<TemplateURLData> GetPrepopulatedTemplateURLData( | 1020 ScopedVector<TemplateURLData> GetPrepopulatedTemplateURLData( |
1021 PrefService* prefs) { | 1021 PrefService* prefs) { |
1022 ScopedVector<TemplateURLData> t_urls; | 1022 ScopedVector<TemplateURLData> t_urls; |
1023 if (!prefs) | 1023 if (!prefs) |
1024 return t_urls.Pass(); | 1024 return t_urls; |
1025 | 1025 |
1026 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); | 1026 const base::ListValue* list = prefs->GetList(prefs::kSearchProviderOverrides); |
1027 if (!list) | 1027 if (!list) |
1028 return t_urls.Pass(); | 1028 return t_urls; |
1029 | 1029 |
1030 size_t num_engines = list->GetSize(); | 1030 size_t num_engines = list->GetSize(); |
1031 for (size_t i = 0; i != num_engines; ++i) { | 1031 for (size_t i = 0; i != num_engines; ++i) { |
1032 const base::DictionaryValue* engine; | 1032 const base::DictionaryValue* engine; |
1033 base::string16 name; | 1033 base::string16 name; |
1034 base::string16 keyword; | 1034 base::string16 keyword; |
1035 std::string search_url; | 1035 std::string search_url; |
1036 std::string favicon_url; | 1036 std::string favicon_url; |
1037 std::string encoding; | 1037 std::string encoding; |
1038 int id = -1; | 1038 int id = -1; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1071 engine->GetString("search_terms_replacement_key", | 1071 engine->GetString("search_terms_replacement_key", |
1072 &search_terms_replacement_key); | 1072 &search_terms_replacement_key); |
1073 t_urls.push_back(MakePrepopulatedTemplateURLData(name, keyword, | 1073 t_urls.push_back(MakePrepopulatedTemplateURLData(name, keyword, |
1074 search_url, suggest_url, instant_url, image_url, new_tab_url, | 1074 search_url, suggest_url, instant_url, image_url, new_tab_url, |
1075 contextual_search_url, search_url_post_params, | 1075 contextual_search_url, search_url_post_params, |
1076 suggest_url_post_params, instant_url_post_params, | 1076 suggest_url_post_params, instant_url_post_params, |
1077 image_url_post_params, favicon_url, encoding, *alternate_urls, | 1077 image_url_post_params, favicon_url, encoding, *alternate_urls, |
1078 search_terms_replacement_key, id).release()); | 1078 search_terms_replacement_key, id).release()); |
1079 } | 1079 } |
1080 } | 1080 } |
1081 return t_urls.Pass(); | 1081 return t_urls; |
1082 } | 1082 } |
1083 | 1083 |
1084 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { | 1084 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { |
1085 return prepopulated_url.is_valid() && | 1085 return prepopulated_url.is_valid() && |
1086 net::registry_controlled_domains::SameDomainOrHost( | 1086 net::registry_controlled_domains::SameDomainOrHost( |
1087 given_url, prepopulated_url, | 1087 given_url, prepopulated_url, |
1088 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 1088 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
1089 } | 1089 } |
1090 | 1090 |
1091 } // namespace | 1091 } // namespace |
(...skipping 14 matching lines...) Expand all Loading... |
1106 } | 1106 } |
1107 | 1107 |
1108 ScopedVector<TemplateURLData> GetPrepopulatedEngines( | 1108 ScopedVector<TemplateURLData> GetPrepopulatedEngines( |
1109 PrefService* prefs, | 1109 PrefService* prefs, |
1110 size_t* default_search_provider_index) { | 1110 size_t* default_search_provider_index) { |
1111 // If there is a set of search engines in the preferences file, it overrides | 1111 // If there is a set of search engines in the preferences file, it overrides |
1112 // the built-in set. | 1112 // the built-in set. |
1113 *default_search_provider_index = 0; | 1113 *default_search_provider_index = 0; |
1114 ScopedVector<TemplateURLData> t_urls = GetPrepopulatedTemplateURLData(prefs); | 1114 ScopedVector<TemplateURLData> t_urls = GetPrepopulatedTemplateURLData(prefs); |
1115 if (!t_urls.empty()) | 1115 if (!t_urls.empty()) |
1116 return t_urls.Pass(); | 1116 return t_urls; |
1117 | 1117 |
1118 const PrepopulatedEngine** engines; | 1118 const PrepopulatedEngine** engines; |
1119 size_t num_engines; | 1119 size_t num_engines; |
1120 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); | 1120 GetPrepopulationSetFromCountryID(prefs, &engines, &num_engines); |
1121 for (size_t i = 0; i != num_engines; ++i) { | 1121 for (size_t i = 0; i != num_engines; ++i) { |
1122 t_urls.push_back( | 1122 t_urls.push_back( |
1123 MakeTemplateURLDataFromPrepopulatedEngine(*engines[i]).release()); | 1123 MakeTemplateURLDataFromPrepopulatedEngine(*engines[i]).release()); |
1124 } | 1124 } |
1125 return t_urls.Pass(); | 1125 return t_urls; |
1126 } | 1126 } |
1127 | 1127 |
1128 scoped_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( | 1128 scoped_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( |
1129 const PrepopulatedEngine& engine) { | 1129 const PrepopulatedEngine& engine) { |
1130 base::ListValue alternate_urls; | 1130 base::ListValue alternate_urls; |
1131 if (engine.alternate_urls) { | 1131 if (engine.alternate_urls) { |
1132 for (size_t i = 0; i < engine.alternate_urls_size; ++i) | 1132 for (size_t i = 0; i < engine.alternate_urls_size; ++i) |
1133 alternate_urls.AppendString(std::string(engine.alternate_urls[i])); | 1133 alternate_urls.AppendString(std::string(engine.alternate_urls[i])); |
1134 } | 1134 } |
1135 | 1135 |
(...skipping 19 matching lines...) Expand all Loading... |
1155 scoped_ptr<TemplateURLData> default_search_provider; | 1155 scoped_ptr<TemplateURLData> default_search_provider; |
1156 size_t default_search_index; | 1156 size_t default_search_index; |
1157 // This could be more efficient. We are loading all the URLs to only keep | 1157 // This could be more efficient. We are loading all the URLs to only keep |
1158 // the first one. | 1158 // the first one. |
1159 ScopedVector<TemplateURLData> loaded_urls = | 1159 ScopedVector<TemplateURLData> loaded_urls = |
1160 GetPrepopulatedEngines(prefs, &default_search_index); | 1160 GetPrepopulatedEngines(prefs, &default_search_index); |
1161 if (default_search_index < loaded_urls.size()) { | 1161 if (default_search_index < loaded_urls.size()) { |
1162 default_search_provider.reset(loaded_urls[default_search_index]); | 1162 default_search_provider.reset(loaded_urls[default_search_index]); |
1163 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); | 1163 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); |
1164 } | 1164 } |
1165 return default_search_provider.Pass(); | 1165 return default_search_provider; |
1166 } | 1166 } |
1167 | 1167 |
1168 SearchEngineType GetEngineType(const TemplateURL& url, | 1168 SearchEngineType GetEngineType(const TemplateURL& url, |
1169 const SearchTermsData& search_terms_data) { | 1169 const SearchTermsData& search_terms_data) { |
1170 // By calling ReplaceSearchTerms, we ensure that even TemplateURLs whose URLs | 1170 // By calling ReplaceSearchTerms, we ensure that even TemplateURLs whose URLs |
1171 // can't be directly inspected (e.g. due to containing {google:baseURL}) can | 1171 // can't be directly inspected (e.g. due to containing {google:baseURL}) can |
1172 // be converted to GURLs we can look at. | 1172 // be converted to GURLs we can look at. |
1173 GURL gurl(url.url_ref().ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs( | 1173 GURL gurl(url.url_ref().ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs( |
1174 base::ASCIIToUTF16("x")), search_terms_data)); | 1174 base::ASCIIToUTF16("x")), search_terms_data)); |
1175 return gurl.is_valid() ? GetEngineType(gurl) : SEARCH_ENGINE_OTHER; | 1175 return gurl.is_valid() ? GetEngineType(gurl) : SEARCH_ENGINE_OTHER; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1265 base::ToUpperASCII(locale_str[begin]), | 1265 base::ToUpperASCII(locale_str[begin]), |
1266 base::ToUpperASCII(locale_str[begin + 1])); | 1266 base::ToUpperASCII(locale_str[begin + 1])); |
1267 } | 1267 } |
1268 | 1268 |
1269 return kCountryIDUnknown; | 1269 return kCountryIDUnknown; |
1270 } | 1270 } |
1271 | 1271 |
1272 #endif // OS_* | 1272 #endif // OS_* |
1273 | 1273 |
1274 } // namespace TemplateURLPrepopulateData | 1274 } // namespace TemplateURLPrepopulateData |
OLD | NEW |