OLD | NEW |
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" |
(...skipping 11 matching lines...) Expand all Loading... |
31 profile_(profile) { | 32 profile_(profile) { |
32 autocomplete_controller_.reset(new AutocompleteController(profile, this, | 33 autocomplete_controller_.reset(new AutocompleteController(profile, this, |
33 chrome::IsInstantExtendedAPIEnabled() ? | 34 chrome::IsInstantExtendedAPIEnabled() ? |
34 AutocompleteClassifier::kInstantExtendedOmniboxProviders : | 35 AutocompleteClassifier::kInstantExtendedOmniboxProviders : |
35 AutocompleteClassifier::kDefaultOmniboxProviders)); | 36 AutocompleteClassifier::kDefaultOmniboxProviders)); |
36 } | 37 } |
37 | 38 |
38 OmniboxController::~OmniboxController() { | 39 OmniboxController::~OmniboxController() { |
39 } | 40 } |
40 | 41 |
| 42 void OmniboxController::StartAutocomplete( |
| 43 string16 user_text, |
| 44 size_t cursor_position, |
| 45 bool prevent_inline_autocomplete, |
| 46 bool prefer_keyword, |
| 47 bool allow_exact_keyword_match) const { |
| 48 ClearPopupKeywordMode(); |
| 49 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch); |
| 50 |
| 51 InstantController* instant_controller = GetInstantController(); |
| 52 if (instant_controller) { |
| 53 instant_controller->OnAutocompleteStart(); |
| 54 // If the embedded page for InstantExtended is fetching its own suggestions, |
| 55 // suppress search suggestions from SearchProvider. We still need |
| 56 // SearchProvider to run for FinalizeInstantQuery. |
| 57 // TODO(dcblack): Once we are done refactoring the omnibox so we don't need |
| 58 // to use FinalizeInstantQuery anymore, we can take out this check and |
| 59 // remove this provider from kInstantExtendedOmniboxProviders. |
| 60 if (instant_controller->WillFetchCompletions()) |
| 61 autocomplete_controller_->search_provider()->SuppressSearchSuggestions(); |
| 62 } |
| 63 |
| 64 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as |
| 65 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it. |
| 66 autocomplete_controller_->Start(AutocompleteInput( |
| 67 user_text, cursor_position, string16(), GURL(), |
| 68 prevent_inline_autocomplete, prefer_keyword, allow_exact_keyword_match, |
| 69 AutocompleteInput::ALL_MATCHES)); |
| 70 } |
| 71 |
41 void OmniboxController::OnResultChanged(bool default_match_changed) { | 72 void OmniboxController::OnResultChanged(bool default_match_changed) { |
42 // TODO(beaudoin): There should be no need to access the popup when using | 73 // TODO(beaudoin): There should be no need to access the popup when using |
43 // instant extended, remove this reference. | 74 // instant extended, remove this reference. |
44 const bool was_open = popup_->IsOpen(); | 75 const bool was_open = popup_->IsOpen(); |
45 if (default_match_changed) { | 76 if (default_match_changed) { |
46 // The default match has changed, we need to let the OmniboxEditModel know | 77 // The default match has changed, we need to let the OmniboxEditModel know |
47 // about new inline autocomplete text (blue highlight). | 78 // about new inline autocomplete text (blue highlight). |
48 string16 inline_autocomplete_text; | 79 string16 inline_autocomplete_text; |
49 string16 keyword; | 80 string16 keyword; |
50 bool is_keyword_hint = false; | 81 bool is_keyword_hint = false; |
(...skipping 21 matching lines...) Expand all Loading... |
72 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, | 103 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, |
73 keyword, is_keyword_hint); | 104 keyword, is_keyword_hint); |
74 } else { | 105 } else { |
75 popup_->OnResultChanged(); | 106 popup_->OnResultChanged(); |
76 } | 107 } |
77 | 108 |
78 if (popup_->IsOpen()) { | 109 if (popup_->IsOpen()) { |
79 // The popup size may have changed, let instant know. | 110 // The popup size may have changed, let instant know. |
80 OnPopupBoundsChanged(popup_->view()->GetTargetBounds()); | 111 OnPopupBoundsChanged(popup_->view()->GetTargetBounds()); |
81 | 112 |
82 InstantController* instant = | 113 InstantController* instant_controller = GetInstantController(); |
83 omnibox_edit_model_->controller()->GetInstant(); | 114 if (instant_controller && !omnibox_edit_model_->in_revert()) { |
84 if (instant && !omnibox_edit_model_->in_revert()) { | 115 instant_controller->HandleAutocompleteResults( |
85 instant->HandleAutocompleteResults( | 116 *autocomplete_controller_->providers(), |
86 *autocomplete_controller()->providers(), | 117 autocomplete_controller_->result()); |
87 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 InstantController* instant_controller = GetInstantController(); |
| 138 if (!instant_controller) |
| 139 return false; |
| 140 |
| 141 // Remove "?" if we're in forced query mode. |
| 142 AutocompleteInput::RemoveForcedQueryStringIfNecessary( |
| 143 autocomplete_controller_->input().type(), &user_text); |
| 144 AutocompleteInput::RemoveForcedQueryStringIfNecessary( |
| 145 autocomplete_controller_->input().type(), &full_text); |
| 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 InstantController* instant_controller = GetInstantController(); |
122 if (instant) | 176 if (instant_controller) |
123 instant->SetPopupBounds(bounds); | 177 instant_controller->SetPopupBounds(bounds); |
124 } | 178 } |
| 179 |
| 180 bool OmniboxController::UseVerbatimInstant(bool just_deleted_text) const { |
| 181 #if defined(OS_MACOSX) |
| 182 // TODO(suzhe): Fix Mac port to display Instant suggest in a separated NSView, |
| 183 // so that we can display Instant suggest along with composition text. |
| 184 const AutocompleteInput& input = autocomplete_controller_->input(); |
| 185 if (input.prevent_inline_autocomplete()) |
| 186 return true; |
| 187 #endif |
| 188 |
| 189 // The value of input.prevent_inline_autocomplete() is determined by the |
| 190 // following conditions: |
| 191 // 1. If the caret is at the end of the text. |
| 192 // 2. If it's in IME composition mode. |
| 193 // We send the caret position to Instant (so it can determine #1 itself), and |
| 194 // we use a separated widget for displaying the Instant suggest (so it doesn't |
| 195 // interfere with #2). So, we don't need to care about the value of |
| 196 // input.prevent_inline_autocomplete() here. |
| 197 return just_deleted_text || popup_->selected_line() != 0; |
| 198 } |
| 199 |
| 200 InstantController* OmniboxController::GetInstantController() const { |
| 201 return omnibox_edit_model_->GetInstantController(); |
| 202 } |
OLD | NEW |