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

Side by Side Diff: chrome/browser/search_engines/template_url.cc

Issue 343823002: Move GenerateSearchURL() and GenerateKeyword() from TemplateURLService to TemplateURL (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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.h" 5 #include "chrome/browser/search_engines/template_url.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/i18n/icu_string_conversions.h" 13 #include "base/i18n/icu_string_conversions.h"
14 #include "base/i18n/rtl.h" 14 #include "base/i18n/rtl.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h" 16 #include "base/metrics/field_trial.h"
17 #include "base/rand_util.h" 17 #include "base/rand_util.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h" 19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "chrome/browser/omnibox/omnibox_field_trial.h" 23 #include "chrome/browser/omnibox/omnibox_field_trial.h"
24 #include "chrome/browser/search/search.h" 24 #include "chrome/browser/search/search.h"
25 #include "chrome/browser/search_engines/template_url_service.h"
26 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
27 #include "components/google/core/browser/google_util.h" 26 #include "components/google/core/browser/google_util.h"
28 #include "components/metrics/proto/omnibox_input_type.pb.h" 27 #include "components/metrics/proto/omnibox_input_type.pb.h"
29 #include "components/search_engines/search_terms_data.h" 28 #include "components/search_engines/search_terms_data.h"
30 #include "extensions/common/constants.h" 29 #include "extensions/common/constants.h"
31 #include "google_apis/google_api_keys.h" 30 #include "google_apis/google_api_keys.h"
32 #include "net/base/escape.h" 31 #include "net/base/escape.h"
33 #include "net/base/mime_util.h" 32 #include "net/base/mime_util.h"
33 #include "net/base/net_util.h"
34 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
35 35
36 namespace { 36 namespace {
37 37
38 // The TemplateURLRef has any number of terms that need to be replaced. Each of 38 // The TemplateURLRef has any number of terms that need to be replaced. Each of
39 // the terms is enclosed in braces. If the character preceeding the final 39 // the terms is enclosed in braces. If the character preceeding the final
40 // brace is a ?, it indicates the term is optional and can be replaced with 40 // brace is a ?, it indicates the term is optional and can be replaced with
41 // an empty string. 41 // an empty string.
42 const char kStartParameter = '{'; 42 const char kStartParameter = '{';
43 const char kEndParameter = '}'; 43 const char kEndParameter = '}';
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
1132 if (data_.search_terms_replacement_key == 1132 if (data_.search_terms_replacement_key ==
1133 kGoogleInstantExtendedEnabledKeyFull) { 1133 kGoogleInstantExtendedEnabledKeyFull) {
1134 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; 1134 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1135 } 1135 }
1136 } 1136 }
1137 1137
1138 TemplateURL::~TemplateURL() { 1138 TemplateURL::~TemplateURL() {
1139 } 1139 }
1140 1140
1141 // static 1141 // static
1142 base::string16 TemplateURL::GenerateKeyword(const GURL& url) {
1143 DCHECK(url.is_valid());
1144 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1145 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
1146 // Special case: if the host was exactly "www." (not sure this can happen but
1147 // perhaps with some weird intranet and custom DNS server?), ensure we at
1148 // least don't return the empty string.
1149 base::string16 keyword(net::StripWWWFromHost(url));
1150 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword;
1151 }
1152
1153 // static
1142 GURL TemplateURL::GenerateFaviconURL(const GURL& url) { 1154 GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
1143 DCHECK(url.is_valid()); 1155 DCHECK(url.is_valid());
1144 GURL::Replacements rep; 1156 GURL::Replacements rep;
1145 1157
1146 const char favicon_path[] = "/favicon.ico"; 1158 const char favicon_path[] = "/favicon.ico";
1147 int favicon_path_len = arraysize(favicon_path) - 1; 1159 int favicon_path_len = arraysize(favicon_path) - 1;
1148 1160
1149 rep.SetPath(favicon_path, url::Component(0, favicon_path_len)); 1161 rep.SetPath(favicon_path, url::Component(0, favicon_path_len));
1150 rep.ClearUsername(); 1162 rep.ClearUsername();
1151 rep.ClearPassword(); 1163 rep.ClearPassword();
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 if (TryEncoding(search_terms_args.search_terms, 1348 if (TryEncoding(search_terms_args.search_terms,
1337 search_terms_args.original_query, i->c_str(), 1349 search_terms_args.original_query, i->c_str(),
1338 is_in_query, encoded_terms, encoded_original_query)) { 1350 is_in_query, encoded_terms, encoded_original_query)) {
1339 *input_encoding = *i; 1351 *input_encoding = *i;
1340 return; 1352 return;
1341 } 1353 }
1342 } 1354 }
1343 NOTREACHED(); 1355 NOTREACHED();
1344 } 1356 }
1345 1357
1358 GURL TemplateURL::GenerateSearchURL(
1359 const SearchTermsData& search_terms_data) const {
1360 if (!url_ref_.IsValid(search_terms_data))
1361 return GURL();
1362
1363 if (!url_ref_.SupportsReplacement(search_terms_data))
1364 return GURL(url());
1365
1366 // Use something obscure for the search terms argument so that in the rare
1367 // case the term replaces the URL it's unlikely another keyword would have the
1368 // same url.
1369 // TODO(jnd): Add additional parameters to get post data when the search URL
1370 // has post parameters.
1371 return GURL(url_ref_.ReplaceSearchTerms(
1372 TemplateURLRef::SearchTermsArgs(
1373 base::ASCIIToUTF16("blah.blah.blah.blah.blah")),
1374 search_terms_data, NULL));
1375 }
1376
1346 void TemplateURL::CopyFrom(const TemplateURL& other) { 1377 void TemplateURL::CopyFrom(const TemplateURL& other) {
1347 if (this == &other) 1378 if (this == &other)
1348 return; 1379 return;
1349 1380
1350 data_ = other.data_; 1381 data_ = other.data_;
1351 url_ref_.InvalidateCachedValues(); 1382 url_ref_.InvalidateCachedValues();
1352 suggestions_url_ref_.InvalidateCachedValues(); 1383 suggestions_url_ref_.InvalidateCachedValues();
1353 instant_url_ref_.InvalidateCachedValues(); 1384 instant_url_ref_.InvalidateCachedValues();
1354 SetPrepopulateId(other.data_.prepopulate_id); 1385 SetPrepopulateId(other.data_.prepopulate_id);
1355 } 1386 }
1356 1387
1357 void TemplateURL::SetURL(const std::string& url) { 1388 void TemplateURL::SetURL(const std::string& url) {
1358 data_.SetURL(url); 1389 data_.SetURL(url);
1359 url_ref_.InvalidateCachedValues(); 1390 url_ref_.InvalidateCachedValues();
1360 } 1391 }
1361 1392
1362 void TemplateURL::SetPrepopulateId(int id) { 1393 void TemplateURL::SetPrepopulateId(int id) {
1363 data_.prepopulate_id = id; 1394 data_.prepopulate_id = id;
1364 const bool prepopulated = id > 0; 1395 const bool prepopulated = id > 0;
1365 url_ref_.prepopulated_ = prepopulated; 1396 url_ref_.prepopulated_ = prepopulated;
1366 suggestions_url_ref_.prepopulated_ = prepopulated; 1397 suggestions_url_ref_.prepopulated_ = prepopulated;
1367 instant_url_ref_.prepopulated_ = prepopulated; 1398 instant_url_ref_.prepopulated_ = prepopulated;
1368 } 1399 }
1369 1400
1370 void TemplateURL::ResetKeywordIfNecessary( 1401 void TemplateURL::ResetKeywordIfNecessary(
1371 const SearchTermsData& search_terms_data, 1402 const SearchTermsData& search_terms_data,
1372 bool force) { 1403 bool force) {
1373 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) { 1404 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) {
1374 DCHECK(GetType() != OMNIBOX_API_EXTENSION); 1405 DCHECK(GetType() != OMNIBOX_API_EXTENSION);
1375 GURL url(TemplateURLService::GenerateSearchURL(this, search_terms_data)); 1406 GURL url(GenerateSearchURL(search_terms_data));
1376 if (url.is_valid()) 1407 if (url.is_valid())
1377 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); 1408 data_.SetKeyword(GenerateKeyword(url));
1378 } 1409 }
1379 } 1410 }
1380 1411
1381 bool TemplateURL::FindSearchTermsInURL( 1412 bool TemplateURL::FindSearchTermsInURL(
1382 const GURL& url, 1413 const GURL& url,
1383 const SearchTermsData& search_terms_data, 1414 const SearchTermsData& search_terms_data,
1384 base::string16* search_terms, 1415 base::string16* search_terms,
1385 url::Parsed::ComponentType* search_term_component, 1416 url::Parsed::ComponentType* search_term_component,
1386 url::Component* search_terms_position) { 1417 url::Component* search_terms_position) {
1387 DCHECK(search_terms); 1418 DCHECK(search_terms);
(...skipping 10 matching lines...) Expand all
1398 // patterns. This means that given patterns 1429 // patterns. This means that given patterns
1399 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], 1430 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1400 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would 1431 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1401 // return false. This is important for at least Google, where such URLs 1432 // return false. This is important for at least Google, where such URLs
1402 // are invalid. 1433 // are invalid.
1403 return !search_terms->empty(); 1434 return !search_terms->empty();
1404 } 1435 }
1405 } 1436 }
1406 return false; 1437 return false;
1407 } 1438 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url.h ('k') | chrome/browser/search_engines/template_url_parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698