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.
5 #include "chrome/browser/ui/omnibox/omnibox_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
9 #include "chrome/browser/autocomplete/autocomplete_match.h"
10 #include "chrome/browser/autocomplete/search_provider.h"
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"
17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
19 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
20 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
21 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
22 #include "chrome/browser/ui/search/instant_controller.h"
23 #include "chrome/common/instant_types.h"
24 #include "extensions/common/constants.h"
25 #include "ui/gfx/rect.h"
29 // Returns the AutocompleteMatch that the InstantController should prefetch, if
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())
45 if (SearchProvider::ShouldPrefetch(*default_match
))
46 return &(*default_match
);
48 return (result
.ShouldHideTopMatch() && (result
.size() > 1) &&
49 SearchProvider::ShouldPrefetch(result
.match_at(1))) ?
50 &result
.match_at(1) : NULL
;
55 OmniboxController::OmniboxController(OmniboxEditModel
* omnibox_edit_model
,
57 : omnibox_edit_model_(omnibox_edit_model
),
59 autocomplete_controller_
.reset(new AutocompleteController(profile
, this,
60 chrome::IsInstantExtendedAPIEnabled() ?
61 AutocompleteClassifier::kInstantExtendedOmniboxProviders
:
62 AutocompleteClassifier::kDefaultOmniboxProviders
));
65 OmniboxController::~OmniboxController() {
68 void OmniboxController::StartAutocomplete(
70 size_t cursor_position
,
71 const GURL
& current_url
,
72 AutocompleteInput::PageClassification current_page_classification
,
73 bool prevent_inline_autocomplete
,
75 bool allow_exact_keyword_match
) const {
76 ClearPopupKeywordMode();
77 popup_
->SetHoveredLine(OmniboxPopupModel::kNoMatch
);
79 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
80 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
81 autocomplete_controller_
->Start(AutocompleteInput(
82 user_text
, cursor_position
, string16(), current_url
,
83 current_page_classification
, prevent_inline_autocomplete
,
84 prefer_keyword
, allow_exact_keyword_match
,
85 AutocompleteInput::ALL_MATCHES
));
88 void OmniboxController::OnResultChanged(bool default_match_changed
) {
89 const bool was_open
= popup_
->IsOpen();
90 if (default_match_changed
) {
91 // The default match has changed, we need to let the OmniboxEditModel know
92 // about new inline autocomplete text (blue highlight).
93 const AutocompleteResult
& result
= this->result();
94 const AutocompleteResult::const_iterator
match(result
.default_match());
95 if (match
!= result
.end()) {
96 current_match_
= *match
;
97 if (!prerender::IsOmniboxEnabled(profile_
))
99 omnibox_edit_model_
->OnCurrentMatchChanged();
101 if (chrome::IsInstantExtendedAPIEnabled() &&
102 omnibox_edit_model_
->GetInstantController()) {
103 InstantSuggestion prefetch_suggestion
;
104 const AutocompleteMatch
* match_to_prefetch
= GetMatchToPrefetch(result
);
105 if (match_to_prefetch
) {
106 prefetch_suggestion
.text
= match_to_prefetch
->contents
;
107 prefetch_suggestion
.metadata
=
108 SearchProvider::GetSuggestMetadata(*match_to_prefetch
);
110 // Send the prefetch suggestion unconditionally to the InstantPage. If
111 // there is no suggestion to prefetch, we need to send a blank query to
112 // clear the prefetched results.
113 omnibox_edit_model_
->GetInstantController()->SetSuggestionToPrefetch(
114 prefetch_suggestion
);
117 InvalidateCurrentMatch();
118 popup_
->OnResultChanged();
119 omnibox_edit_model_
->OnPopupDataChanged(string16(), NULL
, string16(),
123 popup_
->OnResultChanged();
126 if (!popup_
->IsOpen() && was_open
) {
127 // Accept the temporary text as the user text, because it makes little sense
128 // to have temporary text when the popup is closed.
129 omnibox_edit_model_
->AcceptTemporaryTextAsUserText();
133 void OmniboxController::InvalidateCurrentMatch() {
134 current_match_
= AutocompleteMatch();
137 void OmniboxController::ClearPopupKeywordMode() const {
138 if (popup_
->IsOpen() &&
139 popup_
->selected_line_state() == OmniboxPopupModel::KEYWORD
)
140 popup_
->SetSelectedLineState(OmniboxPopupModel::NORMAL
);
143 void OmniboxController::DoPreconnect(const AutocompleteMatch
& match
) {
144 if (!match
.destination_url
.SchemeIs(extensions::kExtensionScheme
)) {
145 // Warm up DNS Prefetch cache, or preconnect to a search service.
146 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match
.type
,
147 AutocompleteMatchType::NUM_TYPES
);
148 if (profile_
->GetNetworkPredictor()) {
149 profile_
->GetNetworkPredictor()->AnticipateOmniboxUrl(
150 match
.destination_url
,
151 predictors::AutocompleteActionPredictor::IsPreconnectable(match
));
153 // We could prefetch the alternate nav URL, if any, but because there
154 // can be many of these as a user types an initial series of characters,
155 // the OS DNS cache could suffer eviction problems for minimal gain.