| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 // Like google:baseURL, but for the Search Suggest capability. | 54 // Like google:baseURL, but for the Search Suggest capability. |
| 55 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL"; | 55 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL"; |
| 56 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}"; | 56 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}"; |
| 57 const char kGoogleCursorPositionParameter[] = "google:cursorPosition"; | 57 const char kGoogleCursorPositionParameter[] = "google:cursorPosition"; |
| 58 const char kGoogleInstantEnabledParameter[] = "google:instantEnabledParameter"; | 58 const char kGoogleInstantEnabledParameter[] = "google:instantEnabledParameter"; |
| 59 const char kGoogleInstantExtendedEnabledParameter[] = | 59 const char kGoogleInstantExtendedEnabledParameter[] = |
| 60 "google:instantExtendedEnabledParameter"; | 60 "google:instantExtendedEnabledParameter"; |
| 61 const char kGoogleInstantExtendedEnabledKey[] = | 61 const char kGoogleInstantExtendedEnabledKey[] = |
| 62 "google:instantExtendedEnabledKey"; | 62 "google:instantExtendedEnabledKey"; |
| 63 const char kGoogleInstantExtendedEnabledKeyFull[] = |
| 64 "{google:instantExtendedEnabledKey}"; |
| 63 const char kGoogleOriginalQueryForSuggestionParameter[] = | 65 const char kGoogleOriginalQueryForSuggestionParameter[] = |
| 64 "google:originalQueryForSuggestion"; | 66 "google:originalQueryForSuggestion"; |
| 65 const char kGoogleRLZParameter[] = "google:RLZ"; | 67 const char kGoogleRLZParameter[] = "google:RLZ"; |
| 66 const char kGoogleSearchClient[] = "google:searchClient"; | 68 const char kGoogleSearchClient[] = "google:searchClient"; |
| 67 const char kGoogleSearchFieldtrialParameter[] = | 69 const char kGoogleSearchFieldtrialParameter[] = |
| 68 "google:searchFieldtrialParameter"; | 70 "google:searchFieldtrialParameter"; |
| 69 const char kGoogleSourceIdParameter[] = "google:sourceId"; | 71 const char kGoogleSourceIdParameter[] = "google:sourceId"; |
| 70 const char kGoogleSuggestAPIKeyParameter[] = "google:suggestAPIKeyParameter"; | 72 const char kGoogleSuggestAPIKeyParameter[] = "google:suggestAPIKeyParameter"; |
| 71 | 73 |
| 72 // Same as kSearchTermsParameter, with no escaping. | 74 // Same as kSearchTermsParameter, with no escaping. |
| (...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 | 742 |
| 741 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data) | 743 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data) |
| 742 : profile_(profile), | 744 : profile_(profile), |
| 743 data_(data), | 745 data_(data), |
| 744 url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH), | 746 url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), TemplateURLRef::SEARCH), |
| 745 suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 747 suggestions_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 746 TemplateURLRef::SUGGEST), | 748 TemplateURLRef::SUGGEST), |
| 747 instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 749 instant_url_ref_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 748 TemplateURLRef::INSTANT) { | 750 TemplateURLRef::INSTANT) { |
| 749 SetPrepopulateId(data_.prepopulate_id); | 751 SetPrepopulateId(data_.prepopulate_id); |
| 752 |
| 753 if (data_.search_terms_replacement_key == |
| 754 kGoogleInstantExtendedEnabledKeyFull) { |
| 755 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; |
| 756 } |
| 750 } | 757 } |
| 751 | 758 |
| 752 TemplateURL::~TemplateURL() { | 759 TemplateURL::~TemplateURL() { |
| 753 } | 760 } |
| 754 | 761 |
| 755 // static | 762 // static |
| 756 GURL TemplateURL::GenerateFaviconURL(const GURL& url) { | 763 GURL TemplateURL::GenerateFaviconURL(const GURL& url) { |
| 757 DCHECK(url.is_valid()); | 764 DCHECK(url.is_valid()); |
| 758 GURL::Replacements rep; | 765 GURL::Replacements rep; |
| 759 | 766 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 return data_.alternate_urls.size() + 1; | 821 return data_.alternate_urls.size() + 1; |
| 815 } | 822 } |
| 816 | 823 |
| 817 const std::string& TemplateURL::GetURL(size_t index) const { | 824 const std::string& TemplateURL::GetURL(size_t index) const { |
| 818 DCHECK_LT(index, URLCount()); | 825 DCHECK_LT(index, URLCount()); |
| 819 | 826 |
| 820 return (index < data_.alternate_urls.size()) ? | 827 return (index < data_.alternate_urls.size()) ? |
| 821 data_.alternate_urls[index] : url(); | 828 data_.alternate_urls[index] : url(); |
| 822 } | 829 } |
| 823 | 830 |
| 824 bool TemplateURL::ExtractSearchTermsFromURL( | 831 bool TemplateURL::ExtractSearchTermsFromURL(const GURL& url, |
| 825 const GURL& url, string16* search_terms) { | 832 string16* search_terms) { |
| 826 DCHECK(search_terms); | 833 DCHECK(search_terms); |
| 827 search_terms->clear(); | 834 search_terms->clear(); |
| 828 | 835 |
| 829 // Then try to match with every pattern. | 836 // Then try to match with every pattern. |
| 830 for (size_t i = 0; i < URLCount(); ++i) { | 837 for (size_t i = 0; i < URLCount(); ++i) { |
| 831 TemplateURLRef ref(this, i); | 838 TemplateURLRef ref(this, i); |
| 832 if (ref.ExtractSearchTermsFromURL(url, search_terms)) { | 839 if (ref.ExtractSearchTermsFromURL(url, search_terms)) { |
| 833 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty | 840 // If ExtractSearchTermsFromURL() returns true and |search_terms| is empty |
| 834 // it means the pattern matched but no search terms were present. In this | 841 // it means the pattern matched but no search terms were present. In this |
| 835 // case we fail immediately without looking for matches in subsequent | 842 // case we fail immediately without looking for matches in subsequent |
| 836 // patterns. This means that given patterns | 843 // patterns. This means that given patterns |
| 837 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 844 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 838 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 845 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 839 // return false. This is important for at least Google, where such URLs | 846 // return false. This is important for at least Google, where such URLs |
| 840 // are invalid. | 847 // are invalid. |
| 841 return !search_terms->empty(); | 848 return !search_terms->empty(); |
| 842 } | 849 } |
| 843 } | 850 } |
| 844 return false; | 851 return false; |
| 845 } | 852 } |
| 846 | 853 |
| 854 bool TemplateURL::HasSearchTermsReplacementKey(const GURL& url) const { |
| 855 // Look for the key both in the query and the ref. |
| 856 std::string params[] = {url.query(), url.ref()}; |
| 857 |
| 858 for (int i = 0; i < 2; ++i) { |
| 859 url_parse::Component query, key, value; |
| 860 query.len = static_cast<int>(params[i].size()); |
| 861 while (url_parse::ExtractQueryKeyValue(params[i].c_str(), &query, &key, |
| 862 &value)) { |
| 863 if (key.is_nonempty() && |
| 864 params[i].substr(key.begin, key.len) == |
| 865 search_terms_replacement_key()) { |
| 866 return true; |
| 867 } |
| 868 } |
| 869 } |
| 870 return false; |
| 871 } |
| 872 |
| 847 void TemplateURL::CopyFrom(const TemplateURL& other) { | 873 void TemplateURL::CopyFrom(const TemplateURL& other) { |
| 848 if (this == &other) | 874 if (this == &other) |
| 849 return; | 875 return; |
| 850 | 876 |
| 851 profile_ = other.profile_; | 877 profile_ = other.profile_; |
| 852 data_ = other.data_; | 878 data_ = other.data_; |
| 853 url_ref_.InvalidateCachedValues(); | 879 url_ref_.InvalidateCachedValues(); |
| 854 suggestions_url_ref_.InvalidateCachedValues(); | 880 suggestions_url_ref_.InvalidateCachedValues(); |
| 855 instant_url_ref_.InvalidateCachedValues(); | 881 instant_url_ref_.InvalidateCachedValues(); |
| 856 SetPrepopulateId(other.data_.prepopulate_id); | 882 SetPrepopulateId(other.data_.prepopulate_id); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 870 } | 896 } |
| 871 | 897 |
| 872 void TemplateURL::ResetKeywordIfNecessary(bool force) { | 898 void TemplateURL::ResetKeywordIfNecessary(bool force) { |
| 873 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { | 899 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { |
| 874 DCHECK(!IsExtensionKeyword()); | 900 DCHECK(!IsExtensionKeyword()); |
| 875 GURL url(TemplateURLService::GenerateSearchURL(this)); | 901 GURL url(TemplateURLService::GenerateSearchURL(this)); |
| 876 if (url.is_valid()) | 902 if (url.is_valid()) |
| 877 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 903 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
| 878 } | 904 } |
| 879 } | 905 } |
| OLD | NEW |