[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 8 | #include <cmath> |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 9 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 11 | #include "base/i18n/break_iterator.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 12 | #include "base/i18n/case_conversion.h" |
[email protected] | d6e58c6e | 2009-10-10 20:40:50 | [diff] [blame] | 13 | #include "base/i18n/icu_string_conversions.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 14 | #include "base/json/json_string_value_serializer.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 17 | #include "base/prefs/pref_service.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 18 | #include "base/strings/string16.h" |
| 19 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 21 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 22 | #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
[email protected] | 9ac4009 | 2010-10-27 23:05:26 | [diff] [blame] | 23 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
[email protected] | 5af9bc8 | 2012-06-29 00:53:48 | [diff] [blame] | 24 | #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
[email protected] | 73c2b163 | 2012-07-02 22:51:38 | [diff] [blame] | 25 | #include "chrome/browser/autocomplete/autocomplete_result.h" |
[email protected] | 2c812ba0 | 2011-07-14 00:23:15 | [diff] [blame] | 26 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 27 | #include "chrome/browser/autocomplete/url_prefix.h" |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 28 | #include "chrome/browser/google/google_util.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 29 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 30 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 31 | #include "chrome/browser/history/in_memory_database.h" |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 32 | #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
[email protected] | 684fcc1 | 2013-03-08 22:50:02 | [diff] [blame] | 33 | #include "chrome/browser/omnibox/omnibox_field_trial.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 34 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 35 | #include "chrome/browser/search/search.h" |
[email protected] | 9899a61 | 2012-08-21 23:50:04 | [diff] [blame] | 36 | #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 37 | #include "chrome/browser/search_engines/template_url_service.h" |
| 38 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 39 | #include "chrome/browser/sync/profile_sync_service.h" |
| 40 | #include "chrome/browser/sync/profile_sync_service_factory.h" |
[email protected] | c4a9d4c | 2013-08-10 07:06:27 | [diff] [blame] | 41 | #include "chrome/browser/ui/browser.h" |
| 42 | #include "chrome/browser/ui/browser_finder.h" |
| 43 | #include "chrome/browser/ui/browser_instant_controller.h" |
| 44 | #include "chrome/browser/ui/search/instant_controller.h" |
[email protected] | 4f3b446 | 2013-07-27 19:20:18 | [diff] [blame] | 45 | #include "chrome/common/net/url_fixer_upper.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 47 | #include "chrome/common/url_constants.h" |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 48 | #include "content/public/browser/user_metrics.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 49 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | #include "net/base/escape.h" |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 51 | #include "net/base/load_flags.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 52 | #include "net/base/net_util.h" |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 53 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 54 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 55 | #include "net/http/http_response_headers.h" |
[email protected] | 3dc1bc4 | 2012-06-19 08:20:53 | [diff] [blame] | 56 | #include "net/url_request/url_fetcher.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 57 | #include "net/url_request/url_request_status.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 58 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 59 | #include "url/url_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 61 | |
| 62 | // Helpers -------------------------------------------------------------------- |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 63 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 64 | namespace { |
| 65 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 66 | // We keep track in a histogram how many suggest requests we send, how |
| 67 | // many suggest requests we invalidate (e.g., due to a user typing |
| 68 | // another character), and how many replies we receive. |
| 69 | // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! *** |
| 70 | // (excluding the end-of-list enum value) |
| 71 | // We do not want values of existing enums to change or else it screws |
| 72 | // up the statistics. |
| 73 | enum SuggestRequestsHistogramValue { |
| 74 | REQUEST_SENT = 1, |
| 75 | REQUEST_INVALIDATED, |
| 76 | REPLY_RECEIVED, |
| 77 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE |
| 78 | }; |
| 79 | |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 80 | // The verbatim score for an input which is not an URL. |
| 81 | const int kNonURLVerbatimRelevance = 1300; |
| 82 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 83 | // Increments the appropriate value in the histogram by one. |
| 84 | void LogOmniboxSuggestRequest( |
| 85 | SuggestRequestsHistogramValue request_value) { |
| 86 | UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value, |
| 87 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE); |
| 88 | } |
| 89 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 90 | bool HasMultipleWords(const base::string16& text) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 91 | base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD); |
| 92 | bool found_word = false; |
| 93 | if (i.Init()) { |
| 94 | while (i.Advance()) { |
| 95 | if (i.IsWord()) { |
| 96 | if (found_word) |
| 97 | return true; |
| 98 | found_word = true; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 105 | // Builds the match contents and classification for the contents, and updates |
| 106 | // the given |AutocompleteMatch|. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 107 | void SetAndClassifyMatchContents(const base::string16& query_string, |
| 108 | const base::string16& input_text, |
| 109 | const base::string16& match_contents, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 110 | AutocompleteMatch* match) { |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 111 | match->contents = match_contents.empty() ? query_string : match_contents; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 112 | |
| 113 | // We do intra-string highlighting for suggestions - the suggested segment |
| 114 | // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 115 | // "youtube", so we'll bold the "tube" section: you*tube*. |
| 116 | if (input_text != match_contents) { |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 117 | size_t input_position = match->contents.find(input_text); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 118 | if (input_position == base::string16::npos) { |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 119 | // The input text is not a substring of the query string, e.g. input |
| 120 | // text is "slasdot" and the query string is "slashdot", so we bold the |
| 121 | // whole thing. |
| 122 | match->contents_class.push_back(ACMatchClassification( |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 123 | 0, ACMatchClassification::MATCH)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 124 | } else { |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 125 | // TODO(beng): ACMatchClassification::MATCH now seems to just mean |
| 126 | // "bold" this. Consider modifying the terminology. |
| 127 | // We don't iterate over the string here annotating all matches because |
| 128 | // it looks odd to have every occurrence of a substring that may be as |
| 129 | // short as a single character highlighted in a query suggestion result, |
| 130 | // e.g. for input text "s" and query string "southwest airlines", it |
| 131 | // looks odd if both the first and last s are highlighted. |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 132 | if (input_position != 0) { |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 133 | match->contents_class.push_back(ACMatchClassification( |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 134 | 0, ACMatchClassification::MATCH)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 135 | } |
| 136 | match->contents_class.push_back( |
| 137 | ACMatchClassification(input_position, ACMatchClassification::NONE)); |
| 138 | size_t next_fragment_position = input_position + input_text.length(); |
| 139 | if (next_fragment_position < query_string.length()) { |
| 140 | match->contents_class.push_back(ACMatchClassification( |
| 141 | next_fragment_position, ACMatchClassification::MATCH)); |
| 142 | } |
| 143 | } |
| 144 | } else { |
| 145 | // Otherwise, |match| is a verbatim (what-you-typed) match, either for the |
| 146 | // default provider or a keyword search provider. |
| 147 | match->contents_class.push_back(ACMatchClassification( |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 148 | 0, ACMatchClassification::NONE)); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 149 | } |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 150 | } |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 151 | |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 152 | AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { |
| 153 | if (type == "ENTITY") |
| 154 | return AutocompleteMatchType::SEARCH_SUGGEST_ENTITY; |
| 155 | if (type == "INFINITE") |
| 156 | return AutocompleteMatchType::SEARCH_SUGGEST_INFINITE; |
| 157 | if (type == "PERSONALIZED") |
| 158 | return AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED; |
| 159 | if (type == "PROFILE") |
| 160 | return AutocompleteMatchType::SEARCH_SUGGEST_PROFILE; |
| 161 | return AutocompleteMatchType::SEARCH_SUGGEST; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 164 | } // namespace |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 165 | |
[email protected] | 033f342 | 2012-03-13 21:24:18 | [diff] [blame] | 166 | |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 167 | // SuggestionDeletionHandler ------------------------------------------------- |
| 168 | |
| 169 | // This class handles making requests to the server in order to delete |
| 170 | // personalized suggestions. |
| 171 | class SuggestionDeletionHandler : public net::URLFetcherDelegate { |
| 172 | public: |
| 173 | typedef base::Callback<void(bool, SuggestionDeletionHandler*)> |
| 174 | DeletionCompletedCallback; |
| 175 | |
| 176 | SuggestionDeletionHandler( |
| 177 | const std::string& deletion_url, |
| 178 | Profile* profile, |
| 179 | const DeletionCompletedCallback& callback); |
| 180 | |
| 181 | virtual ~SuggestionDeletionHandler(); |
| 182 | |
| 183 | private: |
| 184 | // net::URLFetcherDelegate: |
| 185 | virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 186 | |
| 187 | scoped_ptr<net::URLFetcher> deletion_fetcher_; |
| 188 | DeletionCompletedCallback callback_; |
| 189 | |
| 190 | DISALLOW_COPY_AND_ASSIGN(SuggestionDeletionHandler); |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | SuggestionDeletionHandler::SuggestionDeletionHandler( |
| 195 | const std::string& deletion_url, |
| 196 | Profile* profile, |
| 197 | const DeletionCompletedCallback& callback) : callback_(callback) { |
| 198 | GURL url(deletion_url); |
| 199 | DCHECK(url.is_valid()); |
| 200 | |
| 201 | deletion_fetcher_.reset(net::URLFetcher::Create( |
| 202 | SearchProvider::kDeletionURLFetcherID, |
| 203 | url, |
| 204 | net::URLFetcher::GET, |
| 205 | this)); |
| 206 | deletion_fetcher_->SetRequestContext(profile->GetRequestContext()); |
| 207 | deletion_fetcher_->Start(); |
| 208 | }; |
| 209 | |
| 210 | SuggestionDeletionHandler::~SuggestionDeletionHandler() { |
| 211 | }; |
| 212 | |
| 213 | void SuggestionDeletionHandler::OnURLFetchComplete( |
| 214 | const net::URLFetcher* source) { |
| 215 | DCHECK(source == deletion_fetcher_.get()); |
| 216 | callback_.Run( |
| 217 | source->GetStatus().is_success() && (source->GetResponseCode() == 200), |
| 218 | this); |
| 219 | }; |
| 220 | |
| 221 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 222 | // SearchProvider::Providers -------------------------------------------------- |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 223 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 224 | SearchProvider::Providers::Providers(TemplateURLService* template_url_service) |
| 225 | : template_url_service_(template_url_service) { |
| 226 | } |
| 227 | |
| 228 | const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { |
| 229 | return default_provider_.empty() ? NULL : |
| 230 | template_url_service_->GetTemplateURLForKeyword(default_provider_); |
| 231 | } |
| 232 | |
| 233 | const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const { |
| 234 | return keyword_provider_.empty() ? NULL : |
| 235 | template_url_service_->GetTemplateURLForKeyword(keyword_provider_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 238 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 239 | // SearchProvider::Result ----------------------------------------------------- |
| 240 | |
| 241 | SearchProvider::Result::Result(bool from_keyword_provider, |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 242 | int relevance, |
| 243 | bool relevance_from_server) |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 244 | : from_keyword_provider_(from_keyword_provider), |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 245 | relevance_(relevance), |
| 246 | relevance_from_server_(relevance_from_server) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | SearchProvider::Result::~Result() { |
| 250 | } |
| 251 | |
| 252 | |
| 253 | // SearchProvider::SuggestResult ---------------------------------------------- |
| 254 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 255 | SearchProvider::SuggestResult::SuggestResult( |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 256 | const base::string16& suggestion, |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 257 | AutocompleteMatchType::Type type, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 258 | const base::string16& match_contents, |
| 259 | const base::string16& annotation, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 260 | const std::string& suggest_query_params, |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 261 | const std::string& deletion_url, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 262 | bool from_keyword_provider, |
| 263 | int relevance, |
| 264 | bool relevance_from_server, |
| 265 | bool should_prefetch) |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 266 | : Result(from_keyword_provider, relevance, relevance_from_server), |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 267 | suggestion_(suggestion), |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 268 | type_(type), |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 269 | match_contents_(match_contents), |
| 270 | annotation_(annotation), |
| 271 | suggest_query_params_(suggest_query_params), |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 272 | deletion_url_(deletion_url), |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 273 | should_prefetch_(should_prefetch) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | SearchProvider::SuggestResult::~SuggestResult() { |
| 277 | } |
| 278 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 279 | bool SearchProvider::SuggestResult::IsInlineable( |
| 280 | const base::string16& input) const { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 281 | return StartsWith(suggestion_, input, false); |
| 282 | } |
| 283 | |
| 284 | int SearchProvider::SuggestResult::CalculateRelevance( |
| 285 | const AutocompleteInput& input, |
| 286 | bool keyword_provider_requested) const { |
| 287 | if (!from_keyword_provider_ && keyword_provider_requested) |
| 288 | return 100; |
| 289 | return ((input.type() == AutocompleteInput::URL) ? 300 : 600); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | // SearchProvider::NavigationResult ------------------------------------------- |
| 294 | |
| 295 | SearchProvider::NavigationResult::NavigationResult( |
| 296 | const AutocompleteProvider& provider, |
| 297 | const GURL& url, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 298 | const base::string16& description, |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 299 | bool from_keyword_provider, |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 300 | int relevance, |
| 301 | bool relevance_from_server) |
| 302 | : Result(from_keyword_provider, relevance, relevance_from_server), |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 303 | url_(url), |
| 304 | formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 305 | url, provider.StringForURLDisplay(url, true, false))), |
| 306 | description_(description) { |
| 307 | DCHECK(url_.is_valid()); |
| 308 | } |
| 309 | |
| 310 | SearchProvider::NavigationResult::~NavigationResult() { |
| 311 | } |
| 312 | |
| 313 | bool SearchProvider::NavigationResult::IsInlineable( |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 314 | const base::string16& input) const { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 315 | return URLPrefix::BestURLPrefix(formatted_url_, input) != NULL; |
| 316 | } |
| 317 | |
| 318 | int SearchProvider::NavigationResult::CalculateRelevance( |
| 319 | const AutocompleteInput& input, |
| 320 | bool keyword_provider_requested) const { |
| 321 | return (from_keyword_provider_ || !keyword_provider_requested) ? 800 : 150; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | // SearchProvider::CompareScoredResults --------------------------------------- |
| 326 | |
| 327 | class SearchProvider::CompareScoredResults { |
| 328 | public: |
| 329 | bool operator()(const Result& a, const Result& b) { |
| 330 | // Sort in descending relevance order. |
| 331 | return a.relevance() > b.relevance(); |
| 332 | } |
| 333 | }; |
| 334 | |
| 335 | |
| 336 | // SearchProvider::Results ---------------------------------------------------- |
| 337 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 338 | SearchProvider::Results::Results() : verbatim_relevance(-1) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | SearchProvider::Results::~Results() { |
| 342 | } |
| 343 | |
| 344 | void SearchProvider::Results::Clear() { |
| 345 | suggest_results.clear(); |
| 346 | navigation_results.clear(); |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 347 | verbatim_relevance = -1; |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 348 | metadata.clear(); |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | bool SearchProvider::Results::HasServerProvidedScores() const { |
| 352 | if (verbatim_relevance >= 0) |
| 353 | return true; |
| 354 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 355 | // Right now either all results of one type will be server-scored or they will |
| 356 | // all be locally scored, but in case we change this later, we'll just check |
| 357 | // them all. |
| 358 | for (SuggestResults::const_iterator i(suggest_results.begin()); |
| 359 | i != suggest_results.end(); ++i) { |
| 360 | if (i->relevance_from_server()) |
| 361 | return true; |
| 362 | } |
| 363 | for (NavigationResults::const_iterator i(navigation_results.begin()); |
| 364 | i != navigation_results.end(); ++i) { |
| 365 | if (i->relevance_from_server()) |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | return false; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 370 | } |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 371 | |
| 372 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 373 | // SearchProvider ------------------------------------------------------------- |
| 374 | |
| 375 | // static |
| 376 | const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 377 | const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 378 | const int SearchProvider::kDeletionURLFetcherID = 3; |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 379 | int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 380 | const char SearchProvider::kRelevanceFromServerKey[] = "relevance_from_server"; |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 381 | const char SearchProvider::kShouldPrefetchKey[] = "should_prefetch"; |
| 382 | const char SearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 383 | const char SearchProvider::kDeletionUrlKey[] = "deletion_url"; |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 384 | const char SearchProvider::kTrue[] = "true"; |
| 385 | const char SearchProvider::kFalse[] = "false"; |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 386 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 387 | SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
| 388 | Profile* profile) |
| 389 | : AutocompleteProvider(listener, profile, |
| 390 | AutocompleteProvider::TYPE_SEARCH), |
| 391 | providers_(TemplateURLServiceFactory::GetForProfile(profile)), |
| 392 | suggest_results_pending_(0), |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 393 | field_trial_triggered_(false), |
[email protected] | c4a9d4c | 2013-08-10 07:06:27 | [diff] [blame] | 394 | field_trial_triggered_in_session_(false) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 397 | // static |
| 398 | AutocompleteMatch SearchProvider::CreateSearchSuggestion( |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 399 | AutocompleteProvider* autocomplete_provider, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 400 | const AutocompleteInput& input, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 401 | const base::string16& input_text, |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 402 | int relevance, |
| 403 | AutocompleteMatch::Type type, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 404 | bool is_keyword, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 405 | const base::string16& match_contents, |
| 406 | const base::string16& annotation, |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 407 | const TemplateURL* template_url, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 408 | const base::string16& query_string, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 409 | const std::string& suggest_query_params, |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 410 | int accepted_suggestion, |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 411 | int omnibox_start_margin, |
| 412 | bool append_extra_query_params) { |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 413 | AutocompleteMatch match(autocomplete_provider, relevance, false, type); |
| 414 | |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 415 | if (!template_url) |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 416 | return match; |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 417 | match.keyword = template_url->keyword(); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 418 | |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 419 | SetAndClassifyMatchContents(query_string, input_text, match_contents, &match); |
| 420 | |
| 421 | if (!annotation.empty()) |
| 422 | match.description = annotation; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 423 | |
| 424 | match.allowed_to_be_default_match = (input_text == match_contents); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 425 | |
| 426 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 427 | // values preserve that property. Otherwise, if the user starts editing a |
| 428 | // suggestion, non-Search results will suddenly appear. |
| 429 | if (input.type() == AutocompleteInput::FORCED_QUERY) |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame^] | 430 | match.fill_into_edit.assign(base::ASCIIToUTF16("?")); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 431 | if (is_keyword) |
| 432 | match.fill_into_edit.append(match.keyword + char16(' ')); |
| 433 | if (!input.prevent_inline_autocomplete() && |
| 434 | StartsWith(query_string, input_text, false)) { |
[email protected] | 518024c | 2013-07-19 23:40:25 | [diff] [blame] | 435 | match.inline_autocompletion = query_string.substr(input_text.length()); |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 436 | match.allowed_to_be_default_match = true; |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 437 | } |
| 438 | match.fill_into_edit.append(query_string); |
| 439 | |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 440 | const TemplateURLRef& search_url = template_url->url_ref(); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 441 | DCHECK(search_url.SupportsReplacement()); |
| 442 | match.search_terms_args.reset( |
| 443 | new TemplateURLRef::SearchTermsArgs(query_string)); |
| 444 | match.search_terms_args->original_query = input_text; |
| 445 | match.search_terms_args->accepted_suggestion = accepted_suggestion; |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 446 | match.search_terms_args->omnibox_start_margin = omnibox_start_margin; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 447 | match.search_terms_args->suggest_query_params = suggest_query_params; |
[email protected] | 56fa2959 | 2013-07-02 20:25:53 | [diff] [blame] | 448 | match.search_terms_args->append_extra_query_params = |
| 449 | append_extra_query_params; |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 450 | // This is the destination URL sans assisted query stats. This must be set |
| 451 | // so the AutocompleteController can properly de-dupe; the controller will |
| 452 | // eventually overwrite it before it reaches the user. |
| 453 | match.destination_url = |
| 454 | GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); |
| 455 | |
| 456 | // Search results don't look like URLs. |
| 457 | match.transition = is_keyword ? |
| 458 | content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; |
| 459 | |
| 460 | return match; |
| 461 | } |
| 462 | |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 463 | // static |
| 464 | bool SearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
| 465 | return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
| 466 | } |
| 467 | |
| 468 | // static |
| 469 | std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) { |
| 470 | return match.GetAdditionalInfo(kSuggestMetadataKey); |
| 471 | } |
| 472 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 473 | void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 474 | provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 475 | metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 476 | new_entry.set_provider(AsOmniboxEventProviderType()); |
| 477 | new_entry.set_provider_done(done_); |
| 478 | std::vector<uint32> field_trial_hashes; |
| 479 | OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); |
| 480 | for (size_t i = 0; i < field_trial_hashes.size(); ++i) { |
| 481 | if (field_trial_triggered_) |
| 482 | new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
| 483 | if (field_trial_triggered_in_session_) { |
| 484 | new_entry.mutable_field_trial_triggered_in_session()->Add( |
| 485 | field_trial_hashes[i]); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 490 | void SearchProvider::DeleteMatch(const AutocompleteMatch& match) { |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 491 | DCHECK(match.deletable); |
| 492 | |
| 493 | deletion_handlers_.push_back(new SuggestionDeletionHandler( |
| 494 | match.GetAdditionalInfo(SearchProvider::kDeletionUrlKey), |
| 495 | profile_, |
| 496 | base::Bind(&SearchProvider::OnDeletionComplete, base::Unretained(this)))); |
| 497 | |
[email protected] | fba587fc | 2013-12-16 15:51:24 | [diff] [blame] | 498 | HistoryService* const history_service = |
| 499 | HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 500 | TemplateURL* template_url = match.GetTemplateURL(profile_, false); |
| 501 | // This may be NULL if the template corresponding to the keyword has been |
| 502 | // deleted or there is no keyword set. |
| 503 | if (template_url != NULL) { |
| 504 | history_service->DeleteMatchingURLsForKeyword(template_url->id(), |
| 505 | match.contents); |
| 506 | } |
| 507 | |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 508 | // Immediately update the list of matches to show the match was deleted, |
| 509 | // regardless of whether the server request actually succeeds. |
| 510 | DeleteMatchFromMatches(match); |
| 511 | } |
| 512 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 513 | void SearchProvider::ResetSession() { |
| 514 | field_trial_triggered_in_session_ = false; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 515 | } |
| 516 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 517 | SearchProvider::~SearchProvider() { |
| 518 | } |
| 519 | |
| 520 | // static |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 521 | void SearchProvider::RemoveStaleResults(const base::string16& input, |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 522 | int verbatim_relevance, |
| 523 | SuggestResults* suggest_results, |
| 524 | NavigationResults* navigation_results) { |
| 525 | DCHECK_GE(verbatim_relevance, 0); |
| 526 | // Keep pointers to the head of (the highest scoring elements of) |
| 527 | // |suggest_results| and |navigation_results|. Iterate down the lists |
| 528 | // removing non-inlineable results in order of decreasing relevance |
| 529 | // scores. Stop when the highest scoring element among those remaining |
| 530 | // is inlineable or the element is less than |verbatim_relevance|. |
| 531 | // This allows non-inlineable lower-scoring results to remain |
| 532 | // because (i) they are guaranteed to not be inlined and (ii) |
| 533 | // letting them remain reduces visual jank. For instance, as the |
| 534 | // user types the mis-spelled query "fpobar" (for foobar), the |
| 535 | // suggestion "foobar" will be suggested on every keystroke. If the |
| 536 | // SearchProvider always removes all non-inlineable results, the user will |
| 537 | // see visual jitter/jank as the result disappears and re-appears moments |
| 538 | // later as the suggest server returns results. |
| 539 | SuggestResults::iterator sug_it = suggest_results->begin(); |
| 540 | NavigationResults::iterator nav_it = navigation_results->begin(); |
| 541 | while ((sug_it != suggest_results->end()) || |
| 542 | (nav_it != navigation_results->end())) { |
| 543 | const int sug_rel = |
| 544 | (sug_it != suggest_results->end()) ? sug_it->relevance() : -1; |
| 545 | const int nav_rel = |
| 546 | (nav_it != navigation_results->end()) ? nav_it->relevance() : -1; |
| 547 | if (std::max(sug_rel, nav_rel) < verbatim_relevance) |
| 548 | break; |
| 549 | if (sug_rel > nav_rel) { |
| 550 | // The current top result is a search suggestion. |
| 551 | if (sug_it->IsInlineable(input)) |
| 552 | break; |
| 553 | sug_it = suggest_results->erase(sug_it); |
| 554 | } else if (sug_rel == nav_rel) { |
| 555 | // Have both results and they're tied. |
| 556 | const bool sug_inlineable = sug_it->IsInlineable(input); |
| 557 | const bool nav_inlineable = nav_it->IsInlineable(input); |
| 558 | if (!sug_inlineable) |
| 559 | sug_it = suggest_results->erase(sug_it); |
| 560 | if (!nav_inlineable) |
| 561 | nav_it = navigation_results->erase(nav_it); |
| 562 | if (sug_inlineable || nav_inlineable) |
| 563 | break; |
| 564 | } else { |
| 565 | // The current top result is a navigational suggestion. |
| 566 | if (nav_it->IsInlineable(input)) |
| 567 | break; |
| 568 | nav_it = navigation_results->erase(nav_it); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // static |
| 574 | int SearchProvider::CalculateRelevanceForKeywordVerbatim( |
| 575 | AutocompleteInput::Type type, |
| 576 | bool prefer_keyword) { |
| 577 | // This function is responsible for scoring verbatim query matches |
| 578 | // for non-extension keywords. KeywordProvider::CalculateRelevance() |
| 579 | // scores verbatim query matches for extension keywords, as well as |
| 580 | // for keyword matches (i.e., suggestions of a keyword itself, not a |
| 581 | // suggestion of a query on a keyword search engine). These two |
| 582 | // functions are currently in sync, but there's no reason we |
| 583 | // couldn't decide in the future to score verbatim matches |
| 584 | // differently for extension and non-extension keywords. If you |
| 585 | // make such a change, however, you should update this comment to |
| 586 | // describe it, so it's clear why the functions diverge. |
| 587 | if (prefer_keyword) |
| 588 | return 1500; |
| 589 | return (type == AutocompleteInput::QUERY) ? 1450 : 1100; |
| 590 | } |
| 591 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 593 | bool minimal_changes) { |
[email protected] | 04504c24 | 2013-01-22 21:08:55 | [diff] [blame] | 594 | // Do our best to load the model as early as possible. This will reduce |
| 595 | // odds of having the model not ready when really needed (a non-empty input). |
| 596 | TemplateURLService* model = providers_.template_url_service(); |
| 597 | DCHECK(model); |
| 598 | model->Load(); |
| 599 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 600 | matches_.clear(); |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 601 | field_trial_triggered_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 603 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 604 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 605 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 609 | keyword_input_ = input; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 610 | const TemplateURL* keyword_provider = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 611 | KeywordProvider::GetSubstitutingTemplateURLForInput(model, |
| 612 | &keyword_input_); |
| 613 | if (keyword_provider == NULL) |
| 614 | keyword_input_.Clear(); |
| 615 | else if (keyword_input_.text().empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 616 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 617 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 618 | const TemplateURL* default_provider = model->GetDefaultSearchProvider(); |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 619 | if (default_provider && !default_provider->SupportsReplacement()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 620 | default_provider = NULL; |
| 621 | |
| 622 | if (keyword_provider == default_provider) |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 623 | default_provider = NULL; // No use in querying the same provider twice. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 624 | |
| 625 | if (!default_provider && !keyword_provider) { |
| 626 | // No valid providers. |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 627 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
| 631 | // If we're still running an old query but have since changed the query text |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 632 | // or the providers, abort the query. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 633 | base::string16 default_provider_keyword(default_provider ? |
| 634 | default_provider->keyword() : base::string16()); |
| 635 | base::string16 keyword_provider_keyword(keyword_provider ? |
| 636 | keyword_provider->keyword() : base::string16()); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 637 | if (!minimal_changes || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 638 | !providers_.equal(default_provider_keyword, keyword_provider_keyword)) { |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 639 | // Cancel any in-flight suggest requests. |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 640 | if (!done_) |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 641 | Stop(false); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 642 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 644 | providers_.set(default_provider_keyword, keyword_provider_keyword); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 645 | |
| 646 | if (input.text().empty()) { |
| 647 | // User typed "?" alone. Give them a placeholder result indicating what |
| 648 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 649 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 650 | AutocompleteMatch match; |
| 651 | match.provider = this; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 652 | match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 653 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 654 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 655 | match.keyword = providers_.default_provider(); |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 656 | match.allowed_to_be_default_match = true; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 657 | matches_.push_back(match); |
| 658 | } |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 659 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | input_ = input; |
| 664 | |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 665 | DoHistoryQuery(minimal_changes); |
| 666 | StartOrStopSuggestQuery(minimal_changes); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 667 | UpdateMatches(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 670 | void SearchProvider::Stop(bool clear_cached_results) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 671 | StopSuggest(); |
| 672 | done_ = true; |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 673 | |
| 674 | if (clear_cached_results) |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 675 | ClearAllResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 676 | } |
| 677 | |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 678 | void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 679 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 680 | suggest_results_pending_--; |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 681 | LogOmniboxSuggestRequest(REPLY_RECEIVED); |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 682 | DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 683 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 684 | const bool is_keyword = (source == keyword_fetcher_.get()); |
[email protected] | f2916ea | 2013-01-26 02:20:14 | [diff] [blame] | 685 | // Ensure the request succeeded and that the provider used is still available. |
| 686 | // A verbatim match cannot be generated without this provider, causing errors. |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 687 | const bool request_succeeded = |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 688 | source->GetStatus().is_success() && (source->GetResponseCode() == 200) && |
| 689 | (is_keyword ? |
| 690 | providers_.GetKeywordProviderURL() : |
| 691 | providers_.GetDefaultProviderURL()); |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 692 | |
| 693 | // Record response time for suggest requests sent to Google. We care |
| 694 | // only about the common case: the Google default provider used in |
| 695 | // non-keyword mode. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 696 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 697 | if (!is_keyword && default_url && |
[email protected] | 628392f | 2013-08-16 00:34:45 | [diff] [blame] | 698 | (TemplateURLPrepopulateData::GetEngineType(*default_url) == |
[email protected] | 9899a61 | 2012-08-21 23:50:04 | [diff] [blame] | 699 | SEARCH_ENGINE_GOOGLE)) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 700 | const base::TimeDelta elapsed_time = |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 701 | base::TimeTicks::Now() - time_suggest_request_sent_; |
| 702 | if (request_succeeded) { |
| 703 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
| 704 | elapsed_time); |
| 705 | } else { |
| 706 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
| 707 | elapsed_time); |
| 708 | } |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 709 | } |
| 710 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 711 | bool results_updated = false; |
| 712 | if (request_succeeded) { |
[email protected] | ed9b180 | 2013-10-31 04:09:48 | [diff] [blame] | 713 | const net::HttpResponseHeaders* const response_headers = |
| 714 | source->GetResponseHeaders(); |
| 715 | std::string json_data; |
| 716 | source->GetResponseAsString(&json_data); |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 717 | |
[email protected] | ed9b180 | 2013-10-31 04:09:48 | [diff] [blame] | 718 | // JSON is supposed to be UTF-8, but some suggest service providers send |
| 719 | // JSON files in non-UTF-8 encodings. The actual encoding is usually |
| 720 | // specified in the Content-Type header field. |
| 721 | if (response_headers) { |
| 722 | std::string charset; |
| 723 | if (response_headers->GetCharset(&charset)) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 724 | base::string16 data_16; |
[email protected] | ed9b180 | 2013-10-31 04:09:48 | [diff] [blame] | 725 | // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
| 726 | if (base::CodepageToUTF16(json_data, charset.c_str(), |
| 727 | base::OnStringConversionError::FAIL, |
| 728 | &data_16)) |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame^] | 729 | json_data = base::UTF16ToUTF8(data_16); |
[email protected] | ed9b180 | 2013-10-31 04:09:48 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 733 | scoped_ptr<base::Value> data(DeserializeJsonData(json_data)); |
[email protected] | 2e8c601 | 2013-12-03 12:00:01 | [diff] [blame] | 734 | results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 735 | } |
| 736 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 737 | UpdateMatches(); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 738 | if (done_ || results_updated) |
| 739 | listener_->OnProviderUpdate(results_updated); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 740 | } |
| 741 | |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 742 | void SearchProvider::OnDeletionComplete(bool success, |
| 743 | SuggestionDeletionHandler* handler) { |
| 744 | RecordDeletionResult(success); |
| 745 | SuggestionDeletionHandlers::iterator it = std::find( |
| 746 | deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
| 747 | DCHECK(it != deletion_handlers_.end()); |
| 748 | deletion_handlers_.erase(it); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | void SearchProvider::RecordDeletionResult(bool success) { |
| 753 | if (success) { |
| 754 | content::RecordAction( |
| 755 | content::UserMetricsAction("Omnibox.ServerSuggestDelete.Success")); |
| 756 | } else { |
| 757 | content::RecordAction( |
| 758 | content::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure")); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | void SearchProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) { |
| 763 | for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { |
| 764 | // Find the desired match to delete by checking the type and contents. |
| 765 | // We can't check the destination URL, because the autocomplete controller |
| 766 | // may have reformulated that. Not that while checking for matching |
| 767 | // contents works for personalized suggestions, if more match types gain |
| 768 | // deletion support, this algorithm may need to be re-examined. |
| 769 | if (i->contents == match.contents && i->type == match.type) { |
| 770 | matches_.erase(i); |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | listener_->OnProviderUpdate(true); |
| 775 | } |
| 776 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 777 | void SearchProvider::Run() { |
| 778 | // Start a new request with the current input. |
| 779 | suggest_results_pending_ = 0; |
| 780 | time_suggest_request_sent_ = base::TimeTicks::Now(); |
[email protected] | abe441e | 2013-05-06 12:35:05 | [diff] [blame] | 781 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 782 | default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 783 | providers_.GetDefaultProviderURL(), input_)); |
| 784 | keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
| 785 | providers_.GetKeywordProviderURL(), keyword_input_)); |
| 786 | |
| 787 | // Both the above can fail if the providers have been modified or deleted |
| 788 | // since the query began. |
| 789 | if (suggest_results_pending_ == 0) { |
| 790 | UpdateDone(); |
| 791 | // We only need to update the listener if we're actually done. |
| 792 | if (done_) |
| 793 | listener_->OnProviderUpdate(false); |
| 794 | } |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 795 | } |
| 796 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 797 | void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
| 798 | // The history query results are synchronous, so if minimal_changes is true, |
| 799 | // we still have the last results and don't need to do anything. |
| 800 | if (minimal_changes) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | return; |
| 802 | |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 803 | base::TimeTicks do_history_query_start_time(base::TimeTicks::Now()); |
| 804 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 805 | keyword_history_results_.clear(); |
| 806 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 807 | |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 808 | if (OmniboxFieldTrial::SearchHistoryDisable( |
| 809 | input_.current_page_classification())) |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 810 | return; |
| 811 | |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 812 | base::TimeTicks start_time(base::TimeTicks::Now()); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 813 | HistoryService* const history_service = |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 814 | HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 815 | base::TimeTicks now(base::TimeTicks::Now()); |
| 816 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.GetHistoryServiceTime", |
| 817 | now - start_time); |
| 818 | start_time = now; |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 819 | history::URLDatabase* url_db = history_service ? |
| 820 | history_service->InMemoryDatabase() : NULL; |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 821 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.InMemoryDatabaseTime", |
| 822 | base::TimeTicks::Now() - start_time); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 823 | if (!url_db) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 824 | return; |
| 825 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 826 | // Request history for both the keyword and default provider. We grab many |
| 827 | // more matches than we'll ultimately clamp to so that if there are several |
| 828 | // recent multi-word matches who scores are lowered (see |
| 829 | // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring |
| 830 | // matches. Note that this doesn't fix the problem entirely, but merely |
| 831 | // limits it to cases with a very large number of such multi-word matches; for |
| 832 | // now, this seems OK compared with the complexity of a real fix, which would |
| 833 | // require multiple searches and tracking of "single- vs. multi-word" in the |
| 834 | // database. |
| 835 | int num_matches = kMaxMatches * 5; |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 836 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 837 | if (default_url) { |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 838 | start_time = base::TimeTicks::Now(); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 839 | url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(), |
| 840 | num_matches, &default_history_results_); |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 841 | UMA_HISTOGRAM_TIMES( |
| 842 | "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime", |
| 843 | base::TimeTicks::Now() - start_time); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 844 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 845 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 846 | if (keyword_url) { |
| 847 | url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 848 | keyword_input_.text(), num_matches, &keyword_history_results_); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 849 | } |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 850 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.DoHistoryQueryTime", |
| 851 | base::TimeTicks::Now() - do_history_query_start_time); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 852 | } |
| 853 | |
[email protected] | 6dc950f | 2012-07-16 19:49:08 | [diff] [blame] | 854 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 855 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 856 | StopSuggest(); |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 857 | ClearAllResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 858 | return; |
| 859 | } |
| 860 | |
| 861 | // For the minimal_changes case, if we finished the previous query and still |
| 862 | // have its results, or are allowed to keep running it, just do that, rather |
| 863 | // than starting a new query. |
| 864 | if (minimal_changes && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 865 | (!default_results_.suggest_results.empty() || |
| 866 | !default_results_.navigation_results.empty() || |
| 867 | !keyword_results_.suggest_results.empty() || |
| 868 | !keyword_results_.navigation_results.empty() || |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 869 | (!done_ && |
| 870 | input_.matches_requested() == AutocompleteInput::ALL_MATCHES))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 871 | return; |
| 872 | |
| 873 | // We can't keep running any previous query, so halt it. |
| 874 | StopSuggest(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 875 | |
| 876 | // Remove existing results that cannot inline autocomplete the new input. |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 877 | RemoveAllStaleResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 878 | |
| 879 | // We can't start a new query if we're only allowed synchronous results. |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 880 | if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 881 | return; |
| 882 | |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 883 | // To avoid flooding the suggest server, don't send a query until at |
| 884 | // least 100 ms since the last query. |
[email protected] | 515ffa94 | 2012-11-27 20:18:24 | [diff] [blame] | 885 | base::TimeTicks next_suggest_time(time_suggest_request_sent_ + |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 886 | base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs)); |
[email protected] | 515ffa94 | 2012-11-27 20:18:24 | [diff] [blame] | 887 | base::TimeTicks now(base::TimeTicks::Now()); |
| 888 | if (now >= next_suggest_time) { |
| 889 | Run(); |
| 890 | return; |
| 891 | } |
| 892 | timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | } |
| 894 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 895 | bool SearchProvider::IsQuerySuitableForSuggest() const { |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 896 | // Don't run Suggest in incognito mode, if the engine doesn't support it, or |
| 897 | // if the user has disabled it. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 898 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 899 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 900 | if (profile_->IsOffTheRecord() || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 901 | ((!default_url || default_url->suggestions_url().empty()) && |
| 902 | (!keyword_url || keyword_url->suggestions_url().empty())) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 903 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 904 | return false; |
| 905 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 906 | // If the input type might be a URL, we take extra care so that private data |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 907 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 908 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 909 | // FORCED_QUERY means the user is explicitly asking us to search for this, so |
| 910 | // we assume it isn't a URL and/or there isn't private data. |
| 911 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 912 | return true; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 913 | |
[email protected] | f608ea10 | 2013-03-18 15:08:09 | [diff] [blame] | 914 | // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't |
| 915 | // http/https/ftp, we shouldn't send it. Sending things like file: and data: |
| 916 | // is both a waste of time and a disclosure of potentially private, local |
| 917 | // data. Other "schemes" may actually be usernames, and we don't want to send |
| 918 | // passwords. If the scheme is OK, we still need to check other cases below. |
| 919 | // If this is QUERY, then the presence of these schemes means the user |
| 920 | // explicitly typed one, and thus this is probably a URL that's being entered |
| 921 | // and happens to currently be invalid -- in which case we again want to run |
| 922 | // our checks below. Other QUERY cases are less likely to be URLs and thus we |
| 923 | // assume we're OK. |
[email protected] | e9a8204 | 2013-09-15 03:38:29 | [diff] [blame] | 924 | if (!LowerCaseEqualsASCII(input_.scheme(), content::kHttpScheme) && |
[email protected] | 4654bfe | 2013-08-26 03:36:58 | [diff] [blame] | 925 | !LowerCaseEqualsASCII(input_.scheme(), content::kHttpsScheme) && |
[email protected] | 72aa4c2 | 2013-12-01 15:09:35 | [diff] [blame] | 926 | !LowerCaseEqualsASCII(input_.scheme(), content::kFtpScheme)) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 927 | return (input_.type() == AutocompleteInput::QUERY); |
| 928 | |
| 929 | // Don't send URLs with usernames, queries or refs. Some of these are |
| 930 | // private, and the Suggest server is unlikely to have any useful results |
| 931 | // for any of them. Also don't send URLs with ports, as we may initially |
| 932 | // think that a username + password is a host + port (and we don't want to |
| 933 | // send usernames/passwords), and even if the port really is a port, the |
| 934 | // server is once again unlikely to have and useful results. |
[email protected] | 825e16f | 2013-09-30 23:52:58 | [diff] [blame] | 935 | // Note that we only block based on refs if the input is URL-typed, as search |
| 936 | // queries can legitimately have #s in them which the URL parser |
| 937 | // overaggressively categorizes as a url with a ref. |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 938 | const url_parse::Parsed& parts = input_.parts(); |
| 939 | if (parts.username.is_nonempty() || parts.port.is_nonempty() || |
[email protected] | 825e16f | 2013-09-30 23:52:58 | [diff] [blame] | 940 | parts.query.is_nonempty() || |
| 941 | (parts.ref.is_nonempty() && (input_.type() == AutocompleteInput::URL))) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 942 | return false; |
| 943 | |
| 944 | // Don't send anything for https except the hostname. Hostnames are OK |
| 945 | // because they are visible when the TCP connection is established, but the |
| 946 | // specific path may reveal private information. |
[email protected] | 4654bfe | 2013-08-26 03:36:58 | [diff] [blame] | 947 | if (LowerCaseEqualsASCII(input_.scheme(), content::kHttpsScheme) && |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 948 | parts.path.is_nonempty()) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 949 | return false; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 950 | |
| 951 | return true; |
| 952 | } |
| 953 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 954 | void SearchProvider::StopSuggest() { |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 955 | // Increment the appropriate field in the histogram by the number of |
| 956 | // pending requests that were invalidated. |
| 957 | for (int i = 0; i < suggest_results_pending_; i++) |
| 958 | LogOmniboxSuggestRequest(REQUEST_INVALIDATED); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 959 | suggest_results_pending_ = 0; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 960 | timer_.Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 961 | // Stop any in-progress URL fetches. |
| 962 | keyword_fetcher_.reset(); |
| 963 | default_fetcher_.reset(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 964 | } |
| 965 | |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 966 | void SearchProvider::ClearAllResults() { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 967 | keyword_results_.Clear(); |
| 968 | default_results_.Clear(); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 969 | } |
| 970 | |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 971 | void SearchProvider::RemoveAllStaleResults() { |
[email protected] | dc735c0 | 2013-11-12 23:23:41 | [diff] [blame] | 972 | // We only need to remove stale results (which ensures the top-scoring |
| 973 | // match is inlineable) if the user is not in reorder mode. In reorder |
| 974 | // mode, the autocomplete system will reorder results to make sure the |
| 975 | // top result is inlineable. |
| 976 | const bool omnibox_will_reorder_for_legal_default_match = |
| 977 | OmniboxFieldTrial::ReorderForLegalDefaultMatch( |
| 978 | input_.current_page_classification()); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 979 | // In theory it would be better to run an algorithm like that in |
| 980 | // RemoveStaleResults(...) below that uses all four results lists |
| 981 | // and both verbatim scores at once. However, that will be much |
| 982 | // more complicated for little obvious gain. For code simplicity |
| 983 | // and ease in reasoning about the invariants involved, this code |
| 984 | // removes stales results from the keyword provider and default |
| 985 | // provider independently. |
[email protected] | dc735c0 | 2013-11-12 23:23:41 | [diff] [blame] | 986 | if (!omnibox_will_reorder_for_legal_default_match) { |
| 987 | RemoveStaleResults(input_.text(), GetVerbatimRelevance(NULL), |
| 988 | &default_results_.suggest_results, |
| 989 | &default_results_.navigation_results); |
| 990 | if (!keyword_input_.text().empty()) { |
| 991 | RemoveStaleResults(keyword_input_.text(), |
| 992 | GetKeywordVerbatimRelevance(NULL), |
| 993 | &keyword_results_.suggest_results, |
| 994 | &keyword_results_.navigation_results); |
| 995 | } |
| 996 | } |
| 997 | if (keyword_input_.text().empty()) { |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 998 | // User is either in keyword mode with a blank input or out of |
| 999 | // keyword mode entirely. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1000 | keyword_results_.Clear(); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 1001 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1002 | } |
| 1003 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1004 | void SearchProvider::ApplyCalculatedRelevance() { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1005 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 1006 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 1007 | ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results); |
| 1008 | ApplyCalculatedNavigationRelevance(&default_results_.navigation_results); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1009 | default_results_.verbatim_relevance = -1; |
| 1010 | keyword_results_.verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1011 | } |
| 1012 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1013 | void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1014 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1015 | SuggestResult& result = (*list)[i]; |
| 1016 | result.set_relevance( |
| 1017 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 1018 | (list->size() - i - 1)); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1019 | result.set_relevance_from_server(false); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1023 | void SearchProvider::ApplyCalculatedNavigationRelevance( |
| 1024 | NavigationResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1025 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1026 | NavigationResult& result = (*list)[i]; |
| 1027 | result.set_relevance( |
| 1028 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 1029 | (list->size() - i - 1)); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1030 | result.set_relevance_from_server(false); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 1034 | net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 1035 | int id, |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 1036 | const TemplateURL* template_url, |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 1037 | const AutocompleteInput& input) { |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 1038 | if (!template_url || template_url->suggestions_url().empty()) |
| 1039 | return NULL; |
| 1040 | |
| 1041 | // Bail if the suggestion URL is invalid with the given replacements. |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 1042 | TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
| 1043 | search_term_args.cursor_position = input.cursor_position(); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 1044 | search_term_args.page_classification = input.current_page_classification(); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 1045 | GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 1046 | search_term_args)); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 1047 | if (!suggest_url.is_valid()) |
| 1048 | return NULL; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 1049 | // Send the current page URL if user setting and URL requirements are met and |
| 1050 | // the user is in the field trial. |
| 1051 | if (CanSendURL(current_page_url_, suggest_url, template_url, |
| 1052 | input.current_page_classification(), profile_) && |
| 1053 | OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) { |
| 1054 | search_term_args.current_page_url = current_page_url_.spec(); |
| 1055 | // Create the suggest URL again with the current page URL. |
| 1056 | suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms( |
| 1057 | search_term_args)); |
| 1058 | } |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 1059 | |
| 1060 | suggest_results_pending_++; |
| 1061 | LogOmniboxSuggestRequest(REQUEST_SENT); |
| 1062 | |
| 1063 | net::URLFetcher* fetcher = |
| 1064 | net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 1065 | fetcher->SetRequestContext(profile_->GetRequestContext()); |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 1066 | fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 1067 | // Add Chrome experiment state to the request headers. |
| 1068 | net::HttpRequestHeaders headers; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 1069 | chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 1070 | fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 1071 | fetcher->SetExtraRequestHeaders(headers.ToString()); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1072 | fetcher->Start(); |
| 1073 | return fetcher; |
| 1074 | } |
| 1075 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1076 | scoped_ptr<base::Value> SearchProvider::DeserializeJsonData( |
| 1077 | std::string json_data) { |
[email protected] | 2e8c601 | 2013-12-03 12:00:01 | [diff] [blame] | 1078 | // The JSON response should be an array. |
| 1079 | for (size_t response_start_index = json_data.find("["), i = 0; |
| 1080 | response_start_index != std::string::npos && i < 5; |
| 1081 | response_start_index = json_data.find("[", 1), i++) { |
| 1082 | // Remove any XSSI guards to allow for JSON parsing. |
| 1083 | if (response_start_index > 0) |
| 1084 | json_data.erase(0, response_start_index); |
| 1085 | |
| 1086 | JSONStringValueSerializer deserializer(json_data); |
| 1087 | deserializer.set_allow_trailing_comma(true); |
| 1088 | int error_code = 0; |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1089 | scoped_ptr<base::Value> data(deserializer.Deserialize(&error_code, NULL)); |
[email protected] | 2e8c601 | 2013-12-03 12:00:01 | [diff] [blame] | 1090 | if (error_code == 0) |
| 1091 | return data.Pass(); |
| 1092 | } |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1093 | return scoped_ptr<base::Value>(); |
[email protected] | 2e8c601 | 2013-12-03 12:00:01 | [diff] [blame] | 1094 | } |
| 1095 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1096 | bool SearchProvider::ParseSuggestResults(base::Value* root_val, |
| 1097 | bool is_keyword) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1098 | base::string16 query; |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1099 | base::ListValue* root_list = NULL; |
| 1100 | base::ListValue* results_list = NULL; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1101 | const base::string16& input_text = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 1102 | is_keyword ? keyword_input_.text() : input_.text(); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1103 | if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1104 | (query != input_text) || !root_list->GetList(1, &results_list)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1105 | return false; |
| 1106 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1107 | // 3rd element: Description list. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1108 | base::ListValue* descriptions = NULL; |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1109 | root_list->GetList(2, &descriptions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1110 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1111 | // 4th element: Disregard the query URL list for now. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1112 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1113 | // Reset suggested relevance information from the default provider. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1114 | Results* results = is_keyword ? &keyword_results_ : &default_results_; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1115 | results->verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1116 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1117 | // 5th element: Optional key-value pairs from the Suggest server. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1118 | base::ListValue* types = NULL; |
| 1119 | base::ListValue* relevances = NULL; |
| 1120 | base::ListValue* suggestion_details = NULL; |
| 1121 | base::DictionaryValue* extras = NULL; |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1122 | int prefetch_index = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1123 | if (root_list->GetDictionary(4, &extras)) { |
| 1124 | extras->GetList("google:suggesttype", &types); |
| 1125 | |
[email protected] | eb8330d | 2013-06-15 00:55:08 | [diff] [blame] | 1126 | // Discard this list if its size does not match that of the suggestions. |
| 1127 | if (extras->GetList("google:suggestrelevance", &relevances) && |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1128 | (relevances->GetSize() != results_list->GetSize())) |
[email protected] | eb8330d | 2013-06-15 00:55:08 | [diff] [blame] | 1129 | relevances = NULL; |
| 1130 | extras->GetInteger("google:verbatimrelevance", |
| 1131 | &results->verbatim_relevance); |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 1132 | |
| 1133 | // Check if the active suggest field trial (if any) has triggered either |
| 1134 | // for the default provider or keyword provider. |
| 1135 | bool triggered = false; |
| 1136 | extras->GetBoolean("google:fieldtrialtriggered", &triggered); |
| 1137 | field_trial_triggered_ |= triggered; |
| 1138 | field_trial_triggered_in_session_ |= triggered; |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1139 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1140 | base::DictionaryValue* client_data = NULL; |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1141 | if (extras->GetDictionary("google:clientdata", &client_data) && client_data) |
| 1142 | client_data->GetInteger("phi", &prefetch_index); |
| 1143 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1144 | if (extras->GetList("google:suggestdetail", &suggestion_details) && |
| 1145 | suggestion_details->GetSize() != results_list->GetSize()) |
| 1146 | suggestion_details = NULL; |
| 1147 | |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1148 | // Store the metadata that came with the response in case we need to pass it |
| 1149 | // along with the prefetch query to Instant. |
| 1150 | JSONStringValueSerializer json_serializer(&results->metadata); |
| 1151 | json_serializer.Serialize(*extras); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1152 | } |
| 1153 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1154 | // Clear the previous results now that new results are available. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1155 | results->suggest_results.clear(); |
| 1156 | results->navigation_results.clear(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1157 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1158 | base::string16 suggestion; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1159 | std::string type; |
| 1160 | int relevance = -1; |
[email protected] | 3f38159 | 2013-11-05 20:43:51 | [diff] [blame] | 1161 | // Prohibit navsuggest in FORCED_QUERY mode. Users wants queries, not URLs. |
| 1162 | const bool allow_navsuggest = |
| 1163 | (is_keyword ? keyword_input_.type() : input_.type()) != |
| 1164 | AutocompleteInput::FORCED_QUERY; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1165 | for (size_t index = 0; results_list->GetString(index, &suggestion); ++index) { |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 1166 | // Google search may return empty suggestions for weird input characters, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1167 | // they make no sense at all and can cause problems in our code. |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1168 | if (suggestion.empty()) |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 1169 | continue; |
| 1170 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1171 | // Apply valid suggested relevance scores; discard invalid lists. |
| 1172 | if (relevances != NULL && !relevances->GetInteger(index, &relevance)) |
| 1173 | relevances = NULL; |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1174 | if (types && types->GetString(index, &type) && (type == "NAVIGATION")) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1175 | // Do not blindly trust the URL coming from the server to be valid. |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame^] | 1176 | GURL url(URLFixerUpper::FixupURL( |
| 1177 | base::UTF16ToUTF8(suggestion), std::string())); |
[email protected] | 3f38159 | 2013-11-05 20:43:51 | [diff] [blame] | 1178 | if (url.is_valid() && allow_navsuggest) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1179 | base::string16 title; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1180 | if (descriptions != NULL) |
| 1181 | descriptions->GetString(index, &title); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1182 | results->navigation_results.push_back(NavigationResult( |
| 1183 | *this, url, title, is_keyword, relevance, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1184 | } |
| 1185 | } else { |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1186 | AutocompleteMatchType::Type match_type = GetAutocompleteMatchType(type); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1187 | bool should_prefetch = static_cast<int>(index) == prefetch_index; |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 1188 | base::DictionaryValue* suggestion_detail = NULL; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1189 | base::string16 match_contents = suggestion; |
| 1190 | base::string16 annotation; |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1191 | std::string suggest_query_params; |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 1192 | std::string deletion_url; |
| 1193 | |
| 1194 | if (suggestion_details) { |
| 1195 | suggestion_details->GetDictionary(index, &suggestion_detail); |
| 1196 | if (suggestion_detail) { |
| 1197 | suggestion_detail->GetString("du", &deletion_url); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1198 | suggestion_detail->GetString("title", &match_contents) || |
| 1199 | suggestion_detail->GetString("t", &match_contents); |
| 1200 | suggestion_detail->GetString("annotation", &annotation) || |
| 1201 | suggestion_detail->GetString("a", &annotation); |
| 1202 | suggestion_detail->GetString("query_params", &suggest_query_params) || |
| 1203 | suggestion_detail->GetString("q", &suggest_query_params); |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 1204 | } |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1205 | } |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 1206 | |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1207 | // TODO(kochi): Improve calculator suggestion presentation. |
| 1208 | results->suggest_results.push_back(SuggestResult( |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1209 | suggestion, match_type, match_contents, annotation, |
| 1210 | suggest_query_params, deletion_url, is_keyword, relevance, true, |
| 1211 | should_prefetch)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
[email protected] | 43126b719 | 2013-11-14 18:10:34 | [diff] [blame] | 1215 | // Ignore suggested scores for non-keyword matches in keyword mode; if the |
| 1216 | // server is allowed to score these, it could interfere with the user's |
| 1217 | // ability to get good keyword results. |
| 1218 | const bool abandon_suggested_scores = |
| 1219 | !is_keyword && !providers_.keyword_provider().empty(); |
| 1220 | // Apply calculated relevance scores to suggestions if a valid list was |
| 1221 | // not provided or we're abandoning suggested scores entirely. |
| 1222 | if ((relevances == NULL) || abandon_suggested_scores) { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1223 | ApplyCalculatedSuggestRelevance(&results->suggest_results); |
| 1224 | ApplyCalculatedNavigationRelevance(&results->navigation_results); |
[email protected] | 43126b719 | 2013-11-14 18:10:34 | [diff] [blame] | 1225 | // If abandoning scores entirely, also abandon the verbatim score. |
| 1226 | if (abandon_suggested_scores) |
| 1227 | results->verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1228 | } |
[email protected] | 43126b719 | 2013-11-14 18:10:34 | [diff] [blame] | 1229 | |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1230 | // Keep the result lists sorted. |
| 1231 | const CompareScoredResults comparator = CompareScoredResults(); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1232 | std::stable_sort(results->suggest_results.begin(), |
| 1233 | results->suggest_results.end(), |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1234 | comparator); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1235 | std::stable_sort(results->navigation_results.begin(), |
| 1236 | results->navigation_results.end(), |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1237 | comparator); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1238 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1239 | } |
| 1240 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1241 | void SearchProvider::ConvertResultsToAutocompleteMatches() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1242 | // Convert all the results to matches and add them to a map, so we can keep |
| 1243 | // the most relevant match for each result. |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1244 | base::TimeTicks start_time(base::TimeTicks::Now()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1245 | MatchMap map; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1246 | const base::Time no_time; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1247 | int did_not_accept_keyword_suggestion = |
| 1248 | keyword_results_.suggest_results.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1249 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 1250 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1251 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1252 | bool relevance_from_server; |
| 1253 | int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1254 | int did_not_accept_default_suggestion = |
| 1255 | default_results_.suggest_results.empty() ? |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1256 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 1257 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1258 | if (verbatim_relevance > 0) { |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1259 | SuggestResult verbatim( |
| 1260 | input_.text(), AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 1261 | input_.text(), base::string16(), std::string(), std::string(), |
| 1262 | false, verbatim_relevance, relevance_from_server, false); |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1263 | AddMatchToMap(verbatim, input_.text(), std::string(), |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1264 | did_not_accept_default_suggestion, &map); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1265 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1266 | if (!keyword_input_.text().empty()) { |
| 1267 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 1268 | // We only create the verbatim search query match for a keyword |
| 1269 | // if it's not an extension keyword. Extension keywords are handled |
| 1270 | // in KeywordProvider::Start(). (Extensions are complicated...) |
| 1271 | // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
| 1272 | // to the keyword verbatim search query. Do not create other matches |
| 1273 | // of type SEARCH_OTHER_ENGINE. |
[email protected] | bdcbcd8 | 2013-10-28 13:40:25 | [diff] [blame] | 1274 | if (keyword_url && |
| 1275 | (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1276 | bool keyword_relevance_from_server; |
| 1277 | const int keyword_verbatim_relevance = |
| 1278 | GetKeywordVerbatimRelevance(&keyword_relevance_from_server); |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1279 | if (keyword_verbatim_relevance > 0) { |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1280 | SuggestResult verbatim( |
| 1281 | keyword_input_.text(), AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
| 1282 | keyword_input_.text(), base::string16(), std::string(), |
| 1283 | std::string(), true, keyword_verbatim_relevance, |
| 1284 | keyword_relevance_from_server, false); |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1285 | AddMatchToMap(verbatim, keyword_input_.text(), std::string(), |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1286 | did_not_accept_keyword_suggestion, &map); |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1287 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1288 | } |
| 1289 | } |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1290 | AddHistoryResultsToMap(keyword_history_results_, true, |
| 1291 | did_not_accept_keyword_suggestion, &map); |
| 1292 | AddHistoryResultsToMap(default_history_results_, false, |
| 1293 | did_not_accept_default_suggestion, &map); |
| 1294 | |
[email protected] | d1cb6a82 | 2013-09-18 19:43:00 | [diff] [blame] | 1295 | AddSuggestResultsToMap(keyword_results_.suggest_results, |
| 1296 | keyword_results_.metadata, &map); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1297 | AddSuggestResultsToMap(default_results_.suggest_results, |
| 1298 | default_results_.metadata, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1299 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1300 | ACMatches matches; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1301 | for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1302 | matches.push_back(i->second); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1303 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1304 | AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches); |
| 1305 | AddNavigationResultsToMatches(default_results_.navigation_results, &matches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1306 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1307 | // Now add the most relevant matches to |matches_|. We take up to kMaxMatches |
| 1308 | // suggest/navsuggest matches, regardless of origin. If Instant Extended is |
| 1309 | // enabled and we have server-provided (and thus hopefully more accurate) |
| 1310 | // scores for some suggestions, we allow more of those, until we reach |
| 1311 | // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the |
| 1312 | // whole popup). |
| 1313 | // |
| 1314 | // We will always return any verbatim matches, no matter how we obtained their |
| 1315 | // scores, unless we have already accepted AutocompleteResult::kMaxMatches |
| 1316 | // higher-scoring matches under the conditions above. |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1317 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 1318 | "Omnibox.SearchProvider.NumMatchesToSort", matches.size(), 1, 50, 20); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1319 | std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); |
| 1320 | matches_.clear(); |
[email protected] | 3723e6e | 2012-06-11 21:06:56 | [diff] [blame] | 1321 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1322 | size_t num_suggestions = 0; |
| 1323 | for (ACMatches::const_iterator i(matches.begin()); |
| 1324 | (i != matches.end()) && |
| 1325 | (matches_.size() < AutocompleteResult::kMaxMatches); |
| 1326 | ++i) { |
| 1327 | // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword |
| 1328 | // verbatim result, so this condition basically means "if this match is a |
| 1329 | // suggestion of some sort". |
| 1330 | if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) && |
| 1331 | (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) { |
| 1332 | // If we've already hit the limit on non-server-scored suggestions, and |
| 1333 | // this isn't a server-scored suggestion we can add, skip it. |
| 1334 | if ((num_suggestions >= kMaxMatches) && |
| 1335 | (!chrome::IsInstantExtendedAPIEnabled() || |
| 1336 | (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) { |
| 1337 | continue; |
| 1338 | } |
| 1339 | |
| 1340 | ++num_suggestions; |
| 1341 | } |
| 1342 | |
| 1343 | matches_.push_back(*i); |
| 1344 | } |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1345 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime", |
| 1346 | base::TimeTicks::Now() - start_time); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1347 | } |
| 1348 | |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1349 | ACMatches::const_iterator SearchProvider::FindTopMatch( |
| 1350 | bool autocomplete_result_will_reorder_for_default_match) const { |
| 1351 | if (!autocomplete_result_will_reorder_for_default_match) |
| 1352 | return matches_.begin(); |
| 1353 | ACMatches::const_iterator it = matches_.begin(); |
| 1354 | while ((it != matches_.end()) && !it->allowed_to_be_default_match) |
| 1355 | ++it; |
| 1356 | return it; |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1357 | } |
| 1358 | |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1359 | bool SearchProvider::IsTopMatchNavigationInKeywordMode( |
| 1360 | bool autocomplete_result_will_reorder_for_default_match) const { |
| 1361 | ACMatches::const_iterator first_match = |
| 1362 | FindTopMatch(autocomplete_result_will_reorder_for_default_match); |
| 1363 | return !providers_.keyword_provider().empty() && |
| 1364 | (first_match != matches_.end()) && |
| 1365 | (first_match->type == AutocompleteMatchType::NAVSUGGEST); |
| 1366 | } |
| 1367 | |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1368 | bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const { |
| 1369 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 1370 | // If the user is not in keyword mode, return true to say that this |
| 1371 | // constraint is not violated. |
| 1372 | if (keyword_url == NULL) |
| 1373 | return true; |
| 1374 | for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end(); |
| 1375 | ++it) { |
| 1376 | if ((it->keyword == keyword_url->keyword()) && |
| 1377 | it->allowed_to_be_default_match) |
| 1378 | return true; |
| 1379 | } |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1383 | bool SearchProvider::IsTopMatchScoreTooLow( |
| 1384 | bool autocomplete_result_will_reorder_for_default_match) const { |
| 1385 | // In reorder mode, there's no such thing as a score that's too low. |
| 1386 | if (autocomplete_result_will_reorder_for_default_match) |
| 1387 | return false; |
| 1388 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1389 | // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState() |
| 1390 | // rather than CalculateRelevanceForVerbatim() because the latter returns |
| 1391 | // a very low score (250) if keyword mode is active. This is because |
| 1392 | // when keyword mode is active the user probably wants the keyword matches, |
| 1393 | // not matches from the default provider. Hence, we use the version of |
| 1394 | // the function that ignores whether keyword mode is active. This allows |
| 1395 | // SearchProvider to maintain its contract with the AutocompleteController |
| 1396 | // that it will always provide an inlineable match with a reasonable |
| 1397 | // score. |
| 1398 | return matches_.front().relevance < |
| 1399 | CalculateRelevanceForVerbatimIgnoringKeywordModeState(); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1400 | } |
| 1401 | |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1402 | bool SearchProvider::IsTopMatchSearchWithURLInput( |
| 1403 | bool autocomplete_result_will_reorder_for_default_match) const { |
| 1404 | ACMatches::const_iterator first_match = |
| 1405 | FindTopMatch(autocomplete_result_will_reorder_for_default_match); |
| 1406 | return (input_.type() == AutocompleteInput::URL) && |
| 1407 | (first_match != matches_.end()) && |
| 1408 | (first_match->relevance > CalculateRelevanceForVerbatim()) && |
| 1409 | (first_match->type != AutocompleteMatchType::NAVSUGGEST); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1410 | } |
| 1411 | |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 1412 | bool SearchProvider::HasValidDefaultMatch( |
| 1413 | bool autocomplete_result_will_reorder_for_default_match) const { |
| 1414 | // One of the SearchProvider matches may need to be the overall default. If |
| 1415 | // AutocompleteResult is allowed to reorder matches, this means we simply |
| 1416 | // need at least one match in the list to be |allowed_to_be_default_match|. |
| 1417 | // If no reordering is possible, however, then our first match needs to have |
| 1418 | // this flag. |
| 1419 | for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end(); |
| 1420 | ++it) { |
| 1421 | if (it->allowed_to_be_default_match) |
| 1422 | return true; |
| 1423 | if (!autocomplete_result_will_reorder_for_default_match) |
| 1424 | return false; |
| 1425 | } |
| 1426 | return false; |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | void SearchProvider::UpdateMatches() { |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1430 | base::TimeTicks update_matches_start_time(base::TimeTicks::Now()); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1431 | ConvertResultsToAutocompleteMatches(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1432 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1433 | // Check constraints that may be violated by suggested relevances. |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1434 | if (!matches_.empty() && |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1435 | (default_results_.HasServerProvidedScores() || |
| 1436 | keyword_results_.HasServerProvidedScores())) { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1437 | // These blocks attempt to repair undesirable behavior by suggested |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1438 | // relevances with minimal impact, preserving other suggested relevances. |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1439 | |
| 1440 | // True if the omnibox will reorder matches as necessary to make the first |
| 1441 | // one something that is allowed to be the default match. |
| 1442 | const bool omnibox_will_reorder_for_legal_default_match = |
| 1443 | OmniboxFieldTrial::ReorderForLegalDefaultMatch( |
| 1444 | input_.current_page_classification()); |
| 1445 | if (IsTopMatchNavigationInKeywordMode( |
| 1446 | omnibox_will_reorder_for_legal_default_match)) { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1447 | // Correct the suggested relevance scores if the top match is a |
| 1448 | // navigation in keyword mode, since inlining a navigation match |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1449 | // would break the user out of keyword mode. This will only be |
| 1450 | // triggered in regular (non-reorder) mode; in reorder mode, |
| 1451 | // navigation matches are marked as not allowed to be the default |
| 1452 | // match and hence IsTopMatchNavigation() will always return false. |
| 1453 | DCHECK(!omnibox_will_reorder_for_legal_default_match); |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1454 | DemoteKeywordNavigationMatchesPastTopQuery(); |
| 1455 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1456 | DCHECK(!IsTopMatchNavigationInKeywordMode( |
| 1457 | omnibox_will_reorder_for_legal_default_match)); |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1458 | } |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1459 | if (!HasKeywordDefaultMatchInKeywordMode()) { |
| 1460 | // In keyword mode, disregard the keyword verbatim suggested relevance |
| 1461 | // if necessary so there at least one keyword match that's allowed to |
| 1462 | // be the default match. |
| 1463 | keyword_results_.verbatim_relevance = -1; |
| 1464 | ConvertResultsToAutocompleteMatches(); |
| 1465 | } |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1466 | if (IsTopMatchScoreTooLow(omnibox_will_reorder_for_legal_default_match)) { |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1467 | // Disregard the suggested verbatim relevance if the top score is below |
| 1468 | // the usual verbatim value. For example, a BarProvider may rely on |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 1469 | // SearchProvider's verbatim or inlineable matches for input "foo" (all |
| 1470 | // allowed to be default match) to always outrank its own lowly-ranked |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1471 | // "bar" matches that shouldn't be the default match. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1472 | default_results_.verbatim_relevance = -1; |
| 1473 | keyword_results_.verbatim_relevance = -1; |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1474 | ConvertResultsToAutocompleteMatches(); |
[email protected] | e6acd00 | 2012-06-16 22:27:47 | [diff] [blame] | 1475 | } |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1476 | if (IsTopMatchSearchWithURLInput( |
| 1477 | omnibox_will_reorder_for_legal_default_match)) { |
[email protected] | e6acd00 | 2012-06-16 22:27:47 | [diff] [blame] | 1478 | // Disregard the suggested search and verbatim relevances if the input |
| 1479 | // type is URL and the top match is a highly-ranked search suggestion. |
| 1480 | // For example, prevent a search for "foo.com" from outranking another |
| 1481 | // provider's navigation for "foo.com" or "foo.com/url_from_history". |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1482 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 1483 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 1484 | default_results_.verbatim_relevance = -1; |
| 1485 | keyword_results_.verbatim_relevance = -1; |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1486 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1487 | } |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 1488 | if (!HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match)) { |
| 1489 | // If the omnibox is not going to reorder results to put a legal default |
| 1490 | // match at the top, then this provider needs to guarantee that its top |
| 1491 | // scoring result is a legal default match (i.e., it's either a verbatim |
| 1492 | // match or inlinable). For example, input "foo" should not invoke a |
| 1493 | // search for "bar", which would happen if the "bar" search match |
| 1494 | // outranked all other matches. On the other hand, if the omnibox will |
| 1495 | // reorder matches as necessary to put a legal default match at the top, |
| 1496 | // all we need to guarantee is that SearchProvider returns a legal |
| 1497 | // default match. (The omnibox always needs at least one legal default |
| 1498 | // match, and it relies on SearchProvider to always return one.) |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1499 | ApplyCalculatedRelevance(); |
| 1500 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1501 | } |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1502 | DCHECK(!IsTopMatchNavigationInKeywordMode( |
| 1503 | omnibox_will_reorder_for_legal_default_match)); |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1504 | DCHECK(HasKeywordDefaultMatchInKeywordMode()); |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 1505 | DCHECK(!IsTopMatchScoreTooLow( |
| 1506 | omnibox_will_reorder_for_legal_default_match)); |
| 1507 | DCHECK(!IsTopMatchSearchWithURLInput( |
| 1508 | omnibox_will_reorder_for_legal_default_match)); |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 1509 | DCHECK(HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match)); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1510 | } |
| 1511 | |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1512 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 1513 | if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) { |
| 1514 | // If there is a keyword match that is allowed to be the default match, |
| 1515 | // then prohibit default provider matches from being the default match lest |
| 1516 | // such matches cause the user to break out of keyword mode. |
| 1517 | for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); |
| 1518 | ++it) { |
| 1519 | if (it->keyword != keyword_url->keyword()) |
| 1520 | it->allowed_to_be_default_match = false; |
| 1521 | } |
| 1522 | } |
| 1523 | |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1524 | base::TimeTicks update_starred_start_time(base::TimeTicks::Now()); |
[email protected] | cc63dea | 2008-08-21 20:56:31 | [diff] [blame] | 1525 | UpdateStarredStateOfMatches(); |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1526 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime", |
| 1527 | base::TimeTicks::Now() - update_starred_start_time); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1528 | UpdateDone(); |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1529 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateMatchesTime", |
| 1530 | base::TimeTicks::Now() - update_matches_start_time); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | void SearchProvider::AddNavigationResultsToMatches( |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1534 | const NavigationResults& navigation_results, |
| 1535 | ACMatches* matches) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1536 | for (NavigationResults::const_iterator it = navigation_results.begin(); |
| 1537 | it != navigation_results.end(); ++it) { |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1538 | matches->push_back(NavigationToMatch(*it)); |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1539 | // In the absence of suggested relevance scores, use only the single |
| 1540 | // highest-scoring result. (The results are already sorted by relevance.) |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1541 | if (!it->relevance_from_server()) |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1542 | return; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, |
| 1547 | bool is_keyword, |
| 1548 | int did_not_accept_suggestion, |
| 1549 | MatchMap* map) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1550 | if (results.empty()) |
| 1551 | return; |
| 1552 | |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1553 | base::TimeTicks start_time(base::TimeTicks::Now()); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1554 | bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() || |
| 1555 | (input_.type() == AutocompleteInput::URL); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1556 | const base::string16& input_text = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 1557 | is_keyword ? keyword_input_.text() : input_.text(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1558 | bool input_multiple_words = HasMultipleWords(input_text); |
| 1559 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1560 | SuggestResults scored_results; |
| 1561 | if (!prevent_inline_autocomplete && input_multiple_words) { |
| 1562 | // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit |
| 1563 | // queries if the input also has multiple words. But if we were already |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1564 | // autocompleting a multi-word, multi-visit query, and the current input is |
| 1565 | // still a prefix of it, then changing the autocompletion suddenly feels |
| 1566 | // wrong. To detect this case, first score as if only one word has been |
| 1567 | // typed, then check for a best result that is an autocompleted, multi-word |
| 1568 | // query. If we find one, then just keep that score set. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1569 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 1570 | false, input_text, is_keyword); |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1571 | if ((scored_results.front().relevance() < |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1572 | AutocompleteResult::kLowestDefaultScore) || |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1573 | !HasMultipleWords(scored_results.front().suggestion())) |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1574 | scored_results.clear(); // Didn't detect the case above, score normally. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1575 | } |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1576 | if (scored_results.empty()) |
| 1577 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 1578 | input_multiple_words, input_text, |
| 1579 | is_keyword); |
| 1580 | for (SuggestResults::const_iterator i(scored_results.begin()); |
| 1581 | i != scored_results.end(); ++i) { |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1582 | AddMatchToMap(*i, input_text, std::string(), |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1583 | did_not_accept_suggestion, map); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1584 | } |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 1585 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", |
| 1586 | base::TimeTicks::Now() - start_time); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1587 | } |
| 1588 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1589 | SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1590 | const HistoryResults& results, |
| 1591 | bool base_prevent_inline_autocomplete, |
| 1592 | bool input_multiple_words, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1593 | const base::string16& input_text, |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1594 | bool is_keyword) { |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 1595 | AutocompleteClassifier* classifier = |
| 1596 | AutocompleteClassifierFactory::GetForProfile(profile_); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1597 | SuggestResults scored_results; |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1598 | const bool prevent_search_history_inlining = |
| 1599 | OmniboxFieldTrial::SearchHistoryPreventInlining( |
| 1600 | input_.current_page_classification()); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1601 | for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
| 1602 | ++i) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1603 | // Don't autocomplete multi-word queries that have only been seen once |
| 1604 | // unless the user has typed more than one word. |
| 1605 | bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || |
| 1606 | (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term)); |
| 1607 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1608 | // Don't autocomplete search terms that would normally be treated as URLs |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1609 | // when typed. For example, if the user searched for "google.com" and types |
| 1610 | // "goog", don't autocomplete to the search term "google.com". Otherwise, |
| 1611 | // the input will look like a URL but act like a search, which is confusing. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 1612 | // NOTE: We don't check this in the following cases: |
| 1613 | // * When inline autocomplete is disabled, we won't be inline |
| 1614 | // autocompleting this term, so we don't need to worry about confusion as |
| 1615 | // much. This also prevents calling Classify() again from inside the |
| 1616 | // classifier (which will corrupt state and likely crash), since the |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1617 | // classifier always disables inline autocomplete. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 1618 | // * When the user has typed the whole term, the "what you typed" history |
| 1619 | // match will outrank us for URL-like inputs anyway, so we need not do |
| 1620 | // anything special. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1621 | if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1622 | AutocompleteMatch match; |
[email protected] | f608ea10 | 2013-03-18 15:08:09 | [diff] [blame] | 1623 | classifier->Classify(i->term, false, false, &match, NULL); |
[email protected] | 2905f74 | 2011-10-13 03:51:58 | [diff] [blame] | 1624 | prevent_inline_autocomplete = |
[email protected] | 749e7ae0 | 2012-09-05 18:47:46 | [diff] [blame] | 1625 | !AutocompleteMatch::IsSearchType(match.type); |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1626 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1627 | |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1628 | int relevance = CalculateRelevanceForHistory( |
| 1629 | i->time, is_keyword, !prevent_inline_autocomplete, |
| 1630 | prevent_search_history_inlining); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1631 | scored_results.push_back(SuggestResult( |
| 1632 | i->term, AutocompleteMatchType::SEARCH_HISTORY, i->term, |
| 1633 | base::string16(), std::string(), std::string(), is_keyword, relevance, |
| 1634 | false, false)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1635 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1636 | |
| 1637 | // History returns results sorted for us. However, we may have docked some |
| 1638 | // results' scores, so things are no longer in order. Do a stable sort to get |
| 1639 | // things back in order without otherwise disturbing results with equal |
| 1640 | // scores, then force the scores to be unique, so that the order in which |
| 1641 | // they're shown is deterministic. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1642 | std::stable_sort(scored_results.begin(), scored_results.end(), |
| 1643 | CompareScoredResults()); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1644 | int last_relevance = 0; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1645 | for (SuggestResults::iterator i(scored_results.begin()); |
| 1646 | i != scored_results.end(); ++i) { |
| 1647 | if ((i != scored_results.begin()) && (i->relevance() >= last_relevance)) |
| 1648 | i->set_relevance(last_relevance - 1); |
| 1649 | last_relevance = i->relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1650 | } |
| 1651 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1652 | return scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1653 | } |
| 1654 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1655 | void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results, |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1656 | const std::string& metadata, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1657 | MatchMap* map) { |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1658 | for (size_t i = 0; i < results.size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1659 | const bool is_keyword = results[i].from_keyword_provider(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1660 | const base::string16& input = is_keyword ? keyword_input_.text() |
| 1661 | : input_.text(); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1662 | AddMatchToMap(results[i], input, metadata, i, map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1663 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1664 | } |
| 1665 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1666 | int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1667 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 1668 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1669 | // and if it won't suppress verbatim, leaving no default provider matches. |
| 1670 | // Otherwise, if the default provider returned no matches and was still able |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1671 | // to suppress verbatim, the user would have no search/nav matches and may be |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1672 | // left unable to search using their default provider from the omnibox. |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1673 | // Check for results on each verbatim calculation, as results from older |
| 1674 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1675 | bool use_server_relevance = |
| 1676 | (default_results_.verbatim_relevance >= 0) && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1677 | !input_.prevent_inline_autocomplete() && |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1678 | ((default_results_.verbatim_relevance > 0) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1679 | !default_results_.suggest_results.empty() || |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1680 | !default_results_.navigation_results.empty()); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1681 | if (relevance_from_server) |
| 1682 | *relevance_from_server = use_server_relevance; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1683 | return use_server_relevance ? |
| 1684 | default_results_.verbatim_relevance : CalculateRelevanceForVerbatim(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1685 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1686 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1687 | int SearchProvider::CalculateRelevanceForVerbatim() const { |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 1688 | if (!providers_.keyword_provider().empty()) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1689 | return 250; |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1690 | return CalculateRelevanceForVerbatimIgnoringKeywordModeState(); |
| 1691 | } |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1692 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1693 | int SearchProvider:: |
| 1694 | CalculateRelevanceForVerbatimIgnoringKeywordModeState() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1695 | switch (input_.type()) { |
| 1696 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1697 | case AutocompleteInput::QUERY: |
| 1698 | case AutocompleteInput::FORCED_QUERY: |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 1699 | return kNonURLVerbatimRelevance; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1700 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1701 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1702 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1703 | |
| 1704 | default: |
| 1705 | NOTREACHED(); |
| 1706 | return 0; |
| 1707 | } |
| 1708 | } |
| 1709 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1710 | int SearchProvider::GetKeywordVerbatimRelevance( |
| 1711 | bool* relevance_from_server) const { |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1712 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 1713 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
| 1714 | // and if it won't suppress verbatim, leaving no keyword provider matches. |
| 1715 | // Otherwise, if the keyword provider returned no matches and was still able |
| 1716 | // to suppress verbatim, the user would have no search/nav matches and may be |
| 1717 | // left unable to search using their keyword provider from the omnibox. |
| 1718 | // Check for results on each verbatim calculation, as results from older |
| 1719 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1720 | bool use_server_relevance = |
| 1721 | (keyword_results_.verbatim_relevance >= 0) && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1722 | !input_.prevent_inline_autocomplete() && |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1723 | ((keyword_results_.verbatim_relevance > 0) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1724 | !keyword_results_.suggest_results.empty() || |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1725 | !keyword_results_.navigation_results.empty()); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1726 | if (relevance_from_server) |
| 1727 | *relevance_from_server = use_server_relevance; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1728 | return use_server_relevance ? |
| 1729 | keyword_results_.verbatim_relevance : |
| 1730 | CalculateRelevanceForKeywordVerbatim(keyword_input_.type(), |
| 1731 | keyword_input_.prefer_keyword()); |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1732 | } |
| 1733 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1734 | int SearchProvider::CalculateRelevanceForHistory( |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1735 | const base::Time& time, |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1736 | bool is_keyword, |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1737 | bool use_aggressive_method, |
| 1738 | bool prevent_search_history_inlining) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1739 | // The relevance of past searches falls off over time. There are two distinct |
| 1740 | // equations used. If the first equation is used (searches to the primary |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1741 | // provider that we want to score aggressively), the score is in the range |
| 1742 | // 1300-1599 (unless |prevent_search_history_inlining|, in which case |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1743 | // it's in the range 1200-1299). If the second equation is used the |
| 1744 | // relevance of a search 15 minutes ago is discounted 50 points, while the |
| 1745 | // relevance of a search two weeks ago is discounted 450 points. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1746 | double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0); |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1747 | bool is_primary_provider = is_keyword || !providers_.has_keyword_provider(); |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1748 | if (is_primary_provider && use_aggressive_method) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1749 | // Searches with the past two days get a different curve. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1750 | const double autocomplete_time = 2 * 24 * 60 * 60; |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1751 | if (elapsed_time < autocomplete_time) { |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1752 | int max_score = is_keyword ? 1599 : 1399; |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1753 | if (prevent_search_history_inlining) |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1754 | max_score = 1299; |
| 1755 | return max_score - static_cast<int>(99 * |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1756 | std::pow(elapsed_time / autocomplete_time, 2.5)); |
| 1757 | } |
| 1758 | elapsed_time -= autocomplete_time; |
| 1759 | } |
| 1760 | |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 1761 | const int score_discount = |
| 1762 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1763 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 1764 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 1765 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1766 | int base_score; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1767 | if (is_primary_provider) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1768 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1769 | else |
| 1770 | base_score = 200; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1771 | return std::max(0, base_score - score_discount); |
| 1772 | } |
| 1773 | |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1774 | void SearchProvider::AddMatchToMap(const SuggestResult& result, |
| 1775 | const base::string16& input_text, |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1776 | const std::string& metadata, |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1777 | int accepted_suggestion, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1778 | MatchMap* map) { |
[email protected] | c4a9d4c | 2013-08-10 07:06:27 | [diff] [blame] | 1779 | // On non-mobile, ask the instant controller for the appropriate start margin. |
| 1780 | // On mobile the start margin is unused, so leave the value as default there. |
| 1781 | int omnibox_start_margin = chrome::kDisableStartMargin; |
| 1782 | #if !defined(OS_ANDROID) && !defined(IOS) |
| 1783 | if (chrome::IsInstantExtendedAPIEnabled()) { |
| 1784 | Browser* browser = |
| 1785 | chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop()); |
| 1786 | if (browser && browser->instant_controller() && |
| 1787 | browser->instant_controller()->instant()) { |
| 1788 | omnibox_start_margin = |
| 1789 | browser->instant_controller()->instant()->omnibox_bounds().x(); |
| 1790 | } |
| 1791 | } |
| 1792 | #endif // !defined(OS_ANDROID) && !defined(IOS) |
| 1793 | |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1794 | const TemplateURL* template_url = result.from_keyword_provider() ? |
[email protected] | bba9e63 | 2013-06-28 22:52:19 | [diff] [blame] | 1795 | providers_.GetKeywordProviderURL() : providers_.GetDefaultProviderURL(); |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1796 | AutocompleteMatch match = CreateSearchSuggestion( |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 1797 | this, input_, input_text, result.relevance(), result.type(), |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1798 | result.from_keyword_provider(), result.match_contents(), |
| 1799 | result.annotation(), template_url, result.suggestion(), |
| 1800 | result.suggest_query_params(), accepted_suggestion, omnibox_start_margin, |
| 1801 | !result.from_keyword_provider() || providers_.default_provider().empty()); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 1802 | if (!match.destination_url.is_valid()) |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 1803 | return; |
[email protected] | a048de8a | 2013-10-02 18:30:06 | [diff] [blame] | 1804 | match.search_terms_args->bookmark_bar_pinned = |
| 1805 | profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1806 | match.RecordAdditionalInfo(kRelevanceFromServerKey, |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1807 | result.relevance_from_server() ? kTrue : kFalse); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1808 | match.RecordAdditionalInfo(kShouldPrefetchKey, |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1809 | result.should_prefetch() ? kTrue : kFalse); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1810 | |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1811 | if (!result.deletion_url().empty()) { |
| 1812 | GURL url(match.destination_url.GetOrigin().Resolve(result.deletion_url())); |
[email protected] | f8c43e8 | 2013-12-06 12:46:23 | [diff] [blame] | 1813 | if (url.is_valid()) { |
| 1814 | match.RecordAdditionalInfo(kDeletionUrlKey, url.spec()); |
| 1815 | match.deletable = true; |
| 1816 | } |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 1817 | } |
| 1818 | |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1819 | // Metadata is needed only for prefetching queries. |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1820 | if (result.should_prefetch()) |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1821 | match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 1822 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1823 | // Try to add |match| to |map|. If a match for |query_string| is already in |
| 1824 | // |map|, replace it if |match| is more relevant. |
| 1825 | // NOTE: Keep this ToLower() call in sync with url_database.cc. |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1826 | MatchKey match_key( |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1827 | std::make_pair(base::i18n::ToLower(result.suggestion()), |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1828 | match.search_terms_args->suggest_query_params)); |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1829 | const std::pair<MatchMap::iterator, bool> i( |
[email protected] | 621ade06 | 2013-10-28 06:27:43 | [diff] [blame] | 1830 | map->insert(std::make_pair(match_key, match))); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1831 | |
[email protected] | 866f02e | 2013-12-10 17:12:05 | [diff] [blame] | 1832 | bool should_prefetch = result.should_prefetch(); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1833 | if (!i.second) { |
| 1834 | // NOTE: We purposefully do a direct relevance comparison here instead of |
| 1835 | // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items |
| 1836 | // added first" rather than "items alphabetically first" when the scores are |
| 1837 | // equal. The only case this matters is when a user has results with the |
| 1838 | // same score that differ only by capitalization; because the history system |
| 1839 | // returns results sorted by recency, this means we'll pick the most |
| 1840 | // recent such result even if the precision of our relevance score is too |
| 1841 | // low to distinguish the two. |
| 1842 | if (match.relevance > i.first->second.relevance) { |
| 1843 | i.first->second = match; |
| 1844 | } else if (match.keyword == i.first->second.keyword) { |
| 1845 | // Old and new matches are from the same search provider. It is okay to |
| 1846 | // record one match's prefetch data onto a different match (for the same |
| 1847 | // query string) for the following reasons: |
| 1848 | // 1. Because the suggest server only sends down a query string from which |
| 1849 | // we construct a URL, rather than sending a full URL, and because we |
| 1850 | // construct URLs from query strings in the same way every time, the URLs |
| 1851 | // for the two matches will be the same. Therefore, we won't end up |
| 1852 | // prefetching something the server didn't intend. |
| 1853 | // 2. Presumably the server sets the prefetch bit on a match it things is |
| 1854 | // sufficiently relevant that the user is likely to choose it. Surely |
| 1855 | // setting the prefetch bit on a match of even higher relevance won't |
| 1856 | // violate this assumption. |
| 1857 | should_prefetch |= ShouldPrefetch(i.first->second); |
| 1858 | i.first->second.RecordAdditionalInfo(kShouldPrefetchKey, |
| 1859 | should_prefetch ? kTrue : kFalse); |
| 1860 | if (should_prefetch) |
| 1861 | i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
| 1862 | } |
| 1863 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | AutocompleteMatch SearchProvider::NavigationToMatch( |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1867 | const NavigationResult& navigation) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1868 | const base::string16& input = navigation.from_keyword_provider() ? |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1869 | keyword_input_.text() : input_.text(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1870 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1871 | AutocompleteMatchType::NAVSUGGEST); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1872 | match.destination_url = navigation.url(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1873 | |
| 1874 | // First look for the user's input inside the fill_into_edit as it would be |
| 1875 | // without trimming the scheme, so we can find matches at the beginning of the |
| 1876 | // scheme. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1877 | const base::string16& untrimmed_fill_into_edit = navigation.formatted_url(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1878 | const URLPrefix* prefix = |
| 1879 | URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input); |
| 1880 | size_t match_start = (prefix == NULL) ? |
| 1881 | untrimmed_fill_into_edit.find(input) : prefix->prefix.length(); |
| 1882 | size_t inline_autocomplete_offset = (prefix == NULL) ? |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1883 | base::string16::npos : (match_start + input.length()); |
[email protected] | d2445c8 | 2013-11-04 22:28:35 | [diff] [blame] | 1884 | bool trim_http = !AutocompleteInput::HasHTTPScheme(input) && |
| 1885 | (!prefix || (match_start != 0)); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1886 | |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1887 | const std::string languages( |
| 1888 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 1889 | const net::FormatUrlTypes format_types = |
| 1890 | net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP); |
| 1891 | match.fill_into_edit += |
| 1892 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
| 1893 | net::FormatUrl(navigation.url(), languages, format_types, |
| 1894 | net::UnescapeRule::SPACES, NULL, NULL, |
| 1895 | &inline_autocomplete_offset)); |
[email protected] | 1411903 | 2013-11-07 08:14:26 | [diff] [blame] | 1896 | // Preserve the forced query '?' prefix in |match.fill_into_edit|. |
| 1897 | // Otherwise, user edits to a suggestion would show non-Search results. |
| 1898 | if (input_.type() == AutocompleteInput::FORCED_QUERY) { |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame^] | 1899 | match.fill_into_edit.insert(0, base::ASCIIToUTF16("?")); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1900 | if (inline_autocomplete_offset != base::string16::npos) |
[email protected] | 1411903 | 2013-11-07 08:14:26 | [diff] [blame] | 1901 | ++inline_autocomplete_offset; |
| 1902 | } |
[email protected] | 518024c | 2013-07-19 23:40:25 | [diff] [blame] | 1903 | if (!input_.prevent_inline_autocomplete() && |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1904 | (inline_autocomplete_offset != base::string16::npos)) { |
[email protected] | 518024c | 2013-07-19 23:40:25 | [diff] [blame] | 1905 | DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 1906 | // A navsuggestion can only be the default match when there is no |
| 1907 | // keyword provider active, lest it appear first and break the user |
| 1908 | // out of keyword mode. |
| 1909 | match.allowed_to_be_default_match = |
| 1910 | (providers_.GetKeywordProviderURL() == NULL); |
[email protected] | 518024c | 2013-07-19 23:40:25 | [diff] [blame] | 1911 | match.inline_autocompletion = |
| 1912 | match.fill_into_edit.substr(inline_autocomplete_offset); |
| 1913 | } |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1914 | |
| 1915 | match.contents = net::FormatUrl(navigation.url(), languages, |
| 1916 | format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); |
| 1917 | // If the first match in the untrimmed string was inside a scheme that we |
| 1918 | // trimmed, look for a subsequent match. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1919 | if (match_start == base::string16::npos) |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1920 | match_start = match.contents.find(input); |
| 1921 | // Safe if |match_start| is npos; also safe if the input is longer than the |
| 1922 | // remaining contents after |match_start|. |
| 1923 | AutocompleteMatch::ClassifyLocationInString(match_start, input.length(), |
| 1924 | match.contents.length(), ACMatchClassification::URL, |
| 1925 | &match.contents_class); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1926 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1927 | match.description = navigation.description(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1928 | AutocompleteMatch::ClassifyMatchInString(input, match.description, |
| 1929 | ACMatchClassification::NONE, &match.description_class); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1930 | |
| 1931 | match.RecordAdditionalInfo( |
| 1932 | kRelevanceFromServerKey, |
| 1933 | navigation.relevance_from_server() ? kTrue : kFalse); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1934 | match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1935 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1936 | return match; |
| 1937 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1938 | |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1939 | void SearchProvider::DemoteKeywordNavigationMatchesPastTopQuery() { |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1940 | // First, determine the maximum score of any keyword query match (verbatim or |
| 1941 | // query suggestion). |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1942 | bool relevance_from_server; |
| 1943 | int max_query_relevance = GetKeywordVerbatimRelevance(&relevance_from_server); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1944 | if (!keyword_results_.suggest_results.empty()) { |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1945 | const SuggestResult& top_keyword = keyword_results_.suggest_results.front(); |
| 1946 | const int suggest_relevance = top_keyword.relevance(); |
| 1947 | if (suggest_relevance > max_query_relevance) { |
| 1948 | max_query_relevance = suggest_relevance; |
| 1949 | relevance_from_server = top_keyword.relevance_from_server(); |
| 1950 | } else if (suggest_relevance == max_query_relevance) { |
| 1951 | relevance_from_server |= top_keyword.relevance_from_server(); |
| 1952 | } |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1953 | } |
| 1954 | // If no query is supposed to appear, then navigational matches cannot |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1955 | // be demoted past it. Get rid of suggested relevance scores for |
| 1956 | // navsuggestions and introduce the verbatim results again. The keyword |
| 1957 | // verbatim match will outscore the navsuggest matches. |
| 1958 | if (max_query_relevance == 0) { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1959 | ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results); |
| 1960 | ApplyCalculatedNavigationRelevance(&default_results_.navigation_results); |
| 1961 | keyword_results_.verbatim_relevance = -1; |
| 1962 | default_results_.verbatim_relevance = -1; |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1963 | return; |
| 1964 | } |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1965 | // Now we know we can enforce the minimum score constraint even after |
| 1966 | // the navigation matches are demoted. Proceed to demote the navigation |
| 1967 | // matches to enforce the query-must-come-first constraint. |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1968 | // Cap the relevance score of all results. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1969 | for (NavigationResults::iterator it = |
| 1970 | keyword_results_.navigation_results.begin(); |
| 1971 | it != keyword_results_.navigation_results.end(); ++it) { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1972 | if (it->relevance() < max_query_relevance) |
| 1973 | return; |
| 1974 | max_query_relevance = std::max(max_query_relevance - 1, 0); |
| 1975 | it->set_relevance(max_query_relevance); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1976 | it->set_relevance_from_server(relevance_from_server); |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1977 | } |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1978 | } |
| 1979 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1980 | void SearchProvider::UpdateDone() { |
[email protected] | 2cdf117 | 2012-08-26 12:21:33 | [diff] [blame] | 1981 | // We're done when the timer isn't running, there are no suggest queries |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 1982 | // pending, and we're not waiting on Instant. |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 1983 | done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1984 | } |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 1985 | |
| 1986 | bool SearchProvider::CanSendURL( |
| 1987 | const GURL& current_page_url, |
| 1988 | const GURL& suggest_url, |
| 1989 | const TemplateURL* template_url, |
| 1990 | AutocompleteInput::PageClassification page_classification, |
| 1991 | Profile* profile) { |
| 1992 | if (!current_page_url.is_valid()) |
| 1993 | return false; |
| 1994 | |
| 1995 | // TODO(hfung): Show Most Visited on NTP with appropriate verbatim |
| 1996 | // description when the user actively focuses on the omnibox as discussed in |
| 1997 | // crbug/305366 if Most Visited (or something similar) will launch. |
| 1998 | if ((page_classification == |
| 1999 | AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS) || |
| 2000 | (page_classification == |
| 2001 | AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS)) |
| 2002 | return false; |
| 2003 | |
| 2004 | // Only allow HTTP URLs or HTTPS URLs for the same domain as the search |
| 2005 | // provider. |
| 2006 | if ((current_page_url.scheme() != content::kHttpScheme) && |
| 2007 | ((current_page_url.scheme() != content::kHttpsScheme) || |
| 2008 | !net::registry_controlled_domains::SameDomainOrHost( |
| 2009 | current_page_url, suggest_url, |
| 2010 | net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) |
| 2011 | return false; |
| 2012 | |
| 2013 | // Make sure we are sending the suggest request through HTTPS to prevent |
| 2014 | // exposing the current page URL to networks before the search provider. |
| 2015 | if (!suggest_url.SchemeIs(content::kHttpsScheme)) |
| 2016 | return false; |
| 2017 | |
| 2018 | // Don't run if there's no profile or in incognito mode. |
| 2019 | if (profile == NULL || profile->IsOffTheRecord()) |
| 2020 | return false; |
| 2021 | |
| 2022 | // Don't run if we can't get preferences or search suggest is not enabled. |
| 2023 | PrefService* prefs = profile->GetPrefs(); |
| 2024 | if (!prefs->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 2025 | return false; |
| 2026 | |
| 2027 | // Only make the request if we know that the provider supports zero suggest |
| 2028 | // (currently only the prepopulated Google provider). |
| 2029 | if (template_url == NULL || !template_url->SupportsReplacement() || |
| 2030 | TemplateURLPrepopulateData::GetEngineType(*template_url) != |
| 2031 | SEARCH_ENGINE_GOOGLE) |
| 2032 | return false; |
| 2033 | |
| 2034 | // Check field trials and settings allow sending the URL on suggest requests. |
| 2035 | ProfileSyncService* service = |
| 2036 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
| 2037 | browser_sync::SyncPrefs sync_prefs(prefs); |
| 2038 | if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || |
| 2039 | service == NULL || |
| 2040 | !service->IsSyncEnabledAndLoggedIn() || |
| 2041 | !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( |
| 2042 | syncer::PROXY_TABS) || |
| 2043 | service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
| 2044 | return false; |
| 2045 | |
| 2046 | return true; |
| 2047 | } |