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

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

Issue 14698028: Omnibox refactor. OmniboxController now holds an AutocompleteMatch. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 7 years, 6 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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/ui/omnibox/omnibox_edit_model.h" 5 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // NOTE: Be sure and set keyword-related state BEFORE invoking 177 // NOTE: Be sure and set keyword-related state BEFORE invoking
178 // DisplayTextFromUserText(), as its result depends upon this state. 178 // DisplayTextFromUserText(), as its result depends upon this state.
179 keyword_ = state.keyword; 179 keyword_ = state.keyword;
180 is_keyword_hint_ = state.is_keyword_hint; 180 is_keyword_hint_ = state.is_keyword_hint;
181 view_->SetUserText(state.user_text, 181 view_->SetUserText(state.user_text,
182 DisplayTextFromUserText(state.user_text), false); 182 DisplayTextFromUserText(state.user_text), false);
183 view_->SetInstantSuggestion(state.instant_suggestion); 183 view_->SetInstantSuggestion(state.instant_suggestion);
184 } 184 }
185 } 185 }
186 186
187 AutocompleteMatch OmniboxEditModel::CurrentMatch() { 187 AutocompleteMatch OmniboxEditModel::CurrentMatch(
188 GURL* alternate_nav_url) const {
189 // If we have a valid match use it. Otherwise get one for the current text.
190 if (omnibox_controller_->current_match().destination_url.is_valid()) {
191 AutocompleteMatch match = omnibox_controller_->current_match();
192 if (alternate_nav_url && AutocompleteMatch::IsSearchType(match.type)) {
193 // Compute the |alternate_nav_url| if needed.
194 *alternate_nav_url = URLFixerUpper::FixupURL(
195 UTF16ToUTF8(match.fill_into_edit), std::string());
196 if (*alternate_nav_url == match.destination_url)
197 *alternate_nav_url = GURL();
198 }
199 return match;
200 }
201
188 AutocompleteMatch match; 202 AutocompleteMatch match;
189 GetInfoForCurrentText(&match, NULL); 203 GetInfoForCurrentText(&match, alternate_nav_url);
190 return match; 204 return match;
191 } 205 }
192 206
193 bool OmniboxEditModel::UpdatePermanentText(const string16& new_permanent_text) { 207 bool OmniboxEditModel::UpdatePermanentText(const string16& new_permanent_text) {
194 // When there's a new URL, and the user is not editing anything or the edit 208 // When there's a new URL, and the user is not editing anything or the edit
195 // doesn't have focus, we want to revert the edit to show the new URL. (The 209 // doesn't have focus, we want to revert the edit to show the new URL. (The
196 // common case where the edit doesn't have focus is when the user has started 210 // common case where the edit doesn't have focus is when the user has started
197 // an edit and then abandoned it and clicked a link on the page.) 211 // an edit and then abandoned it and clicked a link on the page.)
198 // 212 //
199 // If the page is auto-committing an instant suggestion, however, we generally 213 // If the page is auto-committing an instant suggestion, however, we generally
(...skipping 13 matching lines...) Expand all
213 return visibly_changed_permanent_text; 227 return visibly_changed_permanent_text;
214 } 228 }
215 229
216 GURL OmniboxEditModel::PermanentURL() { 230 GURL OmniboxEditModel::PermanentURL() {
217 return URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), std::string()); 231 return URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), std::string());
218 } 232 }
219 233
220 void OmniboxEditModel::SetUserText(const string16& text) { 234 void OmniboxEditModel::SetUserText(const string16& text) {
221 SetInputInProgress(true); 235 SetInputInProgress(true);
222 InternalSetUserText(text); 236 InternalSetUserText(text);
237 omnibox_controller_->InvalidateCurrentMatch();
223 paste_state_ = NONE; 238 paste_state_ = NONE;
224 has_temporary_text_ = false; 239 has_temporary_text_ = false;
225 is_temporary_text_set_by_instant_ = false; 240 is_temporary_text_set_by_instant_ = false;
226 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch; 241 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch;
227 is_instant_temporary_text_a_search_query_ = false; 242 is_instant_temporary_text_a_search_query_ = false;
228 } 243 }
229 244
230 void OmniboxEditModel::FinalizeInstantQuery(
231 const string16& input_text,
232 const InstantSuggestion& suggestion) {
233 // Should only get called for the HTML popup.
234 #if defined(HTML_INSTANT_EXTENDED_POPUP)
235 if (!popup_model()->result().empty()) {
236 // When a IME is active and a candidate window is open, we don't show
237 // the omnibox popup, though |result()| may be available. Thus we check
238 // whether result().empty() or not instead of whether IsOpen() or not.
239 // We need the finalization of instant query when result() is available.
240 SearchProvider* search_provider =
241 autocomplete_controller()->search_provider();
242 // There may be no providers during testing; guard against that.
243 if (search_provider)
244 search_provider->FinalizeInstantQuery(input_text, suggestion);
245 }
246 #endif
247 }
248
249 void OmniboxEditModel::SetInstantSuggestion( 245 void OmniboxEditModel::SetInstantSuggestion(
250 const InstantSuggestion& suggestion) { 246 const InstantSuggestion& suggestion) {
251 // Should only get called for the HTML popup. 247 // Should only get called for the HTML popup.
252 #if defined(HTML_INSTANT_EXTENDED_POPUP) 248 #if defined(HTML_INSTANT_EXTENDED_POPUP)
253 switch (suggestion.behavior) { 249 omnibox_controller_->SetInstantSuggestion(suggestion);
254 case INSTANT_COMPLETE_NOW:
255 view_->SetInstantSuggestion(string16());
256 if (!suggestion.text.empty())
257 FinalizeInstantQuery(view_->GetText(), suggestion);
258 break;
259
260 case INSTANT_COMPLETE_NEVER: {
261 DCHECK_EQ(INSTANT_SUGGESTION_SEARCH, suggestion.type);
262 view_->SetInstantSuggestion(suggestion.text);
263 autocomplete_controller()->search_provider()->ClearInstantSuggestion();
264 break;
265 }
266
267 case INSTANT_COMPLETE_REPLACE: {
268 const bool save_original_selection = !has_temporary_text_;
269 view_->SetInstantSuggestion(string16());
270 has_temporary_text_ = true;
271 is_temporary_text_set_by_instant_ = true;
272 selected_instant_autocomplete_match_index_ =
273 suggestion.autocomplete_match_index;
274 is_instant_temporary_text_a_search_query_ =
275 suggestion.type == INSTANT_SUGGESTION_SEARCH;
276 // Instant suggestions are never a keyword.
277 keyword_ = string16();
278 is_keyword_hint_ = false;
279 view_->OnTemporaryTextMaybeChanged(suggestion.text,
280 save_original_selection, true);
281 break;
282 }
283 }
284 #endif 250 #endif
285 } 251 }
286 252
287 bool OmniboxEditModel::CommitSuggestedText() { 253 bool OmniboxEditModel::CommitSuggestedText() {
288 const string16 suggestion = view_->GetInstantSuggestion(); 254 const string16 suggestion = view_->GetInstantSuggestion();
289 if (suggestion.empty()) 255 if (suggestion.empty())
290 return false; 256 return false;
291 257
292 // Assume that the gray text we are committing is a search suggestion. 258 // Assume that the gray text we are committing is a search suggestion.
293 const string16 final_text = view_->GetText() + suggestion; 259 const string16 final_text = view_->GetText() + suggestion;
294 view_->OnBeforePossibleChange(); 260 view_->OnBeforePossibleChange();
295 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false, 261 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false,
296 false); 262 false);
297 view_->OnAfterPossibleChange(); 263 view_->OnAfterPossibleChange();
298 return true; 264 return true;
299 } 265 }
300 266
301 void OmniboxEditModel::OnChanged() { 267 void OmniboxEditModel::OnChanged() {
302 // Don't call CurrentMatch() when there's no editing, as in this case we'll 268 // Don't call CurrentMatch() when there's no editing, as in this case we'll
303 // never actually use it. This avoids running the autocomplete providers (and 269 // never actually use it. This avoids running the autocomplete providers (and
304 // any systems they then spin up) during startup. 270 // any systems they then spin up) during startup.
305 const AutocompleteMatch& current_match = user_input_in_progress_ ? 271 const AutocompleteMatch& current_match = user_input_in_progress_ ?
306 CurrentMatch() : AutocompleteMatch(); 272 CurrentMatch(NULL) : AutocompleteMatch();
307 273
308 AutocompleteActionPredictor::Action recommended_action = 274 AutocompleteActionPredictor::Action recommended_action =
309 AutocompleteActionPredictor::ACTION_NONE; 275 AutocompleteActionPredictor::ACTION_NONE;
310 AutocompleteActionPredictor* action_predictor = 276 AutocompleteActionPredictor* action_predictor =
311 user_input_in_progress_ ? 277 user_input_in_progress_ ?
312 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL; 278 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL;
313 if (action_predictor) { 279 if (action_predictor) {
314 action_predictor->RegisterTransitionalMatches(user_text_, result()); 280 action_predictor->RegisterTransitionalMatches(user_text_, result());
315 // Confer with the AutocompleteActionPredictor to determine what action, if 281 // Confer with the AutocompleteActionPredictor to determine what action, if
316 // any, we should take. Get the recommended action here even if we don't 282 // any, we should take. Get the recommended action here even if we don't
(...skipping 21 matching lines...) Expand all
338 user_input_in_progress_, in_escape_handler_, 304 user_input_in_progress_, in_escape_handler_,
339 view_->DeleteAtEndPressed() || just_deleted_text_, 305 view_->DeleteAtEndPressed() || just_deleted_text_,
340 KeywordIsSelected()); 306 KeywordIsSelected());
341 } 307 }
342 308
343 if (!performed_instant) { 309 if (!performed_instant) {
344 // Hide any suggestions we might be showing. 310 // Hide any suggestions we might be showing.
345 view_->SetInstantSuggestion(string16()); 311 view_->SetInstantSuggestion(string16());
346 312
347 // No need to wait any longer for Instant. 313 // No need to wait any longer for Instant.
348 FinalizeInstantQuery(string16(), InstantSuggestion()); 314 omnibox_controller_->FinalizeInstantQuery(string16(), InstantSuggestion());
349 } 315 }
350 316
351 switch (recommended_action) { 317 switch (recommended_action) {
352 case AutocompleteActionPredictor::ACTION_PRERENDER: 318 case AutocompleteActionPredictor::ACTION_PRERENDER:
353 // It's possible that there is no current page, for instance if the tab 319 // It's possible that there is no current page, for instance if the tab
354 // has been closed or on return from a sleep state. 320 // has been closed or on return from a sleep state.
355 // (http://crbug.com/105689) 321 // (http://crbug.com/105689)
356 if (!delegate_->CurrentPageExists()) 322 if (!delegate_->CurrentPageExists())
357 break; 323 break;
358 // Ask for prerendering if the destination URL is different than the 324 // Ask for prerendering if the destination URL is different than the
359 // current URL. 325 // current URL.
360 if (current_match.destination_url != PermanentURL()) 326 if (current_match.destination_url != PermanentURL())
361 delegate_->DoPrerender(current_match); 327 delegate_->DoPrerender(current_match);
362 break; 328 break;
363 case AutocompleteActionPredictor::ACTION_PRECONNECT: 329 case AutocompleteActionPredictor::ACTION_PRECONNECT:
364 omnibox_controller_->DoPreconnect(current_match); 330 omnibox_controller_->DoPreconnect(current_match);
365 break; 331 break;
366 case AutocompleteActionPredictor::ACTION_NONE: 332 case AutocompleteActionPredictor::ACTION_NONE:
367 break; 333 break;
368 } 334 }
369 335
370 controller_->OnChanged(); 336 controller_->OnChanged();
371 } 337 }
372 338
373 void OmniboxEditModel::GetDataForURLExport(GURL* url, 339 void OmniboxEditModel::GetDataForURLExport(GURL* url,
374 string16* title, 340 string16* title,
375 gfx::Image* favicon) { 341 gfx::Image* favicon) {
376 AutocompleteMatch match; 342 *url = CurrentMatch(NULL).destination_url;
377 GetInfoForCurrentText(&match, NULL);
378 *url = match.destination_url;
379 if (*url == URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), 343 if (*url == URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_),
380 std::string())) { 344 std::string())) {
381 *title = controller_->GetTitle(); 345 *title = controller_->GetTitle();
382 *favicon = controller_->GetFavicon(); 346 *favicon = controller_->GetFavicon();
383 } 347 }
384 } 348 }
385 349
386 bool OmniboxEditModel::CurrentTextIsURL() const { 350 bool OmniboxEditModel::CurrentTextIsURL() const {
387 if (view_->toolbar_model()->WouldReplaceSearchURLWithSearchTerms()) 351 if (view_->toolbar_model()->WouldReplaceSearchURLWithSearchTerms())
388 return false; 352 return false;
389 353
390 // If current text is not composed of replaced search terms and 354 // If current text is not composed of replaced search terms and
391 // !user_input_in_progress_, then permanent text is showing and should be a 355 // !user_input_in_progress_, then permanent text is showing and should be a
392 // URL, so no further checking is needed. By avoiding checking in this case, 356 // URL, so no further checking is needed. By avoiding checking in this case,
393 // we avoid calling into the autocomplete providers, and thus initializing the 357 // we avoid calling into the autocomplete providers, and thus initializing the
394 // history system, as long as possible, which speeds startup. 358 // history system, as long as possible, which speeds startup.
395 if (!user_input_in_progress_) 359 if (!user_input_in_progress_)
396 return true; 360 return true;
397 361
398 AutocompleteMatch match; 362 return !AutocompleteMatch::IsSearchType(CurrentMatch(NULL).type);
399 GetInfoForCurrentText(&match, NULL);
400 return !AutocompleteMatch::IsSearchType(match.type);
401 } 363 }
402 364
403 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const { 365 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const {
404 AutocompleteMatch match; 366 return CurrentMatch(NULL).type;
405 GetInfoForCurrentText(&match, NULL);
406 return match.type;
407 } 367 }
408 368
409 void OmniboxEditModel::AdjustTextForCopy(int sel_min, 369 void OmniboxEditModel::AdjustTextForCopy(int sel_min,
410 bool is_all_selected, 370 bool is_all_selected,
411 string16* text, 371 string16* text,
412 GURL* url, 372 GURL* url,
413 bool* write_url) { 373 bool* write_url) {
414 *write_url = false; 374 *write_url = false;
415 375
416 // Do not adjust if selection did not start at the beginning of the field, or 376 // Do not adjust if selection did not start at the beginning of the field, or
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 514
555 bool OmniboxEditModel::IsPasteAndSearch(const string16& text) const { 515 bool OmniboxEditModel::IsPasteAndSearch(const string16& text) const {
556 AutocompleteMatch match; 516 AutocompleteMatch match;
557 ClassifyStringForPasteAndGo(text, &match, NULL); 517 ClassifyStringForPasteAndGo(text, &match, NULL);
558 return AutocompleteMatch::IsSearchType(match.type); 518 return AutocompleteMatch::IsSearchType(match.type);
559 } 519 }
560 520
561 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition, 521 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition,
562 bool for_drop) { 522 bool for_drop) {
563 // Get the URL and transition type for the selected entry. 523 // Get the URL and transition type for the selected entry.
564 AutocompleteMatch match;
565 GURL alternate_nav_url; 524 GURL alternate_nav_url;
566 GetInfoForCurrentText(&match, &alternate_nav_url); 525 AutocompleteMatch match = CurrentMatch(&alternate_nav_url);
567 526
568 // If CTRL is down it means the user wants to append ".com" to the text he 527 // If CTRL is down it means the user wants to append ".com" to the text he
569 // typed. If we can successfully generate a URL_WHAT_YOU_TYPED match doing 528 // typed. If we can successfully generate a URL_WHAT_YOU_TYPED match doing
570 // that, then we use this. These matches are marked as generated by the 529 // that, then we use this. These matches are marked as generated by the
571 // HistoryURLProvider so we only generate them if this provider is present. 530 // HistoryURLProvider so we only generate them if this provider is present.
572 if (control_key_state_ == DOWN_WITHOUT_CHANGE && !KeywordIsSelected() && 531 if (control_key_state_ == DOWN_WITHOUT_CHANGE && !KeywordIsSelected() &&
573 autocomplete_controller()->history_url_provider()) { 532 autocomplete_controller()->history_url_provider()) {
574 // Generate a new AutocompleteInput, copying the latest one but using "com" 533 // Generate a new AutocompleteInput, copying the latest one but using "com"
575 // as the desired TLD. Then use this autocomplete input to generate a 534 // as the desired TLD. Then use this autocomplete input to generate a
576 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent 535 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 if (popup_model()->IsOpen()) { 595 if (popup_model()->IsOpen()) {
637 const base::TimeTicks& now(base::TimeTicks::Now()); 596 const base::TimeTicks& now(base::TimeTicks::Now());
638 base::TimeDelta elapsed_time_since_user_first_modified_omnibox( 597 base::TimeDelta elapsed_time_since_user_first_modified_omnibox(
639 now - time_user_first_modified_omnibox_); 598 now - time_user_first_modified_omnibox_);
640 base::TimeDelta elapsed_time_since_last_change_to_default_match( 599 base::TimeDelta elapsed_time_since_last_change_to_default_match(
641 now - autocomplete_controller()->last_time_default_match_changed()); 600 now - autocomplete_controller()->last_time_default_match_changed());
642 // These elapsed times don't really make sense for ZeroSuggest matches 601 // These elapsed times don't really make sense for ZeroSuggest matches
643 // (because the user does not modify the omnibox for ZeroSuggest), so for 602 // (because the user does not modify the omnibox for ZeroSuggest), so for
644 // those we set the elapsed times to something that will be ignored by 603 // those we set the elapsed times to something that will be ignored by
645 // metrics_log.cc. 604 // metrics_log.cc.
646 if (match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) { 605 if (match.provider &&
606 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) {
647 elapsed_time_since_user_first_modified_omnibox = 607 elapsed_time_since_user_first_modified_omnibox =
648 base::TimeDelta::FromMilliseconds(-1); 608 base::TimeDelta::FromMilliseconds(-1);
649 elapsed_time_since_last_change_to_default_match = 609 elapsed_time_since_last_change_to_default_match =
650 base::TimeDelta::FromMilliseconds(-1); 610 base::TimeDelta::FromMilliseconds(-1);
651 } 611 }
652 // TODO(sreeram): Handle is_temporary_text_set_by_instant_ correctly. 612 // TODO(sreeram): Handle is_temporary_text_set_by_instant_ correctly.
653 OmniboxLog log( 613 OmniboxLog log(
654 autocomplete_controller()->input().text(), 614 autocomplete_controller()->input().text(),
655 just_deleted_text_, 615 just_deleted_text_,
656 autocomplete_controller()->input().type(), 616 autocomplete_controller()->input().type(),
657 popup_model()->selected_line(), 617 popup_model()->selected_line(),
658 -1, // don't yet know tab ID; set later if appropriate 618 -1, // don't yet know tab ID; set later if appropriate
659 delegate_->CurrentPageExists() ? ClassifyPage(delegate_->GetURL()) : 619 delegate_->CurrentPageExists() ? ClassifyPage(delegate_->GetURL()) :
660 metrics::OmniboxEventProto_PageClassification_OTHER, 620 metrics::OmniboxEventProto_PageClassification_OTHER,
661 elapsed_time_since_user_first_modified_omnibox, 621 elapsed_time_since_user_first_modified_omnibox,
662 string16::npos, // completed_length; possibly set later 622 string16::npos, // completed_length; possibly set later
663 elapsed_time_since_last_change_to_default_match, 623 elapsed_time_since_last_change_to_default_match,
664 result()); 624 result());
665 DCHECK(user_input_in_progress_ || 625
666 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) 626 DCHECK(user_input_in_progress_ || (match.provider &&
627 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST))
667 << "We didn't get here through the expected series of calls. " 628 << "We didn't get here through the expected series of calls. "
668 << "time_user_first_modified_omnibox_ is not set correctly and other " 629 << "time_user_first_modified_omnibox_ is not set correctly and other "
669 << "things may be wrong. Match provider: " << match.provider->GetName(); 630 << "things may be wrong. Match provider: "
631 << (match.provider ? match.provider->GetName() : "NULL");
670 DCHECK(log.elapsed_time_since_user_first_modified_omnibox >= 632 DCHECK(log.elapsed_time_since_user_first_modified_omnibox >=
671 log.elapsed_time_since_last_change_to_default_match) 633 log.elapsed_time_since_last_change_to_default_match)
672 << "We should've got the notification that the user modified the " 634 << "We should've got the notification that the user modified the "
673 << "omnibox text at same time or before the most recent time the " 635 << "omnibox text at same time or before the most recent time the "
674 << "default match changed."; 636 << "default match changed.";
637
675 if (index != OmniboxPopupModel::kNoMatch) 638 if (index != OmniboxPopupModel::kNoMatch)
676 log.selected_index = index; 639 log.selected_index = index;
677 if (match.inline_autocomplete_offset != string16::npos) { 640 if (match.inline_autocomplete_offset != string16::npos) {
678 DCHECK_GE(match.fill_into_edit.length(), 641 DCHECK_GE(match.fill_into_edit.length(),
679 match.inline_autocomplete_offset); 642 match.inline_autocomplete_offset);
680 log.completed_length = 643 log.completed_length =
681 match.fill_into_edit.length() - match.inline_autocomplete_offset; 644 match.fill_into_edit.length() - match.inline_autocomplete_offset;
682 } 645 }
683 646
684 if ((disposition == CURRENT_TAB) && delegate_->CurrentPageExists()) { 647 if ((disposition == CURRENT_TAB) && delegate_->CurrentPageExists()) {
685 // If we know the destination is being opened in the current tab, 648 // If we know the destination is being opened in the current tab,
686 // we can easily get the tab ID. (If it's being opened in a new 649 // we can easily get the tab ID. (If it's being opened in a new
687 // tab, we don't know the tab ID yet.) 650 // tab, we don't know the tab ID yet.)
688 log.tab_id = delegate_->GetSessionID().id(); 651 log.tab_id = delegate_->GetSessionID().id();
689 } 652 }
690 autocomplete_controller()->AddProvidersInfo(&log.providers_info); 653 autocomplete_controller()->AddProvidersInfo(&log.providers_info);
691 content::NotificationService::current()->Notify( 654 content::NotificationService::current()->Notify(
692 chrome::NOTIFICATION_OMNIBOX_OPENED_URL, 655 chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
693 content::Source<Profile>(profile_), 656 content::Source<Profile>(profile_),
694 content::Details<OmniboxLog>(&log)); 657 content::Details<OmniboxLog>(&log));
695 HISTOGRAM_ENUMERATION("Omnibox.EventCount", 1, 2); 658 HISTOGRAM_ENUMERATION("Omnibox.EventCount", 1, 2);
696 } 659 }
697 660
698 TemplateURL* template_url = match.GetTemplateURL(profile_, false); 661 TemplateURL* template_url = match.GetTemplateURL(profile_, false);
699 if (template_url) { 662 if (template_url) {
700 if (match.transition == content::PAGE_TRANSITION_KEYWORD) { 663 if (match.transition == content::PAGE_TRANSITION_KEYWORD) {
701 // The user is using a non-substituting keyword or is explicitly in 664 // The user is using a non-substituting keyword or is explicitly in
702 // keyword mode. 665 // keyword mode.
703
704 AutocompleteMatch current_match;
705 GetInfoForCurrentText(&current_match, NULL);
706 const AutocompleteMatch& match = (index == OmniboxPopupModel::kNoMatch) ? 666 const AutocompleteMatch& match = (index == OmniboxPopupModel::kNoMatch) ?
707 current_match : result().match_at(index); 667 CurrentMatch(NULL) : result().match_at(index);
708 668
709 // Don't increment usage count for extension keywords. 669 // Don't increment usage count for extension keywords.
710 if (delegate_->ProcessExtensionKeyword(template_url, match, 670 if (delegate_->ProcessExtensionKeyword(template_url, match,
711 disposition)) { 671 disposition)) {
712 if (disposition != NEW_BACKGROUND_TAB) 672 if (disposition != NEW_BACKGROUND_TAB)
713 view_->RevertAll(); 673 view_->RevertAll();
714 return; 674 return;
715 } 675 }
716 676
717 content::RecordAction(UserMetricsAction("AcceptedKeyword")); 677 content::RecordAction(UserMetricsAction("AcceptedKeyword"));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 743
784 // Ensure the current selection is saved before showing keyword mode 744 // Ensure the current selection is saved before showing keyword mode
785 // so that moving to another line and then reverting the text will restore 745 // so that moving to another line and then reverting the text will restore
786 // the current state properly. 746 // the current state properly.
787 bool save_original_selection = !has_temporary_text_; 747 bool save_original_selection = !has_temporary_text_;
788 has_temporary_text_ = true; 748 has_temporary_text_ = true;
789 is_temporary_text_set_by_instant_ = false; 749 is_temporary_text_set_by_instant_ = false;
790 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch; 750 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch;
791 is_instant_temporary_text_a_search_query_ = false; 751 is_instant_temporary_text_a_search_query_ = false;
792 view_->OnTemporaryTextMaybeChanged( 752 view_->OnTemporaryTextMaybeChanged(
793 DisplayTextFromUserText(CurrentMatch().fill_into_edit), 753 DisplayTextFromUserText(CurrentMatch(NULL).fill_into_edit),
794 save_original_selection, true); 754 save_original_selection, true);
795 755
796 content::RecordAction(UserMetricsAction("AcceptedKeywordHint")); 756 content::RecordAction(UserMetricsAction("AcceptedKeywordHint"));
797 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method, 757 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method,
798 ENTERED_KEYWORD_MODE_NUM_ITEMS); 758 ENTERED_KEYWORD_MODE_NUM_ITEMS);
799 759
800 return true; 760 return true;
801 } 761 }
802 762
803 void OmniboxEditModel::AcceptTemporaryTextAsUserText() { 763 void OmniboxEditModel::AcceptTemporaryTextAsUserText() {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 // TODO(samarth): determine if it is safe to move the call to 845 // TODO(samarth): determine if it is safe to move the call to
886 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us 846 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us
887 // just call SetFocusState() to handle the state change. 847 // just call SetFocusState() to handle the state change.
888 focus_state_ = OMNIBOX_FOCUS_NONE; 848 focus_state_ = OMNIBOX_FOCUS_NONE;
889 control_key_state_ = UP; 849 control_key_state_ = UP;
890 paste_state_ = NONE; 850 paste_state_ = NONE;
891 } 851 }
892 852
893 bool OmniboxEditModel::OnEscapeKeyPressed() { 853 bool OmniboxEditModel::OnEscapeKeyPressed() {
894 if (has_temporary_text_) { 854 if (has_temporary_text_) {
895 AutocompleteMatch match; 855 if (CurrentMatch(NULL).destination_url != original_url_) {
896 GetInfoForCurrentText(&match, NULL);
897 if (match.destination_url != original_url_) {
898 RevertTemporaryText(true); 856 RevertTemporaryText(true);
899 return true; 857 return true;
900 } 858 }
901 } 859 }
902 860
903 // We do not clear the pending entry from the omnibox when a load is first 861 // We do not clear the pending entry from the omnibox when a load is first
904 // stopped. If the user presses Escape while stopped, we clear it. 862 // stopped. If the user presses Escape while stopped, we clear it.
905 if (delegate_->CurrentPageExists() && !delegate_->IsLoading()) { 863 if (delegate_->CurrentPageExists() && !delegate_->IsLoading()) {
906 delegate_->GetNavigationController().DiscardNonCommittedEntries(); 864 delegate_->GetNavigationController().DiscardNonCommittedEntries();
907 view_->Update(NULL); 865 view_->Update(NULL);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 popup_model()->Move(count); 944 popup_model()->Move(count);
987 } 945 }
988 } 946 }
989 } 947 }
990 948
991 void OmniboxEditModel::OnPopupDataChanged( 949 void OmniboxEditModel::OnPopupDataChanged(
992 const string16& text, 950 const string16& text,
993 GURL* destination_for_temporary_text_change, 951 GURL* destination_for_temporary_text_change,
994 const string16& keyword, 952 const string16& keyword,
995 bool is_keyword_hint) { 953 bool is_keyword_hint) {
954 // The popup changed its data, the match in the controller is no longer valid.
955 omnibox_controller_->InvalidateCurrentMatch();
956
996 // Update keyword/hint-related local state. 957 // Update keyword/hint-related local state.
997 bool keyword_state_changed = (keyword_ != keyword) || 958 bool keyword_state_changed = (keyword_ != keyword) ||
998 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()); 959 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty());
999 if (keyword_state_changed) { 960 if (keyword_state_changed) {
1000 keyword_ = keyword; 961 keyword_ = keyword;
1001 is_keyword_hint_ = is_keyword_hint; 962 is_keyword_hint_ = is_keyword_hint;
1002 963
1003 // |is_keyword_hint_| should always be false if |keyword_| is empty. 964 // |is_keyword_hint_| should always be false if |keyword_| is empty.
1004 DCHECK(!keyword_.empty() || !is_keyword_hint_); 965 DCHECK(!keyword_.empty() || !is_keyword_hint_);
1005 } 966 }
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 // determine what keyword, if any, is applicable. 1125 // determine what keyword, if any, is applicable.
1165 // 1126 //
1166 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that 1127 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that
1167 // will have updated our state already, so in that case we don't also return 1128 // will have updated our state already, so in that case we don't also return
1168 // true from this function. 1129 // true from this function.
1169 return !(text_differs && allow_keyword_ui_change && !just_deleted_text && 1130 return !(text_differs && allow_keyword_ui_change && !just_deleted_text &&
1170 no_selection && (selection_start == user_text_.length()) && 1131 no_selection && (selection_start == user_text_.length()) &&
1171 MaybeAcceptKeywordBySpace(user_text_)); 1132 MaybeAcceptKeywordBySpace(user_text_));
1172 } 1133 }
1173 1134
1174 void OmniboxEditModel::OnResultChanged(bool default_match_changed) { 1135 void OmniboxEditModel::OnCurrentMatchChanged(bool is_temporary_set_by_instant) {
1136 has_temporary_text_ = is_temporary_set_by_instant;
1137 is_temporary_text_set_by_instant_ = is_temporary_set_by_instant;
1138
1139 const AutocompleteMatch& match = omnibox_controller_->current_match();
1140
1141 if (is_temporary_set_by_instant) {
1142 view_->OnTemporaryTextMaybeChanged(
1143 DisplayTextFromUserText(match.fill_into_edit), !has_temporary_text_,
1144 false);
1145 } else {
1146 match.GetKeywordUIState(profile_, &keyword_, &is_keyword_hint_);
1147 string16 inline_autocomplete_text;
1148 if (match.inline_autocomplete_offset < match.fill_into_edit.length()) {
1149 // We have blue text, go through OnPopupDataChanged.
1150 // TODO(beaudoin): Merge OnPopupDataChanged with this method once the
1151 // popup handling has completely migrated to omnibox_controller.
1152 inline_autocomplete_text =
1153 match.fill_into_edit.substr(match.inline_autocomplete_offset);
1154 }
1155 popup_model()->OnResultChanged();
1156 OnPopupDataChanged(inline_autocomplete_text, NULL, keyword_,
1157 is_keyword_hint_);
1158 }
1159 }
1160
1161 void OmniboxEditModel::OnGrayTextChanged() {
1162 view_->SetInstantSuggestion(omnibox_controller_->gray_suggestion());
1163 }
1164
1165 string16 OmniboxEditModel::GetViewText() const {
1166 return view_->GetText();
1175 } 1167 }
1176 1168
1177 InstantController* OmniboxEditModel::GetInstantController() const { 1169 InstantController* OmniboxEditModel::GetInstantController() const {
1178 return controller_->GetInstant(); 1170 return controller_->GetInstant();
1179 } 1171 }
1180 1172
1181 bool OmniboxEditModel::query_in_progress() const { 1173 bool OmniboxEditModel::query_in_progress() const {
1182 return !autocomplete_controller()->done(); 1174 return !autocomplete_controller()->done();
1183 } 1175 }
1184 1176
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 instant->OmniboxFocusChanged(state, reason, NULL); 1417 instant->OmniboxFocusChanged(state, reason, NULL);
1426 1418
1427 // Update state and notify view if the omnibox has focus and the caret 1419 // Update state and notify view if the omnibox has focus and the caret
1428 // visibility changed. 1420 // visibility changed.
1429 const bool was_caret_visible = is_caret_visible(); 1421 const bool was_caret_visible = is_caret_visible();
1430 focus_state_ = state; 1422 focus_state_ = state;
1431 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1423 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1432 is_caret_visible() != was_caret_visible) 1424 is_caret_visible() != was_caret_visible)
1433 view_->ApplyCaretVisibility(); 1425 view_->ApplyCaretVisibility();
1434 } 1426 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698