| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 const string16 suggestion = view_->GetInstantSuggestion(); | 204 const string16 suggestion = view_->GetInstantSuggestion(); |
| 205 if (suggestion.empty()) | 205 if (suggestion.empty()) |
| 206 return false; | 206 return false; |
| 207 | 207 |
| 208 FinalizeInstantQuery(view_->GetText(), suggestion, skip_inline_autocomplete); | 208 FinalizeInstantQuery(view_->GetText(), suggestion, skip_inline_autocomplete); |
| 209 return true; | 209 return true; |
| 210 } | 210 } |
| 211 | 211 |
| 212 bool OmniboxEditModel::AcceptCurrentInstantPreview() { | 212 bool OmniboxEditModel::AcceptCurrentInstantPreview() { |
| 213 InstantController* instant = controller_->GetInstant(); | 213 InstantController* instant = controller_->GetInstant(); |
| 214 return instant && instant->CommitIfCurrent(); | 214 return instant && instant->IsCurrent() && |
| 215 instant->CommitCurrentPreview(INSTANT_COMMIT_PRESSED_ENTER); |
| 215 } | 216 } |
| 216 | 217 |
| 217 void OmniboxEditModel::OnChanged() { | 218 void OmniboxEditModel::OnChanged() { |
| 218 // Don't call CurrentMatch() when there's no editing, as in this case we'll | 219 // Don't call CurrentMatch() when there's no editing, as in this case we'll |
| 219 // never actually use it. This avoids running the autocomplete providers (and | 220 // never actually use it. This avoids running the autocomplete providers (and |
| 220 // any systems they then spin up) during startup. | 221 // any systems they then spin up) during startup. |
| 221 const AutocompleteMatch& current_match = user_input_in_progress_ ? | 222 const AutocompleteMatch& current_match = user_input_in_progress_ ? |
| 222 CurrentMatch() : AutocompleteMatch(); | 223 CurrentMatch() : AutocompleteMatch(); |
| 223 | 224 |
| 224 AutocompleteActionPredictor::Action recommended_action = | 225 AutocompleteActionPredictor::Action recommended_action = |
| 225 AutocompleteActionPredictor::ACTION_NONE; | 226 AutocompleteActionPredictor::ACTION_NONE; |
| 226 AutocompleteActionPredictor* action_predictor = | 227 AutocompleteActionPredictor* action_predictor = |
| 227 user_input_in_progress_ ? | 228 user_input_in_progress_ ? |
| 228 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL; | 229 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL; |
| 229 if (action_predictor) { | 230 if (action_predictor) { |
| 230 action_predictor->RegisterTransitionalMatches(user_text_, result()); | 231 action_predictor->RegisterTransitionalMatches(user_text_, result()); |
| 231 // Confer with the AutocompleteActionPredictor to determine what action, if | 232 // Confer with the AutocompleteActionPredictor to determine what action, if |
| 232 // any, we should take. Get the recommended action here even if we don't | 233 // any, we should take. Get the recommended action here even if we don't |
| 233 // need it so we can get stats for anyone who is opted in to UMA, but only | 234 // need it so we can get stats for anyone who is opted in to UMA, but only |
| 234 // get it if the user has actually typed something to avoid constructing it | 235 // get it if the user has actually typed something to avoid constructing it |
| 235 // before it's needed. Note: This event is triggered as part of startup when | 236 // before it's needed. Note: This event is triggered as part of startup when |
| 236 // the initial tab transitions to the start page. | 237 // the initial tab transitions to the start page. |
| 237 recommended_action = | 238 recommended_action = |
| 238 action_predictor->RecommendAction(user_text_, current_match); | 239 action_predictor->RecommendAction(user_text_, current_match); |
| 239 } | 240 } |
| 240 | 241 |
| 241 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.Action", | 242 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.Action", |
| 242 recommended_action, | 243 recommended_action, |
| 243 AutocompleteActionPredictor::LAST_PREDICT_ACTION); | 244 AutocompleteActionPredictor::LAST_PREDICT_ACTION); |
| 244 string16 suggested_text; | |
| 245 | 245 |
| 246 if (DoInstant(current_match, &suggested_text)) { | 246 string16 suggested_text = inline_autocomplete_text_; |
| 247 SetSuggestedText(suggested_text, instant_complete_behavior_); | 247 InstantCompleteBehavior complete_behavior = INSTANT_COMPLETE_NOW; |
| 248 |
| 249 if (DoInstant(current_match, &suggested_text, &complete_behavior)) { |
| 250 SetSuggestedText(suggested_text, complete_behavior); |
| 248 } else { | 251 } else { |
| 249 switch (recommended_action) { | 252 switch (recommended_action) { |
| 250 case AutocompleteActionPredictor::ACTION_PRERENDER: | 253 case AutocompleteActionPredictor::ACTION_PRERENDER: |
| 251 DoPrerender(current_match); | 254 DoPrerender(current_match); |
| 252 break; | 255 break; |
| 253 case AutocompleteActionPredictor::ACTION_PRECONNECT: | 256 case AutocompleteActionPredictor::ACTION_PRECONNECT: |
| 254 DoPreconnect(current_match); | 257 DoPreconnect(current_match); |
| 255 break; | 258 break; |
| 256 case AutocompleteActionPredictor::ACTION_NONE: | 259 case AutocompleteActionPredictor::ACTION_NONE: |
| 257 break; | 260 break; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 (has_selected_text && inline_autocomplete_text_.empty()) || | 436 (has_selected_text && inline_autocomplete_text_.empty()) || |
| 434 (paste_state_ != NONE), keyword_is_selected, | 437 (paste_state_ != NONE), keyword_is_selected, |
| 435 keyword_is_selected || allow_exact_keyword_match_, | 438 keyword_is_selected || allow_exact_keyword_match_, |
| 436 AutocompleteInput::ALL_MATCHES); | 439 AutocompleteInput::ALL_MATCHES); |
| 437 } | 440 } |
| 438 | 441 |
| 439 void OmniboxEditModel::StopAutocomplete() { | 442 void OmniboxEditModel::StopAutocomplete() { |
| 440 if (popup_->IsOpen() && !in_revert_) { | 443 if (popup_->IsOpen() && !in_revert_) { |
| 441 InstantController* instant = controller_->GetInstant(); | 444 InstantController* instant = controller_->GetInstant(); |
| 442 if (instant && !instant->commit_on_mouse_up()) | 445 if (instant && !instant->commit_on_mouse_up()) |
| 443 instant->DestroyPreviewContents(); | 446 instant->Hide(); |
| 444 } | 447 } |
| 445 | 448 |
| 446 autocomplete_controller_->Stop(true); | 449 autocomplete_controller_->Stop(true); |
| 447 } | 450 } |
| 448 | 451 |
| 449 bool OmniboxEditModel::CanPasteAndGo(const string16& text) const { | 452 bool OmniboxEditModel::CanPasteAndGo(const string16& text) const { |
| 450 if (!view_->GetCommandUpdater()->IsCommandEnabled(IDC_OPEN_CURRENT_URL)) | 453 if (!view_->GetCommandUpdater()->IsCommandEnabled(IDC_OPEN_CURRENT_URL)) |
| 451 return false; | 454 return false; |
| 452 | 455 |
| 453 AutocompleteMatch match; | 456 AutocompleteMatch match; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 } else { | 608 } else { |
| 606 controller_->OnAutocompleteAccept(match.destination_url, disposition, | 609 controller_->OnAutocompleteAccept(match.destination_url, disposition, |
| 607 match.transition, alternate_nav_url); | 610 match.transition, alternate_nav_url); |
| 608 } | 611 } |
| 609 | 612 |
| 610 if (match.starred) | 613 if (match.starred) |
| 611 bookmark_utils::RecordBookmarkLaunch(bookmark_utils::LAUNCH_OMNIBOX); | 614 bookmark_utils::RecordBookmarkLaunch(bookmark_utils::LAUNCH_OMNIBOX); |
| 612 | 615 |
| 613 InstantController* instant = controller_->GetInstant(); | 616 InstantController* instant = controller_->GetInstant(); |
| 614 if (instant && !popup_->IsOpen()) | 617 if (instant && !popup_->IsOpen()) |
| 615 instant->DestroyPreviewContents(); | 618 instant->Hide(); |
| 616 in_revert_ = false; | 619 in_revert_ = false; |
| 617 } | 620 } |
| 618 | 621 |
| 619 bool OmniboxEditModel::AcceptKeyword() { | 622 bool OmniboxEditModel::AcceptKeyword() { |
| 620 DCHECK(is_keyword_hint_ && !keyword_.empty()); | 623 DCHECK(is_keyword_hint_ && !keyword_.empty()); |
| 621 | 624 |
| 622 autocomplete_controller_->Stop(false); | 625 autocomplete_controller_->Stop(false); |
| 623 is_keyword_hint_ = false; | 626 is_keyword_hint_ = false; |
| 624 | 627 |
| 625 if (popup_->IsOpen()) | 628 if (popup_->IsOpen()) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 has_focus_ = true; | 675 has_focus_ = true; |
| 673 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; | 676 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; |
| 674 | 677 |
| 675 InstantController* instant = controller_->GetInstant(); | 678 InstantController* instant = controller_->GetInstant(); |
| 676 if (instant) | 679 if (instant) |
| 677 instant->OnAutocompleteGotFocus(); | 680 instant->OnAutocompleteGotFocus(); |
| 678 | 681 |
| 679 NotifySearchTabHelper(); | 682 NotifySearchTabHelper(); |
| 680 } | 683 } |
| 681 | 684 |
| 682 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { | 685 void OmniboxEditModel::OnWillKillFocus() { |
| 683 SetSuggestedText(string16(), INSTANT_COMPLETE_NOW); | 686 SetSuggestedText(string16(), INSTANT_COMPLETE_NOW); |
| 684 | 687 |
| 685 InstantController* instant = controller_->GetInstant(); | 688 if (InstantController* instant = controller_->GetInstant()) |
| 686 if (instant) | 689 instant->OnAutocompleteLostFocus(); |
| 687 instant->OnAutocompleteLostFocus(view_gaining_focus); | |
| 688 | 690 |
| 689 NotifySearchTabHelper(); | 691 NotifySearchTabHelper(); |
| 690 } | 692 } |
| 691 | 693 |
| 692 void OmniboxEditModel::OnKillFocus() { | 694 void OmniboxEditModel::OnKillFocus() { |
| 693 has_focus_ = false; | 695 has_focus_ = false; |
| 694 control_key_state_ = UP; | 696 control_key_state_ = UP; |
| 695 paste_state_ = NONE; | 697 paste_state_ = NONE; |
| 696 } | 698 } |
| 697 | 699 |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1079 GetKeywordForText(keyword).empty(); | 1081 GetKeywordForText(keyword).empty(); |
| 1080 } | 1082 } |
| 1081 | 1083 |
| 1082 void OmniboxEditModel::NotifySearchTabHelper() { | 1084 void OmniboxEditModel::NotifySearchTabHelper() { |
| 1083 if (controller_->GetTabContents()) { | 1085 if (controller_->GetTabContents()) { |
| 1084 controller_->GetTabContents()->search_tab_helper()-> | 1086 controller_->GetTabContents()->search_tab_helper()-> |
| 1085 OmniboxEditModelChanged(this); | 1087 OmniboxEditModelChanged(this); |
| 1086 } | 1088 } |
| 1087 } | 1089 } |
| 1088 | 1090 |
| 1089 bool OmniboxEditModel::DoInstant(const AutocompleteMatch& match, | 1091 bool OmniboxEditModel::DoInstant( |
| 1090 string16* suggested_text) { | 1092 const AutocompleteMatch& match, |
| 1093 string16* suggested_text, |
| 1094 InstantCompleteBehavior* complete_behavior) { |
| 1091 DCHECK(suggested_text); | 1095 DCHECK(suggested_text); |
| 1096 DCHECK(complete_behavior); |
| 1092 | 1097 |
| 1093 if (in_revert_) | 1098 if (in_revert_) |
| 1094 return false; | 1099 return false; |
| 1095 | 1100 |
| 1096 InstantController* instant = controller_->GetInstant(); | 1101 InstantController* instant = controller_->GetInstant(); |
| 1097 | 1102 |
| 1098 if (!instant) | 1103 if (!instant) |
| 1099 return false; | 1104 return false; |
| 1100 | 1105 |
| 1101 if (user_input_in_progress_ && popup_->IsOpen()) { | 1106 if (user_input_in_progress_ && popup_->IsOpen()) { |
| 1102 return instant->Update(match, view_->GetText(), UseVerbatimInstant(), | 1107 return instant->Update(match, DisplayTextFromUserText(user_text_), |
| 1103 suggested_text); | 1108 UseVerbatimInstant(), suggested_text, complete_behavior); |
| 1104 } | 1109 } |
| 1105 | 1110 |
| 1106 // It's possible DoInstant() was called due to an OnChanged() event from the | 1111 // It's possible DoInstant() was called due to an OnChanged() event from the |
| 1107 // omnibox view if the user clicked the renderer while IME composition was | 1112 // omnibox view if the user clicked the renderer while IME composition was |
| 1108 // active. In that case we still want to commit on mouse up, so don't call | 1113 // active. In that case we still want to commit on mouse up, so don't call |
| 1109 // Hide(). | 1114 // Hide(). |
| 1110 if (!instant->commit_on_mouse_up()) | 1115 if (!instant->commit_on_mouse_up()) |
| 1111 instant->Hide(); | 1116 instant->Hide(); |
| 1112 return false; | 1117 return false; |
| 1113 } | 1118 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1173 } | 1178 } |
| 1174 | 1179 |
| 1175 void OmniboxEditModel::ClassifyStringForPasteAndGo( | 1180 void OmniboxEditModel::ClassifyStringForPasteAndGo( |
| 1176 const string16& text, | 1181 const string16& text, |
| 1177 AutocompleteMatch* match, | 1182 AutocompleteMatch* match, |
| 1178 GURL* alternate_nav_url) const { | 1183 GURL* alternate_nav_url) const { |
| 1179 DCHECK(match); | 1184 DCHECK(match); |
| 1180 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, | 1185 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, |
| 1181 string16(), false, false, match, alternate_nav_url); | 1186 string16(), false, false, match, alternate_nav_url); |
| 1182 } | 1187 } |
| OLD | NEW |