Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url.h" | 5 #include "components/search_engines/template_url.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // Display value for kGoogleUnescapedSearchTermsParameter. | 60 // Display value for kGoogleUnescapedSearchTermsParameter. |
| 61 const char kDisplayUnescapedSearchTerms[] = "%S"; | 61 const char kDisplayUnescapedSearchTerms[] = "%S"; |
| 62 | 62 |
| 63 // Used if the count parameter is not optional. Indicates we want 10 search | 63 // Used if the count parameter is not optional. Indicates we want 10 search |
| 64 // results. | 64 // results. |
| 65 const char kDefaultCount[] = "10"; | 65 const char kDefaultCount[] = "10"; |
| 66 | 66 |
| 67 // Used if the output encoding parameter is required. | 67 // Used if the output encoding parameter is required. |
| 68 const char kOutputEncodingType[] = "UTF-8"; | 68 const char kOutputEncodingType[] = "UTF-8"; |
| 69 | 69 |
| 70 constexpr char kGoogleInstantExtendedEnabledKey[] = | |
| 71 "{google:instantExtendedEnabledKey}"; | |
| 72 | |
| 70 // Attempts to encode |terms| and |original_query| in |encoding| and escape | 73 // Attempts to encode |terms| and |original_query| in |encoding| and escape |
| 71 // them. |terms| may be escaped as path or query depending on |is_in_query|; | 74 // them. |terms| may be escaped as path or query depending on |is_in_query|; |
| 72 // |original_query| is always escaped as query. Returns whether the encoding | 75 // |original_query| is always escaped as query. Returns whether the encoding |
| 73 // process succeeded. | 76 // process succeeded. |
| 74 bool TryEncoding(const base::string16& terms, | 77 bool TryEncoding(const base::string16& terms, |
| 75 const base::string16& original_query, | 78 const base::string16& original_query, |
| 76 const char* encoding, | 79 const char* encoding, |
| 77 bool is_in_query, | 80 bool is_in_query, |
| 78 base::string16* escaped_terms, | 81 base::string16* escaped_terms, |
| 79 base::string16* escaped_original_query) { | 82 base::string16* escaped_original_query) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 parameter_position->begin = begin; | 156 parameter_position->begin = begin; |
| 154 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1; | 157 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1; |
| 155 return true; | 158 return true; |
| 156 } | 159 } |
| 157 | 160 |
| 158 bool IsTemplateParameterString(const std::string& param) { | 161 bool IsTemplateParameterString(const std::string& param) { |
| 159 return (param.length() > 2) && (*(param.begin()) == kStartParameter) && | 162 return (param.length() > 2) && (*(param.begin()) == kStartParameter) && |
| 160 (*(param.rbegin()) == kEndParameter); | 163 (*(param.rbegin()) == kEndParameter); |
| 161 } | 164 } |
| 162 | 165 |
| 166 // Special case for search_terms_replacement_key comparison, because of | |
| 167 // its special initialization in TemplateUrl constructor. | |
| 168 bool SearchTermsReplacementKeysMatch( | |
| 169 const std::string& search_terms_replacement_key1, | |
| 170 const std::string& search_terms_replacement_key2) { | |
| 171 if (search_terms_replacement_key1 == search_terms_replacement_key2) | |
| 172 return true; | |
| 173 if (search_terms_replacement_key1 == google_util::kInstantExtendedAPIParam && | |
| 174 search_terms_replacement_key2 == kGoogleInstantExtendedEnabledKey) | |
| 175 return true; | |
| 176 if (search_terms_replacement_key2 == google_util::kInstantExtendedAPIParam && | |
| 177 search_terms_replacement_key1 == kGoogleInstantExtendedEnabledKey) | |
| 178 return true; | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 163 } // namespace | 182 } // namespace |
| 164 | 183 |
| 165 | 184 |
| 166 // TemplateURLRef::SearchTermsArgs -------------------------------------------- | 185 // TemplateURLRef::SearchTermsArgs -------------------------------------------- |
| 167 | 186 |
| 168 TemplateURLRef::SearchTermsArgs::SearchTermsArgs( | 187 TemplateURLRef::SearchTermsArgs::SearchTermsArgs( |
| 169 const base::string16& search_terms) | 188 const base::string16& search_terms) |
| 170 : search_terms(search_terms), | 189 : search_terms(search_terms), |
| 171 input_type(metrics::OmniboxInputType::INVALID), | 190 input_type(metrics::OmniboxInputType::INVALID), |
| 172 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), | 191 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 622 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start)); | 641 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start)); |
| 623 } else if (parameter == "google:imageURL") { | 642 } else if (parameter == "google:imageURL") { |
| 624 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL, | 643 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL, |
| 625 start)); | 644 start)); |
| 626 } else if (parameter == "google:inputType") { | 645 } else if (parameter == "google:inputType") { |
| 627 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE, | 646 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE, |
| 628 start)); | 647 start)); |
| 629 } else if (parameter == "google:instantExtendedEnabledParameter") { | 648 } else if (parameter == "google:instantExtendedEnabledParameter") { |
| 630 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, | 649 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, |
| 631 start)); | 650 start)); |
| 632 } else if (parameter == "google:instantExtendedEnabledKey") { | 651 } else if (parameter == kGoogleInstantExtendedEnabledKey) { |
|
Marc Treib
2016/11/23 10:27:43
Post-commit drive-by: This used to check for "goog
Alexander Yashkin
2016/11/23 10:41:51
Not intended, i'll fix it today.
| |
| 633 url->insert(start, google_util::kInstantExtendedAPIParam); | 652 url->insert(start, google_util::kInstantExtendedAPIParam); |
| 634 } else if (parameter == "google:iOSSearchLanguage") { | 653 } else if (parameter == "google:iOSSearchLanguage") { |
| 635 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start)); | 654 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start)); |
| 636 } else if (parameter == "google:contextualSearchVersion") { | 655 } else if (parameter == "google:contextualSearchVersion") { |
| 637 replacements->push_back( | 656 replacements->push_back( |
| 638 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start)); | 657 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start)); |
| 639 } else if (parameter == "google:contextualSearchContextData") { | 658 } else if (parameter == "google:contextualSearchContextData") { |
| 640 replacements->push_back( | 659 replacements->push_back( |
| 641 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start)); | 660 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start)); |
| 642 } else if (parameter == "google:originalQueryForSuggestion") { | 661 } else if (parameter == "google:originalQueryForSuggestion") { |
| (...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1185 suggestions_url_ref_(this, TemplateURLRef::SUGGEST), | 1204 suggestions_url_ref_(this, TemplateURLRef::SUGGEST), |
| 1186 instant_url_ref_(this, TemplateURLRef::INSTANT), | 1205 instant_url_ref_(this, TemplateURLRef::INSTANT), |
| 1187 image_url_ref_(this, TemplateURLRef::IMAGE), | 1206 image_url_ref_(this, TemplateURLRef::IMAGE), |
| 1188 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), | 1207 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), |
| 1189 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH), | 1208 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH), |
| 1190 type_(type), | 1209 type_(type), |
| 1191 engine_type_(SEARCH_ENGINE_UNKNOWN) { | 1210 engine_type_(SEARCH_ENGINE_UNKNOWN) { |
| 1192 ResizeURLRefVector(); | 1211 ResizeURLRefVector(); |
| 1193 SetPrepopulateId(data_.prepopulate_id); | 1212 SetPrepopulateId(data_.prepopulate_id); |
| 1194 | 1213 |
| 1195 if (data_.search_terms_replacement_key == | 1214 if (data_.search_terms_replacement_key == kGoogleInstantExtendedEnabledKey) |
| 1196 "{google:instantExtendedEnabledKey}") { | |
| 1197 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; | 1215 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; |
| 1198 } | |
| 1199 } | 1216 } |
| 1200 | 1217 |
| 1201 TemplateURL::~TemplateURL() { | 1218 TemplateURL::~TemplateURL() { |
| 1202 } | 1219 } |
| 1203 | 1220 |
| 1204 // static | 1221 // static |
| 1205 base::string16 TemplateURL::GenerateKeyword(const GURL& url) { | 1222 base::string16 TemplateURL::GenerateKeyword(const GURL& url) { |
| 1206 DCHECK(url.is_valid()); | 1223 DCHECK(url.is_valid()); |
| 1207 // Strip "www." off the front of the keyword; otherwise the keyword won't work | 1224 // Strip "www." off the front of the keyword; otherwise the keyword won't work |
| 1208 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . | 1225 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1233 } | 1250 } |
| 1234 | 1251 |
| 1235 // static | 1252 // static |
| 1236 bool TemplateURL::MatchesData(const TemplateURL* t_url, | 1253 bool TemplateURL::MatchesData(const TemplateURL* t_url, |
| 1237 const TemplateURLData* data, | 1254 const TemplateURLData* data, |
| 1238 const SearchTermsData& search_terms_data) { | 1255 const SearchTermsData& search_terms_data) { |
| 1239 if (!t_url || !data) | 1256 if (!t_url || !data) |
| 1240 return !t_url && !data; | 1257 return !t_url && !data; |
| 1241 | 1258 |
| 1242 return (t_url->short_name() == data->short_name()) && | 1259 return (t_url->short_name() == data->short_name()) && |
| 1243 t_url->HasSameKeywordAs(*data, search_terms_data) && | 1260 t_url->HasSameKeywordAs(*data, search_terms_data) && |
| 1244 (t_url->url() == data->url()) && | 1261 (t_url->url() == data->url()) && |
| 1245 (t_url->suggestions_url() == data->suggestions_url) && | 1262 (t_url->suggestions_url() == data->suggestions_url) && |
| 1246 (t_url->instant_url() == data->instant_url) && | 1263 (t_url->instant_url() == data->instant_url) && |
| 1247 (t_url->image_url() == data->image_url) && | 1264 (t_url->image_url() == data->image_url) && |
| 1248 (t_url->new_tab_url() == data->new_tab_url) && | 1265 (t_url->new_tab_url() == data->new_tab_url) && |
| 1249 (t_url->search_url_post_params() == data->search_url_post_params) && | 1266 (t_url->search_url_post_params() == data->search_url_post_params) && |
| 1250 (t_url->suggestions_url_post_params() == | 1267 (t_url->suggestions_url_post_params() == |
| 1251 data->suggestions_url_post_params) && | 1268 data->suggestions_url_post_params) && |
| 1252 (t_url->instant_url_post_params() == data->instant_url_post_params) && | 1269 (t_url->instant_url_post_params() == data->instant_url_post_params) && |
| 1253 (t_url->image_url_post_params() == data->image_url_post_params) && | 1270 (t_url->image_url_post_params() == data->image_url_post_params) && |
| 1254 (t_url->favicon_url() == data->favicon_url) && | 1271 (t_url->favicon_url() == data->favicon_url) && |
| 1255 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) && | 1272 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) && |
| 1256 (t_url->input_encodings() == data->input_encodings) && | 1273 (t_url->input_encodings() == data->input_encodings) && |
| 1257 (t_url->alternate_urls() == data->alternate_urls) && | 1274 (t_url->alternate_urls() == data->alternate_urls) && |
| 1258 (t_url->search_terms_replacement_key() == | 1275 SearchTermsReplacementKeysMatch(t_url->search_terms_replacement_key(), |
| 1259 data->search_terms_replacement_key); | 1276 data->search_terms_replacement_key); |
| 1260 } | 1277 } |
| 1261 | 1278 |
| 1262 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { | 1279 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { |
| 1263 base::string16 bidi_safe_short_name = data_.short_name(); | 1280 base::string16 bidi_safe_short_name = data_.short_name(); |
| 1264 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name); | 1281 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name); |
| 1265 return bidi_safe_short_name; | 1282 return bidi_safe_short_name; |
| 1266 } | 1283 } |
| 1267 | 1284 |
| 1268 bool TemplateURL::SupportsReplacement( | 1285 bool TemplateURL::SupportsReplacement( |
| 1269 const SearchTermsData& search_terms_data) const { | 1286 const SearchTermsData& search_terms_data) const { |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1524 // patterns. This means that given patterns | 1541 // patterns. This means that given patterns |
| 1525 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1542 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1526 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1543 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1527 // return false. This is important for at least Google, where such URLs | 1544 // return false. This is important for at least Google, where such URLs |
| 1528 // are invalid. | 1545 // are invalid. |
| 1529 return !search_terms->empty(); | 1546 return !search_terms->empty(); |
| 1530 } | 1547 } |
| 1531 } | 1548 } |
| 1532 return false; | 1549 return false; |
| 1533 } | 1550 } |
| OLD | NEW |