Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_controller.cc
blob2854181ae2ec13c023bc29844ffdee830e2176d6
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"
27 namespace {
29 // Returns the AutocompleteMatch that the InstantController should prefetch, if
30 // any.
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.
36 // We only care about matches that are the default or the very first entry in
37 // the dropdown (which can happen for non-default matches only if we're hiding
38 // a top verbatim match) or the second entry in the dropdown (which can happen
39 // for non-default matches when a top verbatim match is shown); for other
40 // matches, we think the likelihood of the user selecting them is low enough
41 // that prefetching isn't worth doing.
42 const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
43 // If the default match should be prefetched, do that.
44 const AutocompleteResult::const_iterator default_match(
45 result.default_match());
46 if ((default_match != result.end()) &&
47 SearchProvider::ShouldPrefetch(*default_match))
48 return &(*default_match);
50 // Otherwise, if the top match is a verbatim match and the very next match is
51 // prefetchable, fetch that.
52 if ((result.ShouldHideTopMatch() ||
53 result.TopMatchIsStandaloneVerbatimMatch()) &&
54 (result.size() > 1) &&
55 SearchProvider::ShouldPrefetch(result.match_at(1)))
56 return &result.match_at(1);
58 return NULL;
61 } // namespace
63 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
64 Profile* profile)
65 : omnibox_edit_model_(omnibox_edit_model),
66 profile_(profile),
67 popup_(NULL),
68 autocomplete_controller_(new AutocompleteController(profile, this,
69 AutocompleteClassifier::kDefaultOmniboxProviders)) {
72 OmniboxController::~OmniboxController() {
75 void OmniboxController::StartAutocomplete(
76 base::string16 user_text,
77 size_t cursor_position,
78 const GURL& current_url,
79 AutocompleteInput::PageClassification current_page_classification,
80 bool prevent_inline_autocomplete,
81 bool prefer_keyword,
82 bool allow_exact_keyword_match) const {
83 ClearPopupKeywordMode();
84 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
86 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
87 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
88 autocomplete_controller_->Start(AutocompleteInput(
89 user_text, cursor_position, base::string16(), current_url,
90 current_page_classification, prevent_inline_autocomplete,
91 prefer_keyword, allow_exact_keyword_match,
92 AutocompleteInput::ALL_MATCHES));
95 void OmniboxController::OnResultChanged(bool default_match_changed) {
96 const bool was_open = popup_->IsOpen();
97 if (default_match_changed) {
98 // The default match has changed, we need to let the OmniboxEditModel know
99 // about new inline autocomplete text (blue highlight).
100 const AutocompleteResult& result = this->result();
101 const AutocompleteResult::const_iterator match(result.default_match());
102 if (match != result.end()) {
103 current_match_ = *match;
104 if (!prerender::IsOmniboxEnabled(profile_))
105 DoPreconnect(*match);
106 omnibox_edit_model_->OnCurrentMatchChanged();
108 if (chrome::IsInstantExtendedAPIEnabled() &&
109 omnibox_edit_model_->GetInstantController()) {
110 InstantSuggestion prefetch_suggestion;
111 const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result);
112 if (match_to_prefetch) {
113 prefetch_suggestion.text = match_to_prefetch->contents;
114 prefetch_suggestion.metadata =
115 SearchProvider::GetSuggestMetadata(*match_to_prefetch);
117 // Send the prefetch suggestion unconditionally to the InstantPage. If
118 // there is no suggestion to prefetch, we need to send a blank query to
119 // clear the prefetched results.
120 omnibox_edit_model_->GetInstantController()->SetSuggestionToPrefetch(
121 prefetch_suggestion);
123 } else {
124 InvalidateCurrentMatch();
125 popup_->OnResultChanged();
126 omnibox_edit_model_->OnPopupDataChanged(base::string16(), NULL,
127 base::string16(), false);
129 } else {
130 popup_->OnResultChanged();
133 if (!popup_->IsOpen() && was_open) {
134 // Accept the temporary text as the user text, because it makes little sense
135 // to have temporary text when the popup is closed.
136 omnibox_edit_model_->AcceptTemporaryTextAsUserText();
140 void OmniboxController::InvalidateCurrentMatch() {
141 current_match_ = AutocompleteMatch();
144 void OmniboxController::ClearPopupKeywordMode() const {
145 if (popup_->IsOpen() &&
146 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
147 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
150 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) {
151 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) {
152 // Warm up DNS Prefetch cache, or preconnect to a search service.
153 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type,
154 AutocompleteMatchType::NUM_TYPES);
155 if (profile_->GetNetworkPredictor()) {
156 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl(
157 match.destination_url,
158 predictors::AutocompleteActionPredictor::IsPreconnectable(match));
160 // We could prefetch the alternate nav URL, if any, but because there
161 // can be many of these as a user types an initial series of characters,
162 // the OS DNS cache could suffer eviction problems for minimal gain.