| 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 "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/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 using content::UserMetricsAction; | 63 using content::UserMetricsAction; |
| 64 using predictors::AutocompleteActionPredictor; | 64 using predictors::AutocompleteActionPredictor; |
| 65 using predictors::AutocompleteActionPredictorFactory; | 65 using predictors::AutocompleteActionPredictorFactory; |
| 66 | 66 |
| 67 /////////////////////////////////////////////////////////////////////////////// | 67 /////////////////////////////////////////////////////////////////////////////// |
| 68 // OmniboxEditModel::State | 68 // OmniboxEditModel::State |
| 69 | 69 |
| 70 OmniboxEditModel::State::State(bool user_input_in_progress, | 70 OmniboxEditModel::State::State(bool user_input_in_progress, |
| 71 const string16& user_text, | 71 const string16& user_text, |
| 72 const string16& keyword, | 72 const string16& keyword, |
| 73 bool is_keyword_hint) | 73 bool is_keyword_hint, |
| 74 bool is_caret_visible) |
| 74 : user_input_in_progress(user_input_in_progress), | 75 : user_input_in_progress(user_input_in_progress), |
| 75 user_text(user_text), | 76 user_text(user_text), |
| 76 keyword(keyword), | 77 keyword(keyword), |
| 77 is_keyword_hint(is_keyword_hint) { | 78 is_keyword_hint(is_keyword_hint), |
| 79 is_caret_visible(is_caret_visible) { |
| 78 } | 80 } |
| 79 | 81 |
| 80 OmniboxEditModel::State::~State() { | 82 OmniboxEditModel::State::~State() { |
| 81 } | 83 } |
| 82 | 84 |
| 83 /////////////////////////////////////////////////////////////////////////////// | 85 /////////////////////////////////////////////////////////////////////////////// |
| 84 // OmniboxEditModel | 86 // OmniboxEditModel |
| 85 | 87 |
| 86 OmniboxEditModel::OmniboxEditModel(OmniboxView* view, | 88 OmniboxEditModel::OmniboxEditModel(OmniboxView* view, |
| 87 OmniboxEditController* controller, | 89 OmniboxEditController* controller, |
| 88 Profile* profile) | 90 Profile* profile) |
| 89 : view_(view), | 91 : view_(view), |
| 90 popup_(NULL), | 92 popup_(NULL), |
| 91 controller_(controller), | 93 controller_(controller), |
| 92 has_focus_(false), | 94 has_focus_(false), |
| 95 is_caret_visible_(true), |
| 93 user_input_in_progress_(false), | 96 user_input_in_progress_(false), |
| 94 just_deleted_text_(false), | 97 just_deleted_text_(false), |
| 95 has_temporary_text_(false), | 98 has_temporary_text_(false), |
| 96 is_temporary_text_set_by_instant_(false), | 99 is_temporary_text_set_by_instant_(false), |
| 97 paste_state_(NONE), | 100 paste_state_(NONE), |
| 98 control_key_state_(UP), | 101 control_key_state_(UP), |
| 99 is_keyword_hint_(false), | 102 is_keyword_hint_(false), |
| 100 profile_(profile), | 103 profile_(profile), |
| 101 in_revert_(false), | 104 in_revert_(false), |
| 102 allow_exact_keyword_match_(false) { | 105 allow_exact_keyword_match_(false) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 121 // on switching back, typing will "just work"). | 124 // on switching back, typing will "just work"). |
| 122 const string16 user_text(UserTextFromDisplayText(view_->GetText())); | 125 const string16 user_text(UserTextFromDisplayText(view_->GetText())); |
| 123 if (user_text.empty()) { | 126 if (user_text.empty()) { |
| 124 view_->RevertAll(); | 127 view_->RevertAll(); |
| 125 view_->SelectAll(true); | 128 view_->SelectAll(true); |
| 126 } else { | 129 } else { |
| 127 InternalSetUserText(user_text); | 130 InternalSetUserText(user_text); |
| 128 } | 131 } |
| 129 } | 132 } |
| 130 | 133 |
| 131 return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_); | 134 return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_, |
| 135 is_caret_visible_); |
| 132 } | 136 } |
| 133 | 137 |
| 134 void OmniboxEditModel::RestoreState(const State& state) { | 138 void OmniboxEditModel::RestoreState(const State& state) { |
| 139 SetCaretVisibility(state.is_caret_visible); |
| 135 // Restore any user editing. | 140 // Restore any user editing. |
| 136 if (state.user_input_in_progress) { | 141 if (state.user_input_in_progress) { |
| 137 // NOTE: Be sure and set keyword-related state BEFORE invoking | 142 // NOTE: Be sure and set keyword-related state BEFORE invoking |
| 138 // DisplayTextFromUserText(), as its result depends upon this state. | 143 // DisplayTextFromUserText(), as its result depends upon this state. |
| 139 keyword_ = state.keyword; | 144 keyword_ = state.keyword; |
| 140 is_keyword_hint_ = state.is_keyword_hint; | 145 is_keyword_hint_ = state.is_keyword_hint; |
| 141 view_->SetUserText(state.user_text, | 146 view_->SetUserText(state.user_text, |
| 142 DisplayTextFromUserText(state.user_text), false); | 147 DisplayTextFromUserText(state.user_text), false); |
| 143 } | 148 } |
| 144 } | 149 } |
| (...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 } | 708 } |
| 704 } | 709 } |
| 705 | 710 |
| 706 const AutocompleteResult& OmniboxEditModel::result() const { | 711 const AutocompleteResult& OmniboxEditModel::result() const { |
| 707 return autocomplete_controller_->result(); | 712 return autocomplete_controller_->result(); |
| 708 } | 713 } |
| 709 | 714 |
| 710 void OmniboxEditModel::OnSetFocus(bool control_down) { | 715 void OmniboxEditModel::OnSetFocus(bool control_down) { |
| 711 has_focus_ = true; | 716 has_focus_ = true; |
| 712 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; | 717 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; |
| 718 // Restore caret visibility whenever the user focuses back into the omnibox. |
| 719 SetCaretVisibility(true); |
| 713 | 720 |
| 714 if (InstantController* instant = controller_->GetInstant()) | 721 if (InstantController* instant = controller_->GetInstant()) |
| 715 instant->OmniboxGotFocus(); | 722 instant->OmniboxGotFocus(); |
| 716 | 723 |
| 717 content::WebContents* web_contents = controller_->GetWebContents(); | 724 content::WebContents* web_contents = controller_->GetWebContents(); |
| 718 if (web_contents) { | 725 if (web_contents) { |
| 719 // TODO(jered): We may want to merge this into Start() and just call that | 726 // TODO(jered): We may want to merge this into Start() and just call that |
| 720 // here rather than having a special entry point for zero-suggest. Note | 727 // here rather than having a special entry point for zero-suggest. Note |
| 721 // that we avoid PermanentURL() here because it's not guaranteed to give us | 728 // that we avoid PermanentURL() here because it's not guaranteed to give us |
| 722 // the actual underlying current URL, e.g. if we're on the NTP and the | 729 // the actual underlying current URL, e.g. if we're on the NTP and the |
| 723 // |permanent_text_| is empty. | 730 // |permanent_text_| is empty. |
| 724 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), | 731 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), |
| 725 user_text_); | 732 user_text_); |
| 726 } | 733 } |
| 727 | 734 |
| 728 NotifySearchTabHelper(); | 735 NotifySearchTabHelper(); |
| 729 } | 736 } |
| 730 | 737 |
| 738 void OmniboxEditModel::SetCaretVisibility(bool visible) { |
| 739 if (has_focus_ && visible != is_caret_visible_) { |
| 740 is_caret_visible_ = visible; |
| 741 view_->ApplyCaretVisibility(); |
| 742 } |
| 743 } |
| 744 |
| 731 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { | 745 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { |
| 732 SetInstantSuggestion(InstantSuggestion()); | 746 SetInstantSuggestion(InstantSuggestion()); |
| 733 | 747 |
| 734 if (InstantController* instant = controller_->GetInstant()) | 748 if (InstantController* instant = controller_->GetInstant()) |
| 735 instant->OmniboxLostFocus(view_gaining_focus); | 749 instant->OmniboxLostFocus(view_gaining_focus); |
| 736 | 750 |
| 737 // TODO(jered): Rip this out along with StartZeroSuggest. | 751 // TODO(jered): Rip this out along with StartZeroSuggest. |
| 738 autocomplete_controller_->StopZeroSuggest(); | 752 autocomplete_controller_->StopZeroSuggest(); |
| 739 NotifySearchTabHelper(); | 753 NotifySearchTabHelper(); |
| 740 } | 754 } |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 928 bool just_deleted_text, | 942 bool just_deleted_text, |
| 929 bool allow_keyword_ui_change) { | 943 bool allow_keyword_ui_change) { |
| 930 // Update the paste state as appropriate: if we're just finishing a paste | 944 // Update the paste state as appropriate: if we're just finishing a paste |
| 931 // that replaced all the text, preserve that information; otherwise, if we've | 945 // that replaced all the text, preserve that information; otherwise, if we've |
| 932 // made some other edit, clear paste tracking. | 946 // made some other edit, clear paste tracking. |
| 933 if (paste_state_ == PASTING) | 947 if (paste_state_ == PASTING) |
| 934 paste_state_ = PASTED; | 948 paste_state_ = PASTED; |
| 935 else if (text_differs) | 949 else if (text_differs) |
| 936 paste_state_ = NONE; | 950 paste_state_ = NONE; |
| 937 | 951 |
| 952 // Restore caret visibility whenever the user changes text or selection in the |
| 953 // omnibox. |
| 954 if (text_differs || selection_differs) |
| 955 SetCaretVisibility(true); |
| 956 |
| 938 // Modifying the selection counts as accepting the autocompleted text. | 957 // Modifying the selection counts as accepting the autocompleted text. |
| 939 const bool user_text_changed = | 958 const bool user_text_changed = |
| 940 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); | 959 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); |
| 941 | 960 |
| 942 // If something has changed while the control key is down, prevent | 961 // If something has changed while the control key is down, prevent |
| 943 // "ctrl-enter" until the control key is released. When we do this, we need | 962 // "ctrl-enter" until the control key is released. When we do this, we need |
| 944 // to update the popup if it's open, since the desired_tld will have changed. | 963 // to update the popup if it's open, since the desired_tld will have changed. |
| 945 if ((text_differs || selection_differs) && | 964 if ((text_differs || selection_differs) && |
| 946 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { | 965 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { |
| 947 control_key_state_ = DOWN_WITH_CHANGE; | 966 control_key_state_ = DOWN_WITH_CHANGE; |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1262 } | 1281 } |
| 1263 | 1282 |
| 1264 void OmniboxEditModel::ClassifyStringForPasteAndGo( | 1283 void OmniboxEditModel::ClassifyStringForPasteAndGo( |
| 1265 const string16& text, | 1284 const string16& text, |
| 1266 AutocompleteMatch* match, | 1285 AutocompleteMatch* match, |
| 1267 GURL* alternate_nav_url) const { | 1286 GURL* alternate_nav_url) const { |
| 1268 DCHECK(match); | 1287 DCHECK(match); |
| 1269 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, | 1288 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, |
| 1270 string16(), false, false, match, alternate_nav_url); | 1289 string16(), false, false, match, alternate_nav_url); |
| 1271 } | 1290 } |
| OLD | NEW |