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/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/autocomplete/autocomplete_field_trial.h" | 15 #include "chrome/browser/autocomplete/autocomplete_field_trial.h" |
16 #include "chrome/browser/google/google_util.h" | 16 #include "chrome/browser/google/google_util.h" |
17 #include "chrome/browser/search_engines/search_terms_data.h" | 17 #include "chrome/browser/search_engines/search_terms_data.h" |
18 #include "chrome/browser/search_engines/template_url_service.h" | 18 #include "chrome/browser/search_engines/template_url_service.h" |
19 #include "chrome/common/guid.h" | 19 #include "chrome/common/guid.h" |
20 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
21 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 | 23 |
| 24 namespace { |
| 25 |
24 // The TemplateURLRef has any number of terms that need to be replaced. Each of | 26 // The TemplateURLRef has any number of terms that need to be replaced. Each of |
25 // the terms is enclosed in braces. If the character preceeding the final | 27 // the terms is enclosed in braces. If the character preceeding the final |
26 // brace is a ?, it indicates the term is optional and can be replaced with | 28 // brace is a ?, it indicates the term is optional and can be replaced with |
27 // an empty string. | 29 // an empty string. |
28 static const char kStartParameter = '{'; | 30 const char kStartParameter = '{'; |
29 static const char kEndParameter = '}'; | 31 const char kEndParameter = '}'; |
30 static const char kOptional = '?'; | 32 const char kOptional = '?'; |
31 | 33 |
32 // Known parameters found in the URL. | 34 // Known parameters found in the URL. |
33 static const char kSearchTermsParameter[] = "searchTerms"; | 35 const char kSearchTermsParameter[] = "searchTerms"; |
34 static const char kSearchTermsParameterFull[] = "{searchTerms}"; | 36 const char kSearchTermsParameterFull[] = "{searchTerms}"; |
35 static const char kCountParameter[] = "count"; | 37 const char kCountParameter[] = "count"; |
36 static const char kStartIndexParameter[] = "startIndex"; | 38 const char kStartIndexParameter[] = "startIndex"; |
37 static const char kStartPageParameter[] = "startPage"; | 39 const char kStartPageParameter[] = "startPage"; |
38 static const char kLanguageParameter[] = "language"; | 40 const char kLanguageParameter[] = "language"; |
39 static const char kInputEncodingParameter[] = "inputEncoding"; | 41 const char kInputEncodingParameter[] = "inputEncoding"; |
40 static const char kOutputEncodingParameter[] = "outputEncoding"; | 42 const char kOutputEncodingParameter[] = "outputEncoding"; |
41 | 43 |
42 static const char kGoogleAcceptedSuggestionParameter[] = | 44 const char kGoogleAcceptedSuggestionParameter[] = "google:acceptedSuggestion"; |
43 "google:acceptedSuggestion"; | |
44 // Host/Domain Google searches are relative to. | 45 // Host/Domain Google searches are relative to. |
45 static const char kGoogleBaseURLParameter[] = "google:baseURL"; | 46 const char kGoogleBaseURLParameter[] = "google:baseURL"; |
46 static const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; | 47 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; |
47 // Like google:baseURL, but for the Search Suggest capability. | 48 // Like google:baseURL, but for the Search Suggest capability. |
48 static const char kGoogleBaseSuggestURLParameter[] = | 49 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL"; |
49 "google:baseSuggestURL"; | 50 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}"; |
50 static const char kGoogleBaseSuggestURLParameterFull[] = | 51 const char kGoogleInstantEnabledParameter[] = "google:instantEnabledParameter"; |
51 "{google:baseSuggestURL}"; | 52 const char kGoogleOriginalQueryForSuggestionParameter[] = |
52 static const char kGoogleInstantEnabledParameter[] = | |
53 "google:instantEnabledParameter"; | |
54 static const char kGoogleOriginalQueryForSuggestionParameter[] = | |
55 "google:originalQueryForSuggestion"; | 53 "google:originalQueryForSuggestion"; |
56 static const char kGoogleRLZParameter[] = "google:RLZ"; | 54 const char kGoogleRLZParameter[] = "google:RLZ"; |
57 // Same as kSearchTermsParameter, with no escaping. | 55 // Same as kSearchTermsParameter, with no escaping. |
58 static const char kGoogleSearchFieldtrialParameter[] = | 56 const char kGoogleSearchFieldtrialParameter[] = |
59 "google:searchFieldtrialParameter"; | 57 "google:searchFieldtrialParameter"; |
60 static const char kGoogleUnescapedSearchTermsParameter[] = | 58 const char kGoogleUnescapedSearchTermsParameter[] = |
61 "google:unescapedSearchTerms"; | 59 "google:unescapedSearchTerms"; |
62 static const char kGoogleUnescapedSearchTermsParameterFull[] = | 60 const char kGoogleUnescapedSearchTermsParameterFull[] = |
63 "{google:unescapedSearchTerms}"; | 61 "{google:unescapedSearchTerms}"; |
64 | 62 |
65 // Display value for kSearchTermsParameter. | 63 // Display value for kSearchTermsParameter. |
66 static const char kDisplaySearchTerms[] = "%s"; | 64 const char kDisplaySearchTerms[] = "%s"; |
67 | 65 |
68 // Display value for kGoogleUnescapedSearchTermsParameter. | 66 // Display value for kGoogleUnescapedSearchTermsParameter. |
69 static const char kDisplayUnescapedSearchTerms[] = "%S"; | 67 const char kDisplayUnescapedSearchTerms[] = "%S"; |
70 | 68 |
71 // Used if the count parameter is not optional. Indicates we want 10 search | 69 // Used if the count parameter is not optional. Indicates we want 10 search |
72 // results. | 70 // results. |
73 static const char kDefaultCount[] = "10"; | 71 const char kDefaultCount[] = "10"; |
74 | 72 |
75 // Used if the parameter kOutputEncodingParameter is required. | 73 // Used if the parameter kOutputEncodingParameter is required. |
76 static const char kOutputEncodingType[] = "UTF-8"; | 74 const char kOutputEncodingType[] = "UTF-8"; |
| 75 |
| 76 // Attempts to encode |terms| and |original_query| in |encoding| and escape |
| 77 // them. |terms| may be escaped as path or query depending on |is_in_query|; |
| 78 // |original_query| is always escaped as query. Returns whether the encoding |
| 79 // process succeeded. |
| 80 bool TryEncoding(const string16& terms, |
| 81 const string16& original_query, |
| 82 const char* encoding, |
| 83 bool is_in_query, |
| 84 string16* escaped_terms, |
| 85 string16* escaped_original_query) { |
| 86 DCHECK(escaped_terms); |
| 87 DCHECK(escaped_original_query); |
| 88 std::string encoded_terms; |
| 89 if (!base::UTF16ToCodepage(terms, encoding, |
| 90 base::OnStringConversionError::SKIP, &encoded_terms)) |
| 91 return false; |
| 92 *escaped_terms = UTF8ToUTF16(is_in_query ? |
| 93 net::EscapeQueryParamValue(encoded_terms, true) : |
| 94 net::EscapePath(encoded_terms)); |
| 95 if (original_query.empty()) |
| 96 return true; |
| 97 std::string encoded_original_query; |
| 98 if (!base::UTF16ToCodepage(original_query, encoding, |
| 99 base::OnStringConversionError::SKIP, &encoded_original_query)) |
| 100 return false; |
| 101 *escaped_original_query = |
| 102 UTF8ToUTF16(net::EscapeQueryParamValue(encoded_original_query, true)); |
| 103 return true; |
| 104 } |
| 105 |
| 106 } // namespace |
77 | 107 |
78 | 108 |
79 // TemplateURLRef ------------------------------------------------------------- | 109 // TemplateURLRef ------------------------------------------------------------- |
80 | 110 |
81 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) | 111 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) |
82 : owner_(owner), | 112 : owner_(owner), |
83 type_(type), | 113 type_(type), |
84 parsed_(false), | 114 parsed_(false), |
85 valid_(false), | 115 valid_(false), |
86 supports_replacements_(false), | 116 supports_replacements_(false), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 175 } |
146 } | 176 } |
147 | 177 |
148 string16 encoded_terms; | 178 string16 encoded_terms; |
149 string16 encoded_original_query; | 179 string16 encoded_original_query; |
150 std::string input_encoding; | 180 std::string input_encoding; |
151 // Encode the search terms so that we know the encoding. | 181 // Encode the search terms so that we know the encoding. |
152 for (std::vector<std::string>::const_iterator i( | 182 for (std::vector<std::string>::const_iterator i( |
153 owner_->input_encodings().begin()); | 183 owner_->input_encodings().begin()); |
154 i != owner_->input_encodings().end(); ++i) { | 184 i != owner_->input_encodings().end(); ++i) { |
155 if (net::EscapeQueryParamValue(terms, i->c_str(), is_in_query, | 185 if (TryEncoding(terms, original_query_for_suggestion, i->c_str(), |
156 &encoded_terms)) { | 186 is_in_query, &encoded_terms, &encoded_original_query)) { |
157 if (is_in_query && !original_query_for_suggestion.empty()) { | |
158 net::EscapeQueryParamValue(original_query_for_suggestion, i->c_str(), | |
159 true, &encoded_original_query); | |
160 } | |
161 input_encoding = *i; | 187 input_encoding = *i; |
162 break; | 188 break; |
163 } | 189 } |
164 } | 190 } |
165 if (input_encoding.empty()) { | 191 if (input_encoding.empty()) { |
166 encoded_terms = net::EscapeQueryParamValueUTF8(terms, is_in_query); | |
167 if (is_in_query && !original_query_for_suggestion.empty()) { | |
168 encoded_original_query = | |
169 net::EscapeQueryParamValueUTF8(original_query_for_suggestion, true); | |
170 } | |
171 input_encoding = "UTF-8"; | 192 input_encoding = "UTF-8"; |
| 193 if (!TryEncoding(terms, original_query_for_suggestion, |
| 194 input_encoding.c_str(), is_in_query, &encoded_terms, |
| 195 &encoded_original_query)) |
| 196 NOTREACHED(); |
172 } | 197 } |
173 | 198 |
174 std::string url = parsed_url_; | 199 std::string url = parsed_url_; |
175 | 200 |
176 // replacements_ is ordered in ascending order, as such we need to iterate | 201 // replacements_ is ordered in ascending order, as such we need to iterate |
177 // from the back. | 202 // from the back. |
178 for (Replacements::reverse_iterator i = replacements_.rbegin(); | 203 for (Replacements::reverse_iterator i = replacements_.rbegin(); |
179 i != replacements_.rend(); ++i) { | 204 i != replacements_.rend(); ++i) { |
180 switch (i->type) { | 205 switch (i->type) { |
181 case ENCODING: | 206 case ENCODING: |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 686 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
662 } | 687 } |
663 } | 688 } |
664 | 689 |
665 void TemplateURL::InvalidateCachedValues() { | 690 void TemplateURL::InvalidateCachedValues() { |
666 url_ref_.InvalidateCachedValues(); | 691 url_ref_.InvalidateCachedValues(); |
667 suggestions_url_ref_.InvalidateCachedValues(); | 692 suggestions_url_ref_.InvalidateCachedValues(); |
668 instant_url_ref_.InvalidateCachedValues(); | 693 instant_url_ref_.InvalidateCachedValues(); |
669 ResetKeywordIfNecessary(false); | 694 ResetKeywordIfNecessary(false); |
670 } | 695 } |
OLD | NEW |