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
7#include "base/metrics/histogram.h"
8#include "chrome/browser/autocomplete/autocomplete_classifier.h"
9#include "chrome/browser/net/predictor.h"
10#include "chrome/browser/predictors/autocomplete_action_predictor.h"
11#include "chrome/browser/prerender/prerender_field_trial.h"
12#include "chrome/browser/prerender/prerender_manager.h"
13#include "chrome/browser/prerender/prerender_manager_factory.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/search/search.h"
16#include "chrome/browser/search_engines/template_url_service_factory.h"
17#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
18#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
19#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
20#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
21#include "chrome/common/instant_types.h"
22#include "components/omnibox/autocomplete_match.h"
23#include "components/omnibox/search_provider.h"
24#include "components/search/search.h"
25#include "extensions/common/constants.h"
26#include "ui/gfx/rect.h"
27
28namespace {
29
30// Returns the AutocompleteMatch that the InstantController should prefetch, if
31// any.
32//
33// The SearchProvider may mark some suggestions to be prefetched based on
34// instructions from the suggest server. If such a match ranks sufficiently
35// highly or if kAllowPrefetchNonDefaultMatch field trial is enabled, we'll
36// return it.
37//
38// If the kAllowPrefetchNonDefaultMatch field trial is enabled we return the
39// prefetch suggestion even if it is not the default match. Otherwise we only
40// care about matches that are the default or the very first entry in the
41// dropdown (which can happen for non-default matches only if we're hiding a top
42// verbatim match) or the second entry in the dropdown (which can happen for
43// non-default matches when a top verbatim match is shown); for other matches,
44// we think the likelihood of the user selecting them is low enough that
45// prefetching isn't worth doing.
46const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
47  if (chrome::ShouldAllowPrefetchNonDefaultMatch()) {
48    const AutocompleteResult::const_iterator prefetch_match = std::find_if(
49        result.begin(), result.end(), SearchProvider::ShouldPrefetch);
50    return prefetch_match != result.end() ? &(*prefetch_match) : NULL;
51  }
52
53  // If the default match should be prefetched, do that.
54  const AutocompleteResult::const_iterator default_match(
55      result.default_match());
56  if ((default_match != result.end()) &&
57      SearchProvider::ShouldPrefetch(*default_match))
58    return &(*default_match);
59
60  // Otherwise, if the top match is a verbatim match and the very next match
61  // is prefetchable, fetch that.
62  if ((result.ShouldHideTopMatch() ||
63       result.TopMatchIsStandaloneVerbatimMatch()) &&
64      (result.size() > 1) &&
65      SearchProvider::ShouldPrefetch(result.match_at(1)))
66    return &result.match_at(1);
67
68  return NULL;
69}
70
71}  // namespace
72
73OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
74                                     Profile* profile)
75    : omnibox_edit_model_(omnibox_edit_model),
76      profile_(profile),
77      popup_(NULL),
78      autocomplete_controller_(new AutocompleteController(profile,
79          TemplateURLServiceFactory::GetForProfile(profile), this,
80          AutocompleteClassifier::kDefaultOmniboxProviders)) {
81}
82
83OmniboxController::~OmniboxController() {
84}
85
86void OmniboxController::StartAutocomplete(
87    const AutocompleteInput& input) const {
88  ClearPopupKeywordMode();
89  popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
90
91  // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
92  // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
93  autocomplete_controller_->Start(input);
94}
95
96void OmniboxController::OnResultChanged(bool default_match_changed) {
97  const bool was_open = popup_->IsOpen();
98  if (default_match_changed) {
99    // The default match has changed, we need to let the OmniboxEditModel know
100    // about new inline autocomplete text (blue highlight).
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();
107    } else {
108      InvalidateCurrentMatch();
109      popup_->OnResultChanged();
110      omnibox_edit_model_->OnPopupDataChanged(base::string16(), NULL,
111                                              base::string16(), false);
112    }
113  } else {
114    popup_->OnResultChanged();
115  }
116
117  if (!popup_->IsOpen() && was_open) {
118    // Accept the temporary text as the user text, because it makes little sense
119    // to have temporary text when the popup is closed.
120    omnibox_edit_model_->AcceptTemporaryTextAsUserText();
121  }
122
123  if (chrome::IsInstantExtendedAPIEnabled() &&
124     ((default_match_changed && result().default_match() != result().end()) ||
125      (chrome::ShouldAllowPrefetchNonDefaultMatch() && !result().empty()))) {
126    InstantSuggestion prefetch_suggestion;
127    const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result());
128    if (match_to_prefetch) {
129      prefetch_suggestion.text = match_to_prefetch->contents;
130      prefetch_suggestion.metadata =
131          SearchProvider::GetSuggestMetadata(*match_to_prefetch);
132    }
133    // Send the prefetch suggestion unconditionally to the InstantPage. If
134    // there is no suggestion to prefetch, we need to send a blank query to
135    // clear the prefetched results.
136    omnibox_edit_model_->SetSuggestionToPrefetch(prefetch_suggestion);
137  }
138}
139
140void OmniboxController::InvalidateCurrentMatch() {
141  current_match_ = AutocompleteMatch();
142}
143
144void OmniboxController::ClearPopupKeywordMode() const {
145  if (popup_->IsOpen() &&
146      popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
147    popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
148}
149
150void 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));
159    }
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.
163  }
164}
165