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

Side by Side Diff: components/omnibox/browser/search_provider.cc

Issue 2717893002: Omnibox - Warm Up PSuggest on Focus (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/omnibox/browser/search_provider.h" 5 #include "components/omnibox/browser/search_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <cmath> 9 #include <cmath>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/base64.h" 12 #include "base/base64.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/feature_list.h"
15 #include "base/i18n/break_iterator.h" 16 #include "base/i18n/break_iterator.h"
16 #include "base/i18n/case_conversion.h" 17 #include "base/i18n/case_conversion.h"
17 #include "base/json/json_string_value_serializer.h" 18 #include "base/json/json_string_value_serializer.h"
18 #include "base/metrics/histogram_macros.h" 19 #include "base/metrics/histogram_macros.h"
19 #include "base/metrics/user_metrics.h" 20 #include "base/metrics/user_metrics.h"
20 #include "base/rand_util.h" 21 #include "base/rand_util.h"
21 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h" 23 #include "base/strings/utf_string_conversions.h"
23 #include "base/trace_event/trace_event.h" 24 #include "base/trace_event/trace_event.h"
24 #include "components/data_use_measurement/core/data_use_user_data.h" 25 #include "components/data_use_measurement/core/data_use_user_data.h"
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 TRACE_EVENT0("omnibox", "SearchProvider::Start"); 216 TRACE_EVENT0("omnibox", "SearchProvider::Start");
216 // Do our best to load the model as early as possible. This will reduce 217 // Do our best to load the model as early as possible. This will reduce
217 // odds of having the model not ready when really needed (a non-empty input). 218 // odds of having the model not ready when really needed (a non-empty input).
218 TemplateURLService* model = client()->GetTemplateURLService(); 219 TemplateURLService* model = client()->GetTemplateURLService();
219 DCHECK(model); 220 DCHECK(model);
220 model->Load(); 221 model->Load();
221 222
222 matches_.clear(); 223 matches_.clear();
223 set_field_trial_triggered(false); 224 set_field_trial_triggered(false);
224 225
225 // Can't return search/suggest results for bogus input. 226 // Unless warming up the suggest server on focus, SearchProvider doesn't do
226 if (input.from_omnibox_focus() || 227 // do anything useful for on-focus inputs or empty inputs. Exit early.
227 input.type() == metrics::OmniboxInputType::INVALID) { 228 if (!base::FeatureList::IsEnabled(omnibox::kSearchProviderWarmUpOnFocus) &&
229 (input.from_omnibox_focus() ||
230 input.type() == metrics::OmniboxInputType::INVALID)) {
228 Stop(true, false); 231 Stop(true, false);
229 return; 232 return;
230 } 233 }
231 234
232 keyword_input_ = input; 235 keyword_input_ = input;
233 const TemplateURL* keyword_provider = 236 const TemplateURL* keyword_provider =
234 KeywordProvider::GetSubstitutingTemplateURLForInput(model, 237 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
235 &keyword_input_); 238 &keyword_input_);
236 if (keyword_provider == NULL) 239 if (keyword_provider == NULL)
237 keyword_input_.Clear(); 240 keyword_input_.Clear();
(...skipping 22 matching lines...) Expand all
260 keyword_provider->keyword() : base::string16()); 263 keyword_provider->keyword() : base::string16());
261 if (!minimal_changes || 264 if (!minimal_changes ||
262 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) { 265 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
263 // Cancel any in-flight suggest requests. 266 // Cancel any in-flight suggest requests.
264 if (!done_) 267 if (!done_)
265 Stop(false, false); 268 Stop(false, false);
266 } 269 }
267 270
268 providers_.set(default_provider_keyword, keyword_provider_keyword); 271 providers_.set(default_provider_keyword, keyword_provider_keyword);
269 272
270 if (input.text().empty()) { 273 if (input.from_omnibox_focus()) {
274 // Don't display any suggestions for on-focus requests. Stop the requests
275 // here so that we can create a new request later in this flow (to warm-up
276 // the suggest server by alerting it that the user is likely about to start
277 // typing).
Peter Kasting 2017/03/01 03:00:35 Can we have had requests in-flight before this? G
Mark P 2017/03/01 20:02:32 Probably...? But there are so many edge cases. E
Peter Kasting 2017/03/02 22:50:42 Yes. Otherwise, the dropdown could pop open later
Mark P 2017/03/03 22:12:24 Removed it and removed comment. Added DCHECK to v
278 StopSuggest();
279 ClearAllResults();
280 } else if (input.text().empty()) {
271 // User typed "?" alone. Give them a placeholder result indicating what 281 // User typed "?" alone. Give them a placeholder result indicating what
272 // this syntax does. 282 // this syntax does.
273 if (default_provider) { 283 if (default_provider) {
274 AutocompleteMatch match; 284 AutocompleteMatch match;
275 match.provider = this; 285 match.provider = this;
276 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); 286 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
277 match.contents_class.push_back( 287 match.contents_class.push_back(
278 ACMatchClassification(0, ACMatchClassification::NONE)); 288 ACMatchClassification(0, ACMatchClassification::NONE));
279 match.keyword = providers_.default_provider(); 289 match.keyword = providers_.default_provider();
280 match.allowed_to_be_default_match = true; 290 match.allowed_to_be_default_match = true;
281 matches_.push_back(match); 291 matches_.push_back(match);
282 } 292 }
283 Stop(true, false); 293 Stop(true, false);
284 return; 294 return;
285 } 295 }
286 296
287 input_ = input; 297 input_ = input;
288 298
289 DoHistoryQuery(minimal_changes); 299 // Don't search the query history database for on-focus inputs; this
290 // Answers needs scored history results before any suggest query has been 300 // provider should not be opening the dropdown / giving results for on-focus
291 // started, since the query for answer-bearing results needs additional 301 // inputs.
Peter Kasting 2017/03/01 03:00:35 Nit: Maybe second clause should be "these inputs s
Mark P 2017/03/01 20:02:33 Okay. Done.
292 // prefetch information based on the highest-scored local history result. 302 if (!input.from_omnibox_focus()) {
293 ScoreHistoryResults(raw_default_history_results_, 303 DoHistoryQuery(minimal_changes);
294 false, 304 // Answers needs scored history results before any suggest query has been
295 &transformed_default_history_results_); 305 // started, since the query for answer-bearing results needs additional
296 ScoreHistoryResults(raw_keyword_history_results_, 306 // prefetch information based on the highest-scored local history result.
297 true, 307 ScoreHistoryResults(raw_default_history_results_, false,
298 &transformed_keyword_history_results_); 308 &transformed_default_history_results_);
299 prefetch_data_ = FindAnswersPrefetchData(); 309 ScoreHistoryResults(raw_keyword_history_results_, true,
310 &transformed_keyword_history_results_);
311 prefetch_data_ = FindAnswersPrefetchData();
300 312
301 // Raw results are not needed any more. 313 // Raw results are not needed any more.
302 raw_default_history_results_.clear(); 314 raw_default_history_results_.clear();
303 raw_keyword_history_results_.clear(); 315 raw_keyword_history_results_.clear();
316 }
304 317
305 StartOrStopSuggestQuery(minimal_changes); 318 StartOrStopSuggestQuery(minimal_changes);
306 UpdateMatches(); 319 UpdateMatches();
307 } 320 }
308 321
309 void SearchProvider::Stop(bool clear_cached_results, 322 void SearchProvider::Stop(bool clear_cached_results,
310 bool due_to_user_inactivity) { 323 bool due_to_user_inactivity) {
311 StopSuggest(); 324 StopSuggest();
312 done_ = true; 325 done_ = true;
313 326
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 400
388 // Ensure the request succeeded and that the provider used is still available. 401 // Ensure the request succeeded and that the provider used is still available.
389 // A verbatim match cannot be generated without this provider, causing errors. 402 // A verbatim match cannot be generated without this provider, causing errors.
390 const bool request_succeeded = 403 const bool request_succeeded =
391 source->GetStatus().is_success() && (source->GetResponseCode() == 200) && 404 source->GetStatus().is_success() && (source->GetResponseCode() == 200) &&
392 GetTemplateURL(is_keyword); 405 GetTemplateURL(is_keyword);
393 406
394 LogFetchComplete(request_succeeded, is_keyword); 407 LogFetchComplete(request_succeeded, is_keyword);
395 408
396 bool results_updated = false; 409 bool results_updated = false;
397 if (request_succeeded) { 410 // Ignore (i.e, don't display) any suggestions for on-focus inputs.
Peter Kasting 2017/03/01 03:00:35 Nit: , -> . Might want to say why we should ignor
Mark P 2017/03/01 20:02:33 Did -> .,
411 if (!input_.from_omnibox_focus() && request_succeeded) {
398 std::unique_ptr<base::Value> data( 412 std::unique_ptr<base::Value> data(
399 SearchSuggestionParser::DeserializeJsonData( 413 SearchSuggestionParser::DeserializeJsonData(
400 SearchSuggestionParser::ExtractJsonData(source))); 414 SearchSuggestionParser::ExtractJsonData(source)));
401 if (data) { 415 if (data) {
402 SearchSuggestionParser::Results* results = 416 SearchSuggestionParser::Results* results =
403 is_keyword ? &keyword_results_ : &default_results_; 417 is_keyword ? &keyword_results_ : &default_results_;
404 results_updated = ParseSuggestResults(*data, -1, is_keyword, results); 418 results_updated = ParseSuggestResults(*data, -1, is_keyword, results);
405 if (results_updated) 419 if (results_updated)
406 SortResults(is_keyword, results); 420 SortResults(is_keyword, results);
407 } 421 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", 502 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
489 elapsed_time); 503 elapsed_time);
490 } else { 504 } else {
491 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", 505 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
492 elapsed_time); 506 elapsed_time);
493 } 507 }
494 } 508 }
495 } 509 }
496 510
497 void SearchProvider::UpdateMatches() { 511 void SearchProvider::UpdateMatches() {
512 // Because on-focus inputs display no suggestions, it's inappropriate to
513 // enforce inlineability and default match constraints (which is what the
514 // rest of this function does).
515 if (input_.from_omnibox_focus()) {
516 UpdateDone();
Peter Kasting 2017/03/01 03:00:35 Nit: Maybe most of this function should be a helpe
Mark P 2017/03/01 20:02:32 Refactored to make this cleaner.
517 return;
518 }
519
498 PersistTopSuggestions(&default_results_); 520 PersistTopSuggestions(&default_results_);
499 PersistTopSuggestions(&keyword_results_); 521 PersistTopSuggestions(&keyword_results_);
500 ConvertResultsToAutocompleteMatches(); 522 ConvertResultsToAutocompleteMatches();
501 523
502 // Check constraints that may be violated by suggested relevances. 524 // Check constraints that may be violated by suggested relevances.
503 if (!matches_.empty() && 525 if (!matches_.empty() &&
504 (default_results_.HasServerProvidedScores() || 526 (default_results_.HasServerProvidedScores() ||
505 keyword_results_.HasServerProvidedScores())) { 527 keyword_results_.HasServerProvidedScores())) {
506 // These blocks attempt to repair undesirable behavior by suggested 528 // These blocks attempt to repair undesirable behavior by suggested
507 // relevances with minimal impact, preserving other suggested relevances. 529 // relevances with minimal impact, preserving other suggested relevances.
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); 751 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
730 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); 752 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
731 return !client()->IsOffTheRecord() && client()->SearchSuggestEnabled() && 753 return !client()->IsOffTheRecord() && client()->SearchSuggestEnabled() &&
732 ((default_url && !default_url->suggestions_url().empty() && 754 ((default_url && !default_url->suggestions_url().empty() &&
733 !*query_is_private) || 755 !*query_is_private) ||
734 (keyword_url && !keyword_url->suggestions_url().empty())); 756 (keyword_url && !keyword_url->suggestions_url().empty()));
735 } 757 }
736 758
737 bool SearchProvider::IsQueryPotentionallyPrivate() const { 759 bool SearchProvider::IsQueryPotentionallyPrivate() const {
738 // If the input type might be a URL, we take extra care so that private data 760 // If the input type might be a URL, we take extra care so that private data
739 // isn't sent to the server. 761 // isn't sent to the server.
Peter Kasting 2017/03/01 03:00:35 Nit: While here: This comment seems like it belong
Mark P 2017/03/01 20:02:33 Done. (Moved a slightly revised version of the co
740 762
763 if (input_.text().empty())
764 return false;
765
741 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't 766 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
742 // http/https/ftp, we shouldn't send it. Sending things like file: and data: 767 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
743 // is both a waste of time and a disclosure of potentially private, local 768 // is both a waste of time and a disclosure of potentially private, local
744 // data. Other "schemes" may actually be usernames, and we don't want to send 769 // data. Other "schemes" may actually be usernames, and we don't want to send
745 // passwords. If the scheme is OK, we still need to check other cases below. 770 // passwords. If the scheme is OK, we still need to check other cases below.
746 // If this is QUERY, then the presence of these schemes means the user 771 // If this is QUERY, then the presence of these schemes means the user
747 // explicitly typed one, and thus this is probably a URL that's being entered 772 // explicitly typed one, and thus this is probably a URL that's being entered
748 // and happens to currently be invalid -- in which case we again want to run 773 // and happens to currently be invalid -- in which case we again want to run
749 // our checks below. Other QUERY cases are less likely to be URLs and thus we 774 // our checks below. Other QUERY cases are less likely to be URLs and thus we
750 // assume we're OK. 775 // assume we're OK.
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) 1515 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
1491 matches.push_back(i->second); 1516 matches.push_back(i->second);
1492 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); 1517 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
1493 1518
1494 // If there is a top scoring entry, find the corresponding answer. 1519 // If there is a top scoring entry, find the corresponding answer.
1495 if (!matches.empty()) 1520 if (!matches.empty())
1496 return answers_cache_.GetTopAnswerEntry(matches[0].contents); 1521 return answers_cache_.GetTopAnswerEntry(matches[0].contents);
1497 1522
1498 return AnswersQueryData(); 1523 return AnswersQueryData();
1499 } 1524 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698