Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_controller.cc
blobdb2aaa1065b0ac936923369827b870051ea63dbf
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/bitmap_fetcher/bitmap_fetcher_service_factory.h"
10 #include "chrome/browser/net/predictor.h"
11 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
12 #include "chrome/browser/prerender/prerender_field_trial.h"
13 #include "chrome/browser/prerender/prerender_manager.h"
14 #include "chrome/browser/prerender/prerender_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
20 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
21 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
22 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
23 #include "chrome/common/instant_types.h"
24 #include "components/omnibox/autocomplete_match.h"
25 #include "components/omnibox/search_provider.h"
26 #include "components/search/search.h"
27 #include "extensions/common/constants.h"
28 #include "ui/gfx/geometry/rect.h"
30 namespace {
32 // Returns the AutocompleteMatch that the InstantController should prefetch, if
33 // any.
35 // The SearchProvider may mark some suggestions to be prefetched based on
36 // instructions from the suggest server. If such a match ranks sufficiently
37 // highly or if kAllowPrefetchNonDefaultMatch field trial is enabled, we'll
38 // return it.
40 // If the kAllowPrefetchNonDefaultMatch field trial is enabled we return the
41 // prefetch suggestion even if it is not the default match. Otherwise we only
42 // care about matches that are the default or the very first entry in the
43 // dropdown (which can happen for non-default matches only if we're hiding a top
44 // verbatim match) or the second entry in the dropdown (which can happen for
45 // non-default matches when a top verbatim match is shown); for other matches,
46 // we think the likelihood of the user selecting them is low enough that
47 // prefetching isn't worth doing.
48 const AutocompleteMatch* GetMatchToPrefetch(const AutocompleteResult& result) {
49 if (chrome::ShouldAllowPrefetchNonDefaultMatch()) {
50 const AutocompleteResult::const_iterator prefetch_match = std::find_if(
51 result.begin(), result.end(), SearchProvider::ShouldPrefetch);
52 return prefetch_match != result.end() ? &(*prefetch_match) : NULL;
55 // If the default match should be prefetched, do that.
56 const AutocompleteResult::const_iterator default_match(
57 result.default_match());
58 if ((default_match != result.end()) &&
59 SearchProvider::ShouldPrefetch(*default_match))
60 return &(*default_match);
62 // Otherwise, if the top match is a verbatim match and the very next match
63 // is prefetchable, fetch that.
64 if ((result.ShouldHideTopMatch() ||
65 result.TopMatchIsStandaloneVerbatimMatch()) &&
66 (result.size() > 1) &&
67 SearchProvider::ShouldPrefetch(result.match_at(1)))
68 return &result.match_at(1);
70 return NULL;
73 // Calls back to the OmniboxController when the requested image is downloaded.
74 // This is a separate class instead of being implemented on OmniboxController
75 // because BitmapFetcherService currently takes ownership of this object.
76 // TODO(dschuyler): Make BitmapFetcherService use the more typical non-owning
77 // ObserverList pattern and have OmniboxController implement the Observer call
78 // directly.
79 class AnswerImageObserver : public BitmapFetcherService::Observer {
80 public:
81 explicit AnswerImageObserver(
82 const base::WeakPtr<OmniboxController>& controller)
83 : controller_(controller) {}
85 void OnImageChanged(BitmapFetcherService::RequestId request_id,
86 const SkBitmap& image) override;
88 private:
89 const base::WeakPtr<OmniboxController> controller_;
90 DISALLOW_COPY_AND_ASSIGN(AnswerImageObserver);
93 void AnswerImageObserver::OnImageChanged(
94 BitmapFetcherService::RequestId request_id,
95 const SkBitmap& image) {
96 DCHECK(!image.empty());
97 DCHECK(controller_);
98 controller_->SetAnswerBitmap(image);
101 } // namespace
103 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
104 Profile* profile)
105 : omnibox_edit_model_(omnibox_edit_model),
106 profile_(profile),
107 popup_(NULL),
108 autocomplete_controller_(new AutocompleteController(
109 profile,
110 TemplateURLServiceFactory::GetForProfile(profile),
111 this,
112 AutocompleteClassifier::kDefaultOmniboxProviders)),
113 request_id_(BitmapFetcherService::REQUEST_ID_INVALID),
114 weak_ptr_factory_(this) {
117 OmniboxController::~OmniboxController() {
118 BitmapFetcherService* image_service =
119 BitmapFetcherServiceFactory::GetForBrowserContext(profile_);
120 if (image_service)
121 image_service->CancelRequest(request_id_);
124 void OmniboxController::StartAutocomplete(
125 const AutocompleteInput& input) const {
126 ClearPopupKeywordMode();
127 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
129 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
130 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
131 autocomplete_controller_->Start(input);
134 void OmniboxController::OnResultChanged(bool default_match_changed) {
135 const bool was_open = popup_->IsOpen();
136 if (default_match_changed) {
137 // The default match has changed, we need to let the OmniboxEditModel know
138 // about new inline autocomplete text (blue highlight).
139 const AutocompleteResult::const_iterator match(result().default_match());
140 if (match != result().end()) {
141 current_match_ = *match;
142 if (!prerender::IsOmniboxEnabled(profile_))
143 DoPreconnect(*match);
144 omnibox_edit_model_->OnCurrentMatchChanged();
145 } else {
146 InvalidateCurrentMatch();
147 popup_->OnResultChanged();
148 omnibox_edit_model_->OnPopupDataChanged(base::string16(), NULL,
149 base::string16(), false);
151 } else {
152 popup_->OnResultChanged();
155 if (!popup_->IsOpen() && was_open) {
156 // Accept the temporary text as the user text, because it makes little sense
157 // to have temporary text when the popup is closed.
158 omnibox_edit_model_->AcceptTemporaryTextAsUserText();
161 if (chrome::IsInstantExtendedAPIEnabled() &&
162 ((default_match_changed && result().default_match() != result().end()) ||
163 (chrome::ShouldAllowPrefetchNonDefaultMatch() && !result().empty()))) {
164 InstantSuggestion prefetch_suggestion;
165 const AutocompleteMatch* match_to_prefetch = GetMatchToPrefetch(result());
166 if (match_to_prefetch) {
167 prefetch_suggestion.text = match_to_prefetch->contents;
168 prefetch_suggestion.metadata =
169 SearchProvider::GetSuggestMetadata(*match_to_prefetch);
171 // Send the prefetch suggestion unconditionally to the InstantPage. If
172 // there is no suggestion to prefetch, we need to send a blank query to
173 // clear the prefetched results.
174 omnibox_edit_model_->SetSuggestionToPrefetch(prefetch_suggestion);
177 for (AutocompleteResult::const_iterator match(result().begin());
178 match != result().end(); ++match) {
179 if (match->answer) {
180 BitmapFetcherService* image_service =
181 BitmapFetcherServiceFactory::GetForBrowserContext(profile_);
182 if (image_service) {
183 image_service->CancelRequest(request_id_);
184 request_id_ = image_service->RequestImage(
185 match->answer->second_line().image_url(),
186 new AnswerImageObserver(weak_ptr_factory_.GetWeakPtr()));
188 // We only fetch one answer image.
189 break;
194 void OmniboxController::InvalidateCurrentMatch() {
195 current_match_ = AutocompleteMatch();
198 void OmniboxController::ClearPopupKeywordMode() const {
199 if (popup_->IsOpen() &&
200 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
201 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
204 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) {
205 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) {
206 // Warm up DNS Prefetch cache, or preconnect to a search service.
207 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type,
208 AutocompleteMatchType::NUM_TYPES);
209 if (profile_->GetNetworkPredictor()) {
210 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl(
211 match.destination_url,
212 predictors::AutocompleteActionPredictor::IsPreconnectable(match));
214 // We could prefetch the alternate nav URL, if any, but because there
215 // can be many of these as a user types an initial series of characters,
216 // the OS DNS cache could suffer eviction problems for minimal gain.
220 void OmniboxController::SetAnswerBitmap(const SkBitmap& bitmap) {
221 request_id_ = BitmapFetcherService::REQUEST_ID_INVALID;
222 popup_->SetAnswerBitmap(bitmap);