OLD | NEW |
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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 196 } |
197 | 197 |
198 const OmniboxEditModel::State OmniboxEditModel::GetStateForTabSwitch() { | 198 const OmniboxEditModel::State OmniboxEditModel::GetStateForTabSwitch() { |
199 // Like typing, switching tabs "accepts" the temporary text as the user | 199 // Like typing, switching tabs "accepts" the temporary text as the user |
200 // text, because it makes little sense to have temporary text when the | 200 // text, because it makes little sense to have temporary text when the |
201 // popup is closed. | 201 // popup is closed. |
202 if (user_input_in_progress_) { | 202 if (user_input_in_progress_) { |
203 // Weird edge case to match other browsers: if the edit is empty, revert to | 203 // Weird edge case to match other browsers: if the edit is empty, revert to |
204 // the permanent text (so the user can get it back easily) but select it (so | 204 // the permanent text (so the user can get it back easily) but select it (so |
205 // on switching back, typing will "just work"). | 205 // on switching back, typing will "just work"). |
206 const base::string16 user_text(UserTextFromDisplayText(view_->GetText())); | 206 const base::string16 display_text = view_->GetText(); |
207 if (user_text.empty()) { | 207 if (MaybePrependKeyword(display_text).empty()) { |
208 base::AutoReset<bool> tmp(&in_revert_, true); | 208 base::AutoReset<bool> tmp(&in_revert_, true); |
209 view_->RevertAll(); | 209 view_->RevertAll(); |
210 view_->SelectAll(true); | 210 view_->SelectAll(true); |
211 } else { | 211 } else { |
212 InternalSetUserText(user_text); | 212 InternalSetUserText(display_text); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress", | 216 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress", |
217 user_input_in_progress_); | 217 user_input_in_progress_); |
218 return State( | 218 return State( |
219 user_input_in_progress_, user_text_, view_->GetGrayTextAutocompletion(), | 219 user_input_in_progress_, user_text_, view_->GetGrayTextAutocompletion(), |
220 keyword_, is_keyword_hint_, | 220 keyword_, is_keyword_hint_, |
221 controller_->GetToolbarModel()->url_replacement_enabled(), | 221 controller_->GetToolbarModel()->url_replacement_enabled(), |
222 focus_state_, focus_source_, input_); | 222 focus_state_, focus_source_, input_); |
(...skipping 12 matching lines...) Expand all Loading... |
235 // Restore the autocomplete controller's input, or clear it if this is a new | 235 // Restore the autocomplete controller's input, or clear it if this is a new |
236 // tab. | 236 // tab. |
237 input_ = state ? state->autocomplete_input : AutocompleteInput(); | 237 input_ = state ? state->autocomplete_input : AutocompleteInput(); |
238 if (!state) | 238 if (!state) |
239 return; | 239 return; |
240 | 240 |
241 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); | 241 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); |
242 focus_source_ = state->focus_source; | 242 focus_source_ = state->focus_source; |
243 // Restore any user editing. | 243 // Restore any user editing. |
244 if (state->user_input_in_progress) { | 244 if (state->user_input_in_progress) { |
245 // NOTE: Be sure and set keyword-related state BEFORE invoking | 245 // NOTE: Be sure and set keyword-related state AFTER invoking |
246 // DisplayTextFromUserText(), as its result depends upon this state. | 246 // SetUserText(), as SetUserText() clears the keyword state. |
| 247 view_->SetUserText(state->user_text, false); |
247 keyword_ = state->keyword; | 248 keyword_ = state->keyword; |
248 is_keyword_hint_ = state->is_keyword_hint; | 249 is_keyword_hint_ = state->is_keyword_hint; |
249 view_->SetUserText(state->user_text, | |
250 DisplayTextFromUserText(state->user_text), false); | |
251 view_->SetGrayTextAutocompletion(state->gray_text); | 250 view_->SetGrayTextAutocompletion(state->gray_text); |
252 } | 251 } |
253 } | 252 } |
254 | 253 |
255 AutocompleteMatch OmniboxEditModel::CurrentMatch( | 254 AutocompleteMatch OmniboxEditModel::CurrentMatch( |
256 GURL* alternate_nav_url) const { | 255 GURL* alternate_nav_url) const { |
257 // If we have a valid match use it. Otherwise get one for the current text. | 256 // If we have a valid match use it. Otherwise get one for the current text. |
258 AutocompleteMatch match = omnibox_controller_->current_match(); | 257 AutocompleteMatch match = omnibox_controller_->current_match(); |
259 | 258 |
260 if (!match.destination_url.is_valid()) { | 259 if (!match.destination_url.is_valid()) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 return visibly_changed_permanent_text; | 300 return visibly_changed_permanent_text; |
302 } | 301 } |
303 | 302 |
304 GURL OmniboxEditModel::PermanentURL() { | 303 GURL OmniboxEditModel::PermanentURL() { |
305 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_), | 304 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_), |
306 std::string()); | 305 std::string()); |
307 } | 306 } |
308 | 307 |
309 void OmniboxEditModel::SetUserText(const base::string16& text) { | 308 void OmniboxEditModel::SetUserText(const base::string16& text) { |
310 SetInputInProgress(true); | 309 SetInputInProgress(true); |
| 310 keyword_.clear(); |
| 311 is_keyword_hint_ = false; |
311 InternalSetUserText(text); | 312 InternalSetUserText(text); |
312 omnibox_controller_->InvalidateCurrentMatch(); | 313 omnibox_controller_->InvalidateCurrentMatch(); |
313 paste_state_ = NONE; | 314 paste_state_ = NONE; |
314 has_temporary_text_ = false; | 315 has_temporary_text_ = false; |
315 } | 316 } |
316 | 317 |
317 bool OmniboxEditModel::CommitSuggestedText() { | 318 bool OmniboxEditModel::CommitSuggestedText() { |
318 const base::string16 suggestion = view_->GetGrayTextAutocompletion(); | 319 const base::string16 suggestion = view_->GetGrayTextAutocompletion(); |
319 if (suggestion.empty()) | 320 if (suggestion.empty()) |
320 return false; | 321 return false; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 has_focus() ? permanent_text_.length() : 0, | 474 has_focus() ? permanent_text_.length() : 0, |
474 false, true); | 475 false, true); |
475 client_->OnRevert(); | 476 client_->OnRevert(); |
476 } | 477 } |
477 | 478 |
478 void OmniboxEditModel::StartAutocomplete( | 479 void OmniboxEditModel::StartAutocomplete( |
479 bool has_selected_text, | 480 bool has_selected_text, |
480 bool prevent_inline_autocomplete, | 481 bool prevent_inline_autocomplete, |
481 bool entering_keyword_mode) { | 482 bool entering_keyword_mode) { |
482 size_t cursor_position; | 483 size_t cursor_position; |
| 484 const base::string16 input_text = MaybePrependKeyword(user_text_); |
483 if (inline_autocomplete_text_.empty()) { | 485 if (inline_autocomplete_text_.empty()) { |
484 // Cursor position is equivalent to the current selection's end. | 486 // Cursor position is equivalent to the current selection's end. |
485 size_t start; | 487 size_t start; |
486 view_->GetSelectionBounds(&start, &cursor_position); | 488 view_->GetSelectionBounds(&start, &cursor_position); |
487 // If we're in keyword mode, we're not displaying the full |user_text_|, so | 489 // For keyword searches, the text that AutocompleteInput expects is of the |
488 // the cursor position we got from the view has to be adjusted later by the | 490 // form "<keyword> <query>", where our query is |user_text_|. So if we're |
489 // length of the undisplayed text. If we're just entering keyword mode, | 491 // in keyword mode, we need to adjust the cursor position forward by the |
490 // though, we have to avoid making this adjustment, because we haven't | 492 // length of "<keyword> ". If we're just entering keyword mode, though, we |
491 // actually hidden any text yet, but the caller has already cleared | 493 // have to avoid making this adjustment, because we haven't actually updated |
492 // |is_keyword_hint_|, so DisplayTextFromUserText() will believe we are | 494 // |user_text_| yet, but the caller has already cleared |is_keyword_hint_|, |
493 // already in keyword mode, and will thus mis-adjust the cursor position. | 495 // so MaybePrependKeyword() will believe we are already in keyword mode, and |
494 if (!entering_keyword_mode) { | 496 // will thus mis-adjust the cursor position. |
495 cursor_position += | 497 if (!entering_keyword_mode) |
496 user_text_.length() - DisplayTextFromUserText(user_text_).length(); | 498 cursor_position += input_text.length() - user_text_.length(); |
497 } | |
498 } else { | 499 } else { |
499 // There are some cases where StartAutocomplete() may be called | 500 // There are some cases where StartAutocomplete() may be called |
500 // with non-empty |inline_autocomplete_text_|. In such cases, we cannot | 501 // with non-empty |inline_autocomplete_text_|. In such cases, we cannot |
501 // use the current selection, because it could result with the cursor | 502 // use the current selection, because it could result with the cursor |
502 // position past the last character from the user text. Instead, | 503 // position past the last character from the user text. Instead, |
503 // we assume that the cursor is simply at the end of input. | 504 // we assume that the cursor is simply at the end of input. |
504 // One example is when user presses Ctrl key while having a highlighted | 505 // One example is when user presses Ctrl key while having a highlighted |
505 // inline autocomplete text. | 506 // inline autocomplete text. |
506 // TODO: Rethink how we are going to handle this case to avoid | 507 // TODO: Rethink how we are going to handle this case to avoid |
507 // inconsistent behavior when user presses Ctrl key. | 508 // inconsistent behavior when user presses Ctrl key. |
508 // See http://crbug.com/165961 and http://crbug.com/165968 for more details. | 509 // See http://crbug.com/165961 and http://crbug.com/165968 for more details. |
509 cursor_position = user_text_.length(); | 510 cursor_position = input_text.length(); |
510 } | 511 } |
511 | 512 |
512 GURL current_url; | 513 GURL current_url; |
513 if (client_->CurrentPageExists()) | 514 if (client_->CurrentPageExists()) |
514 current_url = client_->GetURL(); | 515 current_url = client_->GetURL(); |
515 input_ = AutocompleteInput( | 516 input_ = AutocompleteInput( |
516 user_text_, cursor_position, std::string(), current_url, ClassifyPage(), | 517 input_text, cursor_position, std::string(), current_url, ClassifyPage(), |
517 prevent_inline_autocomplete || just_deleted_text_ || | 518 prevent_inline_autocomplete || just_deleted_text_ || |
518 (has_selected_text && inline_autocomplete_text_.empty()) || | 519 (has_selected_text && inline_autocomplete_text_.empty()) || |
519 (paste_state_ != NONE), | 520 (paste_state_ != NONE), |
520 is_keyword_selected(), | 521 is_keyword_selected(), |
521 is_keyword_selected() || allow_exact_keyword_match_, true, false, | 522 is_keyword_selected() || allow_exact_keyword_match_, true, false, |
522 client_->GetSchemeClassifier()); | 523 client_->GetSchemeClassifier()); |
523 | 524 |
524 omnibox_controller_->StartAutocomplete(input_); | 525 omnibox_controller_->StartAutocomplete(input_); |
525 } | 526 } |
526 | 527 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 autocomplete_controller()->history_url_provider()) { | 570 autocomplete_controller()->history_url_provider()) { |
570 // Generate a new AutocompleteInput, copying the latest one but using "com" | 571 // Generate a new AutocompleteInput, copying the latest one but using "com" |
571 // as the desired TLD. Then use this autocomplete input to generate a | 572 // as the desired TLD. Then use this autocomplete input to generate a |
572 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent | 573 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent |
573 // input instead of the currently visible text means we'll ignore any | 574 // input instead of the currently visible text means we'll ignore any |
574 // visible inline autocompletion: if a user types "foo" and is autocompleted | 575 // visible inline autocompletion: if a user types "foo" and is autocompleted |
575 // to "foodnetwork.com", ctrl-enter will navigate to "foo.com", not | 576 // to "foodnetwork.com", ctrl-enter will navigate to "foo.com", not |
576 // "foodnetwork.com". At the time of writing, this behavior matches | 577 // "foodnetwork.com". At the time of writing, this behavior matches |
577 // Internet Explorer, but not Firefox. | 578 // Internet Explorer, but not Firefox. |
578 input_ = AutocompleteInput( | 579 input_ = AutocompleteInput( |
579 has_temporary_text_ ? UserTextFromDisplayText(view_->GetText()) | 580 has_temporary_text_ ? view_->GetText() : input_.text(), |
580 : input_.text(), | |
581 input_.cursor_position(), "com", GURL(), | 581 input_.cursor_position(), "com", GURL(), |
582 input_.current_page_classification(), | 582 input_.current_page_classification(), |
583 input_.prevent_inline_autocomplete(), input_.prefer_keyword(), | 583 input_.prevent_inline_autocomplete(), input_.prefer_keyword(), |
584 input_.allow_exact_keyword_match(), input_.want_asynchronous_matches(), | 584 input_.allow_exact_keyword_match(), input_.want_asynchronous_matches(), |
585 input_.from_omnibox_focus(), client_->GetSchemeClassifier()); | 585 input_.from_omnibox_focus(), client_->GetSchemeClassifier()); |
586 AutocompleteMatch url_match( | 586 AutocompleteMatch url_match( |
587 autocomplete_controller()->history_url_provider()->SuggestExactInput( | 587 autocomplete_controller()->history_url_provider()->SuggestExactInput( |
588 input_, input_.canonicalized_url(), false)); | 588 input_, input_.canonicalized_url(), false)); |
589 | 589 |
590 if (url_match.destination_url.is_valid()) { | 590 if (url_match.destination_url.is_valid()) { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 if (bookmark_model && bookmark_model->IsBookmarked(match.destination_url)) | 782 if (bookmark_model && bookmark_model->IsBookmarked(match.destination_url)) |
783 client_->OnBookmarkLaunched(); | 783 client_->OnBookmarkLaunched(); |
784 } | 784 } |
785 | 785 |
786 bool OmniboxEditModel::AcceptKeyword(EnteredKeywordModeMethod entered_method) { | 786 bool OmniboxEditModel::AcceptKeyword(EnteredKeywordModeMethod entered_method) { |
787 DCHECK(is_keyword_hint_ && !keyword_.empty()); | 787 DCHECK(is_keyword_hint_ && !keyword_.empty()); |
788 | 788 |
789 autocomplete_controller()->Stop(false); | 789 autocomplete_controller()->Stop(false); |
790 is_keyword_hint_ = false; | 790 is_keyword_hint_ = false; |
791 | 791 |
| 792 user_text_ = MaybeStripKeyword(user_text_); |
| 793 |
792 if (popup_model() && popup_model()->IsOpen()) | 794 if (popup_model() && popup_model()->IsOpen()) |
793 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); | 795 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); |
794 else | 796 else |
795 StartAutocomplete(false, true, true); | 797 StartAutocomplete(false, true, true); |
796 | 798 |
797 // When entering keyword mode via tab, the new text to show is whatever the | 799 // When entering keyword mode via tab, the new text to show is whatever the |
798 // newly-selected match in the dropdown is. When entering via space, however, | 800 // newly-selected match in the dropdown is. When entering via space, however, |
799 // we should make sure to use the actual |user_text_| as the basis for the new | 801 // we should make sure to use the actual |user_text_| as the basis for the new |
800 // text. This ensures that if the user types "<keyword><space>" and the | 802 // text. This ensures that if the user types "<keyword><space>" and the |
801 // default match would have inline autocompleted a further string (e.g. | 803 // default match would have inline autocompleted a further string (e.g. |
(...skipping 11 matching lines...) Expand all Loading... |
813 // comments in ClearKeyword(). | 815 // comments in ClearKeyword(). |
814 if (entered_method == ENTERED_KEYWORD_MODE_VIA_TAB) { | 816 if (entered_method == ENTERED_KEYWORD_MODE_VIA_TAB) { |
815 // Ensure the current selection is saved before showing keyword mode | 817 // Ensure the current selection is saved before showing keyword mode |
816 // so that moving to another line and then reverting the text will restore | 818 // so that moving to another line and then reverting the text will restore |
817 // the current state properly. | 819 // the current state properly. |
818 bool save_original_selection = !has_temporary_text_; | 820 bool save_original_selection = !has_temporary_text_; |
819 has_temporary_text_ = true; | 821 has_temporary_text_ = true; |
820 const AutocompleteMatch& match = CurrentMatch(NULL); | 822 const AutocompleteMatch& match = CurrentMatch(NULL); |
821 original_url_ = match.destination_url; | 823 original_url_ = match.destination_url; |
822 view_->OnTemporaryTextMaybeChanged( | 824 view_->OnTemporaryTextMaybeChanged( |
823 DisplayTextFromUserText(match.fill_into_edit), | 825 MaybeStripKeyword(match.fill_into_edit), save_original_selection, |
824 save_original_selection, true); | 826 true); |
825 } else { | 827 } else { |
826 view_->OnTemporaryTextMaybeChanged(DisplayTextFromUserText(user_text_), | 828 view_->OnTemporaryTextMaybeChanged(user_text_, false, true); |
827 false, true); | |
828 } | 829 } |
829 | 830 |
830 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint")); | 831 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint")); |
831 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method, | 832 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method, |
832 ENTERED_KEYWORD_MODE_NUM_ITEMS); | 833 ENTERED_KEYWORD_MODE_NUM_ITEMS); |
833 | 834 |
834 return true; | 835 return true; |
835 } | 836 } |
836 | 837 |
837 void OmniboxEditModel::AcceptTemporaryTextAsUserText() { | 838 void OmniboxEditModel::AcceptTemporaryTextAsUserText() { |
838 InternalSetUserText(UserTextFromDisplayText(view_->GetText())); | 839 InternalSetUserText(view_->GetText()); |
839 has_temporary_text_ = false; | 840 has_temporary_text_ = false; |
840 | 841 |
841 if (user_input_in_progress_ || !in_revert_) | 842 if (user_input_in_progress_ || !in_revert_) |
842 client_->OnInputStateChanged(); | 843 client_->OnInputStateChanged(); |
843 } | 844 } |
844 | 845 |
845 void OmniboxEditModel::ClearKeyword() { | 846 void OmniboxEditModel::ClearKeyword() { |
846 autocomplete_controller()->Stop(false); | 847 autocomplete_controller()->Stop(false); |
847 | 848 |
848 // While we're always in keyword mode upon reaching here, sometimes we've just | 849 // While we're always in keyword mode upon reaching here, sometimes we've just |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 GURL* destination_for_temporary_text_change, | 1064 GURL* destination_for_temporary_text_change, |
1064 const base::string16& keyword, | 1065 const base::string16& keyword, |
1065 bool is_keyword_hint) { | 1066 bool is_keyword_hint) { |
1066 // The popup changed its data, the match in the controller is no longer valid. | 1067 // The popup changed its data, the match in the controller is no longer valid. |
1067 omnibox_controller_->InvalidateCurrentMatch(); | 1068 omnibox_controller_->InvalidateCurrentMatch(); |
1068 | 1069 |
1069 // Update keyword/hint-related local state. | 1070 // Update keyword/hint-related local state. |
1070 bool keyword_state_changed = (keyword_ != keyword) || | 1071 bool keyword_state_changed = (keyword_ != keyword) || |
1071 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()); | 1072 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()); |
1072 if (keyword_state_changed) { | 1073 if (keyword_state_changed) { |
| 1074 bool keyword_was_selected = is_keyword_selected(); |
1073 keyword_ = keyword; | 1075 keyword_ = keyword; |
1074 is_keyword_hint_ = is_keyword_hint; | 1076 is_keyword_hint_ = is_keyword_hint; |
| 1077 if (!keyword_was_selected && is_keyword_selected()) { |
| 1078 // We just entered keyword mode, so remove the keyword from the input. |
| 1079 user_text_ = MaybeStripKeyword(user_text_); |
| 1080 } |
1075 | 1081 |
1076 // |is_keyword_hint_| should always be false if |keyword_| is empty. | 1082 // |is_keyword_hint_| should always be false if |keyword_| is empty. |
1077 DCHECK(!keyword_.empty() || !is_keyword_hint_); | 1083 DCHECK(!keyword_.empty() || !is_keyword_hint_); |
1078 } | 1084 } |
1079 | 1085 |
1080 // Handle changes to temporary text. | 1086 // Handle changes to temporary text. |
1081 if (destination_for_temporary_text_change != NULL) { | 1087 if (destination_for_temporary_text_change != NULL) { |
1082 const bool save_original_selection = !has_temporary_text_; | 1088 const bool save_original_selection = !has_temporary_text_; |
1083 if (save_original_selection) { | 1089 if (save_original_selection) { |
1084 // Save the original selection and URL so it can be reverted later. | 1090 // Save the original selection and URL so it can be reverted later. |
1085 has_temporary_text_ = true; | 1091 has_temporary_text_ = true; |
1086 original_url_ = *destination_for_temporary_text_change; | 1092 original_url_ = *destination_for_temporary_text_change; |
1087 inline_autocomplete_text_.clear(); | 1093 inline_autocomplete_text_.clear(); |
1088 view_->OnInlineAutocompleteTextCleared(); | 1094 view_->OnInlineAutocompleteTextCleared(); |
1089 } | 1095 } |
1090 if (control_key_state_ == DOWN_WITHOUT_CHANGE) { | 1096 if (control_key_state_ == DOWN_WITHOUT_CHANGE) { |
1091 // Arrowing around the popup cancels control-enter. | 1097 // Arrowing around the popup cancels control-enter. |
1092 control_key_state_ = DOWN_WITH_CHANGE; | 1098 control_key_state_ = DOWN_WITH_CHANGE; |
1093 // Now things are a bit screwy: the desired_tld has changed, but if we | 1099 // Now things are a bit screwy: the desired_tld has changed, but if we |
1094 // update the popup, the new order of entries won't match the old, so the | 1100 // update the popup, the new order of entries won't match the old, so the |
1095 // user's selection gets screwy; and if we don't update the popup, and the | 1101 // user's selection gets screwy; and if we don't update the popup, and the |
1096 // user reverts, then the selected item will be as if control is still | 1102 // user reverts, then the selected item will be as if control is still |
1097 // pressed, even though maybe it isn't any more. There is no obvious | 1103 // pressed, even though maybe it isn't any more. There is no obvious |
1098 // right answer here :( | 1104 // right answer here :( |
1099 } | 1105 } |
1100 view_->OnTemporaryTextMaybeChanged(DisplayTextFromUserText(text), | 1106 view_->OnTemporaryTextMaybeChanged(MaybeStripKeyword(text), |
1101 save_original_selection, true); | 1107 save_original_selection, true); |
1102 return; | 1108 return; |
1103 } | 1109 } |
1104 | 1110 |
1105 bool call_controller_onchanged = true; | 1111 bool call_controller_onchanged = true; |
1106 inline_autocomplete_text_ = text; | 1112 inline_autocomplete_text_ = text; |
1107 if (inline_autocomplete_text_.empty()) | 1113 if (inline_autocomplete_text_.empty()) |
1108 view_->OnInlineAutocompleteTextCleared(); | 1114 view_->OnInlineAutocompleteTextCleared(); |
1109 | 1115 |
1110 const base::string16& user_text = | 1116 const base::string16& user_text = |
1111 user_input_in_progress_ ? user_text_ : permanent_text_; | 1117 user_input_in_progress_ ? user_text_ : permanent_text_; |
1112 if (keyword_state_changed && is_keyword_selected()) { | 1118 if (keyword_state_changed && is_keyword_selected()) { |
1113 // If we reach here, the user most likely entered keyword mode by inserting | 1119 // If we reach here, the user most likely entered keyword mode by inserting |
1114 // a space between a keyword name and a search string (as pressing space or | 1120 // a space between a keyword name and a search string (as pressing space or |
1115 // tab after the keyword name alone would have been be handled in | 1121 // tab after the keyword name alone would have been be handled in |
1116 // MaybeAcceptKeywordBySpace() by calling AcceptKeyword(), which won't reach | 1122 // MaybeAcceptKeywordBySpace() by calling AcceptKeyword(), which won't reach |
1117 // here). In this case, we don't want to call | 1123 // here). In this case, we don't want to call |
1118 // OnInlineAutocompleteTextMaybeChanged() as normal, because that will | 1124 // OnInlineAutocompleteTextMaybeChanged() as normal, because that will |
1119 // correctly change the text (to the search string alone) but move the caret | 1125 // correctly change the text (to the search string alone) but move the caret |
1120 // to the end of the string; instead we want the caret at the start of the | 1126 // to the end of the string; instead we want the caret at the start of the |
1121 // search string since that's where it was in the original input. So we set | 1127 // search string since that's where it was in the original input. So we set |
1122 // the text and caret position directly. | 1128 // the text and caret position directly. |
1123 // | 1129 // |
1124 // It may also be possible to reach here if we're reverting from having | 1130 // It may also be possible to reach here if we're reverting from having |
1125 // temporary text back to a default match that's a keyword search, but in | 1131 // temporary text back to a default match that's a keyword search, but in |
1126 // that case the RevertTemporaryText() call below will reset the caret or | 1132 // that case the RevertTemporaryText() call below will reset the caret or |
1127 // selection correctly so the caret positioning we do here won't matter. | 1133 // selection correctly so the caret positioning we do here won't matter. |
1128 view_->SetWindowTextAndCaretPos(DisplayTextFromUserText(user_text), 0, | 1134 view_->SetWindowTextAndCaretPos(user_text, 0, false, false); |
1129 false, false); | |
1130 } else if (view_->OnInlineAutocompleteTextMaybeChanged( | 1135 } else if (view_->OnInlineAutocompleteTextMaybeChanged( |
1131 DisplayTextFromUserText(user_text + inline_autocomplete_text_), | 1136 user_text + inline_autocomplete_text_, user_text.length())) { |
1132 DisplayTextFromUserText(user_text).length())) { | |
1133 call_controller_onchanged = false; | 1137 call_controller_onchanged = false; |
1134 } | 1138 } |
1135 | 1139 |
1136 // If |has_temporary_text_| is true, then we previously had a manual selection | 1140 // If |has_temporary_text_| is true, then we previously had a manual selection |
1137 // but now don't (or |destination_for_temporary_text_change| would have been | 1141 // but now don't (or |destination_for_temporary_text_change| would have been |
1138 // non-NULL). This can happen when deleting the selected item in the popup. | 1142 // non-NULL). This can happen when deleting the selected item in the popup. |
1139 // In this case, we've already reverted the popup to the default match, so we | 1143 // In this case, we've already reverted the popup to the default match, so we |
1140 // need to revert ourselves as well. | 1144 // need to revert ourselves as well. |
1141 if (has_temporary_text_) { | 1145 if (has_temporary_text_) { |
1142 RevertTemporaryText(false); | 1146 RevertTemporaryText(false); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 if ((text_differs || selection_differs) && | 1196 if ((text_differs || selection_differs) && |
1193 (control_key_state_ == DOWN_WITHOUT_CHANGE)) | 1197 (control_key_state_ == DOWN_WITHOUT_CHANGE)) |
1194 control_key_state_ = DOWN_WITH_CHANGE; | 1198 control_key_state_ = DOWN_WITH_CHANGE; |
1195 | 1199 |
1196 if (!user_text_changed) | 1200 if (!user_text_changed) |
1197 return false; | 1201 return false; |
1198 | 1202 |
1199 // If the user text has not changed, we do not want to change the model's | 1203 // If the user text has not changed, we do not want to change the model's |
1200 // state associated with the text. Otherwise, we can get surprising behavior | 1204 // state associated with the text. Otherwise, we can get surprising behavior |
1201 // where the autocompleted text unexpectedly reappears, e.g. crbug.com/55983 | 1205 // where the autocompleted text unexpectedly reappears, e.g. crbug.com/55983 |
1202 InternalSetUserText(UserTextFromDisplayText(new_text)); | 1206 InternalSetUserText(new_text); |
1203 has_temporary_text_ = false; | 1207 has_temporary_text_ = false; |
1204 | 1208 |
1205 // Track when the user has deleted text so we won't allow inline | 1209 // Track when the user has deleted text so we won't allow inline |
1206 // autocomplete. | 1210 // autocomplete. |
1207 just_deleted_text_ = just_deleted_text; | 1211 just_deleted_text_ = just_deleted_text; |
1208 | 1212 |
1209 if (user_input_in_progress_ && user_text_.empty()) { | 1213 if (user_input_in_progress_ && user_text_.empty()) { |
1210 // Log cases where the user started editing and then subsequently cleared | 1214 // Log cases where the user started editing and then subsequently cleared |
1211 // all the text. Note that this explicitly doesn't catch cases like | 1215 // all the text. Note that this explicitly doesn't catch cases like |
1212 // "hit ctrl-l to select whole edit contents, then hit backspace", because | 1216 // "hit ctrl-l to select whole edit contents, then hit backspace", because |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 user_text_ = text; | 1291 user_text_ = text; |
1288 just_deleted_text_ = false; | 1292 just_deleted_text_ = false; |
1289 inline_autocomplete_text_.clear(); | 1293 inline_autocomplete_text_.clear(); |
1290 view_->OnInlineAutocompleteTextCleared(); | 1294 view_->OnInlineAutocompleteTextCleared(); |
1291 } | 1295 } |
1292 | 1296 |
1293 void OmniboxEditModel::ClearPopupKeywordMode() const { | 1297 void OmniboxEditModel::ClearPopupKeywordMode() const { |
1294 omnibox_controller_->ClearPopupKeywordMode(); | 1298 omnibox_controller_->ClearPopupKeywordMode(); |
1295 } | 1299 } |
1296 | 1300 |
1297 base::string16 OmniboxEditModel::DisplayTextFromUserText( | 1301 base::string16 OmniboxEditModel::MaybeStripKeyword( |
1298 const base::string16& text) const { | 1302 const base::string16& text) const { |
1299 return is_keyword_selected() ? | 1303 return is_keyword_selected() ? |
1300 KeywordProvider::SplitReplacementStringFromInput(text, false) : text; | 1304 KeywordProvider::SplitReplacementStringFromInput(text, false) : text; |
1301 } | 1305 } |
1302 | 1306 |
1303 base::string16 OmniboxEditModel::UserTextFromDisplayText( | 1307 base::string16 OmniboxEditModel::MaybePrependKeyword( |
1304 const base::string16& text) const { | 1308 const base::string16& text) const { |
1305 return is_keyword_selected() ? (keyword_ + base::char16(' ') + text) : text; | 1309 return is_keyword_selected() ? (keyword_ + base::char16(' ') + text) : text; |
1306 } | 1310 } |
1307 | 1311 |
1308 void OmniboxEditModel::GetInfoForCurrentText(AutocompleteMatch* match, | 1312 void OmniboxEditModel::GetInfoForCurrentText(AutocompleteMatch* match, |
1309 GURL* alternate_nav_url) const { | 1313 GURL* alternate_nav_url) const { |
1310 DCHECK(match != NULL); | 1314 DCHECK(match != NULL); |
1311 | 1315 |
1312 if (controller_->GetToolbarModel()->WouldPerformSearchTermReplacement( | 1316 if (controller_->GetToolbarModel()->WouldPerformSearchTermReplacement( |
1313 false)) { | 1317 false)) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1345 result().match_at(popup_model()->selected_line()); | 1349 result().match_at(popup_model()->selected_line()); |
1346 *match = | 1350 *match = |
1347 (popup_model()->selected_line_state() == OmniboxPopupModel::KEYWORD) ? | 1351 (popup_model()->selected_line_state() == OmniboxPopupModel::KEYWORD) ? |
1348 *selected_match.associated_keyword : selected_match; | 1352 *selected_match.associated_keyword : selected_match; |
1349 } | 1353 } |
1350 if (alternate_nav_url && | 1354 if (alternate_nav_url && |
1351 (!popup_model() || popup_model()->manually_selected_match().empty())) | 1355 (!popup_model() || popup_model()->manually_selected_match().empty())) |
1352 *alternate_nav_url = result().alternate_nav_url(); | 1356 *alternate_nav_url = result().alternate_nav_url(); |
1353 } else { | 1357 } else { |
1354 client_->GetAutocompleteClassifier()->Classify( | 1358 client_->GetAutocompleteClassifier()->Classify( |
1355 UserTextFromDisplayText(view_->GetText()), is_keyword_selected(), true, | 1359 MaybePrependKeyword(view_->GetText()), is_keyword_selected(), true, |
1356 ClassifyPage(), match, alternate_nav_url); | 1360 ClassifyPage(), match, alternate_nav_url); |
1357 } | 1361 } |
1358 } | 1362 } |
1359 | 1363 |
1360 void OmniboxEditModel::RevertTemporaryText(bool revert_popup) { | 1364 void OmniboxEditModel::RevertTemporaryText(bool revert_popup) { |
1361 // The user typed something, then selected a different item. Restore the | 1365 // The user typed something, then selected a different item. Restore the |
1362 // text they typed and change back to the default item. | 1366 // text they typed and change back to the default item. |
1363 // NOTE: This purposefully does not reset paste_state_. | 1367 // NOTE: This purposefully does not reset paste_state_. |
1364 just_deleted_text_ = false; | 1368 just_deleted_text_ = false; |
1365 has_temporary_text_ = false; | 1369 has_temporary_text_ = false; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1463 // Update state and notify view if the omnibox has focus and the caret | 1467 // Update state and notify view if the omnibox has focus and the caret |
1464 // visibility changed. | 1468 // visibility changed. |
1465 const bool was_caret_visible = is_caret_visible(); | 1469 const bool was_caret_visible = is_caret_visible(); |
1466 focus_state_ = state; | 1470 focus_state_ = state; |
1467 if (focus_state_ != OMNIBOX_FOCUS_NONE && | 1471 if (focus_state_ != OMNIBOX_FOCUS_NONE && |
1468 is_caret_visible() != was_caret_visible) | 1472 is_caret_visible() != was_caret_visible) |
1469 view_->ApplyCaretVisibility(); | 1473 view_->ApplyCaretVisibility(); |
1470 | 1474 |
1471 client_->OnFocusChanged(focus_state_, reason); | 1475 client_->OnFocusChanged(focus_state_, reason); |
1472 } | 1476 } |
OLD | NEW |