blob: 9af3a20a54d1f8d8b4580a253f448d999ce48cae [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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)90dce4d2013-05-29 14:40:03 +01007#include "base/metrics/histogram.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01008#include "chrome/browser/autocomplete/autocomplete_classifier.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01009#include "chrome/browser/autocomplete/autocomplete_match.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010010#include "chrome/browser/autocomplete/search_provider.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010011#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)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/browser/search/search.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010018#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010019#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010020#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
21#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010022#include "chrome/browser/ui/search/instant_controller.h"
23#include "chrome/common/instant_types.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010024#include "extensions/common/constants.h"
25#include "ui/gfx/rect.h"
26
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010027namespace {
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.
39const 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)c2e0dbd2013-05-09 18:35:53 +010054
55OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
56 Profile* profile)
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010057 : omnibox_edit_model_(omnibox_edit_model),
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010058 profile_(profile),
59 autocomplete_controller_(new AutocompleteController(profile, this,
60 AutocompleteClassifier::kDefaultOmniboxProviders)) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010061}
62
63OmniboxController::~OmniboxController() {
64}
65
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010066void OmniboxController::StartAutocomplete(
67 string16 user_text,
68 size_t cursor_position,
Ben Murdocheb525c52013-07-10 11:40:50 +010069 const GURL& current_url,
Ben Murdochd3868032013-07-31 10:55:33 +010070 AutocompleteInput::PageClassification current_page_classification,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010071 bool prevent_inline_autocomplete,
72 bool prefer_keyword,
Ben Murdochba5b9a62013-08-12 14:20:17 +010073 bool allow_exact_keyword_match) const {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010074 ClearPopupKeywordMode();
75 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
76
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010077 // 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 Murdocheb525c52013-07-10 11:40:50 +010080 user_text, cursor_position, string16(), current_url,
Ben Murdochd3868032013-07-31 10:55:33 +010081 current_page_classification, prevent_inline_autocomplete,
82 prefer_keyword, allow_exact_keyword_match,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010083 AutocompleteInput::ALL_MATCHES));
84}
85
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010086void OmniboxController::OnResultChanged(bool default_match_changed) {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010087 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)90dce4d2013-05-29 14:40:03 +010091 const AutocompleteResult& result = this->result();
92 const AutocompleteResult::const_iterator match(result.default_match());
93 if (match != result.end()) {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010094 current_match_ = *match;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010095 if (!prerender::IsOmniboxEnabled(profile_))
96 DoPreconnect(*match);
Ben Murdocheb525c52013-07-10 11:40:50 +010097 omnibox_edit_model_->OnCurrentMatchChanged();
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010098
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)7d4cd472013-06-19 11:58:07 +0100114 } else {
115 InvalidateCurrentMatch();
116 popup_->OnResultChanged();
117 omnibox_edit_model_->OnPopupDataChanged(string16(), NULL, string16(),
118 false);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100119 }
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100120 } else {
121 popup_->OnResultChanged();
122 }
123
Ben Murdocheb525c52013-07-10 11:40:50 +0100124 if (!popup_->IsOpen() && was_open) {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100125 // 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)90dce4d2013-05-29 14:40:03 +0100128 }
129}
130
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100131void OmniboxController::InvalidateCurrentMatch() {
132 current_match_ = AutocompleteMatch();
133}
134
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100135void OmniboxController::ClearPopupKeywordMode() const {
136 if (popup_->IsOpen() &&
137 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
138 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
139}
140
141void 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 Murdocheb525c52013-07-10 11:40:50 +0100149 predictors::AutocompleteActionPredictor::IsPreconnectable(match));
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100150 }
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}