Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: components/search_engines/template_url_prepopulate_data.cc

Issue 1983773002: Cache SearchEngineType of TemplateURL (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-extracting-terms-from-template-url
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string16.h" 14 #include "base/strings/string16.h"
15 #include "base/strings/string_piece.h" 15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "components/google/core/browser/google_util.h" 19 #include "components/google/core/browser/google_util.h"
20 #include "components/pref_registry/pref_registry_syncable.h" 20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "components/prefs/pref_service.h" 21 #include "components/prefs/pref_service.h"
22 #include "components/search_engines/prepopulated_engines.h" 22 #include "components/search_engines/prepopulated_engines.h"
23 #include "components/search_engines/search_engines_pref_names.h" 23 #include "components/search_engines/search_engines_pref_names.h"
24 #include "components/search_engines/template_url.h" 24 #include "components/search_engines/template_url_data.h"
25 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 25 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 27
28 #if defined(OS_WIN) 28 #if defined(OS_WIN)
29 #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.
30 #elif defined(OS_MACOSX) 30 #elif defined(OS_MACOSX)
31 #include "base/mac/scoped_cftyperef.h" 31 #include "base/mac/scoped_cftyperef.h"
32 #endif 32 #endif
33 33
34 #if defined(OS_ANDROID) 34 #if defined(OS_ANDROID)
(...skipping 943 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 const base::StringPiece& new_tab_url, 978 const base::StringPiece& new_tab_url,
979 const base::StringPiece& contextual_search_url, 979 const base::StringPiece& contextual_search_url,
980 const base::StringPiece& search_url_post_params, 980 const base::StringPiece& search_url_post_params,
981 const base::StringPiece& suggest_url_post_params, 981 const base::StringPiece& suggest_url_post_params,
982 const base::StringPiece& instant_url_post_params, 982 const base::StringPiece& instant_url_post_params,
983 const base::StringPiece& image_url_post_params, 983 const base::StringPiece& image_url_post_params,
984 const base::StringPiece& favicon_url, 984 const base::StringPiece& favicon_url,
985 const base::StringPiece& encoding, 985 const base::StringPiece& encoding,
986 const base::ListValue& alternate_urls, 986 const base::ListValue& alternate_urls,
987 const base::StringPiece& search_terms_replacement_key, 987 const base::StringPiece& search_terms_replacement_key,
988 SearchEngineType engine_type,
988 int id) { 989 int id) {
989 std::unique_ptr<TemplateURLData> data(new TemplateURLData); 990 std::unique_ptr<TemplateURLData> data(new TemplateURLData);
990 991
991 data->SetShortName(name); 992 data->SetShortName(name);
992 data->SetKeyword(keyword); 993 data->SetKeyword(keyword);
993 data->SetURL(search_url.as_string()); 994 data->SetURL(search_url.as_string());
994 data->suggestions_url = suggest_url.as_string(); 995 data->suggestions_url = suggest_url.as_string();
995 data->instant_url = instant_url.as_string(); 996 data->instant_url = instant_url.as_string();
996 data->image_url = image_url.as_string(); 997 data->image_url = image_url.as_string();
997 data->new_tab_url = new_tab_url.as_string(); 998 data->new_tab_url = new_tab_url.as_string();
998 data->contextual_search_url = contextual_search_url.as_string(); 999 data->contextual_search_url = contextual_search_url.as_string();
999 data->search_url_post_params = search_url_post_params.as_string(); 1000 data->search_url_post_params = search_url_post_params.as_string();
1000 data->suggestions_url_post_params = suggest_url_post_params.as_string(); 1001 data->suggestions_url_post_params = suggest_url_post_params.as_string();
1001 data->instant_url_post_params = instant_url_post_params.as_string(); 1002 data->instant_url_post_params = instant_url_post_params.as_string();
1002 data->image_url_post_params = image_url_post_params.as_string(); 1003 data->image_url_post_params = image_url_post_params.as_string();
1003 data->favicon_url = GURL(favicon_url.as_string()); 1004 data->favicon_url = GURL(favicon_url.as_string());
1004 data->show_in_default_list = true; 1005 data->show_in_default_list = true;
1005 data->safe_for_autoreplace = true; 1006 data->safe_for_autoreplace = true;
1006 data->input_encodings.push_back(encoding.as_string()); 1007 data->input_encodings.push_back(encoding.as_string());
1007 data->date_created = base::Time(); 1008 data->date_created = base::Time();
1008 data->last_modified = base::Time(); 1009 data->last_modified = base::Time();
1010 data->engine_type = engine_type;
1009 data->prepopulate_id = id; 1011 data->prepopulate_id = id;
1010 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) { 1012 for (size_t i = 0; i < alternate_urls.GetSize(); ++i) {
1011 std::string alternate_url; 1013 std::string alternate_url;
1012 alternate_urls.GetString(i, &alternate_url); 1014 alternate_urls.GetString(i, &alternate_url);
1013 DCHECK(!alternate_url.empty()); 1015 DCHECK(!alternate_url.empty());
1014 data->alternate_urls.push_back(alternate_url); 1016 data->alternate_urls.push_back(alternate_url);
1015 } 1017 }
1016 data->search_terms_replacement_key = search_terms_replacement_key.as_string(); 1018 data->search_terms_replacement_key = search_terms_replacement_key.as_string();
1017 return data; 1019 return data;
1018 } 1020 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1063 engine->GetString("image_url", &image_url); 1065 engine->GetString("image_url", &image_url);
1064 engine->GetString("new_tab_url", &new_tab_url); 1066 engine->GetString("new_tab_url", &new_tab_url);
1065 engine->GetString("contextual_search_url", &contextual_search_url); 1067 engine->GetString("contextual_search_url", &contextual_search_url);
1066 engine->GetString("search_url_post_params", &search_url_post_params); 1068 engine->GetString("search_url_post_params", &search_url_post_params);
1067 engine->GetString("suggest_url_post_params", &suggest_url_post_params); 1069 engine->GetString("suggest_url_post_params", &suggest_url_post_params);
1068 engine->GetString("instant_url_post_params", &instant_url_post_params); 1070 engine->GetString("instant_url_post_params", &instant_url_post_params);
1069 engine->GetString("image_url_post_params", &image_url_post_params); 1071 engine->GetString("image_url_post_params", &image_url_post_params);
1070 engine->GetList("alternate_urls", &alternate_urls); 1072 engine->GetList("alternate_urls", &alternate_urls);
1071 engine->GetString("search_terms_replacement_key", 1073 engine->GetString("search_terms_replacement_key",
1072 &search_terms_replacement_key); 1074 &search_terms_replacement_key);
1075 const SearchEngineType type = SEARCH_ENGINE_UNKNOWN;
Peter Kasting 2016/05/17 04:43:04 Nit: Inline into next statement
Vitaly Baranov 2016/06/03 15:31:45 Done.
1073 t_urls.push_back(MakePrepopulatedTemplateURLData(name, keyword, 1076 t_urls.push_back(MakePrepopulatedTemplateURLData(name, keyword,
1074 search_url, suggest_url, instant_url, image_url, new_tab_url, 1077 search_url, suggest_url, instant_url, image_url, new_tab_url,
1075 contextual_search_url, search_url_post_params, 1078 contextual_search_url, search_url_post_params,
1076 suggest_url_post_params, instant_url_post_params, 1079 suggest_url_post_params, instant_url_post_params,
1077 image_url_post_params, favicon_url, encoding, *alternate_urls, 1080 image_url_post_params, favicon_url, encoding, *alternate_urls,
1078 search_terms_replacement_key, id).release()); 1081 search_terms_replacement_key, type, id).release());
1079 } 1082 }
1080 } 1083 }
1081 return t_urls; 1084 return t_urls;
1082 } 1085 }
1083 1086
1084 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) { 1087 bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
1085 return prepopulated_url.is_valid() && 1088 return prepopulated_url.is_valid() &&
1086 net::registry_controlled_domains::SameDomainOrHost( 1089 net::registry_controlled_domains::SameDomainOrHost(
1087 given_url, prepopulated_url, 1090 given_url, prepopulated_url,
1088 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); 1091 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 alternate_urls.AppendString(std::string(engine.alternate_urls[i])); 1136 alternate_urls.AppendString(std::string(engine.alternate_urls[i]));
1134 } 1137 }
1135 1138
1136 return MakePrepopulatedTemplateURLData( 1139 return MakePrepopulatedTemplateURLData(
1137 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword), 1140 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword),
1138 engine.search_url, engine.suggest_url, engine.instant_url, 1141 engine.search_url, engine.suggest_url, engine.instant_url,
1139 engine.image_url, engine.new_tab_url, engine.contextual_search_url, 1142 engine.image_url, engine.new_tab_url, engine.contextual_search_url,
1140 engine.search_url_post_params, engine.suggest_url_post_params, 1143 engine.search_url_post_params, engine.suggest_url_post_params,
1141 engine.instant_url_post_params, engine.image_url_post_params, 1144 engine.instant_url_post_params, engine.image_url_post_params,
1142 engine.favicon_url, engine.encoding, alternate_urls, 1145 engine.favicon_url, engine.encoding, alternate_urls,
1143 engine.search_terms_replacement_key, engine.id); 1146 engine.search_terms_replacement_key, engine.type, engine.id);
1144 } 1147 }
1145 1148
1146 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) { 1149 void ClearPrepopulatedEnginesInPrefs(PrefService* prefs) {
1147 if (!prefs) 1150 if (!prefs)
1148 return; 1151 return;
1149 1152
1150 prefs->ClearPref(prefs::kSearchProviderOverrides); 1153 prefs->ClearPref(prefs::kSearchProviderOverrides);
1151 prefs->ClearPref(prefs::kSearchProviderOverridesVersion); 1154 prefs->ClearPref(prefs::kSearchProviderOverridesVersion);
1152 } 1155 }
1153 1156
1154 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch( 1157 std::unique_ptr<TemplateURLData> GetPrepopulatedDefaultSearch(
1155 PrefService* prefs) { 1158 PrefService* prefs) {
1156 std::unique_ptr<TemplateURLData> default_search_provider; 1159 std::unique_ptr<TemplateURLData> default_search_provider;
1157 size_t default_search_index; 1160 size_t default_search_index;
1158 // This could be more efficient. We are loading all the URLs to only keep 1161 // This could be more efficient. We are loading all the URLs to only keep
1159 // the first one. 1162 // the first one.
1160 ScopedVector<TemplateURLData> loaded_urls = 1163 ScopedVector<TemplateURLData> loaded_urls =
1161 GetPrepopulatedEngines(prefs, &default_search_index); 1164 GetPrepopulatedEngines(prefs, &default_search_index);
1162 if (default_search_index < loaded_urls.size()) { 1165 if (default_search_index < loaded_urls.size()) {
1163 default_search_provider.reset(loaded_urls[default_search_index]); 1166 default_search_provider.reset(loaded_urls[default_search_index]);
1164 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); 1167 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index);
1165 } 1168 }
1166 return default_search_provider; 1169 return default_search_provider;
1167 } 1170 }
1168 1171
1169 SearchEngineType GetEngineType(const TemplateURL& url,
1170 const SearchTermsData& search_terms_data) {
1171 // By calling ReplaceSearchTerms, we ensure that even TemplateURLs whose URLs
1172 // can't be directly inspected (e.g. due to containing {google:baseURL}) can
1173 // be converted to GURLs we can look at.
1174 GURL gurl(url.url_ref().ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(
1175 base::ASCIIToUTF16("x")), search_terms_data));
1176 return gurl.is_valid() ? GetEngineType(gurl) : SEARCH_ENGINE_OTHER;
1177 }
1178
1179 SearchEngineType GetEngineType(const GURL& url) { 1172 SearchEngineType GetEngineType(const GURL& url) {
1180 DCHECK(url.is_valid()); 1173 DCHECK(url.is_valid());
1181 1174
1182 // Check using TLD+1s, in order to more aggressively match search engine types 1175 // Check using TLD+1s, in order to more aggressively match search engine types
1183 // for data imported from other browsers. 1176 // for data imported from other browsers.
1184 // 1177 //
1185 // First special-case Google, because the prepopulate URL for it will not 1178 // First special-case Google, because the prepopulate URL for it will not
1186 // convert to a GURL and thus won't have an origin. Instead see if the 1179 // convert to a GURL and thus won't have an origin. Instead see if the
1187 // incoming URL's host is "[*.]google.<TLD>". 1180 // incoming URL's host is "[*.]google.<TLD>".
1188 if (google_util::IsGoogleHostname(url.host(), 1181 if (google_util::IsGoogleHostname(url.host(),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1266 base::ToUpperASCII(locale_str[begin]), 1259 base::ToUpperASCII(locale_str[begin]),
1267 base::ToUpperASCII(locale_str[begin + 1])); 1260 base::ToUpperASCII(locale_str[begin + 1]));
1268 } 1261 }
1269 1262
1270 return kCountryIDUnknown; 1263 return kCountryIDUnknown;
1271 } 1264 }
1272 1265
1273 #endif // OS_* 1266 #endif // OS_*
1274 1267
1275 } // namespace TemplateURLPrepopulateData 1268 } // namespace TemplateURLPrepopulateData
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698