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 "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 bool TemplateURLRef::HasGoogleBaseURLs() const { | 463 bool TemplateURLRef::HasGoogleBaseURLs() const { |
464 ParseIfNecessary(); | 464 ParseIfNecessary(); |
465 for (size_t i = 0; i < replacements_.size(); ++i) { | 465 for (size_t i = 0; i < replacements_.size(); ++i) { |
466 if ((replacements_[i].type == GOOGLE_BASE_URL) || | 466 if ((replacements_[i].type == GOOGLE_BASE_URL) || |
467 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL)) | 467 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL)) |
468 return true; | 468 return true; |
469 } | 469 } |
470 return false; | 470 return false; |
471 } | 471 } |
472 | 472 |
473 bool TemplateURLRef::ExtractSearchTermsFromURL(const GURL& url, | 473 bool TemplateURLRef::ExtractSearchTermsFromURL( |
474 string16* search_terms) const { | 474 const GURL& url, |
475 string16* search_terms, | |
476 bool needs_instant_extended) const { | |
475 DCHECK(search_terms); | 477 DCHECK(search_terms); |
476 search_terms->clear(); | 478 search_terms->clear(); |
477 | 479 |
478 ParseIfNecessary(); | 480 ParseIfNecessary(); |
479 | 481 |
480 // We need a search term in the template URL to extract something. | 482 // We need a search term in the template URL to extract something. |
481 if (search_term_key_.empty()) | 483 if (search_term_key_.empty()) |
482 return false; | 484 return false; |
483 | 485 |
484 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/ | 486 // TODO(beaudoin): Support patterns of the form http://foo/{searchTerms}/ |
485 // See crbug.com/153798 | 487 // See crbug.com/153798 |
486 | 488 |
487 // Fill-in the replacements. We don't care about search terms in the pattern, | 489 // Fill-in the replacements. We don't care about search terms in the pattern, |
488 // so we use the empty string. | 490 // so we use the empty string. |
489 GURL pattern(ReplaceSearchTerms(SearchTermsArgs(string16()))); | 491 GURL pattern(ReplaceSearchTerms(SearchTermsArgs(string16()))); |
490 // Host, path and port must match. | 492 // Host, path and port must match. |
491 if (url.port() != pattern.port() || | 493 if (url.port() != pattern.port() || |
492 url.host() != host_ || | 494 url.host() != host_ || |
493 url.path() != path_) { | 495 url.path() != path_) { |
494 return false; | 496 return false; |
495 } | 497 } |
496 | 498 |
497 // Parameter must be present either in the query or the ref. | 499 // Parameter must be present either in the query or the ref. |
498 const std::string& params( | 500 const std::string& params( |
499 (search_term_key_location_ == url_parse::Parsed::QUERY) ? | 501 (search_term_key_location_ == url_parse::Parsed::QUERY) ? |
500 url.query() : url.ref()); | 502 url.query() : url.ref()); |
501 | 503 |
502 url_parse::Component query, key, value; | 504 url_parse::Component query, key, value; |
503 query.len = static_cast<int>(params.size()); | 505 query.len = static_cast<int>(params.size()); |
506 bool search_terms_found = false, replacement_key_found = false; | |
504 while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key, | 507 while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key, |
505 &value)) { | 508 &value)) { |
506 if (key.is_nonempty()) { | 509 if (key.is_nonempty()) { |
507 if (params.substr(key.begin, key.len) == search_term_key_) { | 510 base::StringPiece key_string(params.begin() + key.begin, |
Peter Kasting
2012/12/18 02:09:22
Does using a StringPiece here actually buy much?
beaudoin
2012/12/20 04:40:56
Thought it may have been worth not copying charact
Peter Kasting
2012/12/20 19:40:16
I don't really care too much, the string version j
beaudoin
2013/01/08 16:55:39
Done.
| |
511 params.begin() + key.begin + key.len); | |
512 if (key_string == search_term_key_) { | |
508 // Extract the search term. | 513 // Extract the search term. |
509 *search_terms = net::UnescapeAndDecodeUTF8URLComponent( | 514 *search_terms = net::UnescapeAndDecodeUTF8URLComponent( |
510 params.substr(value.begin, value.len), | 515 params.substr(value.begin, value.len), |
511 net::UnescapeRule::SPACES | | 516 net::UnescapeRule::SPACES | |
512 net::UnescapeRule::URL_SPECIAL_CHARS | | 517 net::UnescapeRule::URL_SPECIAL_CHARS | |
513 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE, | 518 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE, |
514 NULL); | 519 NULL); |
520 search_terms_found = true; | |
521 } | |
522 if (key_string == owner_->search_terms_replacement_key()) { | |
523 int int_value = 0; | |
524 if (value.is_nonempty()) | |
525 base::StringToInt(params.substr(value.begin, value.len), &int_value); | |
526 if (int_value != 0) | |
527 replacement_key_found = true; | |
528 } | |
529 if (search_terms_found && | |
530 (replacement_key_found || !needs_instant_extended)) { | |
515 return true; | 531 return true; |
516 } | 532 } |
517 } | 533 } |
518 } | 534 } |
535 search_terms->clear(); | |
519 return false; | 536 return false; |
520 } | 537 } |
521 | 538 |
522 void TemplateURLRef::InvalidateCachedValues() const { | 539 void TemplateURLRef::InvalidateCachedValues() const { |
523 supports_replacements_ = valid_ = parsed_ = false; | 540 supports_replacements_ = valid_ = parsed_ = false; |
524 host_.clear(); | 541 host_.clear(); |
525 path_.clear(); | 542 path_.clear(); |
526 search_term_key_.clear(); | 543 search_term_key_.clear(); |
527 replacements_.clear(); | 544 replacements_.clear(); |
528 } | 545 } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
820 } | 837 } |
821 | 838 |
822 const std::string& TemplateURL::GetURL(size_t index) const { | 839 const std::string& TemplateURL::GetURL(size_t index) const { |
823 DCHECK_LT(index, URLCount()); | 840 DCHECK_LT(index, URLCount()); |
824 | 841 |
825 return (index < data_.alternate_urls.size()) ? | 842 return (index < data_.alternate_urls.size()) ? |
826 data_.alternate_urls[index] : url(); | 843 data_.alternate_urls[index] : url(); |
827 } | 844 } |
828 | 845 |
829 bool TemplateURL::ExtractSearchTermsFromURL( | 846 bool TemplateURL::ExtractSearchTermsFromURL( |
830 const GURL& url, string16* search_terms) { | 847 const GURL& url, string16* search_terms, bool needs_instant_extended) { |
Peter Kasting
2012/12/18 02:09:22
Nit: One arg per line (and 1st arg can move up a l
beaudoin
2012/12/20 04:40:56
Done.
| |
831 DCHECK(search_terms); | 848 DCHECK(search_terms); |
832 search_terms->clear(); | 849 search_terms->clear(); |
833 | 850 |
834 // Then try to match with every pattern. | 851 // Then try to match with every pattern. |
835 for (size_t i = 0; i < URLCount(); ++i) { | 852 for (size_t i = 0; i < URLCount(); ++i) { |
836 TemplateURLRef ref(this, i); | 853 TemplateURLRef ref(this, i); |
837 if (ref.ExtractSearchTermsFromURL(url, search_terms)) { | 854 if (ref.ExtractSearchTermsFromURL(url, search_terms, |
855 needs_instant_extended)) { | |
838 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty | 856 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty |
839 // it means the pattern matched but no search terms were present. In this | 857 // it means the pattern matched but no search terms were present. In this |
840 // case we fail immediately without looking for matches in subsequent | 858 // case we fail immediately without looking for matches in subsequent |
841 // patterns. This means that given patterns | 859 // patterns. This means that given patterns |
842 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 860 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
843 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 861 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
844 // return false. This is important for at least Google, where such URLs | 862 // return false. This is important for at least Google, where such URLs |
845 // are invalid. | 863 // are invalid. |
846 return !search_terms->empty(); | 864 return !search_terms->empty(); |
847 } | 865 } |
(...skipping 27 matching lines...) Expand all Loading... | |
875 } | 893 } |
876 | 894 |
877 void TemplateURL::ResetKeywordIfNecessary(bool force) { | 895 void TemplateURL::ResetKeywordIfNecessary(bool force) { |
878 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { | 896 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { |
879 DCHECK(!IsExtensionKeyword()); | 897 DCHECK(!IsExtensionKeyword()); |
880 GURL url(TemplateURLService::GenerateSearchURL(this)); | 898 GURL url(TemplateURLService::GenerateSearchURL(this)); |
881 if (url.is_valid()) | 899 if (url.is_valid()) |
882 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 900 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
883 } | 901 } |
884 } | 902 } |
OLD | NEW |