Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 any pending | |
| 275 // requests here (there likely aren't yet, though it doesn't hurt to be safe | |
|
Peter Kasting
2017/03/02 22:50:42
Nit: Missing paren
Mark P
2017/03/03 22:12:24
Now moot.
| |
| 276 // so that we can create a new request later in this flow (to warm-up the | |
| 277 // suggest server by alerting it that the user is likely about to start | |
| 278 // typing). | |
| 279 StopSuggest(); | |
| 280 ClearAllResults(); | |
| 281 } else if (input.text().empty()) { | |
| 271 // User typed "?" alone. Give them a placeholder result indicating what | 282 // User typed "?" alone. Give them a placeholder result indicating what |
| 272 // this syntax does. | 283 // this syntax does. |
| 273 if (default_provider) { | 284 if (default_provider) { |
| 274 AutocompleteMatch match; | 285 AutocompleteMatch match; |
| 275 match.provider = this; | 286 match.provider = this; |
| 276 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); | 287 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
| 277 match.contents_class.push_back( | 288 match.contents_class.push_back( |
| 278 ACMatchClassification(0, ACMatchClassification::NONE)); | 289 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 279 match.keyword = providers_.default_provider(); | 290 match.keyword = providers_.default_provider(); |
| 280 match.allowed_to_be_default_match = true; | 291 match.allowed_to_be_default_match = true; |
| 281 matches_.push_back(match); | 292 matches_.push_back(match); |
| 282 } | 293 } |
| 283 Stop(true, false); | 294 Stop(true, false); |
| 284 return; | 295 return; |
| 285 } | 296 } |
| 286 | 297 |
| 287 input_ = input; | 298 input_ = input; |
| 288 | 299 |
| 289 DoHistoryQuery(minimal_changes); | 300 // Don't search the query history database for on-focus inputs; these inputs |
| 290 // Answers needs scored history results before any suggest query has been | 301 // should only be used to warm up the suggest server. |
| 291 // started, since the query for answer-bearing results needs additional | 302 if (!input.from_omnibox_focus()) { |
| 292 // prefetch information based on the highest-scored local history result. | 303 DoHistoryQuery(minimal_changes); |
| 293 ScoreHistoryResults(raw_default_history_results_, | 304 // Answers needs scored history results before any suggest query has been |
| 294 false, | 305 // started, since the query for answer-bearing results needs additional |
| 295 &transformed_default_history_results_); | 306 // prefetch information based on the highest-scored local history result. |
| 296 ScoreHistoryResults(raw_keyword_history_results_, | 307 ScoreHistoryResults(raw_default_history_results_, false, |
| 297 true, | 308 &transformed_default_history_results_); |
| 298 &transformed_keyword_history_results_); | 309 ScoreHistoryResults(raw_keyword_history_results_, true, |
| 299 prefetch_data_ = FindAnswersPrefetchData(); | 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 Loading... | |
| 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. |
| 411 // SearchProvider is not intended to give suggestions on on-focus inputs; | |
| 412 // that's left to ZeroSuggestProvider and friends. Furthermore, it's not | |
| 413 // clear if the suggest server will send back sensible results to the | |
| 414 // request we're constructing here for on-focus inputs. | |
| 415 if (!input_.from_omnibox_focus() && request_succeeded) { | |
| 398 std::unique_ptr<base::Value> data( | 416 std::unique_ptr<base::Value> data( |
| 399 SearchSuggestionParser::DeserializeJsonData( | 417 SearchSuggestionParser::DeserializeJsonData( |
| 400 SearchSuggestionParser::ExtractJsonData(source))); | 418 SearchSuggestionParser::ExtractJsonData(source))); |
| 401 if (data) { | 419 if (data) { |
| 402 SearchSuggestionParser::Results* results = | 420 SearchSuggestionParser::Results* results = |
| 403 is_keyword ? &keyword_results_ : &default_results_; | 421 is_keyword ? &keyword_results_ : &default_results_; |
| 404 results_updated = ParseSuggestResults(*data, -1, is_keyword, results); | 422 results_updated = ParseSuggestResults(*data, -1, is_keyword, results); |
| 405 if (results_updated) | 423 if (results_updated) |
| 406 SortResults(is_keyword, results); | 424 SortResults(is_keyword, results); |
| 407 } | 425 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", | 506 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
| 489 elapsed_time); | 507 elapsed_time); |
| 490 } else { | 508 } else { |
| 491 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", | 509 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
| 492 elapsed_time); | 510 elapsed_time); |
| 493 } | 511 } |
| 494 } | 512 } |
| 495 } | 513 } |
| 496 | 514 |
| 497 void SearchProvider::UpdateMatches() { | 515 void SearchProvider::UpdateMatches() { |
| 498 PersistTopSuggestions(&default_results_); | 516 // On-focus inputs display no suggestions, so we do not need to persist the |
| 499 PersistTopSuggestions(&keyword_results_); | 517 // previous top suggestions, add new suggestions, or revise suggestions to |
| 500 ConvertResultsToAutocompleteMatches(); | 518 // enforce constraints about inlineability in this case. Indeed, most of |
| 519 // these steps would be bad, as they'd add a suggestion of some form, thus | |
| 520 // opening the dropdown (which we do not want to happen). | |
| 521 if (!input_.from_omnibox_focus()) { | |
| 522 PersistTopSuggestions(&default_results_); | |
| 523 PersistTopSuggestions(&keyword_results_); | |
| 524 ConvertResultsToAutocompleteMatches(); | |
| 525 EnforceConstraints(); | |
| 526 UMA_HISTOGRAM_CUSTOM_COUNTS("Omnibox.SearchProviderMatches", | |
| 527 matches_.size(), 1, 6, 7); | |
| 528 RecordTopSuggestion(); | |
| 529 } | |
| 501 | 530 |
| 502 // Check constraints that may be violated by suggested relevances. | 531 UpdateDone(); |
| 532 } | |
| 533 | |
| 534 void SearchProvider::EnforceConstraints() { | |
| 503 if (!matches_.empty() && | 535 if (!matches_.empty() && |
| 504 (default_results_.HasServerProvidedScores() || | 536 (default_results_.HasServerProvidedScores() || |
| 505 keyword_results_.HasServerProvidedScores())) { | 537 keyword_results_.HasServerProvidedScores())) { |
| 506 // These blocks attempt to repair undesirable behavior by suggested | 538 // These blocks attempt to repair undesirable behavior by suggested |
| 507 // relevances with minimal impact, preserving other suggested relevances. | 539 // relevances with minimal impact, preserving other suggested relevances. |
| 508 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 540 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 509 const bool is_extension_keyword = (keyword_url != NULL) && | 541 const bool is_extension_keyword = (keyword_url != NULL) && |
| 510 (keyword_url->type() == TemplateURL::OMNIBOX_API_EXTENSION); | 542 (keyword_url->type() == TemplateURL::OMNIBOX_API_EXTENSION); |
| 511 if ((keyword_url != NULL) && !is_extension_keyword && | 543 if ((keyword_url != NULL) && !is_extension_keyword && |
| 512 (AutocompleteResult::FindTopMatch(&matches_) == matches_.end())) { | 544 (AutocompleteResult::FindTopMatch(&matches_) == matches_.end())) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 542 default_results_.verbatim_relevance = 1; | 574 default_results_.verbatim_relevance = 1; |
| 543 // We do not have to alter keyword_results_.verbatim_relevance here. | 575 // We do not have to alter keyword_results_.verbatim_relevance here. |
| 544 // If the user is in keyword mode, we already reverted (earlier in this | 576 // If the user is in keyword mode, we already reverted (earlier in this |
| 545 // function) the instructions to suppress keyword verbatim. | 577 // function) the instructions to suppress keyword verbatim. |
| 546 ConvertResultsToAutocompleteMatches(); | 578 ConvertResultsToAutocompleteMatches(); |
| 547 } | 579 } |
| 548 DCHECK(!IsTopMatchSearchWithURLInput()); | 580 DCHECK(!IsTopMatchSearchWithURLInput()); |
| 549 DCHECK(is_extension_keyword || | 581 DCHECK(is_extension_keyword || |
| 550 (AutocompleteResult::FindTopMatch(&matches_) != matches_.end())); | 582 (AutocompleteResult::FindTopMatch(&matches_) != matches_.end())); |
| 551 } | 583 } |
| 552 UMA_HISTOGRAM_CUSTOM_COUNTS( | 584 } |
| 553 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7); | |
| 554 | 585 |
| 555 // Record the top suggestion (if any) for future use. | 586 void SearchProvider::RecordTopSuggestion() { |
| 556 top_query_suggestion_match_contents_ = base::string16(); | 587 top_query_suggestion_match_contents_ = base::string16(); |
| 557 top_navigation_suggestion_ = GURL(); | 588 top_navigation_suggestion_ = GURL(); |
| 558 ACMatches::const_iterator first_match = | 589 ACMatches::const_iterator first_match = |
| 559 AutocompleteResult::FindTopMatch(matches_); | 590 AutocompleteResult::FindTopMatch(matches_); |
| 560 if ((first_match != matches_.end()) && | 591 if ((first_match != matches_.end()) && |
| 561 !first_match->inline_autocompletion.empty()) { | 592 !first_match->inline_autocompletion.empty()) { |
| 562 // Identify if this match came from a query suggestion or a navsuggestion. | 593 // Identify if this match came from a query suggestion or a navsuggestion. |
| 563 // In either case, extracts the identifying feature of the suggestion | 594 // In either case, extracts the identifying feature of the suggestion |
| 564 // (query string or navigation url). | 595 // (query string or navigation url). |
| 565 if (AutocompleteMatch::IsSearchType(first_match->type)) | 596 if (AutocompleteMatch::IsSearchType(first_match->type)) |
| 566 top_query_suggestion_match_contents_ = first_match->contents; | 597 top_query_suggestion_match_contents_ = first_match->contents; |
| 567 else | 598 else |
| 568 top_navigation_suggestion_ = first_match->destination_url; | 599 top_navigation_suggestion_ = first_match->destination_url; |
| 569 } | 600 } |
| 570 | |
| 571 UpdateDone(); | |
| 572 } | 601 } |
| 573 | 602 |
| 574 void SearchProvider::Run(bool query_is_private) { | 603 void SearchProvider::Run(bool query_is_private) { |
| 575 // Start a new request with the current input. | 604 // Start a new request with the current input. |
| 576 time_suggest_request_sent_ = base::TimeTicks::Now(); | 605 time_suggest_request_sent_ = base::TimeTicks::Now(); |
| 577 | 606 |
| 578 if (!query_is_private) { | 607 if (!query_is_private) { |
| 579 default_fetcher_ = | 608 default_fetcher_ = |
| 580 CreateSuggestFetcher(kDefaultProviderURLFetcherID, | 609 CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 581 providers_.GetDefaultProviderURL(), input_); | 610 providers_.GetDefaultProviderURL(), input_); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 728 // keyword input to a keyword suggest server, if any.) | 757 // keyword input to a keyword suggest server, if any.) |
| 729 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); | 758 const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 730 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 759 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 731 return !client()->IsOffTheRecord() && client()->SearchSuggestEnabled() && | 760 return !client()->IsOffTheRecord() && client()->SearchSuggestEnabled() && |
| 732 ((default_url && !default_url->suggestions_url().empty() && | 761 ((default_url && !default_url->suggestions_url().empty() && |
| 733 !*query_is_private) || | 762 !*query_is_private) || |
| 734 (keyword_url && !keyword_url->suggestions_url().empty())); | 763 (keyword_url && !keyword_url->suggestions_url().empty())); |
| 735 } | 764 } |
| 736 | 765 |
| 737 bool SearchProvider::IsQueryPotentionallyPrivate() const { | 766 bool SearchProvider::IsQueryPotentionallyPrivate() const { |
| 738 // If the input type might be a URL, we take extra care so that private data | 767 if (input_.text().empty()) |
| 739 // isn't sent to the server. | 768 return false; |
| 740 | 769 |
| 741 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't | 770 // 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: | 771 // 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 | 772 // 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 | 773 // 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. | 774 // 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 | 775 // 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 | 776 // 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 | 777 // 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 | 778 // our checks below. Other QUERY cases are less likely to be URLs and thus we |
| 750 // assume we're OK. | 779 // assume we're OK. |
| 751 if (!base::LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) && | 780 if (!base::LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) && |
| (...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1490 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) | 1519 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
| 1491 matches.push_back(i->second); | 1520 matches.push_back(i->second); |
| 1492 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); | 1521 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); |
| 1493 | 1522 |
| 1494 // If there is a top scoring entry, find the corresponding answer. | 1523 // If there is a top scoring entry, find the corresponding answer. |
| 1495 if (!matches.empty()) | 1524 if (!matches.empty()) |
| 1496 return answers_cache_.GetTopAnswerEntry(matches[0].contents); | 1525 return answers_cache_.GetTopAnswerEntry(matches[0].contents); |
| 1497 | 1526 |
| 1498 return AnswersQueryData(); | 1527 return AnswersQueryData(); |
| 1499 } | 1528 } |
| OLD | NEW |