OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 } | 137 } |
138 | 138 |
139 // static | 139 // static |
140 bool InstantController::IsEnabled(Profile* profile) { | 140 bool InstantController::IsEnabled(Profile* profile) { |
141 const PrefService* prefs = profile ? profile->GetPrefs() : NULL; | 141 const PrefService* prefs = profile ? profile->GetPrefs() : NULL; |
142 return prefs && prefs->GetBoolean(prefs::kInstantEnabled); | 142 return prefs && prefs->GetBoolean(prefs::kInstantEnabled); |
143 } | 143 } |
144 | 144 |
145 bool InstantController::Update(const AutocompleteMatch& match, | 145 bool InstantController::Update(const AutocompleteMatch& match, |
146 const string16& user_text, | 146 const string16& user_text, |
147 const string16& full_text, | |
147 bool verbatim, | 148 bool verbatim, |
148 string16* suggested_text, | 149 string16* suggested_text, |
149 InstantCompleteBehavior* complete_behavior) { | 150 InstantCompleteBehavior* complete_behavior) { |
150 const TabContents* active_tab = delegate_->GetActiveTabContents(); | 151 const TabContents* active_tab = delegate_->GetActiveTabContents(); |
151 | 152 |
152 // We could get here with no active tab if the Browser is closing. | 153 // We could get here with no active tab if the Browser is closing. |
153 if (!active_tab) { | 154 if (!active_tab) { |
154 Hide(); | 155 Hide(); |
155 return false; | 156 return false; |
156 } | 157 } |
157 | 158 |
158 std::string instant_url; | 159 std::string instant_url; |
159 Profile* profile = active_tab->profile(); | 160 Profile* profile = active_tab->profile(); |
160 | 161 |
161 // If the match's TemplateURL isn't valid, it is likely not a query. | 162 // If the match's TemplateURL isn't valid, it is likely not a query. |
162 if (!GetInstantURL(match.GetTemplateURL(profile), &instant_url)) { | 163 if (!GetInstantURL(match.GetTemplateURL(profile), &instant_url)) { |
163 Hide(); | 164 Hide(); |
164 return false; | 165 return false; |
165 } | 166 } |
166 | 167 |
167 string16 full_text = user_text + *suggested_text; | |
168 | |
169 if (full_text.empty()) { | 168 if (full_text.empty()) { |
170 Hide(); | 169 Hide(); |
171 return false; | 170 return false; |
172 } | 171 } |
173 | 172 |
174 // The presence of any suggested_text implies verbatim. | |
175 DCHECK(suggested_text->empty() || verbatim) | |
176 << user_text << "|" << *suggested_text; | |
177 | |
178 ResetLoader(instant_url, active_tab); | 173 ResetLoader(instant_url, active_tab); |
179 last_active_tab_ = active_tab; | 174 last_active_tab_ = active_tab; |
180 | 175 |
181 // Track the non-Instant search URL for this query. | 176 // Track the non-Instant search URL for this query. |
182 url_for_history_ = match.destination_url; | 177 url_for_history_ = match.destination_url; |
183 last_transition_type_ = match.transition; | 178 last_transition_type_ = match.transition; |
184 | 179 |
180 // In EXTENDED mode, we send only |user_text| as the query text. In all other | |
181 // modes, we use the entire |full_text|. | |
182 const string16& query_text = mode_ == EXTENDED ? user_text : full_text; | |
183 string16 last_query_text = mode_ == EXTENDED ? last_user_text_ | |
Peter Kasting
2012/08/24 22:27:19
Nit: Break after ==, not before :
sreeram
2012/08/24 23:09:55
Done.
| |
184 : last_full_text_; | |
185 last_user_text_ = user_text; | 185 last_user_text_ = user_text; |
186 last_full_text_ = full_text; | |
186 | 187 |
187 // Don't send an update to the loader if the query text hasn't changed. | 188 // Don't send an update to the loader if the query text hasn't changed. |
188 if (full_text == last_full_text_ && verbatim == last_verbatim_) { | 189 if (query_text == last_query_text && verbatim == last_verbatim_) { |
189 // Since we are updating |suggested_text|, shouldn't we also update | 190 // Reuse the last suggestion, as it's still valid. |
190 // |last_full_text_|? No. There's no guarantee that our suggestion will | |
191 // actually be inline autocompleted. For example, it may get trumped by | |
192 // a history suggestion. If our suggestion does make it, the omnibox will | |
193 // call Update() again, at which time we'll update |last_full_text_|. | |
194 *suggested_text = last_suggestion_.text; | 191 *suggested_text = last_suggestion_.text; |
Peter Kasting
2012/08/24 22:27:19
This looks like a case where we set this outparam
sreeram
2012/08/24 23:09:55
For the current query (if it's not a duplicate of
| |
195 *complete_behavior = last_suggestion_.behavior; | 192 *complete_behavior = last_suggestion_.behavior; |
196 | 193 |
197 // We need to call Show() here because of this: | 194 // We need to call Show() here because of this: |
198 // 1. User has typed a query (say Q). Instant overlay is showing results. | 195 // 1. User has typed a query (say Q). Instant overlay is showing results. |
199 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of | 196 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of |
200 // these cause the overlay to Hide(). | 197 // these cause the overlay to Hide(). |
201 // 3. User arrows-up to Q or types Q again. The last text we processed is | 198 // 3. User arrows-up to Q or types Q again. The last text we processed is |
202 // still Q, so we don't Update() the loader, but we do need to Show(). | 199 // still Q, so we don't Update() the loader, but we do need to Show(). |
203 if (loader_processed_last_update_ && | 200 if (loader_processed_last_update_ && |
204 (mode_ == INSTANT || mode_ == EXTENDED)) { | 201 (mode_ == INSTANT || mode_ == EXTENDED)) { |
205 Show(); | 202 Show(); |
206 } | 203 } |
207 return true; | 204 return true; |
208 } | 205 } |
209 | 206 |
210 last_full_text_ = full_text; | |
211 last_verbatim_ = verbatim; | 207 last_verbatim_ = verbatim; |
212 loader_processed_last_update_ = false; | 208 loader_processed_last_update_ = false; |
213 | |
214 // Reset the last suggestion, as it's no longer valid. | |
215 suggested_text->clear(); | |
216 last_suggestion_ = InstantSuggestion(); | 209 last_suggestion_ = InstantSuggestion(); |
217 *complete_behavior = INSTANT_COMPLETE_NOW; | |
218 | 210 |
219 if (mode_ != SILENT) { | 211 if (mode_ != SILENT) { |
220 loader_->Update(last_full_text_, last_verbatim_); | 212 loader_->Update(query_text, verbatim); |
221 | 213 |
222 content::NotificationService::current()->Notify( | 214 content::NotificationService::current()->Notify( |
223 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 215 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
224 content::Source<InstantController>(this), | 216 content::Source<InstantController>(this), |
225 content::NotificationService::NoDetails()); | 217 content::NotificationService::NoDetails()); |
226 } | 218 } |
227 | 219 |
228 return true; | 220 return true; |
229 } | 221 } |
230 | 222 |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 return false; | 640 return false; |
649 } | 641 } |
650 | 642 |
651 return true; | 643 return true; |
652 } | 644 } |
653 | 645 |
654 bool InstantController::IsOutOfDate() const { | 646 bool InstantController::IsOutOfDate() const { |
655 return !last_active_tab_ || | 647 return !last_active_tab_ || |
656 last_active_tab_ != delegate_->GetActiveTabContents(); | 648 last_active_tab_ != delegate_->GetActiveTabContents(); |
657 } | 649 } |
OLD | NEW |