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

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

Issue 1855423003: Interpret '?' and Ctrl-K or Ctrl-E as putting omnibox in keyword search mode for Default Search Pro… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compilation on Mac and removed forced query unit tests Created 4 years, 8 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
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 const char kOmniboxUserTextClearedHistogram[] = "Omnibox.UserTextCleared"; 64 const char kOmniboxUserTextClearedHistogram[] = "Omnibox.UserTextCleared";
65 65
66 enum UserTextClearedType { 66 enum UserTextClearedType {
67 OMNIBOX_USER_TEXT_CLEARED_BY_EDITING = 0, 67 OMNIBOX_USER_TEXT_CLEARED_BY_EDITING = 0,
68 OMNIBOX_USER_TEXT_CLEARED_WITH_ESCAPE = 1, 68 OMNIBOX_USER_TEXT_CLEARED_WITH_ESCAPE = 1,
69 OMNIBOX_USER_TEXT_CLEARED_NUM_OF_ITEMS, 69 OMNIBOX_USER_TEXT_CLEARED_NUM_OF_ITEMS,
70 }; 70 };
71 71
72 // Histogram name which counts the number of times the user enters 72 // Histogram name which counts the number of times the user enters
73 // keyword hint mode and via what method. The possible values are listed 73 // keyword hint mode and via what method. The possible values are listed
74 // in the EnteredKeywordModeMethod enum which is defined in the .h file. 74 // in the KeywordModeEntryMethod enum which is defined in the .h file.
75 const char kEnteredKeywordModeHistogram[] = "Omnibox.EnteredKeywordMode"; 75 const char kEnteredKeywordModeHistogram[] = "Omnibox.EnteredKeywordMode";
76 76
77 // Histogram name which counts the number of milliseconds a user takes 77 // Histogram name which counts the number of milliseconds a user takes
78 // between focusing and editing the omnibox. 78 // between focusing and editing the omnibox.
79 const char kFocusToEditTimeHistogram[] = "Omnibox.FocusToEditTime"; 79 const char kFocusToEditTimeHistogram[] = "Omnibox.FocusToEditTime";
80 80
81 // Histogram name which counts the number of milliseconds a user takes 81 // Histogram name which counts the number of milliseconds a user takes
82 // between focusing and opening an omnibox match. 82 // between focusing and opening an omnibox match.
83 const char kFocusToOpenTimeHistogram[] = "Omnibox.FocusToOpenTimeAnyPopupState"; 83 const char kFocusToOpenTimeHistogram[] = "Omnibox.FocusToOpenTimeAnyPopupState";
84 84
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } // namespace 142 } // namespace
143 143
144 144
145 // OmniboxEditModel::State ---------------------------------------------------- 145 // OmniboxEditModel::State ----------------------------------------------------
146 146
147 OmniboxEditModel::State::State(bool user_input_in_progress, 147 OmniboxEditModel::State::State(bool user_input_in_progress,
148 const base::string16& user_text, 148 const base::string16& user_text,
149 const base::string16& gray_text, 149 const base::string16& gray_text,
150 const base::string16& keyword, 150 const base::string16& keyword,
151 bool is_keyword_hint, 151 bool is_keyword_hint,
152 KeywordModeEntryMethod keyword_mode_entry_method,
152 bool url_replacement_enabled, 153 bool url_replacement_enabled,
153 OmniboxFocusState focus_state, 154 OmniboxFocusState focus_state,
154 FocusSource focus_source, 155 FocusSource focus_source,
155 const AutocompleteInput& autocomplete_input) 156 const AutocompleteInput& autocomplete_input)
156 : user_input_in_progress(user_input_in_progress), 157 : user_input_in_progress(user_input_in_progress),
157 user_text(user_text), 158 user_text(user_text),
158 gray_text(gray_text), 159 gray_text(gray_text),
159 keyword(keyword), 160 keyword(keyword),
160 is_keyword_hint(is_keyword_hint), 161 is_keyword_hint(is_keyword_hint),
162 keyword_mode_entry_method(keyword_mode_entry_method),
161 url_replacement_enabled(url_replacement_enabled), 163 url_replacement_enabled(url_replacement_enabled),
162 focus_state(focus_state), 164 focus_state(focus_state),
163 focus_source(focus_source), 165 focus_source(focus_source),
164 autocomplete_input(autocomplete_input) { 166 autocomplete_input(autocomplete_input) {
165 } 167 }
166 168
167 OmniboxEditModel::State::State(const State& other) = default; 169 OmniboxEditModel::State::State(const State& other) = default;
168 170
169 OmniboxEditModel::State::~State() { 171 OmniboxEditModel::State::~State() {
170 } 172 }
171 173
172 174
173 // OmniboxEditModel ----------------------------------------------------------- 175 // OmniboxEditModel -----------------------------------------------------------
174 176
175 OmniboxEditModel::OmniboxEditModel(OmniboxView* view, 177 OmniboxEditModel::OmniboxEditModel(OmniboxView* view,
176 OmniboxEditController* controller, 178 OmniboxEditController* controller,
177 scoped_ptr<OmniboxClient> client) 179 scoped_ptr<OmniboxClient> client)
178 : client_(std::move(client)), 180 : client_(std::move(client)),
179 view_(view), 181 view_(view),
180 controller_(controller), 182 controller_(controller),
181 focus_state_(OMNIBOX_FOCUS_NONE), 183 focus_state_(OMNIBOX_FOCUS_NONE),
182 focus_source_(INVALID), 184 focus_source_(INVALID),
183 user_input_in_progress_(false), 185 user_input_in_progress_(false),
184 user_input_since_focus_(true), 186 user_input_since_focus_(true),
185 just_deleted_text_(false), 187 just_deleted_text_(false),
188 clearing_keyword_(false),
186 has_temporary_text_(false), 189 has_temporary_text_(false),
187 paste_state_(NONE), 190 paste_state_(NONE),
188 control_key_state_(UP), 191 control_key_state_(UP),
189 is_keyword_hint_(false), 192 is_keyword_hint_(false),
190 in_revert_(false), 193 in_revert_(false),
191 allow_exact_keyword_match_(false) { 194 allow_exact_keyword_match_(false) {
192 omnibox_controller_.reset(new OmniboxController(this, client_.get())); 195 omnibox_controller_.reset(new OmniboxController(this, client_.get()));
193 } 196 }
194 197
195 OmniboxEditModel::~OmniboxEditModel() { 198 OmniboxEditModel::~OmniboxEditModel() {
(...skipping 14 matching lines...) Expand all
210 view_->SelectAll(true); 213 view_->SelectAll(true);
211 } else { 214 } else {
212 InternalSetUserText(user_text); 215 InternalSetUserText(user_text);
213 } 216 }
214 } 217 }
215 218
216 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress", 219 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress",
217 user_input_in_progress_); 220 user_input_in_progress_);
218 return State( 221 return State(
219 user_input_in_progress_, user_text_, view_->GetGrayTextAutocompletion(), 222 user_input_in_progress_, user_text_, view_->GetGrayTextAutocompletion(),
220 keyword_, is_keyword_hint_, 223 keyword_, is_keyword_hint_, keyword_mode_entry_method_,
221 controller_->GetToolbarModel()->url_replacement_enabled(), 224 controller_->GetToolbarModel()->url_replacement_enabled(),
222 focus_state_, focus_source_, input_); 225 focus_state_, focus_source_, input_);
223 } 226 }
224 227
225 void OmniboxEditModel::RestoreState(const State* state) { 228 void OmniboxEditModel::RestoreState(const State* state) {
226 // We need to update the permanent text correctly and revert the view 229 // We need to update the permanent text correctly and revert the view
227 // regardless of whether there is saved state. 230 // regardless of whether there is saved state.
228 bool url_replacement_enabled = !state || state->url_replacement_enabled; 231 bool url_replacement_enabled = !state || state->url_replacement_enabled;
229 controller_->GetToolbarModel()->set_url_replacement_enabled( 232 controller_->GetToolbarModel()->set_url_replacement_enabled(
230 url_replacement_enabled); 233 url_replacement_enabled);
231 permanent_text_ = controller_->GetToolbarModel()->GetText(); 234 permanent_text_ = controller_->GetToolbarModel()->GetText();
232 // Don't muck with the search term replacement state, as we've just set it 235 // Don't muck with the search term replacement state, as we've just set it
233 // correctly. 236 // correctly.
234 view_->RevertWithoutResettingSearchTermReplacement(); 237 view_->RevertWithoutResettingSearchTermReplacement();
235 // Restore the autocomplete controller's input, or clear it if this is a new 238 // Restore the autocomplete controller's input, or clear it if this is a new
236 // tab. 239 // tab.
237 input_ = state ? state->autocomplete_input : AutocompleteInput(); 240 input_ = state ? state->autocomplete_input : AutocompleteInput();
238 if (!state) 241 if (!state)
239 return; 242 return;
240 243
241 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); 244 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH);
242 focus_source_ = state->focus_source; 245 focus_source_ = state->focus_source;
243 // Restore any user editing. 246 // Restore any user editing.
244 if (state->user_input_in_progress) { 247 if (state->user_input_in_progress) {
245 // NOTE: Be sure and set keyword-related state BEFORE invoking 248 // NOTE: Be sure and set keyword-related state BEFORE invoking
246 // DisplayTextFromUserText(), as its result depends upon this state. 249 // DisplayTextFromUserText(), as its result depends upon this state.
247 keyword_ = state->keyword; 250 keyword_ = state->keyword;
248 is_keyword_hint_ = state->is_keyword_hint; 251 is_keyword_hint_ = state->is_keyword_hint;
252 keyword_mode_entry_method_ = state->keyword_mode_entry_method;
249 view_->SetUserText(state->user_text, 253 view_->SetUserText(state->user_text,
250 DisplayTextFromUserText(state->user_text), false); 254 DisplayTextFromUserText(state->user_text), false);
251 view_->SetGrayTextAutocompletion(state->gray_text); 255 view_->SetGrayTextAutocompletion(state->gray_text);
252 } 256 }
253 } 257 }
254 258
255 AutocompleteMatch OmniboxEditModel::CurrentMatch( 259 AutocompleteMatch OmniboxEditModel::CurrentMatch(
256 GURL* alternate_nav_url) const { 260 GURL* alternate_nav_url) const {
257 // If we have a valid match use it. Otherwise get one for the current text. 261 // If we have a valid match use it. Otherwise get one for the current text.
258 AutocompleteMatch match = omnibox_controller_->current_match(); 262 AutocompleteMatch match = omnibox_controller_->current_match();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 325
322 const base::string16 final_text = view_->GetText() + suggestion; 326 const base::string16 final_text = view_->GetText() + suggestion;
323 view_->OnBeforePossibleChange(); 327 view_->OnBeforePossibleChange();
324 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false, 328 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false,
325 false); 329 false);
326 view_->OnAfterPossibleChange(true); 330 view_->OnAfterPossibleChange(true);
327 return true; 331 return true;
328 } 332 }
329 333
330 void OmniboxEditModel::OnChanged() { 334 void OmniboxEditModel::OnChanged() {
335 // If the user input a "?", put them into keyword mode.
336 if (!clearing_keyword_ && !just_deleted_text_ &&
337 user_text_ == base::ASCIIToUTF16("?")) {
338 user_text_ = base::string16();
339 EnterKeywordModeForDefaultSearchProvider(
340 METHOD_QUESTION_MARK);
341 }
342
331 // Hide any suggestions we might be showing. 343 // Hide any suggestions we might be showing.
332 view_->SetGrayTextAutocompletion(base::string16()); 344 view_->SetGrayTextAutocompletion(base::string16());
333 345
334 // Don't call CurrentMatch() when there's no editing, as in this case we'll 346 // Don't call CurrentMatch() when there's no editing, as in this case we'll
335 // never actually use it. This avoids running the autocomplete providers (and 347 // never actually use it. This avoids running the autocomplete providers (and
336 // any systems they then spin up) during startup. 348 // any systems they then spin up) during startup.
337 const AutocompleteMatch& current_match = user_input_in_progress_ ? 349 const AutocompleteMatch& current_match = user_input_in_progress_ ?
338 CurrentMatch(NULL) : AutocompleteMatch(); 350 CurrentMatch(NULL) : AutocompleteMatch();
339 351
340 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_, 352 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_,
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 match.transition = ui::PAGE_TRANSITION_LINK; 630 match.transition = ui::PAGE_TRANSITION_LINK;
619 } 631 }
620 632
621 client_->OnInputAccepted(match); 633 client_->OnInputAccepted(match);
622 634
623 DCHECK(popup_model()); 635 DCHECK(popup_model());
624 view_->OpenMatch(match, disposition, alternate_nav_url, base::string16(), 636 view_->OpenMatch(match, disposition, alternate_nav_url, base::string16(),
625 popup_model()->selected_line()); 637 popup_model()->selected_line());
626 } 638 }
627 639
640 void OmniboxEditModel::EnterKeywordModeForDefaultSearchProvider(
641 KeywordModeEntryMethod keyword_mode_entry_method) {
642 autocomplete_controller()->Stop(false);
643
644 base::string16 query = DisplayTextFromUserText(user_text_);
645 keyword_ = client_->GetTemplateURLService()->
646 GetDefaultSearchProvider()->keyword();
647 is_keyword_hint_ = false;
648 keyword_mode_entry_method_ = keyword_mode_entry_method;
649 InternalSetUserText(keyword_ + base::ASCIIToUTF16(" ") + query);
650
651 view_->OnTemporaryTextMaybeChanged(user_text_, false, true);
Peter Kasting 2016/04/15 20:32:47 Calling InternalSetUserText() + OnTemporaryTextMay
Tom (Use chromium acct) 2016/04/28 17:40:30 Done.
652 StartAutocomplete(false, false, true);
653
654 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram,
655 keyword_mode_entry_method,
656 KEYWORD_MODE_ENTRY_METHOD_NUM_ITEMS);
657 }
658
628 void OmniboxEditModel::OpenMatch(AutocompleteMatch match, 659 void OmniboxEditModel::OpenMatch(AutocompleteMatch match,
629 WindowOpenDisposition disposition, 660 WindowOpenDisposition disposition,
630 const GURL& alternate_nav_url, 661 const GURL& alternate_nav_url,
631 const base::string16& pasted_text, 662 const base::string16& pasted_text,
632 size_t index) { 663 size_t index) {
633 const base::TimeTicks& now(base::TimeTicks::Now()); 664 const base::TimeTicks& now(base::TimeTicks::Now());
634 base::TimeDelta elapsed_time_since_user_first_modified_omnibox( 665 base::TimeDelta elapsed_time_since_user_first_modified_omnibox(
635 now - time_user_first_modified_omnibox_); 666 now - time_user_first_modified_omnibox_);
636 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime( 667 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime(
637 elapsed_time_since_user_first_modified_omnibox, &match); 668 elapsed_time_since_user_first_modified_omnibox, &match);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 match.transition | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); 807 match.transition | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
777 if (observer && observer->HasSeenPendingLoad()) 808 if (observer && observer->HasSeenPendingLoad())
778 ignore_result(observer.release()); // The observer will delete itself. 809 ignore_result(observer.release()); // The observer will delete itself.
779 } 810 }
780 811
781 BookmarkModel* bookmark_model = client_->GetBookmarkModel(); 812 BookmarkModel* bookmark_model = client_->GetBookmarkModel();
782 if (bookmark_model && bookmark_model->IsBookmarked(match.destination_url)) 813 if (bookmark_model && bookmark_model->IsBookmarked(match.destination_url))
783 client_->OnBookmarkLaunched(); 814 client_->OnBookmarkLaunched();
784 } 815 }
785 816
786 bool OmniboxEditModel::AcceptKeyword(EnteredKeywordModeMethod entered_method) { 817 bool OmniboxEditModel::AcceptKeyword(
818 KeywordModeEntryMethod keyword_mode_entry_method) {
787 DCHECK(is_keyword_hint_ && !keyword_.empty()); 819 DCHECK(is_keyword_hint_ && !keyword_.empty());
788 820
789 autocomplete_controller()->Stop(false); 821 autocomplete_controller()->Stop(false);
790 is_keyword_hint_ = false; 822 is_keyword_hint_ = false;
823 keyword_mode_entry_method_ = keyword_mode_entry_method;
791 824
792 if (popup_model() && popup_model()->IsOpen()) 825 if (popup_model() && popup_model()->IsOpen())
793 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); 826 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD);
794 else 827 else
795 StartAutocomplete(false, true, true); 828 StartAutocomplete(false, true, true);
796 829
797 // When entering keyword mode via tab, the new text to show is whatever the 830 // 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, 831 // 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 832 // 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 833 // text. This ensures that if the user types "<keyword><space>" and the
801 // default match would have inline autocompleted a further string (e.g. 834 // default match would have inline autocompleted a further string (e.g.
802 // because there's a past multi-word search beginning with this keyword), the 835 // because there's a past multi-word search beginning with this keyword), the
803 // inline autocompletion doesn't get filled in as the keyword search query 836 // inline autocompletion doesn't get filled in as the keyword search query
804 // text. 837 // text.
805 // 838 //
806 // We also treat tabbing into keyword mode like tabbing through the popup in 839 // We also treat tabbing into keyword mode like tabbing through the popup in
807 // that we set |has_temporary_text_|, whereas pressing space is treated like 840 // that we set |has_temporary_text_|, whereas pressing space is treated like
808 // a new keystroke that changes the current match instead of overlaying it 841 // a new keystroke that changes the current match instead of overlaying it
809 // with a temporary one. This is important because rerunning autocomplete 842 // with a temporary one. This is important because rerunning autocomplete
810 // after the user pressed space, which will have happened just before reaching 843 // after the user pressed space, which will have happened just before reaching
811 // here, may have generated a new match, which the user won't actually see and 844 // here, may have generated a new match, which the user won't actually see and
812 // which we don't want to switch back to when existing keyword mode; see 845 // which we don't want to switch back to when existing keyword mode; see
813 // comments in ClearKeyword(). 846 // comments in ClearKeyword().
814 if (entered_method == ENTERED_KEYWORD_MODE_VIA_TAB) { 847 if (keyword_mode_entry_method == METHOD_TAB) {
815 // Ensure the current selection is saved before showing keyword mode 848 // Ensure the current selection is saved before showing keyword mode
816 // so that moving to another line and then reverting the text will restore 849 // so that moving to another line and then reverting the text will restore
817 // the current state properly. 850 // the current state properly.
818 bool save_original_selection = !has_temporary_text_; 851 bool save_original_selection = !has_temporary_text_;
819 has_temporary_text_ = true; 852 has_temporary_text_ = true;
820 const AutocompleteMatch& match = CurrentMatch(NULL); 853 const AutocompleteMatch& match = CurrentMatch(NULL);
821 original_url_ = match.destination_url; 854 original_url_ = match.destination_url;
822 view_->OnTemporaryTextMaybeChanged( 855 view_->OnTemporaryTextMaybeChanged(
823 DisplayTextFromUserText(match.fill_into_edit), 856 DisplayTextFromUserText(match.fill_into_edit),
824 save_original_selection, true); 857 save_original_selection, true);
825 } else { 858 } else {
826 view_->OnTemporaryTextMaybeChanged(DisplayTextFromUserText(user_text_), 859 view_->OnTemporaryTextMaybeChanged(DisplayTextFromUserText(user_text_),
827 false, true); 860 false, true);
828 } 861 }
829 862
830 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint")); 863 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint"));
831 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method, 864 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram,
832 ENTERED_KEYWORD_MODE_NUM_ITEMS); 865 keyword_mode_entry_method,
866 KEYWORD_MODE_ENTRY_METHOD_NUM_ITEMS);
833 867
834 return true; 868 return true;
835 } 869 }
836 870
837 void OmniboxEditModel::AcceptTemporaryTextAsUserText() { 871 void OmniboxEditModel::AcceptTemporaryTextAsUserText() {
838 InternalSetUserText(UserTextFromDisplayText(view_->GetText())); 872 InternalSetUserText(UserTextFromDisplayText(view_->GetText()));
839 has_temporary_text_ = false; 873 has_temporary_text_ = false;
840 874
841 if (user_input_in_progress_ || !in_revert_) 875 if (user_input_in_progress_ || !in_revert_)
842 client_->OnInputStateChanged(); 876 client_->OnInputStateChanged();
843 } 877 }
844 878
845 void OmniboxEditModel::ClearKeyword() { 879 void OmniboxEditModel::ClearKeyword() {
846 autocomplete_controller()->Stop(false); 880 autocomplete_controller()->Stop(false);
881 base::AutoReset<bool> reset_cleared_keyword(&clearing_keyword_, true);
847 882
848 // While we're always in keyword mode upon reaching here, sometimes we've just 883 // While we're always in keyword mode upon reaching here, sometimes we've just
849 // toggled in via space or tab, and sometimes we're on a non-toggled line 884 // toggled in via space or tab, and sometimes we're on a non-toggled line
850 // (usually because the user has typed a search string). Keep track of the 885 // (usually because the user has typed a search string). Keep track of the
851 // difference, as we'll need it below. 886 // difference, as we'll need it below.
852 bool was_toggled_into_keyword_mode = 887 bool was_toggled_into_keyword_mode =
853 popup_model()->selected_line_state() == OmniboxPopupModel::KEYWORD; 888 popup_model()->selected_line_state() == OmniboxPopupModel::KEYWORD;
854 889
855 omnibox_controller_->ClearPopupKeywordMode(); 890 omnibox_controller_->ClearPopupKeywordMode();
856 891
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 // without re-enabling keyword mode. The common case is state 3, where 941 // without re-enabling keyword mode. The common case is state 3, where
907 // the user entered keyword mode unintentionally, so backspacing 942 // the user entered keyword mode unintentionally, so backspacing
908 // immediately out of keyword mode should keep the space. In states 1 and 943 // immediately out of keyword mode should keep the space. In states 1 and
909 // 2, having the space is "safer" behavior. For instance, if the user types 944 // 2, having the space is "safer" behavior. For instance, if the user types
910 // "google.com f" or "google.com<tab>f" in the omnibox, moves the cursor to 945 // "google.com f" or "google.com<tab>f" in the omnibox, moves the cursor to
911 // the left, and presses backspace to leave keyword mode (state 1), it's 946 // the left, and presses backspace to leave keyword mode (state 1), it's
912 // better to have the space because it may be what the user wanted. The 947 // better to have the space because it may be what the user wanted. The
913 // user can easily delete it. On the other hand, if there is no space and 948 // user can easily delete it. On the other hand, if there is no space and
914 // the user wants it, it's more work to add because typing the space will 949 // the user wants it, it's more work to add because typing the space will
915 // enter keyword mode, which then the user would have to leave again. 950 // enter keyword mode, which then the user would have to leave again.
916 const base::string16 window_text = 951
917 keyword_ + base::ASCIIToUTF16(" ") + view_->GetText(); 952 // If we entered keyword mode in a special way like using a keyboard
918 view_->SetWindowTextAndCaretPos(window_text.c_str(), keyword_.length() + 1, 953 // shortcut or typing a question mark in a blank omnibox, don't restore the
919 false, false); 954 // keyword. Instead, restore the question mark iff the user originally
955 // typed one.
956 base::string16 prefix;
957 if (keyword_mode_entry_method_ == METHOD_KEYBOARD_SHORTCUT)
Peter Kasting 2016/04/15 00:58:55 Nit: Simpler: if (keyword_mode_entry_method_
Tom (Use chromium acct) 2016/04/15 05:20:52 Done.
958 prefix = base::string16();
959 else if (keyword_mode_entry_method_ == METHOD_QUESTION_MARK)
960 prefix = base::ASCIIToUTF16("?");
961 else
962 prefix = keyword_ + base::ASCIIToUTF16(" ");
963
920 keyword_.clear(); 964 keyword_.clear();
921 is_keyword_hint_ = false; 965 is_keyword_hint_ = false;
966 InternalSetUserText(prefix + view_->GetText());
Peter Kasting 2016/04/15 00:58:55 Why does this need to be added? Shouldn't OnAfter
Tom (Use chromium acct) 2016/04/15 05:20:52 Without it, if I press Ctrl-K and then backspace,
Tom (Use chromium acct) 2016/04/28 17:40:30 Removed call to InternalSetUserText
967 view_->SetWindowTextAndCaretPos(prefix + view_->GetText(), prefix.length(),
968 false, false);
969
922 view_->OnAfterPossibleChange(false); 970 view_->OnAfterPossibleChange(false);
971 StartAutocomplete(false, false, false);
923 } 972 }
924 } 973 }
925 974
926 void OmniboxEditModel::OnSetFocus(bool control_down) { 975 void OmniboxEditModel::OnSetFocus(bool control_down) {
927 last_omnibox_focus_ = base::TimeTicks::Now(); 976 last_omnibox_focus_ = base::TimeTicks::Now();
928 user_input_since_focus_ = false; 977 user_input_since_focus_ = false;
929 978
930 // If the omnibox lost focus while the caret was hidden and then regained 979 // If the omnibox lost focus while the caret was hidden and then regained
931 // focus, OnSetFocus() is called and should restore visibility. Note that 980 // focus, OnSetFocus() is called and should restore visibility. Note that
932 // focus can be regained without an accompanying call to 981 // focus can be regained without an accompanying call to
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 if (call_controller_onchanged) 1197 if (call_controller_onchanged)
1149 OnChanged(); 1198 OnChanged();
1150 } 1199 }
1151 1200
1152 bool OmniboxEditModel::OnAfterPossibleChange(const base::string16& old_text, 1201 bool OmniboxEditModel::OnAfterPossibleChange(const base::string16& old_text,
1153 const base::string16& new_text, 1202 const base::string16& new_text,
1154 size_t selection_start, 1203 size_t selection_start,
1155 size_t selection_end, 1204 size_t selection_end,
1156 bool selection_differs, 1205 bool selection_differs,
1157 bool text_differs, 1206 bool text_differs,
1207 bool keyword_differs,
1158 bool just_deleted_text, 1208 bool just_deleted_text,
1159 bool allow_keyword_ui_change) { 1209 bool allow_keyword_ui_change) {
1160 // Update the paste state as appropriate: if we're just finishing a paste 1210 // Update the paste state as appropriate: if we're just finishing a paste
1161 // that replaced all the text, preserve that information; otherwise, if we've 1211 // that replaced all the text, preserve that information; otherwise, if we've
1162 // made some other edit, clear paste tracking. 1212 // made some other edit, clear paste tracking.
1163 if (paste_state_ == PASTING) 1213 if (paste_state_ == PASTING)
1164 paste_state_ = PASTED; 1214 paste_state_ = PASTED;
1165 else if (text_differs) 1215 else if (text_differs)
1166 paste_state_ = NONE; 1216 paste_state_ = NONE;
1167 1217
(...skipping 19 matching lines...) Expand all
1187 const bool user_text_changed = 1237 const bool user_text_changed =
1188 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); 1238 text_differs || (selection_differs && !inline_autocomplete_text_.empty());
1189 1239
1190 // If something has changed while the control key is down, prevent 1240 // If something has changed while the control key is down, prevent
1191 // "ctrl-enter" until the control key is released. 1241 // "ctrl-enter" until the control key is released.
1192 if ((text_differs || selection_differs) && 1242 if ((text_differs || selection_differs) &&
1193 (control_key_state_ == DOWN_WITHOUT_CHANGE)) 1243 (control_key_state_ == DOWN_WITHOUT_CHANGE))
1194 control_key_state_ = DOWN_WITH_CHANGE; 1244 control_key_state_ = DOWN_WITH_CHANGE;
1195 1245
1196 if (!user_text_changed) 1246 if (!user_text_changed)
1197 return false; 1247 return keyword_differs;
1198 1248
1199 // If the user text has not changed, we do not want to change the model's 1249 // 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 1250 // state associated with the text. Otherwise, we can get surprising behavior
1201 // where the autocompleted text unexpectedly reappears, e.g. crbug.com/55983 1251 // where the autocompleted text unexpectedly reappears, e.g. crbug.com/55983
1202 InternalSetUserText(UserTextFromDisplayText(new_text)); 1252 InternalSetUserText(UserTextFromDisplayText(new_text));
1203 has_temporary_text_ = false; 1253 has_temporary_text_ = false;
1204 1254
1205 // Track when the user has deleted text so we won't allow inline 1255 // Track when the user has deleted text so we won't allow inline
1206 // autocomplete. 1256 // autocomplete.
1207 just_deleted_text_ = just_deleted_text; 1257 just_deleted_text_ = just_deleted_text;
(...skipping 15 matching lines...) Expand all
1223 // |allow_exact_keyword_match_| will be used by StartAutocomplete() method, 1273 // |allow_exact_keyword_match_| will be used by StartAutocomplete() method,
1224 // which will be called by |view_->UpdatePopup()|; so after that returns we 1274 // which will be called by |view_->UpdatePopup()|; so after that returns we
1225 // can safely reset this flag. 1275 // can safely reset this flag.
1226 allow_exact_keyword_match_ = text_differs && allow_keyword_ui_change && 1276 allow_exact_keyword_match_ = text_differs && allow_keyword_ui_change &&
1227 !just_deleted_text && no_selection && 1277 !just_deleted_text && no_selection &&
1228 CreatedKeywordSearchByInsertingSpaceInMiddle(old_text, user_text_, 1278 CreatedKeywordSearchByInsertingSpaceInMiddle(old_text, user_text_,
1229 selection_start); 1279 selection_start);
1230 view_->UpdatePopup(); 1280 view_->UpdatePopup();
1231 if (allow_exact_keyword_match_) { 1281 if (allow_exact_keyword_match_) {
1232 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, 1282 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram,
1233 ENTERED_KEYWORD_MODE_VIA_SPACE_IN_MIDDLE, 1283 METHOD_SPACE_IN_MIDDLE,
1234 ENTERED_KEYWORD_MODE_NUM_ITEMS); 1284 KEYWORD_MODE_ENTRY_METHOD_NUM_ITEMS);
1235 allow_exact_keyword_match_ = false; 1285 allow_exact_keyword_match_ = false;
1236 } 1286 }
1237 1287
1238 // Change to keyword mode if the user is now pressing space after a keyword 1288 // Change to keyword mode if the user is now pressing space after a keyword
1239 // name. Note that if this is the case, then even if there was no keyword 1289 // name. Note that if this is the case, then even if there was no keyword
1240 // hint when we entered this function (e.g. if the user has used space to 1290 // hint when we entered this function (e.g. if the user has used space to
1241 // replace some selected text that was adjoined to this keyword), there will 1291 // replace some selected text that was adjoined to this keyword), there will
1242 // be one now because of the call to UpdatePopup() above; so it's safe for 1292 // be one now because of the call to UpdatePopup() above; so it's safe for
1243 // MaybeAcceptKeywordBySpace() to look at |keyword_| and |is_keyword_hint_| to 1293 // MaybeAcceptKeywordBySpace() to look at |keyword_| and |is_keyword_hint_| to
1244 // determine what keyword, if any, is applicable. 1294 // determine what keyword, if any, is applicable.
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1369 view_->OnRevertTemporaryText(); 1419 view_->OnRevertTemporaryText();
1370 } 1420 }
1371 1421
1372 bool OmniboxEditModel::MaybeAcceptKeywordBySpace( 1422 bool OmniboxEditModel::MaybeAcceptKeywordBySpace(
1373 const base::string16& new_text) { 1423 const base::string16& new_text) {
1374 size_t keyword_length = new_text.length() - 1; 1424 size_t keyword_length = new_text.length() - 1;
1375 return (paste_state_ == NONE) && is_keyword_hint_ && 1425 return (paste_state_ == NONE) && is_keyword_hint_ &&
1376 (keyword_.length() == keyword_length) && 1426 (keyword_.length() == keyword_length) &&
1377 IsSpaceCharForAcceptingKeyword(new_text[keyword_length]) && 1427 IsSpaceCharForAcceptingKeyword(new_text[keyword_length]) &&
1378 !new_text.compare(0, keyword_length, keyword_, 0, keyword_length) && 1428 !new_text.compare(0, keyword_length, keyword_, 0, keyword_length) &&
1379 AcceptKeyword(ENTERED_KEYWORD_MODE_VIA_SPACE_AT_END); 1429 AcceptKeyword(METHOD_SPACE_AT_END);
1380 } 1430 }
1381 1431
1382 bool OmniboxEditModel::CreatedKeywordSearchByInsertingSpaceInMiddle( 1432 bool OmniboxEditModel::CreatedKeywordSearchByInsertingSpaceInMiddle(
1383 const base::string16& old_text, 1433 const base::string16& old_text,
1384 const base::string16& new_text, 1434 const base::string16& new_text,
1385 size_t caret_position) const { 1435 size_t caret_position) const {
1386 DCHECK_GE(new_text.length(), caret_position); 1436 DCHECK_GE(new_text.length(), caret_position);
1387 1437
1388 // Check simple conditions first. 1438 // Check simple conditions first.
1389 if ((paste_state_ != NONE) || (caret_position < 2) || 1439 if ((paste_state_ != NONE) || (caret_position < 2) ||
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 // Update state and notify view if the omnibox has focus and the caret 1513 // Update state and notify view if the omnibox has focus and the caret
1464 // visibility changed. 1514 // visibility changed.
1465 const bool was_caret_visible = is_caret_visible(); 1515 const bool was_caret_visible = is_caret_visible();
1466 focus_state_ = state; 1516 focus_state_ = state;
1467 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1517 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1468 is_caret_visible() != was_caret_visible) 1518 is_caret_visible() != was_caret_visible)
1469 view_->ApplyCaretVisibility(); 1519 view_->ApplyCaretVisibility();
1470 1520
1471 client_->OnFocusChanged(focus_state_, reason); 1521 client_->OnFocusChanged(focus_state_, reason);
1472 } 1522 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698