Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: chrome/browser/ui/omnibox/omnibox_controller.cc

Issue 15003002: Omnibox refactor. Move StartAutocomplete and DoInstant to controller. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reverted dummy test. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/omnibox/omnibox_controller.h" 5 #include "chrome/browser/ui/omnibox/omnibox_controller.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/autocomplete/autocomplete_classifier.h" 8 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
9 #include "chrome/browser/autocomplete/autocomplete_match.h" 9 #include "chrome/browser/autocomplete/autocomplete_match.h"
10 #include "chrome/browser/autocomplete/search_provider.h"
10 #include "chrome/browser/net/predictor.h" 11 #include "chrome/browser/net/predictor.h"
11 #include "chrome/browser/predictors/autocomplete_action_predictor.h" 12 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
12 #include "chrome/browser/prerender/prerender_field_trial.h" 13 #include "chrome/browser/prerender/prerender_field_trial.h"
13 #include "chrome/browser/prerender/prerender_manager.h" 14 #include "chrome/browser/prerender/prerender_manager.h"
14 #include "chrome/browser/prerender/prerender_manager_factory.h" 15 #include "chrome/browser/prerender/prerender_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/search.h" 17 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" 18 #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_edit_model.h"
19 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 20 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
20 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h" 21 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
21 #include "chrome/browser/ui/search/instant_controller.h" 22 #include "chrome/browser/ui/search/instant_controller.h"
22 #include "extensions/common/constants.h" 23 #include "extensions/common/constants.h"
23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect.h"
24 25
25 using predictors::AutocompleteActionPredictor; 26 using predictors::AutocompleteActionPredictor;
26 27
27 28
28 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model, 29 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
29 Profile* profile) 30 Profile* profile,
31 InstantController* instant_controller)
30 : omnibox_edit_model_(omnibox_edit_model), 32 : omnibox_edit_model_(omnibox_edit_model),
31 profile_(profile) { 33 profile_(profile),
34 instant_controller_(instant_controller) {
32 autocomplete_controller_.reset(new AutocompleteController(profile, this, 35 autocomplete_controller_.reset(new AutocompleteController(profile, this,
33 chrome::IsInstantExtendedAPIEnabled() ? 36 chrome::IsInstantExtendedAPIEnabled() ?
34 AutocompleteClassifier::kInstantExtendedOmniboxProviders : 37 AutocompleteClassifier::kInstantExtendedOmniboxProviders :
35 AutocompleteClassifier::kDefaultOmniboxProviders)); 38 AutocompleteClassifier::kDefaultOmniboxProviders));
36 } 39 }
37 40
38 OmniboxController::~OmniboxController() { 41 OmniboxController::~OmniboxController() {
39 } 42 }
40 43
44 void OmniboxController::StartAutocomplete(
45 string16 user_text,
46 size_t cursor_position,
47 bool prevent_inline_autocomplete,
48 bool prefer_keyword,
49 bool allow_exact_keyword_match) const {
50 ClearPopupKeywordMode();
51 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
52
53 if (instant_controller_) {
54 instant_controller_->OnAutocompleteStart();
55 // If the embedded page for InstantExtended is fetching its own suggestions,
56 // suppress search suggestions from SearchProvider. We still need
57 // SearchProvider to run for FinalizeInstantQuery.
58 // TODO(dcblack): Once we are done refactoring the omnibox so we don't need
59 // to use FinalizeInstantQuery anymore, we can take out this check and
60 // remove this provider from kInstantExtendedOmniboxProviders.
61 if (instant_controller_->WillFetchCompletions())
62 autocomplete_controller_->search_provider()->SuppressSearchSuggestions();
63 }
64
65 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
66 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
67 autocomplete_controller_->Start(AutocompleteInput(
68 user_text, cursor_position, string16(), GURL(),
69 prevent_inline_autocomplete, prefer_keyword, allow_exact_keyword_match,
70 AutocompleteInput::ALL_MATCHES));
71 }
72
41 void OmniboxController::OnResultChanged(bool default_match_changed) { 73 void OmniboxController::OnResultChanged(bool default_match_changed) {
42 // TODO(beaudoin): There should be no need to access the popup when using 74 // TODO(beaudoin): There should be no need to access the popup when using
43 // instant extended, remove this reference. 75 // instant extended, remove this reference.
44 const bool was_open = popup_->IsOpen(); 76 const bool was_open = popup_->IsOpen();
45 if (default_match_changed) { 77 if (default_match_changed) {
46 // The default match has changed, we need to let the OmniboxEditModel know 78 // The default match has changed, we need to let the OmniboxEditModel know
47 // about new inline autocomplete text (blue highlight). 79 // about new inline autocomplete text (blue highlight).
48 string16 inline_autocomplete_text; 80 string16 inline_autocomplete_text;
49 string16 keyword; 81 string16 keyword;
50 bool is_keyword_hint = false; 82 bool is_keyword_hint = false;
(...skipping 21 matching lines...) Expand all
72 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, 104 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL,
73 keyword, is_keyword_hint); 105 keyword, is_keyword_hint);
74 } else { 106 } else {
75 popup_->OnResultChanged(); 107 popup_->OnResultChanged();
76 } 108 }
77 109
78 if (popup_->IsOpen()) { 110 if (popup_->IsOpen()) {
79 // The popup size may have changed, let instant know. 111 // The popup size may have changed, let instant know.
80 OnPopupBoundsChanged(popup_->view()->GetTargetBounds()); 112 OnPopupBoundsChanged(popup_->view()->GetTargetBounds());
81 113
82 InstantController* instant = 114 if (instant_controller_ && !omnibox_edit_model_->in_revert()) {
83 omnibox_edit_model_->controller()->GetInstant(); 115 instant_controller_->HandleAutocompleteResults(
84 if (instant && !omnibox_edit_model_->in_revert()) {
85 instant->HandleAutocompleteResults(
86 *autocomplete_controller()->providers(), 116 *autocomplete_controller()->providers(),
87 autocomplete_controller()->result()); 117 autocomplete_controller()->result());
88 } 118 }
89 } else if (was_open) { 119 } else if (was_open) {
90 // Accept the temporary text as the user text, because it makes little sense 120 // Accept the temporary text as the user text, because it makes little sense
91 // to have temporary text when the popup is closed. 121 // to have temporary text when the popup is closed.
92 omnibox_edit_model_->AcceptTemporaryTextAsUserText(); 122 omnibox_edit_model_->AcceptTemporaryTextAsUserText();
93 // The popup has been closed, let instant know. 123 // The popup has been closed, let instant know.
94 OnPopupBoundsChanged(gfx::Rect()); 124 OnPopupBoundsChanged(gfx::Rect());
95 } 125 }
96 } 126 }
97 127
128 bool OmniboxController::DoInstant(const AutocompleteMatch& match,
129 string16 user_text,
130 string16 full_text,
131 size_t selection_start,
132 size_t selection_end,
133 bool user_input_in_progress,
134 bool in_escape_handler,
135 bool just_deleted_text,
136 bool keyword_is_selected) {
137 if (!instant_controller_)
138 return false;
139
140 // Remove "?" if we're in forced query mode.
141 AutocompleteInput::RemoveForcedQueryStringIfNecessary(
142 autocomplete_controller_->input().type(), &user_text);
143 AutocompleteInput::RemoveForcedQueryStringIfNecessary(
144 autocomplete_controller_->input().type(), &full_text);
145
146 return instant_controller_->Update(
147 match, user_text, full_text, selection_start, selection_end,
148 UseVerbatimInstant(just_deleted_text), user_input_in_progress,
149 popup_->IsOpen(), in_escape_handler, keyword_is_selected);
150 }
151
98 void OmniboxController::ClearPopupKeywordMode() const { 152 void OmniboxController::ClearPopupKeywordMode() const {
99 if (popup_->IsOpen() && 153 if (popup_->IsOpen() &&
100 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD) 154 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
101 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL); 155 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
102 } 156 }
103 157
104 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) { 158 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) {
105 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) { 159 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) {
106 // Warm up DNS Prefetch cache, or preconnect to a search service. 160 // Warm up DNS Prefetch cache, or preconnect to a search service.
107 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type, 161 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type,
108 AutocompleteMatchType::NUM_TYPES); 162 AutocompleteMatchType::NUM_TYPES);
109 if (profile_->GetNetworkPredictor()) { 163 if (profile_->GetNetworkPredictor()) {
110 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl( 164 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl(
111 match.destination_url, 165 match.destination_url,
112 AutocompleteActionPredictor::IsPreconnectable(match)); 166 AutocompleteActionPredictor::IsPreconnectable(match));
113 } 167 }
114 // We could prefetch the alternate nav URL, if any, but because there 168 // We could prefetch the alternate nav URL, if any, but because there
115 // can be many of these as a user types an initial series of characters, 169 // can be many of these as a user types an initial series of characters,
116 // the OS DNS cache could suffer eviction problems for minimal gain. 170 // the OS DNS cache could suffer eviction problems for minimal gain.
117 } 171 }
118 } 172 }
119 173
120 void OmniboxController::OnPopupBoundsChanged(const gfx::Rect& bounds) { 174 void OmniboxController::OnPopupBoundsChanged(const gfx::Rect& bounds) {
121 InstantController* instant = omnibox_edit_model_->controller()->GetInstant(); 175 if (instant_controller_)
122 if (instant) 176 instant_controller_->SetPopupBounds(bounds);
123 instant->SetPopupBounds(bounds);
124 } 177 }
178
179 bool OmniboxController::UseVerbatimInstant(bool just_deleted_text) const {
180 #if defined(OS_MACOSX)
181 // TODO(suzhe): Fix Mac port to display Instant suggest in a separated NSView,
182 // so that we can display Instant suggest along with composition text.
183 const AutocompleteInput& input = autocomplete_controller()->input();
184 if (input.prevent_inline_autocomplete())
185 return true;
186 #endif
187
188 // The value of input.prevent_inline_autocomplete() is determined by the
189 // following conditions:
190 // 1. If the caret is at the end of the text.
191 // 2. If it's in IME composition mode.
192 // We send the caret position to Instant (so it can determine #1 itself), and
193 // we use a separated widget for displaying the Instant suggest (so it doesn't
194 // interfere with #2). So, we don't need to care about the value of
195 // input.prevent_inline_autocomplete() here.
196 return just_deleted_text || popup_->selected_line() != 0;
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698