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

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 TemplateUrlService not updating sync guid after Repair 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(
171 const std::string& search_terms_replacement_key1,
172 const std::string& search_terms_replacement_key2) {
173 if (search_terms_replacement_key1 == google_util::kInstantExtendedAPIParam &&
174 search_terms_replacement_key2 == kGoogleInstantExtendedEnabledKeyFull)
175 return true;
176 if (search_terms_replacement_key2 == google_util::kInstantExtendedAPIParam &&
177 search_terms_replacement_key1 == kGoogleInstantExtendedEnabledKeyFull)
178 return true;
179 return search_terms_replacement_key1 == search_terms_replacement_key2;
Peter Kasting 2017/02/02 00:04:26 Nit: This implementation is shorter and does the c
Alexander Yashkin 2017/02/03 14:44:25 Clever idea, done.
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 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start)); 617 Replacement(TemplateURLRef::GOOGLE_IMAGE_THUMBNAIL, start));
599 } else if (parameter == "google:imageURL") { 618 } else if (parameter == "google:imageURL") {
600 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL, 619 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_IMAGE_URL,
601 start)); 620 start));
602 } else if (parameter == "google:inputType") { 621 } else if (parameter == "google:inputType") {
603 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE, 622 replacements->push_back(Replacement(TemplateURLRef::GOOGLE_INPUT_TYPE,
604 start)); 623 start));
605 } else if (parameter == "google:instantExtendedEnabledParameter") { 624 } else if (parameter == "google:instantExtendedEnabledParameter") {
606 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, 625 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED,
607 start)); 626 start));
608 } else if (parameter == "google:instantExtendedEnabledKey") { 627 } else if (parameter == kGoogleInstantExtendedEnabledKey) {
609 url->insert(start, google_util::kInstantExtendedAPIParam); 628 url->insert(start, google_util::kInstantExtendedAPIParam);
610 } else if (parameter == "google:iOSSearchLanguage") { 629 } else if (parameter == "google:iOSSearchLanguage") {
611 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start)); 630 replacements->push_back(Replacement(GOOGLE_IOS_SEARCH_LANGUAGE, start));
612 } else if (parameter == "google:contextualSearchVersion") { 631 } else if (parameter == "google:contextualSearchVersion") {
613 replacements->push_back( 632 replacements->push_back(
614 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start)); 633 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start));
615 } else if (parameter == "google:contextualSearchContextData") { 634 } else if (parameter == "google:contextualSearchContextData") {
616 replacements->push_back( 635 replacements->push_back(
617 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start)); 636 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start));
618 } else if (parameter == "google:originalQueryForSuggestion") { 637 } else if (parameter == "google:originalQueryForSuggestion") {
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 instant_url_ref_(this, TemplateURLRef::INSTANT), 1171 instant_url_ref_(this, TemplateURLRef::INSTANT),
1153 image_url_ref_(this, TemplateURLRef::IMAGE), 1172 image_url_ref_(this, TemplateURLRef::IMAGE),
1154 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), 1173 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB),
1155 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH), 1174 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH),
1156 type_(type), 1175 type_(type),
1157 engine_type_(SEARCH_ENGINE_UNKNOWN) { 1176 engine_type_(SEARCH_ENGINE_UNKNOWN) {
1158 ResizeURLRefVector(); 1177 ResizeURLRefVector();
1159 SetPrepopulateId(data_.prepopulate_id); 1178 SetPrepopulateId(data_.prepopulate_id);
1160 1179
1161 if (data_.search_terms_replacement_key == 1180 if (data_.search_terms_replacement_key ==
1162 "{google:instantExtendedEnabledKey}") { 1181 kGoogleInstantExtendedEnabledKeyFull)
1163 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; 1182 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam;
1164 }
1165 } 1183 }
1166 1184
1167 TemplateURL::~TemplateURL() { 1185 TemplateURL::~TemplateURL() {
1168 } 1186 }
1169 1187
1170 // static 1188 // static
1171 base::string16 TemplateURL::GenerateKeyword(const GURL& url) { 1189 base::string16 TemplateURL::GenerateKeyword(const GURL& url) {
1172 DCHECK(url.is_valid()); 1190 DCHECK(url.is_valid());
1173 // Strip "www." off the front of the keyword; otherwise the keyword won't work 1191 // 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 . 1192 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
(...skipping 24 matching lines...) Expand all
1199 } 1217 }
1200 1218
1201 // static 1219 // static
1202 bool TemplateURL::MatchesData(const TemplateURL* t_url, 1220 bool TemplateURL::MatchesData(const TemplateURL* t_url,
1203 const TemplateURLData* data, 1221 const TemplateURLData* data,
1204 const SearchTermsData& search_terms_data) { 1222 const SearchTermsData& search_terms_data) {
1205 if (!t_url || !data) 1223 if (!t_url || !data)
1206 return !t_url && !data; 1224 return !t_url && !data;
1207 1225
1208 return (t_url->short_name() == data->short_name()) && 1226 return (t_url->short_name() == data->short_name()) &&
1209 t_url->HasSameKeywordAs(*data, search_terms_data) && 1227 t_url->HasSameKeywordAs(*data, search_terms_data) &&
1210 (t_url->url() == data->url()) && 1228 (t_url->url() == data->url()) &&
1211 (t_url->suggestions_url() == data->suggestions_url) && 1229 (t_url->suggestions_url() == data->suggestions_url) &&
1212 (t_url->instant_url() == data->instant_url) && 1230 (t_url->instant_url() == data->instant_url) &&
1213 (t_url->image_url() == data->image_url) && 1231 (t_url->image_url() == data->image_url) &&
1214 (t_url->new_tab_url() == data->new_tab_url) && 1232 (t_url->new_tab_url() == data->new_tab_url) &&
1215 (t_url->search_url_post_params() == data->search_url_post_params) && 1233 (t_url->search_url_post_params() == data->search_url_post_params) &&
1216 (t_url->suggestions_url_post_params() == 1234 (t_url->suggestions_url_post_params() ==
1217 data->suggestions_url_post_params) && 1235 data->suggestions_url_post_params) &&
1218 (t_url->instant_url_post_params() == data->instant_url_post_params) && 1236 (t_url->instant_url_post_params() == data->instant_url_post_params) &&
1219 (t_url->image_url_post_params() == data->image_url_post_params) && 1237 (t_url->image_url_post_params() == data->image_url_post_params) &&
1220 (t_url->favicon_url() == data->favicon_url) && 1238 (t_url->favicon_url() == data->favicon_url) &&
1221 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) && 1239 (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) &&
1222 (t_url->input_encodings() == data->input_encodings) && 1240 (t_url->input_encodings() == data->input_encodings) &&
1223 (t_url->alternate_urls() == data->alternate_urls) && 1241 (t_url->alternate_urls() == data->alternate_urls) &&
1224 (t_url->search_terms_replacement_key() == 1242 SearchTermsReplacementKeysMatch(t_url->search_terms_replacement_key(),
1225 data->search_terms_replacement_key); 1243 data->search_terms_replacement_key);
1226 } 1244 }
1227 1245
1228 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { 1246 base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const {
1229 base::string16 bidi_safe_short_name = data_.short_name(); 1247 base::string16 bidi_safe_short_name = data_.short_name();
1230 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name); 1248 base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name);
1231 return bidi_safe_short_name; 1249 return bidi_safe_short_name;
1232 } 1250 }
1233 1251
1234 bool TemplateURL::SupportsReplacement( 1252 bool TemplateURL::SupportsReplacement(
1235 const SearchTermsData& search_terms_data) const { 1253 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 1508 // patterns. This means that given patterns
1491 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], 1509 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1492 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would 1510 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1493 // return false. This is important for at least Google, where such URLs 1511 // return false. This is important for at least Google, where such URLs
1494 // are invalid. 1512 // are invalid.
1495 return !search_terms->empty(); 1513 return !search_terms->empty();
1496 } 1514 }
1497 } 1515 }
1498 return false; 1516 return false;
1499 } 1517 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698