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

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

Issue 10908226: Introduces a search term extraction mechanism working for arbitrary search providers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 3 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 | Annotate | Revision Log
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 "base/guid.h" 7 #include "base/guid.h"
8 #include "base/i18n/case_conversion.h" 8 #include "base/i18n/case_conversion.h"
9 #include "base/i18n/icu_string_conversions.h" 9 #include "base/i18n/icu_string_conversions.h"
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/string_split.h"
14 #include "base/string_util.h" 15 #include "base/string_util.h"
15 #include "base/stringprintf.h" 16 #include "base/stringprintf.h"
16 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/autocomplete/autocomplete_field_trial.h" 18 #include "chrome/browser/autocomplete/autocomplete_field_trial.h"
18 #include "chrome/browser/google/google_util.h" 19 #include "chrome/browser/google/google_util.h"
19 #include "chrome/browser/search_engines/search_terms_data.h" 20 #include "chrome/browser/search_engines/search_terms_data.h"
20 #include "chrome/browser/search_engines/template_url_service.h" 21 #include "chrome/browser/search_engines/template_url_service.h"
21 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
22 #include "google_apis/google_api_keys.h" 23 #include "google_apis/google_api_keys.h"
23 #include "net/base/escape.h" 24 #include "net/base/escape.h"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 : search_terms(search_terms), 117 : search_terms(search_terms),
117 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE) { 118 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE) {
118 } 119 }
119 120
120 121
121 // TemplateURLRef ------------------------------------------------------------- 122 // TemplateURLRef -------------------------------------------------------------
122 123
123 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) 124 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type)
124 : owner_(owner), 125 : owner_(owner),
125 type_(type), 126 type_(type),
127 index_in_owner_(-1),
126 parsed_(false), 128 parsed_(false),
127 valid_(false), 129 valid_(false),
128 supports_replacements_(false), 130 supports_replacements_(false),
131 search_term_key_location_(url_parse::Parsed::QUERY),
129 prepopulated_(false) { 132 prepopulated_(false) {
130 DCHECK(owner_); 133 DCHECK(owner_);
134 DCHECK(type_ != INDEXED);
135 }
136
137 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner)
138 : owner_(owner),
139 type_(INDEXED),
140 index_in_owner_(index_in_owner),
141 parsed_(false),
142 valid_(false),
143 supports_replacements_(false),
144 search_term_key_location_(url_parse::Parsed::QUERY),
145 prepopulated_(false) {
146 DCHECK(owner_);
147 DCHECK(index_in_owner_ >= 0L && index_in_owner_ < owner_->URLCount());
131 } 148 }
132 149
133 TemplateURLRef::~TemplateURLRef() { 150 TemplateURLRef::~TemplateURLRef() {
134 } 151 }
135 152
136 std::string TemplateURLRef::GetURL() const { 153 std::string TemplateURLRef::GetURL() const {
137 switch (type_) { 154 switch (type_) {
138 case SEARCH: return owner_->url(); 155 case SEARCH: return owner_->url();
139 case SUGGEST: return owner_->suggestions_url(); 156 case SUGGEST: return owner_->suggestions_url();
140 case INSTANT: return owner_->instant_url(); 157 case INSTANT: return owner_->instant_url();
158 case INDEXED: return owner_->GetURL(index_in_owner_);
141 default: NOTREACHED(); return std::string(); // NOLINT 159 default: NOTREACHED(); return std::string(); // NOLINT
142 } 160 }
143 } 161 }
144 162
145 bool TemplateURLRef::SupportsReplacement() const { 163 bool TemplateURLRef::SupportsReplacement() const {
146 UIThreadSearchTermsData search_terms_data(owner_->profile()); 164 UIThreadSearchTermsData search_terms_data(owner_->profile());
147 return SupportsReplacementUsingTermsData(search_terms_data); 165 return SupportsReplacementUsingTermsData(search_terms_data);
148 } 166 }
149 167
150 bool TemplateURLRef::SupportsReplacementUsingTermsData( 168 bool TemplateURLRef::SupportsReplacementUsingTermsData(
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 bool TemplateURLRef::HasGoogleBaseURLs() const { 415 bool TemplateURLRef::HasGoogleBaseURLs() const {
398 ParseIfNecessary(); 416 ParseIfNecessary();
399 for (size_t i = 0; i < replacements_.size(); ++i) { 417 for (size_t i = 0; i < replacements_.size(); ++i) {
400 if ((replacements_[i].type == GOOGLE_BASE_URL) || 418 if ((replacements_[i].type == GOOGLE_BASE_URL) ||
401 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL)) 419 (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
402 return true; 420 return true;
403 } 421 }
404 return false; 422 return false;
405 } 423 }
406 424
425 string16 TemplateURLRef::ExtractSearchTermsFromURL(const GURL& url) const {
426 ParseIfNecessary();
427
428 // We need a search term in the template URL to extract something.
429 if (search_term_key_.empty())
430 return string16();
431
432 // TODO(beaudoin): Support {Anything} parameter to act as a path wildcard.
433 // See crbug/139176
434
435 // Fill-in the replacements. We don't care about search terms in the pattern,
436 // so we use the empty string.
437 GURL pattern(ReplaceSearchTerms(SearchTermsArgs(string16())));
438 // Scheme, host, path and port must match.
439 if (!url.SchemeIs(pattern.scheme().c_str()) ||
440 url.port() != pattern.port() ||
441 url.host() != host_ ||
442 url.path() != path_) {
443 return string16();
444 }
445
446 // Parameter must be present either in the query or the ref.
447 std::string params;
448 switch (search_term_key_location_) {
449 case url_parse::Parsed::QUERY:
450 params = url.query();
451 break;
452 case url_parse::Parsed::REF:
453 params = url.ref();
454 break;
455 default:
456 NOTREACHED();
457 return string16();
458 }
459
460 url_parse::Component query, key, value;
461 query.len = static_cast<int>(params.size());
462 while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key,
463 &value)) {
464 if (key.is_nonempty() && value.is_nonempty()) {
465 if (params.substr(key.begin, key.len) == search_term_key_) {
466 // Extract the search term.
467 return net::UnescapeAndDecodeUTF8URLComponent(
468 params.substr(value.begin, value.len),
469 net::UnescapeRule::SPACES |
470 net::UnescapeRule::URL_SPECIAL_CHARS |
471 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE,
472 NULL);
473 }
474 }
475 }
476 return string16();
477 }
478
407 void TemplateURLRef::InvalidateCachedValues() const { 479 void TemplateURLRef::InvalidateCachedValues() const {
408 supports_replacements_ = valid_ = parsed_ = false; 480 supports_replacements_ = valid_ = parsed_ = false;
409 host_.clear(); 481 host_.clear();
410 path_.clear(); 482 path_.clear();
411 search_term_key_.clear(); 483 search_term_key_.clear();
412 replacements_.clear(); 484 replacements_.clear();
413 } 485 }
414 486
415 bool TemplateURLRef::ParseParameter(size_t start, 487 bool TemplateURLRef::ParseParameter(size_t start,
416 size_t end, 488 size_t end,
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 void TemplateURLRef::ParseHostAndSearchTermKey( 621 void TemplateURLRef::ParseHostAndSearchTermKey(
550 const SearchTermsData& search_terms_data) const { 622 const SearchTermsData& search_terms_data) const {
551 std::string url_string(GetURL()); 623 std::string url_string(GetURL());
552 ReplaceSubstringsAfterOffset(&url_string, 0, 624 ReplaceSubstringsAfterOffset(&url_string, 0,
553 kGoogleBaseURLParameterFull, 625 kGoogleBaseURLParameterFull,
554 search_terms_data.GoogleBaseURLValue()); 626 search_terms_data.GoogleBaseURLValue());
555 ReplaceSubstringsAfterOffset(&url_string, 0, 627 ReplaceSubstringsAfterOffset(&url_string, 0,
556 kGoogleBaseSuggestURLParameterFull, 628 kGoogleBaseSuggestURLParameterFull,
557 search_terms_data.GoogleBaseSuggestURLValue()); 629 search_terms_data.GoogleBaseSuggestURLValue());
558 630
631 search_term_key_.clear();
632 host_.clear();
633 path_.clear();
634 search_term_key_location_ = url_parse::Parsed::REF;
635
559 GURL url(url_string); 636 GURL url(url_string);
560 if (!url.is_valid()) 637 if (!url.is_valid())
561 return; 638 return;
562 639
563 std::string query_string = url.query(); 640 // We want to prioritize search terms in the ref rather than ones in the
564 if (query_string.empty()) 641 // query.
565 return; 642 if (!url.ref().empty())
643 FindSearchTermsKey(url.ref());
566 644
645 // If not found in ref string, look for them in query.
646 if (search_term_key_.empty() && !url.query().empty()) {
647 search_term_key_location_ = url_parse::Parsed::QUERY;
648 FindSearchTermsKey(url.query());
649 }
650
651 if (!search_term_key_.empty()) {
652 host_ = url.host();
653 path_ = url.path();
654 }
655 }
656
657 void TemplateURLRef::FindSearchTermsKey(const std::string& params) const {
567 url_parse::Component query, key, value; 658 url_parse::Component query, key, value;
568 query.len = static_cast<int>(query_string.size()); 659 query.len = static_cast<int>(params.size());
569 while (url_parse::ExtractQueryKeyValue(query_string.c_str(), &query, &key, 660 while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key,
570 &value)) { 661 &value)) {
571 if (key.is_nonempty() && value.is_nonempty()) { 662 if (key.is_nonempty() && value.is_nonempty()) {
572 std::string value_string = query_string.substr(value.begin, value.len); 663 std::string value_string = params.substr(value.begin, value.len);
573 if (value_string.find(kSearchTermsParameterFull, 0) != 664 if (value_string.find(kSearchTermsParameterFull, 0) !=
574 std::string::npos || 665 std::string::npos ||
575 value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) != 666 value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) !=
576 std::string::npos) { 667 std::string::npos) {
577 search_term_key_ = query_string.substr(key.begin, key.len); 668 search_term_key_ = params.substr(key.begin, key.len);
578 host_ = url.host();
579 path_ = url.path();
580 break; 669 break;
581 } 670 }
582 } 671 }
583 } 672 }
584 } 673 }
585 674
586 675
587 // TemplateURLData ------------------------------------------------------------ 676 // TemplateURLData ------------------------------------------------------------
588 677
589 TemplateURLData::TemplateURLData() 678 TemplateURLData::TemplateURLData()
(...skipping 19 matching lines...) Expand all
609 // Case sensitive keyword matching is confusing. As such, we force all 698 // Case sensitive keyword matching is confusing. As such, we force all
610 // keywords to be lower case. 699 // keywords to be lower case.
611 keyword_ = base::i18n::ToLower(keyword); 700 keyword_ = base::i18n::ToLower(keyword);
612 } 701 }
613 702
614 void TemplateURLData::SetURL(const std::string& url) { 703 void TemplateURLData::SetURL(const std::string& url) {
615 DCHECK(!url.empty()); 704 DCHECK(!url.empty());
616 url_ = url; 705 url_ = url;
617 } 706 }
618 707
708 std::string TemplateURLData::SerializeAlternateURLs() const {
709 std::string result;
710 for (size_t i = 0; i < alternate_urls_.size(); ++i) {
711 // Sanity check that the URL doesn't contain a comma.
712 DCHECK(alternate_urls_[i].find(',') == std::string::npos);
713 if (result.length() != 0)
714 result.append(",");
715 result.append(alternate_urls_[i]);
716 }
717 return result;
718 }
719
720 void TemplateURLData::DeserializeAndSetAlternateURLs(
721 const std::string& alternate_urls) {
722 base::SplitString(alternate_urls, ',', &alternate_urls_);
723 }
619 724
620 // TemplateURL ---------------------------------------------------------------- 725 // TemplateURL ----------------------------------------------------------------
621 726
622 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data) 727 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data)
623 : profile_(profile), 728 : profile_(profile),
624 data_(data), 729 data_(data),
625 url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH), 730 url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH),
626 suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), 731 suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
627 TemplateURLRef::SUGGEST), 732 TemplateURLRef::SUGGEST),
628 instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), 733 instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 788
684 std::string TemplateURL::GetExtensionId() const { 789 std::string TemplateURL::GetExtensionId() const {
685 DCHECK(IsExtensionKeyword()); 790 DCHECK(IsExtensionKeyword());
686 return GURL(data_.url()).host(); 791 return GURL(data_.url()).host();
687 } 792 }
688 793
689 bool TemplateURL::IsExtensionKeyword() const { 794 bool TemplateURL::IsExtensionKeyword() const {
690 return GURL(data_.url()).SchemeIs(chrome::kExtensionScheme); 795 return GURL(data_.url()).SchemeIs(chrome::kExtensionScheme);
691 } 796 }
692 797
798 size_t TemplateURL::URLCount() const {
799 DCHECK(!url().empty());
800
801 int count = 1; // At least one for url().
802 if (!instant_url().empty())
803 count++;
804 count += data_.alternate_urls().size();
805 return count;
806 }
807
808 const std::string& TemplateURL::GetURL(size_t index) const {
809 DCHECK(!url().empty());
810 DCHECK(index >= 0);
811
812 if (index < data_.alternate_urls().size())
813 return data_.alternate_urls()[index];
814
815 index -= data_.alternate_urls().size();
816 if (!instant_url().empty()) {
817 if (index == 0)
818 return instant_url();
819 index--;
820 }
821 DCHECK(index == 0);
822 return url();
823 }
824
825 string16 TemplateURL::ExtractSearchTermsFromURL(const GURL& url) {
826 for (size_t i = 0; i < URLCount(); ++i) {
827 TemplateURLRef ref(this, i);
828 string16 result(ref.ExtractSearchTermsFromURL(url));
829 if (!result.empty())
830 return result;
831 }
832 return string16();
833 }
834
693 void TemplateURL::CopyFrom(const TemplateURL& other) { 835 void TemplateURL::CopyFrom(const TemplateURL& other) {
694 if (this == &other) 836 if (this == &other)
695 return; 837 return;
696 838
697 profile_ = other.profile_; 839 profile_ = other.profile_;
698 data_ = other.data_; 840 data_ = other.data_;
699 url_ref_.InvalidateCachedValues(); 841 url_ref_.InvalidateCachedValues();
700 suggestions_url_ref_.InvalidateCachedValues(); 842 suggestions_url_ref_.InvalidateCachedValues();
701 instant_url_ref_.InvalidateCachedValues(); 843 instant_url_ref_.InvalidateCachedValues();
702 SetPrepopulateId(other.data_.prepopulate_id); 844 SetPrepopulateId(other.data_.prepopulate_id);
(...skipping 13 matching lines...) Expand all
716 } 858 }
717 859
718 void TemplateURL::ResetKeywordIfNecessary(bool force) { 860 void TemplateURL::ResetKeywordIfNecessary(bool force) {
719 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { 861 if (IsGoogleSearchURLWithReplaceableKeyword() || force) {
720 DCHECK(!IsExtensionKeyword()); 862 DCHECK(!IsExtensionKeyword());
721 GURL url(TemplateURLService::GenerateSearchURL(this)); 863 GURL url(TemplateURLService::GenerateSearchURL(this));
722 if (url.is_valid()) 864 if (url.is_valid())
723 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); 865 data_.SetKeyword(TemplateURLService::GenerateKeyword(url));
724 } 866 }
725 } 867 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698