[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 6 | |
| 7 | #include "base/callback.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 8 | #include "base/i18n/case_conversion.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 9 | #include "base/json/json_string_value_serializer.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 11 | #include "base/prefs/pref_service.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | 2d91578 | 2013-08-29 09:50:21 | [diff] [blame] | 16 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 17 | #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 18 | #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 19 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 20 | #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 21 | #include "chrome/browser/autocomplete/history_url_provider.h" |
| 22 | #include "chrome/browser/autocomplete/search_provider.h" |
| 23 | #include "chrome/browser/autocomplete/url_prefix.h" |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 24 | #include "chrome/browser/history/history_types.h" |
| 25 | #include "chrome/browser/history/top_sites.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 26 | #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 27 | #include "chrome/browser/omnibox/omnibox_field_trial.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profile.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 29 | #include "chrome/browser/search/search.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 30 | #include "chrome/browser/search_engines/template_url_service.h" |
| 31 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | 4f3b446 | 2013-07-27 19:20:18 | [diff] [blame] | 32 | #include "chrome/common/net/url_fixer_upper.h" |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 34 | #include "chrome/common/url_constants.h" |
[email protected] | 0cfddf1b | 2014-03-12 01:46:00 | [diff] [blame] | 35 | #include "content/public/browser/user_metrics.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 36 | #include "net/base/escape.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 37 | #include "net/base/load_flags.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 38 | #include "net/base/net_util.h" |
| 39 | #include "net/http/http_request_headers.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 40 | #include "net/http/http_response_headers.h" |
| 41 | #include "net/url_request/url_fetcher.h" |
| 42 | #include "net/url_request/url_request_status.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 43 | #include "url/gurl.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 44 | |
| 45 | namespace { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 46 | |
| 47 | // TODO(hfung): The histogram code was copied and modified from |
| 48 | // search_provider.cc. Refactor and consolidate the code. |
| 49 | // We keep track in a histogram how many suggest requests we send, how |
| 50 | // many suggest requests we invalidate (e.g., due to a user typing |
| 51 | // another character), and how many replies we receive. |
| 52 | // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! *** |
| 53 | // (excluding the end-of-list enum value) |
| 54 | // We do not want values of existing enums to change or else it screws |
| 55 | // up the statistics. |
| 56 | enum ZeroSuggestRequestsHistogramValue { |
| 57 | ZERO_SUGGEST_REQUEST_SENT = 1, |
| 58 | ZERO_SUGGEST_REQUEST_INVALIDATED, |
| 59 | ZERO_SUGGEST_REPLY_RECEIVED, |
| 60 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE |
| 61 | }; |
| 62 | |
| 63 | void LogOmniboxZeroSuggestRequest( |
| 64 | ZeroSuggestRequestsHistogramValue request_value) { |
| 65 | UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
| 66 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
| 67 | } |
| 68 | |
| 69 | // The maximum relevance of the top match from this provider. |
| 70 | const int kDefaultVerbatimZeroSuggestRelevance = 1300; |
| 71 | |
| 72 | // Relevance value to use if it was not set explicitly by the server. |
| 73 | const int kDefaultZeroSuggestRelevance = 100; |
| 74 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 75 | } // namespace |
| 76 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 77 | // static |
| 78 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
| 79 | AutocompleteProviderListener* listener, |
| 80 | Profile* profile) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 81 | return new ZeroSuggestProvider(listener, profile); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 84 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
| 85 | bool /*minimal_changes*/) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 88 | void ZeroSuggestProvider::ResetSession() { |
| 89 | // The user has started editing in the omnibox, so leave |
| 90 | // |field_trial_triggered_in_session_| unchanged and set |
| 91 | // |field_trial_triggered_| to false since zero suggest is inactive now. |
| 92 | field_trial_triggered_ = false; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame^] | 93 | |
| 94 | // This call clears out |matches_| so that they don't pollute prefix-based |
| 95 | // queries. |
| 96 | // TODO(mariakhomenko): Change the model to clear |matches_| on Start() like |
| 97 | // all the other providers. |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 98 | Stop(true); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
| 102 | have_pending_request_ = false; |
| 103 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 104 | |
| 105 | std::string json_data; |
| 106 | source->GetResponseAsString(&json_data); |
| 107 | const bool request_succeeded = |
| 108 | source->GetStatus().is_success() && source->GetResponseCode() == 200; |
| 109 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 110 | if (request_succeeded) { |
[email protected] | 843e4afb | 2014-02-07 23:11:03 | [diff] [blame] | 111 | scoped_ptr<base::Value> data(DeserializeJsonData(json_data)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 112 | if (data.get()) |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 113 | ParseSuggestResults(*data.get(), false, &results_); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 114 | } |
| 115 | done_ = true; |
| 116 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 117 | ConvertResultsToAutocompleteMatches(); |
| 118 | if (!matches_.empty()) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 119 | listener_->OnProviderUpdate(true); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 25d7e74 | 2013-07-30 17:31:06 | [diff] [blame] | 122 | void ZeroSuggestProvider::StartZeroSuggest( |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 123 | const GURL& current_page_url, |
[email protected] | 25d7e74 | 2013-07-30 17:31:06 | [diff] [blame] | 124 | AutocompleteInput::PageClassification page_classification, |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 125 | const base::string16& permanent_text) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 126 | Stop(true); |
| 127 | field_trial_triggered_ = false; |
| 128 | field_trial_triggered_in_session_ = false; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 129 | permanent_text_ = permanent_text; |
| 130 | current_query_ = current_page_url.spec(); |
| 131 | current_page_classification_ = page_classification; |
| 132 | current_url_match_ = MatchForCurrentURL(); |
| 133 | |
| 134 | const TemplateURL* default_provider = |
| 135 | template_url_service_->GetDefaultSearchProvider(); |
| 136 | if (default_provider == NULL) |
| 137 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame^] | 138 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 139 | base::string16 prefix; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 140 | TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame^] | 141 | GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
| 142 | search_term_args)); |
| 143 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 144 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame^] | 145 | |
| 146 | // No need to send the current page URL in personalized suggest field trial. |
| 147 | if (CanSendURL(current_page_url, suggest_url, default_provider, |
| 148 | current_page_classification_, profile_) && |
| 149 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 150 | // Update suggest_url to include the current_page_url. |
| 151 | search_term_args.current_page_url = current_query_; |
| 152 | suggest_url = GURL(default_provider->suggestions_url_ref(). |
| 153 | ReplaceSearchTerms(search_term_args)); |
| 154 | } else if (!CanShowZeroSuggestWithoutSendingURL(suggest_url, |
| 155 | current_page_url)) { |
| 156 | return; |
| 157 | } |
| 158 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 159 | done_ = false; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 160 | // TODO(jered): Consider adding locally-sourced zero-suggestions here too. |
| 161 | // These may be useful on the NTP or more relevant to the user than server |
| 162 | // suggestions, if based on local browsing history. |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 163 | Run(suggest_url); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 166 | ZeroSuggestProvider::ZeroSuggestProvider( |
| 167 | AutocompleteProviderListener* listener, |
| 168 | Profile* profile) |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 169 | : BaseSearchProvider(listener, profile, |
| 170 | AutocompleteProvider::TYPE_ZERO_SUGGEST), |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 171 | template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 172 | have_pending_request_(false), |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 173 | weak_ptr_factory_(this) { |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 177 | } |
| 178 | |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 179 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL( |
| 180 | const SuggestResult& result) const { |
| 181 | // Zero suggest provider should not receive keyword results. |
| 182 | DCHECK(!result.from_keyword_provider()); |
| 183 | return template_url_service_->GetDefaultSearchProvider(); |
| 184 | } |
| 185 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 186 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
| 187 | return AutocompleteInput( |
| 188 | base::string16(), base::string16::npos, base::string16(), |
| 189 | GURL(current_query_), current_page_classification_, true, false, false, |
| 190 | AutocompleteInput::ALL_MATCHES); |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool ZeroSuggestProvider::ShouldAppendExtraParams( |
| 194 | const SuggestResult& result) const { |
| 195 | // We always use the default provider for search, so append the params. |
| 196 | return true; |
| 197 | } |
| 198 | |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 199 | void ZeroSuggestProvider::StopSuggest() { |
| 200 | if (have_pending_request_) |
| 201 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
| 202 | have_pending_request_ = false; |
| 203 | fetcher_.reset(); |
| 204 | } |
| 205 | |
| 206 | void ZeroSuggestProvider::ClearAllResults() { |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 207 | // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 208 | // value in the |results_| object. |verbatim_relevance| is used at the |
| 209 | // beginning of the next StartZeroSuggest() call to determine the current url |
| 210 | // match relevance. |
| 211 | results_.suggest_results.clear(); |
| 212 | results_.navigation_results.clear(); |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 213 | current_query_.clear(); |
| 214 | matches_.clear(); |
| 215 | } |
| 216 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 217 | int ZeroSuggestProvider::GetDefaultResultRelevance() const { |
| 218 | return kDefaultZeroSuggestRelevance; |
| 219 | } |
| 220 | |
[email protected] | 0cfddf1b | 2014-03-12 01:46:00 | [diff] [blame] | 221 | void ZeroSuggestProvider::RecordDeletionResult(bool success) { |
| 222 | if (success) { |
| 223 | content::RecordAction( |
| 224 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success")); |
| 225 | } else { |
| 226 | content::RecordAction( |
| 227 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure")); |
| 228 | } |
| 229 | } |
| 230 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 231 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 232 | const SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 233 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 234 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 235 | AddMatchToMap(results[i], std::string(), i, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 238 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 239 | const NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 240 | AutocompleteMatch match(this, navigation.relevance(), false, |
| 241 | AutocompleteMatchType::NAVSUGGEST); |
| 242 | match.destination_url = navigation.url(); |
| 243 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 244 | // Zero suggest results should always omit protocols and never appear bold. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 245 | const std::string languages( |
| 246 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 247 | match.contents = net::FormatUrl(navigation.url(), languages, |
| 248 | net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL); |
| 249 | match.fill_into_edit += |
| 250 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
| 251 | match.contents); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 252 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 253 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 254 | match.contents.length(), ACMatchClassification::URL, |
| 255 | &match.contents_class); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 256 | |
| 257 | match.description = |
| 258 | AutocompleteMatch::SanitizeString(navigation.description()); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 259 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 260 | match.description.length(), ACMatchClassification::NONE, |
| 261 | &match.description_class); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 262 | return match; |
| 263 | } |
| 264 | |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 265 | void ZeroSuggestProvider::Run(const GURL& suggest_url) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 266 | have_pending_request_ = false; |
| 267 | const int kFetcherID = 1; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 268 | fetcher_.reset( |
| 269 | net::URLFetcher::Create(kFetcherID, |
| 270 | suggest_url, |
| 271 | net::URLFetcher::GET, this)); |
| 272 | fetcher_->SetRequestContext(profile_->GetRequestContext()); |
| 273 | fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 274 | // Add Chrome experiment state to the request headers. |
| 275 | net::HttpRequestHeaders headers; |
| 276 | chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
| 277 | fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 278 | fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| 279 | |
| 280 | fetcher_->Start(); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 281 | |
| 282 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 283 | most_visited_urls_.clear(); |
| 284 | history::TopSites* ts = profile_->GetTopSites(); |
| 285 | if (ts) { |
| 286 | ts->GetMostVisitedURLs( |
| 287 | base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
[email protected] | ce767ab2 | 2013-11-12 03:50:09 | [diff] [blame] | 288 | weak_ptr_factory_.GetWeakPtr()), false); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 289 | } |
| 290 | } |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 291 | have_pending_request_ = true; |
| 292 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 293 | } |
| 294 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 295 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 296 | const history::MostVisitedURLList& urls) { |
| 297 | most_visited_urls_ = urls; |
| 298 | } |
| 299 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 300 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 301 | matches_.clear(); |
| 302 | |
| 303 | const TemplateURL* default_provider = |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 304 | template_url_service_->GetDefaultSearchProvider(); |
| 305 | // Fail if we can't set the clickthrough URL for query suggestions. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 306 | if (default_provider == NULL || !default_provider->SupportsReplacement()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 307 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 308 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 309 | MatchMap map; |
| 310 | AddSuggestResultsToMap(results_.suggest_results, &map); |
| 311 | |
| 312 | const int num_query_results = map.size(); |
| 313 | const int num_nav_results = results_.navigation_results.size(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 314 | const int num_results = num_query_results + num_nav_results; |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 315 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); |
| 316 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); |
| 317 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 318 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 319 | // Show Most Visited results after ZeroSuggest response is received. |
| 320 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 321 | if (!current_url_match_.destination_url.is_valid()) |
| 322 | return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 323 | matches_.push_back(current_url_match_); |
| 324 | int relevance = 600; |
| 325 | if (num_results > 0) { |
| 326 | UMA_HISTOGRAM_COUNTS( |
| 327 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 328 | most_visited_urls_.size()); |
| 329 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 330 | const base::string16 current_query_string16( |
| 331 | base::ASCIIToUTF16(current_query_)); |
| 332 | const std::string languages( |
| 333 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 334 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 335 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 336 | NavigationResult nav(*this, url.url, url.title, false, relevance, true, |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 337 | current_query_string16, languages); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 338 | matches_.push_back(NavigationToMatch(nav)); |
| 339 | --relevance; |
| 340 | } |
| 341 | return; |
| 342 | } |
| 343 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 344 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 345 | return; |
| 346 | |
| 347 | // TODO(jered): Rip this out once the first match is decoupled from the |
| 348 | // current typing in the omnibox. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 349 | matches_.push_back(current_url_match_); |
| 350 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 351 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 352 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 353 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 354 | const NavigationResults& nav_results(results_.navigation_results); |
| 355 | for (NavigationResults::const_iterator it(nav_results.begin()); |
| 356 | it != nav_results.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 357 | matches_.push_back(NavigationToMatch(*it)); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 360 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 361 | AutocompleteInput input(permanent_text_, base::string16::npos, base::string16(), |
[email protected] | 25d7e74 | 2013-07-30 17:31:06 | [diff] [blame] | 362 | GURL(current_query_), current_page_classification_, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 363 | false, false, true, AutocompleteInput::ALL_MATCHES); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 364 | |
[email protected] | 2d91578 | 2013-08-29 09:50:21 | [diff] [blame] | 365 | AutocompleteMatch match; |
| 366 | AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( |
[email protected] | 51abb7b | 2014-02-09 23:00:08 | [diff] [blame] | 367 | permanent_text_, false, true, current_page_classification_, &match, NULL); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 368 | match.is_history_what_you_typed_match = false; |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 369 | match.allowed_to_be_default_match = true; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 370 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 371 | // The placeholder suggestion for the current URL has high relevance so |
| 372 | // that it is in the first suggestion slot and inline autocompleted. It |
| 373 | // gets dropped as soon as the user types something. |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 374 | match.relevance = GetVerbatimRelevance(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 375 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 376 | return match; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 377 | } |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 378 | |
| 379 | int ZeroSuggestProvider::GetVerbatimRelevance() const { |
| 380 | return results_.verbatim_relevance >= 0 ? |
| 381 | results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; |
| 382 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame^] | 383 | |
| 384 | bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL( |
| 385 | const GURL& suggest_url, |
| 386 | const GURL& current_page_url) const { |
| 387 | if (!ZeroSuggestEnabled(suggest_url, |
| 388 | template_url_service_->GetDefaultSearchProvider(), |
| 389 | current_page_classification_, profile_)) |
| 390 | return false; |
| 391 | |
| 392 | // If we cannot send URLs, then only the MostVisited and Personalized |
| 393 | // variations can be shown. |
| 394 | if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 395 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 396 | return false; |
| 397 | |
| 398 | // Only show zero suggest for HTTP[S] pages. |
| 399 | // TODO(mariakhomenko): We may be able to expand this set to include pages |
| 400 | // with other schemes (e.g. chrome://). That may require improvements to |
| 401 | // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 402 | if (!current_page_url.is_valid() || |
| 403 | ((current_page_url.scheme() != content::kHttpScheme) && |
| 404 | (current_page_url.scheme() != content::kHttpsScheme))) |
| 405 | return false; |
| 406 | |
| 407 | return true; |
| 408 | } |