| 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| 11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
| 12 #include "base/i18n/icu_string_conversions.h" | 12 #include "base/i18n/icu_string_conversions.h" |
| 13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "chrome/browser/google/google_util.h" | 22 #include "chrome/browser/google/google_util.h" |
| 23 #include "chrome/browser/search/search.h" |
| 23 #include "chrome/browser/search_engines/search_terms_data.h" | 24 #include "chrome/browser/search_engines/search_terms_data.h" |
| 24 #include "chrome/browser/search_engines/template_url_service.h" | 25 #include "chrome/browser/search_engines/template_url_service.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/chrome_version_info.h" | 27 #include "chrome/common/chrome_version_info.h" |
| 27 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 28 #include "extensions/common/constants.h" | 29 #include "extensions/common/constants.h" |
| 29 #include "google_apis/google_api_keys.h" | 30 #include "google_apis/google_api_keys.h" |
| 30 #include "net/base/escape.h" | 31 #include "net/base/escape.h" |
| 31 #include "net/base/mime_util.h" | 32 #include "net/base/mime_util.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 | 54 |
| 54 const char kGoogleAssistedQueryStatsParameter[] = "google:assistedQueryStats"; | 55 const char kGoogleAssistedQueryStatsParameter[] = "google:assistedQueryStats"; |
| 55 | 56 |
| 56 // Host/Domain Google searches are relative to. | 57 // Host/Domain Google searches are relative to. |
| 57 const char kGoogleBaseURLParameter[] = "google:baseURL"; | 58 const char kGoogleBaseURLParameter[] = "google:baseURL"; |
| 58 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; | 59 const char kGoogleBaseURLParameterFull[] = "{google:baseURL}"; |
| 59 | 60 |
| 60 // Like google:baseURL, but for the Search Suggest capability. | 61 // Like google:baseURL, but for the Search Suggest capability. |
| 61 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL"; | 62 const char kGoogleBaseSuggestURLParameter[] = "google:baseSuggestURL"; |
| 62 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}"; | 63 const char kGoogleBaseSuggestURLParameterFull[] = "{google:baseSuggestURL}"; |
| 64 const char kGoogleBookmarkBarPinnedParameter[] = "google:bookmarkBarPinned"; |
| 63 const char kGoogleCursorPositionParameter[] = "google:cursorPosition"; | 65 const char kGoogleCursorPositionParameter[] = "google:cursorPosition"; |
| 64 const char kGoogleInstantEnabledParameter[] = "google:instantEnabledParameter"; | 66 const char kGoogleInstantEnabledParameter[] = "google:instantEnabledParameter"; |
| 65 const char kGoogleInstantExtendedEnabledParameter[] = | 67 const char kGoogleInstantExtendedEnabledParameter[] = |
| 66 "google:instantExtendedEnabledParameter"; | 68 "google:instantExtendedEnabledParameter"; |
| 67 const char kGoogleInstantExtendedEnabledKey[] = | 69 const char kGoogleInstantExtendedEnabledKey[] = |
| 68 "google:instantExtendedEnabledKey"; | 70 "google:instantExtendedEnabledKey"; |
| 69 const char kGoogleInstantExtendedEnabledKeyFull[] = | 71 const char kGoogleInstantExtendedEnabledKeyFull[] = |
| 70 "{google:instantExtendedEnabledKey}"; | 72 "{google:instantExtendedEnabledKey}"; |
| 71 const char kGoogleNTPIsThemedParameter[] = "google:ntpIsThemedParameter"; | 73 const char kGoogleNTPIsThemedParameter[] = "google:ntpIsThemedParameter"; |
| 72 const char kGoogleOmniboxStartMarginParameter[] = | 74 const char kGoogleOmniboxStartMarginParameter[] = |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 return version; | 177 return version; |
| 176 } | 178 } |
| 177 return "unknown"; | 179 return "unknown"; |
| 178 } | 180 } |
| 179 | 181 |
| 180 bool IsTemplateParameterString(const std::string& param) { | 182 bool IsTemplateParameterString(const std::string& param) { |
| 181 return (param.length() > 2) && (*(param.begin()) == kStartParameter) && | 183 return (param.length() > 2) && (*(param.begin()) == kStartParameter) && |
| 182 (*(param.rbegin()) == kEndParameter); | 184 (*(param.rbegin()) == kEndParameter); |
| 183 } | 185 } |
| 184 | 186 |
| 187 bool ShowingSearchTermsOnSRP() { |
| 188 return chrome::IsInstantExtendedAPIEnabled() && |
| 189 !chrome::ShouldSuppressInstantExtendedOnSRP(); |
| 190 } |
| 191 |
| 185 } // namespace | 192 } // namespace |
| 186 | 193 |
| 187 | 194 |
| 188 // TemplateURLRef::SearchTermsArgs -------------------------------------------- | 195 // TemplateURLRef::SearchTermsArgs -------------------------------------------- |
| 189 | 196 |
| 190 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(const string16& search_terms) | 197 TemplateURLRef::SearchTermsArgs::SearchTermsArgs(const string16& search_terms) |
| 191 : search_terms(search_terms), | 198 : search_terms(search_terms), |
| 192 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), | 199 accepted_suggestion(NO_SUGGESTIONS_AVAILABLE), |
| 193 cursor_position(string16::npos), | 200 cursor_position(string16::npos), |
| 194 omnibox_start_margin(-1), | 201 omnibox_start_margin(-1), |
| 195 page_classification(AutocompleteInput::INVALID_SPEC), | 202 page_classification(AutocompleteInput::INVALID_SPEC), |
| 203 bookmark_bar_pinned(false), |
| 196 append_extra_query_params(false) { | 204 append_extra_query_params(false) { |
| 197 } | 205 } |
| 198 | 206 |
| 199 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() { | 207 TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() { |
| 200 } | 208 } |
| 201 | 209 |
| 202 | 210 |
| 203 // TemplateURLRef ------------------------------------------------------------- | 211 // TemplateURLRef ------------------------------------------------------------- |
| 204 | 212 |
| 205 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) | 213 TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) |
| 206 : owner_(owner), | 214 : owner_(owner), |
| 207 type_(type), | 215 type_(type), |
| 208 index_in_owner_(-1), | 216 index_in_owner_(-1), |
| 209 parsed_(false), | 217 parsed_(false), |
| 210 valid_(false), | 218 valid_(false), |
| 211 supports_replacements_(false), | 219 supports_replacements_(false), |
| 212 search_term_key_location_(url_parse::Parsed::QUERY), | 220 search_term_key_location_(url_parse::Parsed::QUERY), |
| 213 prepopulated_(false) { | 221 prepopulated_(false), |
| 222 showing_search_terms_(ShowingSearchTermsOnSRP()) { |
| 214 DCHECK(owner_); | 223 DCHECK(owner_); |
| 215 DCHECK_NE(INDEXED, type_); | 224 DCHECK_NE(INDEXED, type_); |
| 216 } | 225 } |
| 217 | 226 |
| 218 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner) | 227 TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner) |
| 219 : owner_(owner), | 228 : owner_(owner), |
| 220 type_(INDEXED), | 229 type_(INDEXED), |
| 221 index_in_owner_(index_in_owner), | 230 index_in_owner_(index_in_owner), |
| 222 parsed_(false), | 231 parsed_(false), |
| 223 valid_(false), | 232 valid_(false), |
| 224 supports_replacements_(false), | 233 supports_replacements_(false), |
| 225 search_term_key_location_(url_parse::Parsed::QUERY), | 234 search_term_key_location_(url_parse::Parsed::QUERY), |
| 226 prepopulated_(false) { | 235 prepopulated_(false), |
| 236 showing_search_terms_(ShowingSearchTermsOnSRP()) { |
| 227 DCHECK(owner_); | 237 DCHECK(owner_); |
| 228 DCHECK_LT(index_in_owner_, owner_->URLCount()); | 238 DCHECK_LT(index_in_owner_, owner_->URLCount()); |
| 229 } | 239 } |
| 230 | 240 |
| 231 TemplateURLRef::~TemplateURLRef() { | 241 TemplateURLRef::~TemplateURLRef() { |
| 232 } | 242 } |
| 233 | 243 |
| 234 std::string TemplateURLRef::GetURL() const { | 244 std::string TemplateURLRef::GetURL() const { |
| 235 switch (type_) { | 245 switch (type_) { |
| 236 case SEARCH: return owner_->url(); | 246 case SEARCH: return owner_->url(); |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 replacements->push_back(Replacement(SEARCH_TERMS, start)); | 539 replacements->push_back(Replacement(SEARCH_TERMS, start)); |
| 530 } else if (parameter == kCountParameter) { | 540 } else if (parameter == kCountParameter) { |
| 531 if (!optional) | 541 if (!optional) |
| 532 url->insert(start, kDefaultCount); | 542 url->insert(start, kDefaultCount); |
| 533 } else if (parameter == kGoogleAssistedQueryStatsParameter) { | 543 } else if (parameter == kGoogleAssistedQueryStatsParameter) { |
| 534 replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start)); | 544 replacements->push_back(Replacement(GOOGLE_ASSISTED_QUERY_STATS, start)); |
| 535 } else if (parameter == kGoogleBaseURLParameter) { | 545 } else if (parameter == kGoogleBaseURLParameter) { |
| 536 replacements->push_back(Replacement(GOOGLE_BASE_URL, start)); | 546 replacements->push_back(Replacement(GOOGLE_BASE_URL, start)); |
| 537 } else if (parameter == kGoogleBaseSuggestURLParameter) { | 547 } else if (parameter == kGoogleBaseSuggestURLParameter) { |
| 538 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start)); | 548 replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL, start)); |
| 549 } else if (parameter == kGoogleBookmarkBarPinnedParameter) { |
| 550 replacements->push_back(Replacement(GOOGLE_BOOKMARK_BAR_PINNED, start)); |
| 539 } else if (parameter == kGoogleCursorPositionParameter) { | 551 } else if (parameter == kGoogleCursorPositionParameter) { |
| 540 replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start)); | 552 replacements->push_back(Replacement(GOOGLE_CURSOR_POSITION, start)); |
| 541 } else if (parameter == kGoogleImageOriginalHeight) { | 553 } else if (parameter == kGoogleImageOriginalHeight) { |
| 542 replacements->push_back( | 554 replacements->push_back( |
| 543 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start)); | 555 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_HEIGHT, start)); |
| 544 } else if (parameter == kGoogleImageOriginalWidth) { | 556 } else if (parameter == kGoogleImageOriginalWidth) { |
| 545 replacements->push_back( | 557 replacements->push_back( |
| 546 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start)); | 558 Replacement(TemplateURLRef::GOOGLE_IMAGE_ORIGINAL_WIDTH, start)); |
| 547 } else if (parameter == kGoogleImageSearchSource) { | 559 } else if (parameter == kGoogleImageSearchSource) { |
| 548 url->insert(start, GetGoogleImageSearchSource()); | 560 url->insert(start, GetGoogleImageSearchSource()); |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url); | 833 std::string(), search_terms_data.GoogleBaseURLValue(), *i, &url); |
| 822 break; | 834 break; |
| 823 | 835 |
| 824 case GOOGLE_BASE_SUGGEST_URL: | 836 case GOOGLE_BASE_SUGGEST_URL: |
| 825 DCHECK(!i->is_post_param); | 837 DCHECK(!i->is_post_param); |
| 826 HandleReplacement( | 838 HandleReplacement( |
| 827 std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i, | 839 std::string(), search_terms_data.GoogleBaseSuggestURLValue(), *i, |
| 828 &url); | 840 &url); |
| 829 break; | 841 break; |
| 830 | 842 |
| 843 case GOOGLE_BOOKMARK_BAR_PINNED: |
| 844 if (showing_search_terms_) { |
| 845 // Log whether the bookmark bar is pinned when the user is seeing |
| 846 // InstantExtended on the SRP. |
| 847 DCHECK(!i->is_post_param); |
| 848 HandleReplacement( |
| 849 "bmbp", search_terms_args.bookmark_bar_pinned ? "1" : "0", *i, |
| 850 &url); |
| 851 } |
| 852 break; |
| 853 |
| 831 case GOOGLE_CURSOR_POSITION: | 854 case GOOGLE_CURSOR_POSITION: |
| 832 DCHECK(!i->is_post_param); | 855 DCHECK(!i->is_post_param); |
| 833 if (search_terms_args.cursor_position != string16::npos) | 856 if (search_terms_args.cursor_position != string16::npos) |
| 834 HandleReplacement( | 857 HandleReplacement( |
| 835 "cp", | 858 "cp", |
| 836 base::StringPrintf("%" PRIuS, search_terms_args.cursor_position), | 859 base::StringPrintf("%" PRIuS, search_terms_args.cursor_position), |
| 837 *i, | 860 *i, |
| 838 &url); | 861 &url); |
| 839 break; | 862 break; |
| 840 | 863 |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1278 // patterns. This means that given patterns | 1301 // patterns. This means that given patterns |
| 1279 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1302 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1280 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1303 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1281 // return false. This is important for at least Google, where such URLs | 1304 // return false. This is important for at least Google, where such URLs |
| 1282 // are invalid. | 1305 // are invalid. |
| 1283 return !search_terms->empty(); | 1306 return !search_terms->empty(); |
| 1284 } | 1307 } |
| 1285 } | 1308 } |
| 1286 return false; | 1309 return false; |
| 1287 } | 1310 } |
| OLD | NEW |