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

Side by Side Diff: components/omnibox/browser/omnibox_edit_model.cc

Issue 2425703003: Fix failing DCHECK in AutoCompleteInput() (Closed)
Patch Set: use pkasting's suggested code Created 4 years, 2 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
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "components/omnibox/browser/omnibox_edit_model.h" 5 #include "components/omnibox/browser/omnibox_edit_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 InternalSetUserText(base::string16()); 399 InternalSetUserText(base::string16());
400 keyword_.clear(); 400 keyword_.clear();
401 is_keyword_hint_ = false; 401 is_keyword_hint_ = false;
402 has_temporary_text_ = false; 402 has_temporary_text_ = false;
403 view_->SetWindowTextAndCaretPos(permanent_text_, 403 view_->SetWindowTextAndCaretPos(permanent_text_,
404 has_focus() ? permanent_text_.length() : 0, 404 has_focus() ? permanent_text_.length() : 0,
405 false, true); 405 false, true);
406 client_->OnRevert(); 406 client_->OnRevert();
407 } 407 }
408 408
409 void OmniboxEditModel::StartAutocomplete( 409 void OmniboxEditModel::StartAutocomplete(bool has_selected_text,
410 bool has_selected_text, 410 bool prevent_inline_autocomplete) {
411 bool prevent_inline_autocomplete, 411 const base::string16 input_text = MaybePrependKeyword(user_text_);
412 bool entering_keyword_mode) { 412
413 // Compute the cursor position. There are three cases:
414 // 1. The user is in the midst of typing; there is no inline autocompletion.
415 // 2. The user is in the midst of typing; there is inline autocompletion.
416 // 3. The user is not in the midst of typing, but is triggering this some
417 // other way, e.g. hitting ctrl-K while the view is showing the permanent
418 // or temporary text.
413 size_t cursor_position; 419 size_t cursor_position;
414 const base::string16 input_text = MaybePrependKeyword(user_text_); 420 if (!has_temporary_text_ && (user_text_ == view_->GetText())) {
415 if (inline_autocomplete_text_.empty()) { 421 // Case 1 above. In this case there's a meaningful current cursor position,
416 // Cursor position is equivalent to the current selection's end. 422 // so we read it from the view. (Note that if there is a selected range,
423 // the "cursor position" is considered to be the selection's end.)
417 size_t start; 424 size_t start;
418 view_->GetSelectionBounds(&start, &cursor_position); 425 view_->GetSelectionBounds(&start, &cursor_position);
426
419 // For keyword searches, the text that AutocompleteInput expects is of the 427 // For keyword searches, the text that AutocompleteInput expects is of the
420 // form "<keyword> <query>", where our query is |user_text_|. So if we're 428 // form "<keyword> <query>", where our query is |user_text_|. So we need to
421 // in keyword mode, we need to adjust the cursor position forward by the 429 // adjust the cursor position forward by the length of any keyword added by
422 // length of "<keyword> ". If we're just entering keyword mode, though, we 430 // MaybePrependKeyword() above.
423 // have to avoid making this adjustment, because we haven't actually updated 431 cursor_position += input_text.length() - user_text_.length();
424 // |user_text_| yet, but the caller has already cleared |is_keyword_hint_|,
425 // so MaybePrependKeyword() will believe we are already in keyword mode, and
426 // will thus mis-adjust the cursor position.
427 if (!entering_keyword_mode)
428 cursor_position += input_text.length() - user_text_.length();
429 } else { 432 } else {
430 // There are some cases where StartAutocomplete() may be called 433 // Case 2 or 3 above. In case 2, the existing inline autocompletion will be
431 // with non-empty |inline_autocomplete_text_|. In such cases, we cannot 434 // ignored for this next autocomplete run. The current cursor position is
432 // use the current selection, because it could result with the cursor 435 // always effectively "the end of the input text". (If the user changes
433 // position past the last character from the user text. Instead, 436 // this cursor position by arrowing, it will accept the inline
434 // we assume that the cursor is simply at the end of input. 437 // autocompletion, which would put us in case 1.) In case 3, there is no
435 // One example is when user presses Ctrl key while having a highlighted 438 // meaningful current cursor position; the correct default behavior is to
436 // inline autocomplete text. 439 // simply claim the cursor is at the end of the input.
437 // TODO: Rethink how we are going to handle this case to avoid
438 // inconsistent behavior when user presses Ctrl key.
439 // See http://crbug.com/165961 and http://crbug.com/165968 for more details.
440 cursor_position = input_text.length(); 440 cursor_position = input_text.length();
441 } 441 }
442 442
443 GURL current_url; 443 GURL current_url;
444 if (client_->CurrentPageExists()) 444 if (client_->CurrentPageExists())
445 current_url = client_->GetURL(); 445 current_url = client_->GetURL();
446 input_ = AutocompleteInput( 446 input_ = AutocompleteInput(
447 input_text, cursor_position, std::string(), current_url, ClassifyPage(), 447 input_text, cursor_position, std::string(), current_url, ClassifyPage(),
448 prevent_inline_autocomplete || just_deleted_text_ || 448 prevent_inline_autocomplete || just_deleted_text_ ||
449 (has_selected_text && inline_autocomplete_text_.empty()) || 449 (has_selected_text && inline_autocomplete_text_.empty()) ||
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 void OmniboxEditModel::EnterKeywordModeForDefaultSearchProvider( 559 void OmniboxEditModel::EnterKeywordModeForDefaultSearchProvider(
560 KeywordModeEntryMethod entry_method) { 560 KeywordModeEntryMethod entry_method) {
561 autocomplete_controller()->Stop(false); 561 autocomplete_controller()->Stop(false);
562 562
563 user_input_in_progress_ = true; 563 user_input_in_progress_ = true;
564 keyword_ = client_->GetTemplateURLService()-> 564 keyword_ = client_->GetTemplateURLService()->
565 GetDefaultSearchProvider()->keyword(); 565 GetDefaultSearchProvider()->keyword();
566 is_keyword_hint_ = false; 566 is_keyword_hint_ = false;
567 keyword_mode_entry_method_ = entry_method; 567 keyword_mode_entry_method_ = entry_method;
568 568
569 StartAutocomplete(false, false, true); 569 StartAutocomplete(false, false);
570 570
571 UMA_HISTOGRAM_ENUMERATION( 571 UMA_HISTOGRAM_ENUMERATION(
572 kEnteredKeywordModeHistogram, static_cast<int>(entry_method), 572 kEnteredKeywordModeHistogram, static_cast<int>(entry_method),
573 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS)); 573 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS));
574 } 574 }
575 575
576 void OmniboxEditModel::OpenMatch(AutocompleteMatch match, 576 void OmniboxEditModel::OpenMatch(AutocompleteMatch match,
577 WindowOpenDisposition disposition, 577 WindowOpenDisposition disposition,
578 const GURL& alternate_nav_url, 578 const GURL& alternate_nav_url,
579 const base::string16& pasted_text, 579 const base::string16& pasted_text,
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 740
741 is_keyword_hint_ = false; 741 is_keyword_hint_ = false;
742 keyword_mode_entry_method_ = entry_method; 742 keyword_mode_entry_method_ = entry_method;
743 user_text_ = MaybeStripKeyword(user_text_); 743 user_text_ = MaybeStripKeyword(user_text_);
744 744
745 user_text_ = MaybeStripKeyword(user_text_); 745 user_text_ = MaybeStripKeyword(user_text_);
746 746
747 if (PopupIsOpen()) 747 if (PopupIsOpen())
748 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); 748 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD);
749 else 749 else
750 StartAutocomplete(false, true, true); 750 StartAutocomplete(false, true);
751 751
752 // When entering keyword mode via tab, the new text to show is whatever the 752 // When entering keyword mode via tab, the new text to show is whatever the
753 // newly-selected match in the dropdown is. When entering via space, however, 753 // newly-selected match in the dropdown is. When entering via space, however,
754 // we should make sure to use the actual |user_text_| as the basis for the new 754 // we should make sure to use the actual |user_text_| as the basis for the new
755 // text. This ensures that if the user types "<keyword><space>" and the 755 // text. This ensures that if the user types "<keyword><space>" and the
756 // default match would have inline autocompleted a further string (e.g. 756 // default match would have inline autocompleted a further string (e.g.
757 // because there's a past multi-word search beginning with this keyword), the 757 // because there's a past multi-word search beginning with this keyword), the
758 // inline autocompletion doesn't get filled in as the keyword search query 758 // inline autocompletion doesn't get filled in as the keyword search query
759 // text. 759 // text.
760 // 760 //
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
1439 // Update state and notify view if the omnibox has focus and the caret 1439 // Update state and notify view if the omnibox has focus and the caret
1440 // visibility changed. 1440 // visibility changed.
1441 const bool was_caret_visible = is_caret_visible(); 1441 const bool was_caret_visible = is_caret_visible();
1442 focus_state_ = state; 1442 focus_state_ = state;
1443 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1443 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1444 is_caret_visible() != was_caret_visible) 1444 is_caret_visible() != was_caret_visible)
1445 view_->ApplyCaretVisibility(); 1445 view_->ApplyCaretVisibility();
1446 1446
1447 client_->OnFocusChanged(focus_state_, reason); 1447 client_->OnFocusChanged(focus_state_, reason);
1448 } 1448 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698