| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/autocomplete/autocomplete_edit.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 14 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_match.h" | 15 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 16 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" | 17 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" |
| 18 #include "chrome/browser/autocomplete/keyword_provider.h" | 18 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 19 #include "chrome/browser/autocomplete/search_provider.h" | 19 #include "chrome/browser/autocomplete/search_provider.h" |
| 20 #include "chrome/browser/command_updater.h" | 20 #include "chrome/browser/command_updater.h" |
| 21 #include "chrome/browser/extensions/extension_omnibox_api.h" | 21 #include "chrome/browser/extensions/extension_omnibox_api.h" |
| 22 #include "chrome/browser/google/google_url_tracker.h" | 22 #include "chrome/browser/google/google_url_tracker.h" |
| 23 #include "chrome/browser/instant/instant_controller.h" | 23 #include "chrome/browser/instant/instant_controller.h" |
| 24 #include "chrome/browser/net/predictor_api.h" | 24 #include "chrome/browser/net/predictor_api.h" |
| 25 #include "chrome/browser/net/url_fixer_upper.h" | 25 #include "chrome/browser/net/url_fixer_upper.h" |
| 26 #include "chrome/browser/prerender/prerender_manager.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/search_engines/template_url.h" | 28 #include "chrome/browser/search_engines/template_url.h" |
| 28 #include "chrome/browser/search_engines/template_url_service.h" | 29 #include "chrome/browser/search_engines/template_url_service.h" |
| 29 #include "chrome/browser/search_engines/template_url_service_factory.h" | 30 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 30 #include "chrome/browser/ui/browser_list.h" | 31 #include "chrome/browser/ui/browser_list.h" |
| 31 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 32 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 32 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 33 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 33 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
| 34 #include "content/browser/user_metrics.h" | 35 #include "content/browser/user_metrics.h" |
| 35 #include "content/common/notification_service.h" | 36 #include "content/common/notification_service.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 popup_(NULL), | 73 popup_(NULL), |
| 73 controller_(controller), | 74 controller_(controller), |
| 74 has_focus_(false), | 75 has_focus_(false), |
| 75 user_input_in_progress_(false), | 76 user_input_in_progress_(false), |
| 76 just_deleted_text_(false), | 77 just_deleted_text_(false), |
| 77 has_temporary_text_(false), | 78 has_temporary_text_(false), |
| 78 paste_state_(NONE), | 79 paste_state_(NONE), |
| 79 control_key_state_(UP), | 80 control_key_state_(UP), |
| 80 is_keyword_hint_(false), | 81 is_keyword_hint_(false), |
| 81 profile_(profile), | 82 profile_(profile), |
| 82 update_instant_(true), | 83 in_revert_(false), |
| 83 allow_exact_keyword_match_(false), | 84 allow_exact_keyword_match_(false), |
| 84 instant_complete_behavior_(INSTANT_COMPLETE_DELAYED) { | 85 instant_complete_behavior_(INSTANT_COMPLETE_DELAYED) { |
| 85 } | 86 } |
| 86 | 87 |
| 87 AutocompleteEditModel::~AutocompleteEditModel() { | 88 AutocompleteEditModel::~AutocompleteEditModel() { |
| 88 } | 89 } |
| 89 | 90 |
| 90 void AutocompleteEditModel::SetProfile(Profile* profile) { | 91 void AutocompleteEditModel::SetProfile(Profile* profile) { |
| 91 DCHECK(profile); | 92 DCHECK(profile); |
| 92 profile_ = profile; | 93 profile_ = profile; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 207 |
| 207 bool AutocompleteEditModel::AcceptCurrentInstantPreview() { | 208 bool AutocompleteEditModel::AcceptCurrentInstantPreview() { |
| 208 return InstantController::CommitIfCurrent(controller_->GetInstant()); | 209 return InstantController::CommitIfCurrent(controller_->GetInstant()); |
| 209 } | 210 } |
| 210 | 211 |
| 211 void AutocompleteEditModel::OnChanged() { | 212 void AutocompleteEditModel::OnChanged() { |
| 212 InstantController* instant = controller_->GetInstant(); | 213 InstantController* instant = controller_->GetInstant(); |
| 213 string16 suggested_text; | 214 string16 suggested_text; |
| 214 TabContentsWrapper* tab = controller_->GetTabContentsWrapper(); | 215 TabContentsWrapper* tab = controller_->GetTabContentsWrapper(); |
| 215 bool might_support_instant = false; | 216 bool might_support_instant = false; |
| 216 if (update_instant_ && instant && tab) { | 217 if (!in_revert_ && tab) { |
| 217 if (user_input_in_progress() && popup_->IsOpen()) { | 218 if (instant) { |
| 218 AutocompleteMatch current_match = CurrentMatch(); | 219 if (user_input_in_progress() && popup_->IsOpen()) { |
| 219 if (current_match.destination_url == PermanentURL()) { | 220 AutocompleteMatch current_match = CurrentMatch(); |
| 220 // The destination is the same as the current url. This typically | 221 if (current_match.destination_url == PermanentURL()) { |
| 221 // happens if the user presses the down error in the omnibox, in which | 222 // The destination is the same as the current url. This typically |
| 222 // case we don't want to load a preview. | 223 // happens if the user presses the down error in the omnibox, in which |
| 223 instant->DestroyPreviewContentsAndLeaveActive(); | 224 // case we don't want to load a preview. |
| 225 instant->DestroyPreviewContentsAndLeaveActive(); |
| 226 } else { |
| 227 instant->Update(tab, current_match, view_->GetText(), |
| 228 UseVerbatimInstant(), &suggested_text); |
| 229 } |
| 224 } else { | 230 } else { |
| 225 instant->Update(tab, CurrentMatch(), view_->GetText(), | 231 instant->DestroyPreviewContents(); |
| 226 UseVerbatimInstant(), &suggested_text); | |
| 227 } | 232 } |
| 228 } else { | 233 might_support_instant = instant->MightSupportInstant(); |
| 229 instant->DestroyPreviewContents(); | 234 } else if (user_input_in_progress() && popup_->IsOpen()) { |
| 235 // Start Prerender of this page instead. |
| 236 CHECK(tab->tab_contents()); |
| 237 prerender::PrerenderManager* prerender_manager = |
| 238 tab->tab_contents()->profile()->GetPrerenderManager(); |
| 239 if (prerender_manager) { |
| 240 prerender_manager->AddPrerender(prerender::ORIGIN_OMNIBOX, |
| 241 CurrentMatch().destination_url); |
| 242 } |
| 230 } | 243 } |
| 231 might_support_instant = instant->MightSupportInstant(); | |
| 232 } | 244 } |
| 233 | 245 |
| 234 if (!might_support_instant) { | 246 if (!might_support_instant) { |
| 235 // Hide any suggestions we might be showing. | 247 // Hide any suggestions we might be showing. |
| 236 view_->SetInstantSuggestion(string16(), false); | 248 view_->SetInstantSuggestion(string16(), false); |
| 237 | 249 |
| 238 // No need to wait any longer for instant. | 250 // No need to wait any longer for instant. |
| 239 FinalizeInstantQuery(string16(), string16(), false); | 251 FinalizeInstantQuery(string16(), string16(), false); |
| 240 } else { | 252 } else { |
| 241 SetSuggestedText(suggested_text, instant_complete_behavior_); | 253 SetSuggestedText(suggested_text, instant_complete_behavior_); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 autocomplete_controller_->Start( | 404 autocomplete_controller_->Start( |
| 393 user_text_, GetDesiredTLD(), | 405 user_text_, GetDesiredTLD(), |
| 394 prevent_inline_autocomplete || just_deleted_text_ || | 406 prevent_inline_autocomplete || just_deleted_text_ || |
| 395 (has_selected_text && inline_autocomplete_text_.empty()) || | 407 (has_selected_text && inline_autocomplete_text_.empty()) || |
| 396 (paste_state_ != NONE), keyword_is_selected, | 408 (paste_state_ != NONE), keyword_is_selected, |
| 397 keyword_is_selected || allow_exact_keyword_match_, | 409 keyword_is_selected || allow_exact_keyword_match_, |
| 398 AutocompleteInput::ALL_MATCHES); | 410 AutocompleteInput::ALL_MATCHES); |
| 399 } | 411 } |
| 400 | 412 |
| 401 void AutocompleteEditModel::StopAutocomplete() { | 413 void AutocompleteEditModel::StopAutocomplete() { |
| 402 if (popup_->IsOpen() && update_instant_) { | 414 if (popup_->IsOpen() && !in_revert_) { |
| 403 InstantController* instant = controller_->GetInstant(); | 415 InstantController* instant = controller_->GetInstant(); |
| 404 if (instant && !instant->commit_on_mouse_up()) | 416 if (instant && !instant->commit_on_mouse_up()) |
| 405 instant->DestroyPreviewContents(); | 417 instant->DestroyPreviewContents(); |
| 406 } | 418 } |
| 407 | 419 |
| 408 autocomplete_controller_->Stop(true); | 420 autocomplete_controller_->Stop(true); |
| 409 } | 421 } |
| 410 | 422 |
| 411 bool AutocompleteEditModel::CanPasteAndGo(const string16& text) const { | 423 bool AutocompleteEditModel::CanPasteAndGo(const string16& text) const { |
| 412 if (!view_->GetCommandUpdater()->IsCommandEnabled(IDC_OPEN_CURRENT_URL)) | 424 if (!view_->GetCommandUpdater()->IsCommandEnabled(IDC_OPEN_CURRENT_URL)) |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 if (template_url) { | 539 if (template_url) { |
| 528 UserMetrics::RecordAction(UserMetricsAction("AcceptedKeyword")); | 540 UserMetrics::RecordAction(UserMetricsAction("AcceptedKeyword")); |
| 529 template_url_service->IncrementUsageCount(template_url); | 541 template_url_service->IncrementUsageCount(template_url); |
| 530 } | 542 } |
| 531 | 543 |
| 532 // NOTE: We purposefully don't increment the usage count of the default | 544 // NOTE: We purposefully don't increment the usage count of the default |
| 533 // search engine, if applicable; see comments in template_url.h. | 545 // search engine, if applicable; see comments in template_url.h. |
| 534 } | 546 } |
| 535 | 547 |
| 536 if (disposition != NEW_BACKGROUND_TAB) { | 548 if (disposition != NEW_BACKGROUND_TAB) { |
| 537 update_instant_ = false; | 549 in_revert_ = true; |
| 538 view_->RevertAll(); // Revert the box to its unedited state | 550 view_->RevertAll(); // Revert the box to its unedited state |
| 539 } | 551 } |
| 540 | 552 |
| 541 if (match.type == AutocompleteMatch::EXTENSION_APP) { | 553 if (match.type == AutocompleteMatch::EXTENSION_APP) { |
| 542 LaunchAppFromOmnibox(match, profile_, disposition); | 554 LaunchAppFromOmnibox(match, profile_, disposition); |
| 543 } else { | 555 } else { |
| 544 controller_->OnAutocompleteAccept(match.destination_url, disposition, | 556 controller_->OnAutocompleteAccept(match.destination_url, disposition, |
| 545 match.transition, alternate_nav_url); | 557 match.transition, alternate_nav_url); |
| 546 } | 558 } |
| 547 | 559 |
| 548 InstantController* instant = controller_->GetInstant(); | 560 InstantController* instant = controller_->GetInstant(); |
| 549 if (instant && !popup_->IsOpen()) | 561 if (instant && !popup_->IsOpen()) |
| 550 instant->DestroyPreviewContents(); | 562 instant->DestroyPreviewContents(); |
| 551 update_instant_ = true; | 563 in_revert_ = false; |
| 552 } | 564 } |
| 553 | 565 |
| 554 bool AutocompleteEditModel::AcceptKeyword() { | 566 bool AutocompleteEditModel::AcceptKeyword() { |
| 555 DCHECK(is_keyword_hint_ && !keyword_.empty()); | 567 DCHECK(is_keyword_hint_ && !keyword_.empty()); |
| 556 | 568 |
| 557 view_->OnBeforePossibleChange(); | 569 view_->OnBeforePossibleChange(); |
| 558 view_->SetWindowTextAndCaretPos(string16(), 0); | 570 view_->SetWindowTextAndCaretPos(string16(), 0); |
| 559 is_keyword_hint_ = false; | 571 is_keyword_hint_ = false; |
| 560 view_->OnAfterPossibleChange(); | 572 view_->OnAfterPossibleChange(); |
| 561 just_deleted_text_ = false; // OnAfterPossibleChange() erroneously sets this | 573 just_deleted_text_ = false; // OnAfterPossibleChange() erroneously sets this |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1013 // static | 1025 // static |
| 1014 bool AutocompleteEditModel::IsSpaceCharForAcceptingKeyword(wchar_t c) { | 1026 bool AutocompleteEditModel::IsSpaceCharForAcceptingKeyword(wchar_t c) { |
| 1015 switch (c) { | 1027 switch (c) { |
| 1016 case 0x0020: // Space | 1028 case 0x0020: // Space |
| 1017 case 0x3000: // Ideographic Space | 1029 case 0x3000: // Ideographic Space |
| 1018 return true; | 1030 return true; |
| 1019 default: | 1031 default: |
| 1020 return false; | 1032 return false; |
| 1021 } | 1033 } |
| 1022 } | 1034 } |
| OLD | NEW |