Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1 | // Copyright 2013 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/ui/omnibox/omnibox_controller.h" |
| 6 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 7 | #include "base/metrics/histogram.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 8 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 9 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 10 | #include "chrome/browser/autocomplete/search_provider.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 11 | #include "chrome/browser/net/predictor.h" |
| 12 | #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
| 13 | #include "chrome/browser/prerender/prerender_field_trial.h" |
| 14 | #include "chrome/browser/prerender/prerender_manager.h" |
| 15 | #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 16 | #include "chrome/browser/profiles/profile.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "chrome/browser/search/search.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 18 | #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 19 | #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 20 | #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| 21 | #include "chrome/browser/ui/omnibox/omnibox_popup_view.h" |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 22 | #include "chrome/browser/ui/search/instant_controller.h" |
| 23 | #include "chrome/common/instant_types.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 24 | #include "extensions/common/constants.h" |
| 25 | #include "ui/gfx/rect.h" |
| 26 | |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // Returns the AutocompleteMatch that the InstantController should prefetch, if |
| 30 | // any. |
| 31 | // |
| 32 | // The SearchProvider may mark some suggestions to be prefetched based on |
| 33 | // instructions from the suggest server. If such a match ranks sufficiently |
| 34 | // highly, we'll return it. We only care about matches that are the default or |
| 35 | // else the very first entry in the dropdown (which can happen for non-default |
| 36 | // matches only if we're hiding a top verbatim match); for other matches, we |
| 37 | // think the likelihood of the user selecting them is low enough that |
| 38 | // prefetching isn't worth doing. |
| 39 | const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) { |
| 40 | const AutocompleteResult::const_iterator default_match( |
| 41 | result.default_match()); |
| 42 | if (default_match == result.end()) |
| 43 | return NULL; |
| 44 | |
| 45 | if (SearchProvider::ShouldPrefetch(*default_match)) |
| 46 | return &(*default_match); |
| 47 | |
| 48 | return (result.ShouldHideTopMatch() && (result.size() > 1) && |
| 49 | SearchProvider::ShouldPrefetch(result.match_at(1))) ? |
| 50 | &result.match_at(1) : NULL; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 54 | |
| 55 | OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model, |
| 56 | Profile* profile) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 57 | : omnibox_edit_model_(omnibox_edit_model), |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 58 | profile_(profile), |
| 59 | autocomplete_controller_(new AutocompleteController(profile, this, |
| 60 | AutocompleteClassifier::kDefaultOmniboxProviders)) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | OmniboxController::~OmniboxController() { |
| 64 | } |
| 65 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 66 | void OmniboxController::StartAutocomplete( |
| 67 | string16 user_text, |
| 68 | size_t cursor_position, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 69 | const GURL& current_url, |
Ben Murdoch | d386803 | 2013-07-31 10:55:33 +0100 | [diff] [blame] | 70 | AutocompleteInput::PageClassification current_page_classification, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 71 | bool prevent_inline_autocomplete, |
| 72 | bool prefer_keyword, |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 73 | bool allow_exact_keyword_match) const { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 74 | ClearPopupKeywordMode(); |
| 75 | popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch); |
| 76 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 77 | // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as |
| 78 | // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it. |
| 79 | autocomplete_controller_->Start(AutocompleteInput( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 80 | user_text, cursor_position, string16(), current_url, |
Ben Murdoch | d386803 | 2013-07-31 10:55:33 +0100 | [diff] [blame] | 81 | current_page_classification, prevent_inline_autocomplete, |
| 82 | prefer_keyword, allow_exact_keyword_match, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 83 | AutocompleteInput::ALL_MATCHES)); |
| 84 | } |
| 85 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 86 | void OmniboxController::OnResultChanged(bool default_match_changed) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 87 | const bool was_open = popup_->IsOpen(); |
| 88 | if (default_match_changed) { |
| 89 | // The default match has changed, we need to let the OmniboxEditModel know |
| 90 | // about new inline autocomplete text (blue highlight). |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 91 | const AutocompleteResult& result = this->result(); |
| 92 | const AutocompleteResult::const_iterator match(result.default_match()); |
| 93 | if (match != result.end()) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 94 | current_match_ = *match; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 95 | if (!prerender::IsOmniboxEnabled(profile_)) |
| 96 | DoPreconnect(*match); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 97 | omnibox_edit_model_->OnCurrentMatchChanged(); |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 98 | |
| 99 | if (chrome::IsInstantExtendedAPIEnabled() && |
| 100 | omnibox_edit_model_->GetInstantController()) { |
| 101 | InstantSuggestion prefetch_suggestion; |
| 102 | const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result); |
| 103 | if (match_to_prefetch) { |
| 104 | prefetch_suggestion.text = match_to_prefetch->contents; |
| 105 | prefetch_suggestion.metadata = |
| 106 | SearchProvider::GetSuggestMetadata(*match_to_prefetch); |
| 107 | } |
| 108 | // Send the prefetch suggestion unconditionally to the InstantPage. If |
| 109 | // there is no suggestion to prefetch, we need to send a blank query to |
| 110 | // clear the prefetched results. |
| 111 | omnibox_edit_model_->GetInstantController()->SetSuggestionToPrefetch( |
| 112 | prefetch_suggestion); |
| 113 | } |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 114 | } else { |
| 115 | InvalidateCurrentMatch(); |
| 116 | popup_->OnResultChanged(); |
| 117 | omnibox_edit_model_->OnPopupDataChanged(string16(), NULL, string16(), |
| 118 | false); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 119 | } |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 120 | } else { |
| 121 | popup_->OnResultChanged(); |
| 122 | } |
| 123 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 124 | if (!popup_->IsOpen() && was_open) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 125 | // Accept the temporary text as the user text, because it makes little sense |
| 126 | // to have temporary text when the popup is closed. |
| 127 | omnibox_edit_model_->AcceptTemporaryTextAsUserText(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 131 | void OmniboxController::InvalidateCurrentMatch() { |
| 132 | current_match_ = AutocompleteMatch(); |
| 133 | } |
| 134 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 135 | void OmniboxController::ClearPopupKeywordMode() const { |
| 136 | if (popup_->IsOpen() && |
| 137 | popup_->selected_line_state() == OmniboxPopupModel::KEYWORD) |
| 138 | popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL); |
| 139 | } |
| 140 | |
| 141 | void OmniboxController::DoPreconnect(const AutocompleteMatch& match) { |
| 142 | if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) { |
| 143 | // Warm up DNS Prefetch cache, or preconnect to a search service. |
| 144 | UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type, |
| 145 | AutocompleteMatchType::NUM_TYPES); |
| 146 | if (profile_->GetNetworkPredictor()) { |
| 147 | profile_->GetNetworkPredictor()->AnticipateOmniboxUrl( |
| 148 | match.destination_url, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 149 | predictors::AutocompleteActionPredictor::IsPreconnectable(match)); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 150 | } |
| 151 | // We could prefetch the alternate nav URL, if any, but because there |
| 152 | // can be many of these as a user types an initial series of characters, |
| 153 | // the OS DNS cache could suffer eviction problems for minimal gain. |
| 154 | } |
| 155 | } |