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

Side by Side Diff: components/search_engines/template_url.cc

Issue 2659353002: Fix TemplateUrl::MatchesData comparison of search_terms_replacement_key (reland) (Closed)
Patch Set: Fixed after review round 3 Created 3 years, 10 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
OLDNEW
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
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 constexpr char kGoogleInstantExtendedEnabledKeyFull[] =
73 "{google:instantExtendedEnabledKey}";
74
70 // Attempts to encode |terms| and |original_query| in |encoding| and escape 75 // 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|; 76 // 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 77 // |original_query| is always escaped as query. Returns whether the encoding
73 // process succeeded. 78 // process succeeded.
74 bool TryEncoding(const base::string16& terms, 79 bool TryEncoding(const base::string16& terms,
75 const base::string16& original_query, 80 const base::string16& original_query,
76 const char* encoding, 81 const char* encoding,
77 bool is_in_query, 82 bool is_in_query,
78 base::string16* escaped_terms, 83 base::string16* escaped_terms,
79 base::string16* escaped_original_query) { 84 base::string16* escaped_original_query) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 parameter_position->begin = begin; 158 parameter_position->begin = begin;
154 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1; 159 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1;
155 return true; 160 return true;
156 } 161 }
157 162
158 bool IsTemplateParameterString(const std::string& param) { 163 bool IsTemplateParameterString(const std::string& param) {
159 return (param.length() > 2) && (*(param.begin()) == kStartParameter) && 164 return (param.length() > 2) && (*(param.begin()) == kStartParameter) &&
160 (*(param.rbegin()) == kEndParameter); 165 (*(param.rbegin()) == kEndParameter);
161 } 166 }
162 167
168 // Special case for search_terms_replacement_key comparison, because of
169 // its special initialization in TemplateUrl constructor.
170 bool SearchTermsReplacementKeysMatch(const std::string& key1,
171 const std::string& key2) {
172 const auto IsInstantExtended = [](const std::string& key) {
173 return (key == google_util::kInstantExtendedAPIParam) ||
174 (key == kGoogleInstantExtendedEnabledKeyFull);
175 };
176 return (key1 == key2) || (IsInstantExtended(key1) && IsInstantExtended(key2));
177 }
178
163 } // namespace 179 } // namespace
164 180
165 181
166 // TemplateURLRef::SearchTermsArgs -------------------------------------------- 182 // TemplateURLRef::SearchTermsArgs --------------------------------------------
167 183
168 TemplateURLRef::SearchTermsArgs::SearchTermsArgs( 184 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
169 const base::string16& search_terms) 185 const base::string16& search_terms)
170 : search_terms(search_terms), 186 : search_terms(search_terms),
171 input_type(metrics::OmniboxInputType::INVALID), 187 input_type(metrics::OmniboxInputType::INVALID),
172 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), 188 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE),
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start)); 614 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
599 } else if (parameter == "google:imageURL") { 615 } else if (parameter == "google:imageURL") {
600 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL, 616 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
601 start)); 617 start));
602 } else if (parameter == "google:inputType") { 618 } else if (parameter == "google:inputType") {
603 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE, 619 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE,
604 start)); 620 start));
605 } else if (parameter == "google:instantExtendedEnabledParameter") { 621 } else if (parameter == "google:instantExtendedEnabledParameter") {
606 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, 622 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
607 start)); 623 start));
608 } else if (parameter == "google:instantExtendedEnabledKey") { 624 } else if (parameter == kGoogleInstantExtendedEnabledKey) {
609 url->insert(start, google_util::kInstantExtendedAPIParam); 625 url->insert(start, google_util::kInstantExtendedAPIParam);
610 } else if (parameter == "google:iOSSearchLanguage") { 626 } else if (parameter == "google:iOSSearchLanguage") {
611 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start)); 627 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start));
612 } else if (parameter == "google:contextualSearchVersion") { 628 } else if (parameter == "google:contextualSearchVersion") {
613 replacements->push_back( 629 replacements->push_back(
614 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start)); 630 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start));
615 } else if (parameter == "google:contextualSearchContextData") { 631 } else if (parameter == "google:contextualSearchContextData") {
616 replacements->push_back( 632 replacements->push_back(
617 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start)); 633 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start));
618 } else if (parameter == "google:originalQueryForSuggestion") { 634 } else if (parameter == "google:originalQueryForSuggestion") {
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 instant_url_ref_(this, TemplateURLRef::INSTANT), 1168 instant_url_ref_(this, TemplateURLRef::INSTANT),
1153 image_url_ref_(this, TemplateURLRef::IMAGE), 1169 image_url_ref_(this, TemplateURLRef::IMAGE),
1154 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), 1170 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB),
1155 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH), 1171 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH),
1156 type_(type), 1172 type_(type),
1157 engine_type_(SEARCH_ENGINE_UNKNOWN) { 1173 engine_type_(SEARCH_ENGINE_UNKNOWN) {
1158 ResizeURLRefVector(); 1174 ResizeURLRefVector();
1159 SetPrepopulateId(data_.prepopulate_id); 1175 SetPrepopulateId(data_.prepopulate_id);
1160 1176
1161 if (data_.search_terms_replacement_key == 1177 if (data_.search_terms_replacement_key ==
1162 "{google:instantExtendedEnabledKey}") { 1178 kGoogleInstantExtendedEnabledKeyFull)
1163 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; 1179 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1164 }
1165 } 1180 }
1166 1181
1167 TemplateURL::~TemplateURL() { 1182 TemplateURL::~TemplateURL() {
1168 } 1183 }
1169 1184
1170 // static 1185 // static
1171 base::string16 TemplateURL::GenerateKeyword(const GURL& url) { 1186 base::string16 TemplateURL::GenerateKeyword(const GURL& url) {
1172 DCHECK(url.is_valid()); 1187 DCHECK(url.is_valid());
1173 // Strip "www." off the front of the keyword; otherwise the keyword won't work 1188 // Strip "www." off the front of the keyword; otherwise the keyword won't work
1174 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . 1189 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
(...skipping 24 matching lines...) Expand all
1199 } 1214 }
1200 1215
1201 // static 1216 // static
1202 bool TemplateURL::MatchesData(const TemplateURL* t_url, 1217 bool TemplateURL::MatchesData(const TemplateURL* t_url,
1203 const TemplateURLData* data, 1218 const TemplateURLData* data,
1204 const SearchTermsData& search_terms_data) { 1219 const SearchTermsData& search_terms_data) {
1205 if (!t_url || !data) 1220 if (!t_url || !data)
1206 return !t_url && !data; 1221 return !t_url && !data;
1207 1222
1208 return (t_url->short_name() == data->short_name()) && 1223 return (t_url->short_name() == data->short_name()) &&
1209 t_url->HasSameKeywordAs(*data, search_terms_data) && 1224 t_url->HasSameKeywordAs(*data, search_terms_data) &&
1210 (t_url->url() == data->url()) && 1225 (t_url->url() == data->url()) &&
1211 (t_url->suggestions_url() == data->suggestions_url) && 1226 (t_url->suggestions_url() == data->suggestions_url) &&
1212 (t_url->instant_url() == data->instant_url) && 1227 (t_url->instant_url() == data->instant_url) &&
1213 (t_url->image_url() == data->image_url) && 1228 (t_url->image_url() == data->image_url) &&
1214 (t_url->new_tab_url() == data->new_tab_url) && 1229 (t_url->new_tab_url() == data->new_tab_url) &&
1215 (t_url->search_url_post_params() == data->search_url_post_params) && 1230 (t_url->search_url_post_params() == data->search_url_post_params) &&
1216 (t_url->suggestions_url_post_params() == 1231 (t_url->suggestions_url_post_params() ==
1217 data->suggestions_url_post_params) && 1232 data->suggestions_url_post_params) &&
1218 (t_url->instant_url_post_params() == data->instant_url_post_params) && 1233 (t_url->instant_url_post_params() == data->instant_url_post_params) &&
1219 (t_url->image_url_post_params() == data->image_url_post_params) && 1234 (t_url->image_url_post_params() == data->image_url_post_params) &&
1220 (t_url->favicon_url() == data->favicon_url) && 1235 (t_url->favicon_url() == data->favicon_url) &&
1221 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) && 1236 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) &&
1222 (t_url->input_encodings() == data->input_encodings) && 1237 (t_url->input_encodings() == data->input_encodings) &&
1223 (t_url->alternate_urls() == data->alternate_urls) && 1238 (t_url->alternate_urls() == data->alternate_urls) &&
1224 (t_url->search_terms_replacement_key() == 1239 SearchTermsReplacementKeysMatch(t_url->search_terms_replacement_key(),
1225 data->search_terms_replacement_key); 1240 data->search_terms_replacement_key);
1226 } 1241 }
1227 1242
1228 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { 1243 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
1229 base::string16 bidi_safe_short_name = data_.short_name(); 1244 base::string16 bidi_safe_short_name = data_.short_name();
1230 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name); 1245 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
1231 return bidi_safe_short_name; 1246 return bidi_safe_short_name;
1232 } 1247 }
1233 1248
1234 bool TemplateURL::SupportsReplacement( 1249 bool TemplateURL::SupportsReplacement(
1235 const SearchTermsData& search_terms_data) const { 1250 const SearchTermsData& search_terms_data) const {
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 // patterns. This means that given patterns 1505 // patterns. This means that given patterns
1491 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], 1506 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1492 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would 1507 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1493 // return false. This is important for at least Google, where such URLs 1508 // return false. This is important for at least Google, where such URLs
1494 // are invalid. 1509 // are invalid.
1495 return !search_terms->empty(); 1510 return !search_terms->empty();
1496 } 1511 }
1497 } 1512 }
1498 return false; 1513 return false;
1499 } 1514 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698