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/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 InstantController::InstantController(chrome::BrowserInstantController* browser, | 145 InstantController::InstantController(chrome::BrowserInstantController* browser, |
146 bool extended_enabled) | 146 bool extended_enabled) |
147 : browser_(browser), | 147 : browser_(browser), |
148 extended_enabled_(extended_enabled), | 148 extended_enabled_(extended_enabled), |
149 instant_enabled_(false), | 149 instant_enabled_(false), |
150 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 150 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
151 last_omnibox_text_has_inline_autocompletion_(false), | 151 last_omnibox_text_has_inline_autocompletion_(false), |
152 last_verbatim_(false), | 152 last_verbatim_(false), |
153 last_transition_type_(content::PAGE_TRANSITION_LINK), | 153 last_transition_type_(content::PAGE_TRANSITION_LINK), |
154 last_match_was_search_(false), | 154 last_match_was_search_(false), |
155 is_omnibox_focused_(false), | 155 omnibox_focus_state_(FOCUS_NONE), |
156 allow_preview_to_show_search_suggestions_(false) { | 156 allow_preview_to_show_search_suggestions_(false) { |
157 } | 157 } |
158 | 158 |
159 InstantController::~InstantController() { | 159 InstantController::~InstantController() { |
160 } | 160 } |
161 | 161 |
162 bool InstantController::Update(const AutocompleteMatch& match, | 162 bool InstantController::Update(const AutocompleteMatch& match, |
163 const string16& user_text, | 163 const string16& user_text, |
164 const string16& full_text, | 164 const string16& full_text, |
165 size_t selection_start, | 165 size_t selection_start, |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
524 // Delay deletion as we could've gotten here from an InstantLoader method. | 524 // Delay deletion as we could've gotten here from an InstantLoader method. |
525 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); | 525 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); |
526 | 526 |
527 // Try to create another loader immediately so that it is ready for the next | 527 // Try to create another loader immediately so that it is ready for the next |
528 // user interaction. | 528 // user interaction. |
529 CreateDefaultLoader(); | 529 CreateDefaultLoader(); |
530 | 530 |
531 return true; | 531 return true; |
532 } | 532 } |
533 | 533 |
534 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | 534 void InstantController::OmniboxFocusChanged( |
535 DVLOG(1) << "OmniboxLostFocus"; | 535 OmniboxFocusState state, |
536 is_omnibox_focused_ = false; | 536 OmniboxFocusChangeReason reason, |
537 gfx::NativeView view_gaining_focus) { | |
538 DVLOG(1) << "OmniboxFocusChanged: " << omnibox_focus_state_ << " to " | |
539 << state << " for reason " << reason; | |
537 | 540 |
541 OmniboxFocusState old_focus_state = omnibox_focus_state_; | |
542 omnibox_focus_state_ = state; | |
538 if (!extended_enabled_ && !instant_enabled_) | 543 if (!extended_enabled_ && !instant_enabled_) |
539 return; | 544 return; |
540 | 545 |
541 // If the preview isn't showing search suggestions, nothing to do. The check | 546 // Tell the page if the key capture mode changed unless the focus state |
542 // for GetPreviewContents() (which normally is redundant, given IsCurrent()) | 547 // changed because of TYPING. This is because in that case, the browser hasn't |
543 // is to handle the case when we get here during a commit. | 548 // really stopped capturing key strokes. |
544 if (!IsCurrent() || !GetPreviewContents()) { | 549 // |
545 OnStaleLoader(); | 550 // (More practically, if we don't do this check, the page would receieve |
sreeram
2012/12/05 19:48:24
Nit: receieve -> receive
samarth
2012/12/08 00:55:47
Done.
| |
546 return; | 551 // keycapturechange before the corresponding onchange, and the page would have |
547 } | 552 // no way of telling whether the keycapturechange happened because of some |
553 // actual user action or just because they started typing.) | |
554 if (extended_enabled_ && GetPreviewContents() && | |
555 reason != FOCUS_CHANGE_TYPING) | |
556 loader_->KeyCaptureChanged(omnibox_focus_state_ == FOCUS_INVISIBLE); | |
548 | 557 |
549 #if defined(OS_MACOSX) | 558 // If focus went from outside the omnibox to the omnibox, preload the default |
550 if (!loader_->is_pointer_down_from_activate()) | 559 // search engine, in anticipation of the user typing a query. If the reverse |
551 HideLoader(); | 560 // happened, commit or discard the preview. |
552 #else | 561 if (state != FOCUS_NONE && old_focus_state == FOCUS_NONE) |
553 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), | 562 CreateDefaultLoader(); |
554 loader_->contents())) | 563 else if (state == FOCUS_NONE && old_focus_state != FOCUS_NONE) |
555 CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); | 564 OmniboxLostFocus(view_gaining_focus); |
556 else | |
557 HideLoader(); | |
558 #endif | |
559 } | |
560 | |
561 void InstantController::OmniboxGotFocus() { | |
562 DVLOG(1) << "OmniboxGotFocus"; | |
563 is_omnibox_focused_ = true; | |
564 | |
565 if (!extended_enabled_ && !instant_enabled_) | |
566 return; | |
567 | |
568 CreateDefaultLoader(); | |
569 } | 565 } |
570 | 566 |
571 void InstantController::SearchModeChanged( | 567 void InstantController::SearchModeChanged( |
572 const chrome::search::Mode& old_mode, | 568 const chrome::search::Mode& old_mode, |
573 const chrome::search::Mode& new_mode) { | 569 const chrome::search::Mode& new_mode) { |
574 if (!extended_enabled_) | 570 if (!extended_enabled_) |
575 return; | 571 return; |
576 | 572 |
577 DVLOG(1) << "SearchModeChanged: [origin:mode] " << old_mode.origin << ":" | 573 DVLOG(1) << "SearchModeChanged: [origin:mode] " << old_mode.origin << ":" |
578 << old_mode.mode << " to " << new_mode.origin << ":" | 574 << old_mode.mode << " to " << new_mode.origin << ":" |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
754 | 750 |
755 void InstantController::InstantLoaderContentsFocused() { | 751 void InstantController::InstantLoaderContentsFocused() { |
756 #if defined(USE_AURA) | 752 #if defined(USE_AURA) |
757 // On aura the omnibox only receives a focus lost if we initiate the focus | 753 // On aura the omnibox only receives a focus lost if we initiate the focus |
758 // change. This does that. | 754 // change. This does that. |
759 if (!model_.mode().is_default()) | 755 if (!model_.mode().is_default()) |
760 browser_->InstantPreviewFocused(); | 756 browser_->InstantPreviewFocused(); |
761 #endif | 757 #endif |
762 } | 758 } |
763 | 759 |
760 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { | |
761 DVLOG(1) << "OmniboxLostFocus"; | |
762 // If the preview isn't showing search suggestions, nothing to do. The check | |
763 // for GetPreviewContents() (which normally is redundant, given IsCurrent()) | |
764 // is to handle the case when we get here during a commit. | |
765 if (!IsCurrent() || !GetPreviewContents()) { | |
766 OnStaleLoader(); | |
767 return; | |
768 } | |
769 | |
770 #if defined(OS_MACOSX) | |
771 if (!loader_->is_pointer_down_from_activate()) | |
772 HideLoader(); | |
773 #else | |
774 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), | |
775 loader_->contents())) | |
776 CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); | |
777 else | |
778 HideLoader(); | |
779 #endif | |
780 } | |
781 | |
764 bool InstantController::ResetLoader(const TemplateURL* template_url, | 782 bool InstantController::ResetLoader(const TemplateURL* template_url, |
765 const content::WebContents* active_tab) { | 783 const content::WebContents* active_tab) { |
766 std::string instant_url; | 784 std::string instant_url; |
767 if (!GetInstantURL(template_url, &instant_url)) | 785 if (!GetInstantURL(template_url, &instant_url)) |
768 return false; | 786 return false; |
769 | 787 |
770 if (loader_ && loader_->instant_url() == instant_url) | 788 if (loader_ && loader_->instant_url() == instant_url) |
771 return true; | 789 return true; |
772 | 790 |
773 HideInternal(); | 791 HideInternal(); |
774 loader_.reset(new InstantLoader(this, instant_url)); | 792 loader_.reset(new InstantLoader(this, instant_url)); |
775 loader_->InitContents(active_tab); | 793 loader_->InitContents(active_tab); |
776 | 794 |
777 // Ensure the searchbox API has the correct initial state. | 795 // Ensure the searchbox API has the correct initial state. |
778 if (extended_enabled_) { | 796 if (extended_enabled_) { |
779 browser_->UpdateThemeInfoForPreview(); | 797 browser_->UpdateThemeInfoForPreview(); |
780 loader_->SetDisplayInstantResults(instant_enabled_); | 798 loader_->SetDisplayInstantResults(instant_enabled_); |
781 loader_->SearchModeChanged(search_mode_); | 799 loader_->SearchModeChanged(search_mode_); |
800 loader_->KeyCaptureChanged(omnibox_focus_state_ == FOCUS_INVISIBLE); | |
782 } | 801 } |
783 | 802 |
784 // Restart the stale loader timer. | 803 // Restart the stale loader timer. |
785 stale_loader_timer_.Start(FROM_HERE, | 804 stale_loader_timer_.Start(FROM_HERE, |
786 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 805 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
787 &InstantController::OnStaleLoader); | 806 &InstantController::OnStaleLoader); |
788 | 807 |
789 return true; | 808 return true; |
790 } | 809 } |
791 | 810 |
792 bool InstantController::CreateDefaultLoader() { | 811 bool InstantController::CreateDefaultLoader() { |
793 // If there's no active tab, the browser is closing. | 812 // If there's no active tab, the browser is closing. |
794 const content::WebContents* active_tab = browser_->GetActiveWebContents(); | 813 const content::WebContents* active_tab = browser_->GetActiveWebContents(); |
795 if (!active_tab) | 814 if (!active_tab) |
796 return false; | 815 return false; |
797 | 816 |
798 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( | 817 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
799 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> | 818 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> |
800 GetDefaultSearchProvider(); | 819 GetDefaultSearchProvider(); |
801 | 820 |
802 return ResetLoader(template_url, active_tab); | 821 return ResetLoader(template_url, active_tab); |
803 } | 822 } |
804 | 823 |
805 void InstantController::OnStaleLoader() { | 824 void InstantController::OnStaleLoader() { |
806 // If the preview is showing or the omnibox has focus, don't delete the | 825 // If the preview is showing or the omnibox has focus, don't delete the |
807 // loader. It will get refreshed the next time the preview is hidden or the | 826 // loader. It will get refreshed the next time the preview is hidden or the |
808 // omnibox loses focus. | 827 // omnibox loses focus. |
809 if (!stale_loader_timer_.IsRunning() && !is_omnibox_focused_ && | 828 if (!stale_loader_timer_.IsRunning() && omnibox_focus_state_ == FOCUS_NONE && |
810 model_.mode().is_default()) { | 829 model_.mode().is_default()) { |
811 loader_.reset(); | 830 loader_.reset(); |
812 CreateDefaultLoader(); | 831 CreateDefaultLoader(); |
813 } | 832 } |
814 } | 833 } |
815 | 834 |
816 void InstantController::ResetInstantTab() { | 835 void InstantController::ResetInstantTab() { |
817 if (search_mode_.is_origin_search()) { | 836 if (search_mode_.is_origin_search()) { |
818 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 837 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
819 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 838 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1000 } | 1019 } |
1001 | 1020 |
1002 std::map<std::string, int>::const_iterator iter = | 1021 std::map<std::string, int>::const_iterator iter = |
1003 blacklisted_urls_.find(*instant_url); | 1022 blacklisted_urls_.find(*instant_url); |
1004 if (iter != blacklisted_urls_.end() && | 1023 if (iter != blacklisted_urls_.end() && |
1005 iter->second > kMaxInstantSupportFailures) | 1024 iter->second > kMaxInstantSupportFailures) |
1006 return false; | 1025 return false; |
1007 | 1026 |
1008 return true; | 1027 return true; |
1009 } | 1028 } |
OLD | NEW |