OLD | NEW |
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/google/google_util.h" | 23 #include "chrome/browser/google/google_util.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 "chrome/common/chrome_version_info.h" | 26 #include "chrome/common/chrome_version_info.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 1303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1347 if (TryEncoding(search_terms_args.search_terms, | 1347 if (TryEncoding(search_terms_args.search_terms, |
1348 search_terms_args.original_query, i->c_str(), | 1348 search_terms_args.original_query, i->c_str(), |
1349 is_in_query, encoded_terms, encoded_original_query)) { | 1349 is_in_query, encoded_terms, encoded_original_query)) { |
1350 *input_encoding = *i; | 1350 *input_encoding = *i; |
1351 return; | 1351 return; |
1352 } | 1352 } |
1353 } | 1353 } |
1354 NOTREACHED(); | 1354 NOTREACHED(); |
1355 } | 1355 } |
1356 | 1356 |
| 1357 // static |
| 1358 base::string16 TemplateURL::GenerateKeyword(const GURL& url) { |
| 1359 DCHECK(url.is_valid()); |
| 1360 // Strip "www." off the front of the keyword; otherwise the keyword won't work |
| 1361 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . |
| 1362 // Special case: if the host was exactly "www." (not sure this can happen but |
| 1363 // perhaps with some weird intranet and custom DNS server?), ensure we at |
| 1364 // least don't return the empty string. |
| 1365 base::string16 keyword(net::StripWWWFromHost(url)); |
| 1366 return keyword.empty() ? base::ASCIIToUTF16("www") : keyword; |
| 1367 } |
| 1368 |
| 1369 GURL TemplateURL::GenerateSearchURL( |
| 1370 const SearchTermsData& search_terms_data) const { |
| 1371 if (!url_ref_.IsValid(search_terms_data)) |
| 1372 return GURL(); |
| 1373 |
| 1374 if (!url_ref_.SupportsReplacement(search_terms_data)) |
| 1375 return GURL(url()); |
| 1376 |
| 1377 // Use something obscure for the search terms argument so that in the rare |
| 1378 // case the term replaces the URL it's unlikely another keyword would have the |
| 1379 // same url. |
| 1380 // TODO(jnd): Add additional parameters to get post data when the search URL |
| 1381 // has post parameters. |
| 1382 return GURL(url_ref_.ReplaceSearchTerms( |
| 1383 TemplateURLRef::SearchTermsArgs( |
| 1384 base::ASCIIToUTF16("blah.blah.blah.blah.blah")), |
| 1385 search_terms_data, NULL)); |
| 1386 } |
| 1387 |
1357 void TemplateURL::CopyFrom(const TemplateURL& other) { | 1388 void TemplateURL::CopyFrom(const TemplateURL& other) { |
1358 if (this == &other) | 1389 if (this == &other) |
1359 return; | 1390 return; |
1360 | 1391 |
1361 data_ = other.data_; | 1392 data_ = other.data_; |
1362 url_ref_.InvalidateCachedValues(); | 1393 url_ref_.InvalidateCachedValues(); |
1363 suggestions_url_ref_.InvalidateCachedValues(); | 1394 suggestions_url_ref_.InvalidateCachedValues(); |
1364 instant_url_ref_.InvalidateCachedValues(); | 1395 instant_url_ref_.InvalidateCachedValues(); |
1365 SetPrepopulateId(other.data_.prepopulate_id); | 1396 SetPrepopulateId(other.data_.prepopulate_id); |
1366 } | 1397 } |
1367 | 1398 |
1368 void TemplateURL::SetURL(const std::string& url) { | 1399 void TemplateURL::SetURL(const std::string& url) { |
1369 data_.SetURL(url); | 1400 data_.SetURL(url); |
1370 url_ref_.InvalidateCachedValues(); | 1401 url_ref_.InvalidateCachedValues(); |
1371 } | 1402 } |
1372 | 1403 |
1373 void TemplateURL::SetPrepopulateId(int id) { | 1404 void TemplateURL::SetPrepopulateId(int id) { |
1374 data_.prepopulate_id = id; | 1405 data_.prepopulate_id = id; |
1375 const bool prepopulated = id > 0; | 1406 const bool prepopulated = id > 0; |
1376 url_ref_.prepopulated_ = prepopulated; | 1407 url_ref_.prepopulated_ = prepopulated; |
1377 suggestions_url_ref_.prepopulated_ = prepopulated; | 1408 suggestions_url_ref_.prepopulated_ = prepopulated; |
1378 instant_url_ref_.prepopulated_ = prepopulated; | 1409 instant_url_ref_.prepopulated_ = prepopulated; |
1379 } | 1410 } |
1380 | 1411 |
1381 void TemplateURL::ResetKeywordIfNecessary( | 1412 void TemplateURL::ResetKeywordIfNecessary( |
1382 const SearchTermsData& search_terms_data, | 1413 const SearchTermsData& search_terms_data, |
1383 bool force) { | 1414 bool force) { |
1384 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) { | 1415 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) { |
1385 DCHECK(GetType() != OMNIBOX_API_EXTENSION); | 1416 DCHECK(GetType() != OMNIBOX_API_EXTENSION); |
1386 GURL url(TemplateURLService::GenerateSearchURL(this, search_terms_data)); | 1417 GURL url(GenerateSearchURL(search_terms_data)); |
1387 if (url.is_valid()) | 1418 if (url.is_valid()) |
1388 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 1419 data_.SetKeyword(GenerateKeyword(url)); |
1389 } | 1420 } |
1390 } | 1421 } |
1391 | 1422 |
1392 bool TemplateURL::FindSearchTermsInURL( | 1423 bool TemplateURL::FindSearchTermsInURL( |
1393 const GURL& url, | 1424 const GURL& url, |
1394 const SearchTermsData& search_terms_data, | 1425 const SearchTermsData& search_terms_data, |
1395 base::string16* search_terms, | 1426 base::string16* search_terms, |
1396 url::Parsed::ComponentType* search_term_component, | 1427 url::Parsed::ComponentType* search_term_component, |
1397 url::Component* search_terms_position) { | 1428 url::Component* search_terms_position) { |
1398 DCHECK(search_terms); | 1429 DCHECK(search_terms); |
(...skipping 10 matching lines...) Expand all Loading... |
1409 // patterns. This means that given patterns | 1440 // patterns. This means that given patterns |
1410 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1441 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
1411 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1442 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
1412 // return false. This is important for at least Google, where such URLs | 1443 // return false. This is important for at least Google, where such URLs |
1413 // are invalid. | 1444 // are invalid. |
1414 return !search_terms->empty(); | 1445 return !search_terms->empty(); |
1415 } | 1446 } |
1416 } | 1447 } |
1417 return false; | 1448 return false; |
1418 } | 1449 } |
OLD | NEW |