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

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: Fix typo Created 4 years, 6 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 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 1125 return t_urls;
1126 } 1126 }
1127 1127
1128 std::vector<const PrepopulatedEngine*> GetAllPrepopulatedEngines() {
1129 return std::vector<const PrepopulatedEngine*>(std::begin(kAllEngines),
1130 std::end(kAllEngines));
1131 }
1132
1128 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine( 1133 std::unique_ptr<TemplateURLData> MakeTemplateURLDataFromPrepopulatedEngine(
1129 const PrepopulatedEngine& engine) { 1134 const PrepopulatedEngine& engine) {
1130 base::ListValue alternate_urls; 1135 base::ListValue alternate_urls;
1131 if (engine.alternate_urls) { 1136 if (engine.alternate_urls) {
1132 for (size_t i = 0; i < engine.alternate_urls_size; ++i) 1137 for (size_t i = 0; i < engine.alternate_urls_size; ++i)
1133 alternate_urls.AppendString(std::string(engine.alternate_urls[i])); 1138 alternate_urls.AppendString(std::string(engine.alternate_urls[i]));
1134 } 1139 }
1135 1140
1136 return MakePrepopulatedTemplateURLData( 1141 return MakePrepopulatedTemplateURLData(
1137 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword), 1142 base::WideToUTF16(engine.name), base::WideToUTF16(engine.keyword),
(...skipping 21 matching lines...) Expand all
1159 // the first one. 1164 // the first one.
1160 ScopedVector<TemplateURLData> loaded_urls = 1165 ScopedVector<TemplateURLData> loaded_urls =
1161 GetPrepopulatedEngines(prefs, &default_search_index); 1166 GetPrepopulatedEngines(prefs, &default_search_index);
1162 if (default_search_index < loaded_urls.size()) { 1167 if (default_search_index < loaded_urls.size()) {
1163 default_search_provider.reset(loaded_urls[default_search_index]); 1168 default_search_provider.reset(loaded_urls[default_search_index]);
1164 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); 1169 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index);
1165 } 1170 }
1166 return default_search_provider; 1171 return default_search_provider;
1167 } 1172 }
1168 1173
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) { 1174 SearchEngineType GetEngineType(const GURL& url) {
1180 DCHECK(url.is_valid()); 1175 DCHECK(url.is_valid());
1181 1176
1182 // Check using TLD+1s, in order to more aggressively match search engine types 1177 // Check using TLD+1s, in order to more aggressively match search engine types
1183 // for data imported from other browsers. 1178 // for data imported from other browsers.
1184 // 1179 //
1185 // First special-case Google, because the prepopulate URL for it will not 1180 // 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 1181 // convert to a GURL and thus won't have an origin. Instead see if the
1187 // incoming URL's host is "[*.]google.<TLD>". 1182 // incoming URL's host is "[*.]google.<TLD>".
1188 if (google_util::IsGoogleHostname(url.host(), 1183 if (google_util::IsGoogleHostname(url.host(),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1266 base::ToUpperASCII(locale_str[begin]), 1261 base::ToUpperASCII(locale_str[begin]),
1267 base::ToUpperASCII(locale_str[begin + 1])); 1262 base::ToUpperASCII(locale_str[begin + 1]));
1268 } 1263 }
1269 1264
1270 return kCountryIDUnknown; 1265 return kCountryIDUnknown;
1271 } 1266 }
1272 1267
1273 #endif // OS_* 1268 #endif // OS_*
1274 1269
1275 } // namespace TemplateURLPrepopulateData 1270 } // namespace TemplateURLPrepopulateData
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698