OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/autocomplete/autocomplete_popup_model.h" | 5 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "unicode/ubidi.h" | 9 #include "unicode/ubidi.h" |
10 | 10 |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 13 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
14 #include "chrome/browser/autocomplete/autocomplete_match.h" | 14 #include "chrome/browser/autocomplete/autocomplete_match.h" |
15 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" | 15 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/search_engines/template_url.h" | 18 #include "chrome/browser/search_engines/template_url.h" |
19 #include "chrome/browser/search_engines/template_url_service.h" | 19 #include "chrome/browser/search_engines/template_url_service.h" |
20 #include "chrome/browser/search_engines/template_url_service_factory.h" | 20 #include "chrome/browser/search_engines/template_url_service_factory.h" |
21 #include "ui/gfx/rect.h" | 21 #include "ui/gfx/rect.h" |
22 | 22 |
23 /////////////////////////////////////////////////////////////////////////////// | 23 /////////////////////////////////////////////////////////////////////////////// |
24 // AutocompletePopupModel | 24 // AutocompletePopupModel |
25 | 25 |
| 26 const size_t AutocompletePopupModel::kNoMatch = -1; |
| 27 |
26 AutocompletePopupModel::AutocompletePopupModel( | 28 AutocompletePopupModel::AutocompletePopupModel( |
27 AutocompletePopupView* popup_view, | 29 AutocompletePopupView* popup_view, |
28 AutocompleteEditModel* edit_model) | 30 AutocompleteEditModel* edit_model) |
29 : view_(popup_view), | 31 : view_(popup_view), |
30 edit_model_(edit_model), | 32 edit_model_(edit_model), |
31 hovered_line_(kNoMatch), | 33 hovered_line_(kNoMatch), |
32 selected_line_(kNoMatch) { | 34 selected_line_(kNoMatch), |
| 35 selected_line_state_(NORMAL) { |
33 edit_model->set_popup_model(this); | 36 edit_model->set_popup_model(this); |
34 } | 37 } |
35 | 38 |
36 AutocompletePopupModel::~AutocompletePopupModel() { | 39 AutocompletePopupModel::~AutocompletePopupModel() { |
37 } | 40 } |
38 | 41 |
39 bool AutocompletePopupModel::IsOpen() const { | 42 bool AutocompletePopupModel::IsOpen() const { |
40 return view_->IsOpen(); | 43 return view_->IsOpen(); |
41 } | 44 } |
42 | 45 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // Track the user's selection until they cancel it. | 79 // Track the user's selection until they cancel it. |
77 manually_selected_match_.destination_url = match.destination_url; | 80 manually_selected_match_.destination_url = match.destination_url; |
78 manually_selected_match_.provider_affinity = match.provider; | 81 manually_selected_match_.provider_affinity = match.provider; |
79 manually_selected_match_.is_history_what_you_typed_match = | 82 manually_selected_match_.is_history_what_you_typed_match = |
80 match.is_history_what_you_typed_match; | 83 match.is_history_what_you_typed_match; |
81 } | 84 } |
82 | 85 |
83 if (line == selected_line_ && !force) | 86 if (line == selected_line_ && !force) |
84 return; // Nothing else to do. | 87 return; // Nothing else to do. |
85 | 88 |
86 // We need to update |selected_line_| before calling OnPopupDataChanged(), so | 89 // We need to update |selected_line_state_| and |selected_line_| before |
87 // that when the edit notifies its controller that something has changed, the | 90 // calling InvalidateLine(), since it will check them to determine how to |
88 // controller can get the correct updated data. | 91 // draw. We also need to update |selected_line_| before calling |
| 92 // OnPopupDataChanged(), so that when the edit notifies its controller that |
| 93 // something has changed, the controller can get the correct updated data. |
89 // | 94 // |
90 // NOTE: We should never reach here with no selected line; the same code that | 95 // NOTE: We should never reach here with no selected line; the same code that |
91 // opened the popup and made it possible to get here should have also set a | 96 // opened the popup and made it possible to get here should have also set a |
92 // selected line. | 97 // selected line. |
93 CHECK(selected_line_ != kNoMatch); | 98 CHECK(selected_line_ != kNoMatch); |
94 GURL current_destination(result.match_at(selected_line_).destination_url); | 99 GURL current_destination(result.match_at(selected_line_).destination_url); |
95 view_->InvalidateLine(selected_line_); | 100 const size_t prev_selected_line = selected_line_; |
| 101 selected_line_state_ = NORMAL; |
96 selected_line_ = line; | 102 selected_line_ = line; |
| 103 view_->InvalidateLine(prev_selected_line); |
97 view_->InvalidateLine(selected_line_); | 104 view_->InvalidateLine(selected_line_); |
98 | 105 |
99 // Update the edit with the new data for this match. | 106 // Update the edit with the new data for this match. |
100 // TODO(pkasting): If |selected_line_| moves to the controller, this can be | 107 // TODO(pkasting): If |selected_line_| moves to the controller, this can be |
101 // eliminated and just become a call to the observer on the edit. | 108 // eliminated and just become a call to the observer on the edit. |
102 string16 keyword; | 109 string16 keyword; |
103 const bool is_keyword_hint = GetKeywordForMatch(match, &keyword); | 110 const bool is_keyword_hint = match.GetKeyword(&keyword); |
| 111 |
104 if (reset_to_default) { | 112 if (reset_to_default) { |
105 string16 inline_autocomplete_text; | 113 string16 inline_autocomplete_text; |
106 if ((match.inline_autocomplete_offset != string16::npos) && | 114 if ((match.inline_autocomplete_offset != string16::npos) && |
107 (match.inline_autocomplete_offset < match.fill_into_edit.length())) { | 115 (match.inline_autocomplete_offset < match.fill_into_edit.length())) { |
108 inline_autocomplete_text = | 116 inline_autocomplete_text = |
109 match.fill_into_edit.substr(match.inline_autocomplete_offset); | 117 match.fill_into_edit.substr(match.inline_autocomplete_offset); |
110 } | 118 } |
111 edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, | 119 edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, |
112 keyword, is_keyword_hint); | 120 keyword, is_keyword_hint); |
113 } else { | 121 } else { |
114 edit_model_->OnPopupDataChanged(match.fill_into_edit, ¤t_destination, | 122 edit_model_->OnPopupDataChanged(match.fill_into_edit, ¤t_destination, |
115 keyword, is_keyword_hint); | 123 keyword, is_keyword_hint); |
116 } | 124 } |
117 | 125 |
118 // Repaint old and new selected lines immediately, so that the edit doesn't | 126 // Repaint old and new selected lines immediately, so that the edit doesn't |
119 // appear to update [much] faster than the popup. | 127 // appear to update [much] faster than the popup. |
120 view_->PaintUpdatesNow(); | 128 view_->PaintUpdatesNow(); |
121 } | 129 } |
122 | 130 |
123 void AutocompletePopupModel::ResetToDefaultMatch() { | 131 void AutocompletePopupModel::ResetToDefaultMatch() { |
124 const AutocompleteResult& result = this->result(); | 132 const AutocompleteResult& result = this->result(); |
125 CHECK(!result.empty()); | 133 CHECK(!result.empty()); |
126 SetSelectedLine(result.default_match() - result.begin(), true, false); | 134 SetSelectedLine(result.default_match() - result.begin(), true, false); |
127 view_->OnDragCanceled(); | 135 view_->OnDragCanceled(); |
128 } | 136 } |
129 | 137 |
130 bool AutocompletePopupModel::GetKeywordForMatch(const AutocompleteMatch& match, | |
131 string16* keyword) const { | |
132 // Assume we have no keyword until we find otherwise. | |
133 keyword->clear(); | |
134 | |
135 if (match.template_url && | |
136 TemplateURL::SupportsReplacement(match.template_url) && | |
137 match.transition == content::PAGE_TRANSITION_KEYWORD) { | |
138 // The current match is a keyword, return that as the selected keyword. | |
139 keyword->assign(match.template_url->keyword()); | |
140 return false; | |
141 } | |
142 | |
143 // See if the current match's fill_into_edit corresponds to a keyword. | |
144 return GetKeywordForText(match.fill_into_edit, keyword); | |
145 } | |
146 | |
147 bool AutocompletePopupModel::GetKeywordForText(const string16& text, | |
148 string16* keyword) const { | |
149 // Creates keyword_hint first in case |keyword| is a pointer to |text|. | |
150 const string16 keyword_hint(TemplateURLService::CleanUserInputKeyword(text)); | |
151 | |
152 // Assume we have no keyword until we find otherwise. | |
153 keyword->clear(); | |
154 | |
155 if (keyword_hint.empty()) | |
156 return false; | |
157 Profile* profile = edit_model_->profile(); | |
158 TemplateURLService* url_service = | |
159 TemplateURLServiceFactory::GetForProfile(profile); | |
160 if (!url_service) | |
161 return false; | |
162 url_service->Load(); | |
163 | |
164 // Don't provide a hint if this keyword doesn't support replacement. | |
165 const TemplateURL* const template_url = | |
166 url_service->GetTemplateURLForKeyword(keyword_hint); | |
167 if (!TemplateURL::SupportsReplacement(template_url)) | |
168 return false; | |
169 | |
170 // Don't provide a hint for inactive/disabled extension keywords. | |
171 if (template_url->IsExtensionKeyword()) { | |
172 const Extension* extension = profile->GetExtensionService()-> | |
173 GetExtensionById(template_url->GetExtensionId(), false); | |
174 if (!extension || (profile->IsOffTheRecord() && | |
175 !profile->GetExtensionService()->IsIncognitoEnabled(extension->id()))) | |
176 return false; | |
177 } | |
178 | |
179 keyword->assign(keyword_hint); | |
180 return true; | |
181 } | |
182 | |
183 void AutocompletePopupModel::Move(int count) { | 138 void AutocompletePopupModel::Move(int count) { |
184 const AutocompleteResult& result = this->result(); | 139 const AutocompleteResult& result = this->result(); |
185 if (result.empty()) | 140 if (result.empty()) |
186 return; | 141 return; |
187 | 142 |
188 // The user is using the keyboard to change the selection, so stop tracking | 143 // The user is using the keyboard to change the selection, so stop tracking |
189 // hover. | 144 // hover. |
190 SetHoveredLine(kNoMatch); | 145 SetHoveredLine(kNoMatch); |
191 | 146 |
192 // Clamp the new line to [0, result_.count() - 1]. | 147 // Clamp the new line to [0, result_.count() - 1]. |
193 const size_t new_line = selected_line_ + count; | 148 const size_t new_line = selected_line_ + count; |
194 SetSelectedLine(((count < 0) && (new_line >= selected_line_)) ? 0 : new_line, | 149 SetSelectedLine(((count < 0) && (new_line >= selected_line_)) ? 0 : new_line, |
195 false, false); | 150 false, false); |
196 } | 151 } |
197 | 152 |
| 153 void AutocompletePopupModel::SetSelectedLineState(LineState state) { |
| 154 DCHECK(!result().empty()); |
| 155 DCHECK_NE(kNoMatch, selected_line_); |
| 156 |
| 157 const AutocompleteMatch& match = result().match_at(selected_line_); |
| 158 DCHECK(match.associated_keyword.get()); |
| 159 |
| 160 selected_line_state_ = state; |
| 161 view_->InvalidateLine(selected_line_); |
| 162 } |
| 163 |
198 void AutocompletePopupModel::TryDeletingCurrentItem() { | 164 void AutocompletePopupModel::TryDeletingCurrentItem() { |
199 // We could use InfoForCurrentSelection() here, but it seems better to try | 165 // We could use InfoForCurrentSelection() here, but it seems better to try |
200 // and shift-delete the actual selection, rather than any "in progress, not | 166 // and shift-delete the actual selection, rather than any "in progress, not |
201 // yet visible" one. | 167 // yet visible" one. |
202 if (selected_line_ == kNoMatch) | 168 if (selected_line_ == kNoMatch) |
203 return; | 169 return; |
204 | 170 |
205 // Cancel the query so the matches don't change on the user. | 171 // Cancel the query so the matches don't change on the user. |
206 autocomplete_controller()->Stop(false); | 172 autocomplete_controller()->Stop(false); |
207 | 173 |
(...skipping 28 matching lines...) Expand all Loading... |
236 match.template_url->GetExtensionId()); | 202 match.template_url->GetExtensionId()); |
237 } | 203 } |
238 | 204 |
239 void AutocompletePopupModel::OnResultChanged() { | 205 void AutocompletePopupModel::OnResultChanged() { |
240 const AutocompleteResult& result = this->result(); | 206 const AutocompleteResult& result = this->result(); |
241 selected_line_ = result.default_match() == result.end() ? | 207 selected_line_ = result.default_match() == result.end() ? |
242 kNoMatch : static_cast<size_t>(result.default_match() - result.begin()); | 208 kNoMatch : static_cast<size_t>(result.default_match() - result.begin()); |
243 // There had better not be a nonempty result set with no default match. | 209 // There had better not be a nonempty result set with no default match. |
244 CHECK((selected_line_ != kNoMatch) || result.empty()); | 210 CHECK((selected_line_ != kNoMatch) || result.empty()); |
245 manually_selected_match_.Clear(); | 211 manually_selected_match_.Clear(); |
| 212 selected_line_state_ = NORMAL; |
246 // If we're going to trim the window size to no longer include the hovered | 213 // If we're going to trim the window size to no longer include the hovered |
247 // line, turn hover off. Practically, this shouldn't happen, but it | 214 // line, turn hover off. Practically, this shouldn't happen, but it |
248 // doesn't hurt to be defensive. | 215 // doesn't hurt to be defensive. |
249 if ((hovered_line_ != kNoMatch) && (result.size() <= hovered_line_)) | 216 if ((hovered_line_ != kNoMatch) && (result.size() <= hovered_line_)) |
250 SetHoveredLine(kNoMatch); | 217 SetHoveredLine(kNoMatch); |
251 | 218 |
252 view_->UpdatePopupAppearance(); | 219 view_->UpdatePopupAppearance(); |
253 } | 220 } |
OLD | NEW |