Chromium Code Reviews| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 "google:unescapedSearchTerms"; | 95 "google:unescapedSearchTerms"; |
| 96 const char kGoogleUnescapedSearchTermsParameterFull[] = | 96 const char kGoogleUnescapedSearchTermsParameterFull[] = |
| 97 "{google:unescapedSearchTerms}"; | 97 "{google:unescapedSearchTerms}"; |
| 98 | 98 |
| 99 const char kGoogleImageSearchSource[] = "google:imageSearchSource"; | 99 const char kGoogleImageSearchSource[] = "google:imageSearchSource"; |
| 100 const char kGoogleImageThumbnailParameter[] = "google:imageThumbnail"; | 100 const char kGoogleImageThumbnailParameter[] = "google:imageThumbnail"; |
| 101 const char kGoogleImageURLParameter[] = "google:imageURL"; | 101 const char kGoogleImageURLParameter[] = "google:imageURL"; |
| 102 const char kGoogleImageOriginalWidth[] = "google:imageOriginalWidth"; | 102 const char kGoogleImageOriginalWidth[] = "google:imageOriginalWidth"; |
| 103 const char kGoogleImageOriginalHeight[] = "google:imageOriginalHeight"; | 103 const char kGoogleImageOriginalHeight[] = "google:imageOriginalHeight"; |
| 104 | 104 |
| 105 // Contextual search parameters. | |
|
Peter Kasting
2014/06/06 17:51:33
Nit: It seems like these and the image search cons
jeremycho
2014/06/06 21:51:45
Done.
| |
| 106 const char kGoogleContextualSearchVersion[] = "google:contextualSearchVersion"; | |
| 107 const char kGoogleContextualSearchContextData[] = | |
| 108 "google:contextualSearchContextData"; | |
| 109 | |
| 105 // Display value for kSearchTermsParameter. | 110 // Display value for kSearchTermsParameter. |
| 106 const char kDisplaySearchTerms[] = "%s"; | 111 const char kDisplaySearchTerms[] = "%s"; |
| 107 | 112 |
| 108 // Display value for kGoogleUnescapedSearchTermsParameter. | 113 // Display value for kGoogleUnescapedSearchTermsParameter. |
| 109 const char kDisplayUnescapedSearchTerms[] = "%S"; | 114 const char kDisplayUnescapedSearchTerms[] = "%S"; |
| 110 | 115 |
| 111 // Used if the count parameter is not optional. Indicates we want 10 search | 116 // Used if the count parameter is not optional. Indicates we want 10 search |
| 112 // results. | 117 // results. |
| 113 const char kDefaultCount[] = "10"; | 118 const char kDefaultCount[] = "10"; |
| 114 | 119 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 TemplateURLRef::SearchTermsArgs::SearchTermsArgs( | 206 TemplateURLRef::SearchTermsArgs::SearchTermsArgs( |
| 202 const base::string16& search_terms) | 207 const base::string16& search_terms) |
| 203 : search_terms(search_terms), | 208 : search_terms(search_terms), |
| 204 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), | 209 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), |
| 205 cursor_position(base::string16::npos), | 210 cursor_position(base::string16::npos), |
| 206 omnibox_start_margin(-1), | 211 omnibox_start_margin(-1), |
| 207 page_classification(AutocompleteInput::INVALID_SPEC), | 212 page_classification(AutocompleteInput::INVALID_SPEC), |
| 208 bookmark_bar_pinned(false), | 213 bookmark_bar_pinned(false), |
| 209 append_extra_query_params(false), | 214 append_extra_query_params(false), |
| 210 force_instant_results(false), | 215 force_instant_results(false), |
| 211 from_app_list(false) { | 216 from_app_list(false), |
| 217 contextual_search_params(ContextualSearchParams()) { | |
| 212 } | 218 } |
| 213 | 219 |
| 214 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() { | 220 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() { |
| 215 } | 221 } |
| 216 | 222 |
| 223 TemplateURLRef::SearchTermsArgs::ContextualSearchParams:: | |
| 224 ContextualSearchParams() | |
| 225 : version(-1), | |
| 226 start(base::string16::npos), | |
| 227 end(base::string16::npos) { | |
| 228 } | |
| 217 | 229 |
| 218 // TemplateURLRef ------------------------------------------------------------- | 230 // TemplateURLRef ------------------------------------------------------------- |
| 219 | 231 |
| 220 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) | 232 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) |
| 221 : owner_(owner), | 233 : owner_(owner), |
| 222 type_(type), | 234 type_(type), |
| 223 index_in_owner_(-1), | 235 index_in_owner_(-1), |
| 224 parsed_(false), | 236 parsed_(false), |
| 225 valid_(false), | 237 valid_(false), |
| 226 supports_replacements_(false), | 238 supports_replacements_(false), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 243 showing_search_terms_(ShowingSearchTermsOnSRP()) { | 255 showing_search_terms_(ShowingSearchTermsOnSRP()) { |
| 244 DCHECK(owner_); | 256 DCHECK(owner_); |
| 245 DCHECK_LT(index_in_owner_, owner_->URLCount()); | 257 DCHECK_LT(index_in_owner_, owner_->URLCount()); |
| 246 } | 258 } |
| 247 | 259 |
| 248 TemplateURLRef::~TemplateURLRef() { | 260 TemplateURLRef::~TemplateURLRef() { |
| 249 } | 261 } |
| 250 | 262 |
| 251 std::string TemplateURLRef::GetURL() const { | 263 std::string TemplateURLRef::GetURL() const { |
| 252 switch (type_) { | 264 switch (type_) { |
| 253 case SEARCH: return owner_->url(); | 265 case SEARCH: return owner_->url(); |
| 254 case SUGGEST: return owner_->suggestions_url(); | 266 case SUGGEST: return owner_->suggestions_url(); |
| 255 case INSTANT: return owner_->instant_url(); | 267 case INSTANT: return owner_->instant_url(); |
| 256 case IMAGE: return owner_->image_url(); | 268 case IMAGE: return owner_->image_url(); |
| 257 case NEW_TAB: return owner_->new_tab_url(); | 269 case NEW_TAB: return owner_->new_tab_url(); |
| 258 case INDEXED: return owner_->GetURL(index_in_owner_); | 270 case CONTEXTUAL_SEARCH: return owner_->contextual_search_url(); |
| 259 default: NOTREACHED(); return std::string(); // NOLINT | 271 case INDEXED: return owner_->GetURL(index_in_owner_); |
| 272 default: NOTREACHED(); return std::string(); // NOLINT | |
| 260 } | 273 } |
| 261 } | 274 } |
| 262 | 275 |
| 263 std::string TemplateURLRef::GetPostParamsString() const { | 276 std::string TemplateURLRef::GetPostParamsString() const { |
| 264 switch (type_) { | 277 switch (type_) { |
| 265 case INDEXED: | 278 case INDEXED: |
| 266 case SEARCH: return owner_->search_url_post_params(); | 279 case SEARCH: return owner_->search_url_post_params(); |
| 267 case SUGGEST: return owner_->suggestions_url_post_params(); | 280 case SUGGEST: return owner_->suggestions_url_post_params(); |
| 268 case INSTANT: return owner_->instant_url_post_params(); | 281 case INSTANT: return owner_->instant_url_post_params(); |
| 269 case NEW_TAB: return std::string(); | 282 case NEW_TAB: return std::string(); |
| 270 case IMAGE: return owner_->image_url_post_params(); | 283 case CONTEXTUAL_SEARCH: return std::string(); |
| 284 case IMAGE: return owner_->image_url_post_params(); | |
| 271 default: NOTREACHED(); return std::string(); // NOLINT | 285 default: NOTREACHED(); return std::string(); // NOLINT |
| 272 } | 286 } |
| 273 } | 287 } |
| 274 | 288 |
| 275 bool TemplateURLRef::UsesPOSTMethodUsingTermsData( | 289 bool TemplateURLRef::UsesPOSTMethodUsingTermsData( |
| 276 const SearchTermsData* search_terms_data) const { | 290 const SearchTermsData* search_terms_data) const { |
| 277 if (search_terms_data) | 291 if (search_terms_data) |
| 278 ParseIfNecessaryUsingTermsData(*search_terms_data); | 292 ParseIfNecessaryUsingTermsData(*search_terms_data); |
| 279 else | 293 else |
| 280 ParseIfNecessary(); | 294 ParseIfNecessary(); |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 584 replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start)); | 598 replacements->push_back(Replacement(GOOGLE_FORCE_INSTANT_RESULTS, start)); |
| 585 } else if (parameter == kGoogleInstantExtendedEnabledParameter) { | 599 } else if (parameter == kGoogleInstantExtendedEnabledParameter) { |
| 586 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, | 600 replacements->push_back(Replacement(GOOGLE_INSTANT_EXTENDED_ENABLED, |
| 587 start)); | 601 start)); |
| 588 } else if (parameter == kGoogleInstantExtendedEnabledKey) { | 602 } else if (parameter == kGoogleInstantExtendedEnabledKey) { |
| 589 url->insert(start, google_util::kInstantExtendedAPIParam); | 603 url->insert(start, google_util::kInstantExtendedAPIParam); |
| 590 } else if (parameter == kGoogleNTPIsThemedParameter) { | 604 } else if (parameter == kGoogleNTPIsThemedParameter) { |
| 591 replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start)); | 605 replacements->push_back(Replacement(GOOGLE_NTP_IS_THEMED, start)); |
| 592 } else if (parameter == kGoogleOmniboxStartMarginParameter) { | 606 } else if (parameter == kGoogleOmniboxStartMarginParameter) { |
| 593 replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start)); | 607 replacements->push_back(Replacement(GOOGLE_OMNIBOX_START_MARGIN, start)); |
| 608 } else if (parameter == kGoogleContextualSearchVersion) { | |
| 609 replacements->push_back( | |
| 610 Replacement(GOOGLE_CONTEXTUAL_SEARCH_VERSION, start)); | |
| 611 } else if (parameter == kGoogleContextualSearchContextData) { | |
| 612 replacements->push_back( | |
| 613 Replacement(GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA, start)); | |
| 594 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) { | 614 } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) { |
| 595 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, | 615 replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, |
| 596 start)); | 616 start)); |
| 597 } else if (parameter == kGooglePageClassificationParameter) { | 617 } else if (parameter == kGooglePageClassificationParameter) { |
| 598 replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start)); | 618 replacements->push_back(Replacement(GOOGLE_PAGE_CLASSIFICATION, start)); |
| 599 } else if (parameter == kGoogleRLZParameter) { | 619 } else if (parameter == kGoogleRLZParameter) { |
| 600 replacements->push_back(Replacement(GOOGLE_RLZ, start)); | 620 replacements->push_back(Replacement(GOOGLE_RLZ, start)); |
| 601 } else if (parameter == kGoogleSearchClient) { | 621 } else if (parameter == kGoogleSearchClient) { |
| 602 replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start)); | 622 replacements->push_back(Replacement(GOOGLE_SEARCH_CLIENT, start)); |
| 603 } else if (parameter == kGoogleSearchFieldtrialParameter) { | 623 } else if (parameter == kGoogleSearchFieldtrialParameter) { |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 920 DCHECK(!i->is_post_param); | 940 DCHECK(!i->is_post_param); |
| 921 if (search_terms_args.omnibox_start_margin >= 0) { | 941 if (search_terms_args.omnibox_start_margin >= 0) { |
| 922 HandleReplacement( | 942 HandleReplacement( |
| 923 "es_sm", | 943 "es_sm", |
| 924 base::IntToString(search_terms_args.omnibox_start_margin), | 944 base::IntToString(search_terms_args.omnibox_start_margin), |
| 925 *i, | 945 *i, |
| 926 &url); | 946 &url); |
| 927 } | 947 } |
| 928 break; | 948 break; |
| 929 | 949 |
| 950 case GOOGLE_CONTEXTUAL_SEARCH_VERSION: | |
| 951 if (search_terms_args.contextual_search_params.version >= 0) { | |
| 952 HandleReplacement( | |
| 953 "ctxs", | |
| 954 base::IntToString( | |
| 955 search_terms_args.contextual_search_params.version), | |
| 956 *i, | |
| 957 &url); | |
| 958 } | |
| 959 break; | |
| 960 | |
| 961 case GOOGLE_CONTEXTUAL_SEARCH_CONTEXT_DATA: { | |
| 962 DCHECK(!i->is_post_param); | |
| 963 std::string context_data; | |
| 964 | |
| 965 if (search_terms_args.contextual_search_params.start != | |
|
Peter Kasting
2014/06/06 17:51:33
Nit: If above all these you do something like this
jeremycho
2014/06/06 21:51:45
Done.
| |
| 966 std::string::npos) { | |
| 967 context_data.append("ctxs_start=" + base::IntToString( | |
| 968 search_terms_args.contextual_search_params.start) + "&"); | |
| 969 } | |
| 970 | |
| 971 if (search_terms_args.contextual_search_params.end != | |
| 972 std::string::npos) { | |
| 973 context_data.append("ctxs_end=" + base::IntToString( | |
| 974 search_terms_args.contextual_search_params.end) + "&"); | |
| 975 } | |
| 976 | |
| 977 const std::string selection = | |
| 978 search_terms_args.contextual_search_params.selection; | |
| 979 if (!selection.empty()) | |
| 980 context_data.append("q=" + selection + "&"); | |
| 981 | |
| 982 const std::string content = | |
| 983 search_terms_args.contextual_search_params.content; | |
| 984 if (!content.empty()) | |
| 985 context_data.append("ctxs_content=" + content + "&"); | |
| 986 | |
| 987 const std::string base_page_url = | |
| 988 search_terms_args.contextual_search_params.base_page_url; | |
| 989 if (!base_page_url.empty()) | |
| 990 context_data.append("ctxs_url=" + base_page_url + "&"); | |
| 991 | |
| 992 const std::string encoding = | |
| 993 search_terms_args.contextual_search_params.encoding; | |
| 994 if (!encoding.empty()) { | |
| 995 context_data.append("ctxs_encoding=" + encoding + "&"); | |
| 996 } | |
| 997 | |
| 998 HandleReplacement(std::string(), context_data, *i, &url); | |
| 999 break; | |
| 1000 } | |
| 1001 | |
| 930 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION: | 1002 case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION: |
| 931 DCHECK(!i->is_post_param); | 1003 DCHECK(!i->is_post_param); |
| 932 if (search_terms_args.accepted_suggestion >= 0 || | 1004 if (search_terms_args.accepted_suggestion >= 0 || |
| 933 !search_terms_args.assisted_query_stats.empty()) { | 1005 !search_terms_args.assisted_query_stats.empty()) { |
| 934 HandleReplacement( | 1006 HandleReplacement( |
| 935 "oq", base::UTF16ToUTF8(encoded_original_query), *i, &url); | 1007 "oq", base::UTF16ToUTF8(encoded_original_query), *i, &url); |
| 936 } | 1008 } |
| 937 break; | 1009 break; |
| 938 | 1010 |
| 939 case GOOGLE_PAGE_CLASSIFICATION: | 1011 case GOOGLE_PAGE_CLASSIFICATION: |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1091 | 1163 |
| 1092 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data) | 1164 TemplateURL::TemplateURL(Profile* profile, const TemplateURLData& data) |
| 1093 : profile_(profile), | 1165 : profile_(profile), |
| 1094 data_(data), | 1166 data_(data), |
| 1095 url_ref_(this, TemplateURLRef::SEARCH), | 1167 url_ref_(this, TemplateURLRef::SEARCH), |
| 1096 suggestions_url_ref_(this, | 1168 suggestions_url_ref_(this, |
| 1097 TemplateURLRef::SUGGEST), | 1169 TemplateURLRef::SUGGEST), |
| 1098 instant_url_ref_(this, | 1170 instant_url_ref_(this, |
| 1099 TemplateURLRef::INSTANT), | 1171 TemplateURLRef::INSTANT), |
| 1100 image_url_ref_(this, TemplateURLRef::IMAGE), | 1172 image_url_ref_(this, TemplateURLRef::IMAGE), |
| 1101 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB) { | 1173 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), |
| 1174 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH) { | |
| 1102 SetPrepopulateId(data_.prepopulate_id); | 1175 SetPrepopulateId(data_.prepopulate_id); |
| 1103 | 1176 |
| 1104 if (data_.search_terms_replacement_key == | 1177 if (data_.search_terms_replacement_key == |
| 1105 kGoogleInstantExtendedEnabledKeyFull) { | 1178 kGoogleInstantExtendedEnabledKeyFull) { |
| 1106 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; | 1179 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; |
| 1107 } | 1180 } |
| 1108 } | 1181 } |
| 1109 | 1182 |
| 1110 TemplateURL::~TemplateURL() { | 1183 TemplateURL::~TemplateURL() { |
| 1111 } | 1184 } |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1379 // patterns. This means that given patterns | 1452 // patterns. This means that given patterns |
| 1380 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1453 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1381 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1454 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1382 // return false. This is important for at least Google, where such URLs | 1455 // return false. This is important for at least Google, where such URLs |
| 1383 // are invalid. | 1456 // are invalid. |
| 1384 return !search_terms->empty(); | 1457 return !search_terms->empty(); |
| 1385 } | 1458 } |
| 1386 } | 1459 } |
| 1387 return false; | 1460 return false; |
| 1388 } | 1461 } |
| OLD | NEW |