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

Side by Side Diff: chrome/browser/instant/instant_controller.cc

Issue 11413217: Instant API: tell page whether the browser is capturing key strokes. (Closed) Base URL: http://git.chromium.org/chromium/src.git@focus
Patch Set: Fixed comments, mouse handling logic. Created 8 years 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 "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
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 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 // Delay deletion as we could've gotten here from an InstantLoader method. 526 // Delay deletion as we could've gotten here from an InstantLoader method.
527 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); 527 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release());
528 528
529 // Try to create another loader immediately so that it is ready for the next 529 // Try to create another loader immediately so that it is ready for the next
530 // user interaction. 530 // user interaction.
531 CreateDefaultLoader(); 531 CreateDefaultLoader();
532 532
533 return true; 533 return true;
534 } 534 }
535 535
536 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { 536 void InstantController::OmniboxFocusChanged(
537 DVLOG(1) << "OmniboxLostFocus"; 537 OmniboxFocusState state,
538 is_omnibox_focused_ = false; 538 OmniboxFocusChangeReason reason,
539 gfx::NativeView view_gaining_focus) {
540 DVLOG(1) << "OmniboxFocusChanged: " << omnibox_focus_state_ << " to "
541 << state << " for reason " << reason;
539 542
543 OmniboxFocusState old_focus_state = omnibox_focus_state_;
544 omnibox_focus_state_ = state;
540 if (!extended_enabled_ && !instant_enabled_) 545 if (!extended_enabled_ && !instant_enabled_)
541 return; 546 return;
542 547
543 // If the preview isn't showing search suggestions, nothing to do. The check 548 // Tell the page if the key capture mode changed unless the focus state
544 // for GetPreviewContents() (which normally is redundant, given IsCurrent()) 549 // changed because of TYPING. This is because in that case, the browser hasn't
545 // is to handle the case when we get here during a commit. 550 // really stopped capturing key strokes.
546 if (!IsCurrent() || !GetPreviewContents()) { 551 //
547 OnStaleLoader(); 552 // (More practically, if we don't do this check, the page would receieve
548 return; 553 // keycapturechange before the corresponding onchange, and the page would have
549 } 554 // no way of telling whether the keycapturechange happened because of some
555 // actual user action or just because they started typing.)
556 if (extended_enabled_ && GetPreviewContents() &&
557 reason != FOCUS_CHANGE_TYPING)
558 loader_->KeyCaptureChanged(omnibox_focus_state_ == FOCUS_INVISIBLE);
550 559
551 #if defined(OS_MACOSX) 560 // If focus went from outside the omnibox to the omnibox, preload the default
552 if (!loader_->is_pointer_down_from_activate()) 561 // search engine, in anticipation of the user typing a query. If the reverse
553 HideLoader(); 562 // happened, commit or discard the preview.
554 #else 563 if (state != FOCUS_NONE && old_focus_state == FOCUS_NONE)
555 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), 564 CreateDefaultLoader();
556 loader_->contents())) 565 else if (state == FOCUS_NONE && old_focus_state != FOCUS_NONE)
557 CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST); 566 OmniboxLostFocus(view_gaining_focus);
558 else
559 HideLoader();
560 #endif
561 }
562
563 void InstantController::OmniboxGotFocus() {
564 DVLOG(1) << "OmniboxGotFocus";
565 is_omnibox_focused_ = true;
566
567 if (!extended_enabled_ && !instant_enabled_)
568 return;
569
570 CreateDefaultLoader();
571 } 567 }
572 568
573 void InstantController::SearchModeChanged( 569 void InstantController::SearchModeChanged(
574 const chrome::search::Mode& old_mode, 570 const chrome::search::Mode& old_mode,
575 const chrome::search::Mode& new_mode) { 571 const chrome::search::Mode& new_mode) {
576 if (!extended_enabled_) 572 if (!extended_enabled_)
577 return; 573 return;
578 574
579 DVLOG(1) << "SearchModeChanged: [origin:mode] " << old_mode.origin << ":" 575 DVLOG(1) << "SearchModeChanged: [origin:mode] " << old_mode.origin << ":"
580 << old_mode.mode << " to " << new_mode.origin << ":" 576 << old_mode.mode << " to " << new_mode.origin << ":"
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 755
760 void InstantController::InstantLoaderContentsFocused() { 756 void InstantController::InstantLoaderContentsFocused() {
761 #if defined(USE_AURA) 757 #if defined(USE_AURA)
762 // On aura the omnibox only receives a focus lost if we initiate the focus 758 // On aura the omnibox only receives a focus lost if we initiate the focus
763 // change. This does that. 759 // change. This does that.
764 if (!model_.mode().is_default()) 760 if (!model_.mode().is_default())
765 browser_->InstantPreviewFocused(); 761 browser_->InstantPreviewFocused();
766 #endif 762 #endif
767 } 763 }
768 764
765 void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) {
766 DVLOG(1) << "OmniboxLostFocus";
767 // If the preview isn't showing search suggestions, nothing to do. The check
768 // for GetPreviewContents() (which normally is redundant, given IsCurrent())
769 // is to handle the case when we get here during a commit.
770 if (!IsCurrent() || !GetPreviewContents()) {
771 OnStaleLoader();
772 return;
773 }
774
775 #if defined(OS_MACOSX)
776 if (!loader_->is_pointer_down_from_activate())
777 HideLoader();
778 #else
779 if (IsViewInContents(GetViewGainingFocus(view_gaining_focus),
780 loader_->contents()))
781 CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST);
782 else
783 HideLoader();
784 #endif
785 }
786
769 bool InstantController::ResetLoader(const TemplateURL* template_url, 787 bool InstantController::ResetLoader(const TemplateURL* template_url,
770 const content::WebContents* active_tab) { 788 const content::WebContents* active_tab) {
771 std::string instant_url; 789 std::string instant_url;
772 if (!GetInstantURL(template_url, &instant_url)) 790 if (!GetInstantURL(template_url, &instant_url))
773 return false; 791 return false;
774 792
775 if (loader_ && loader_->instant_url() == instant_url) 793 if (loader_ && loader_->instant_url() == instant_url)
776 return true; 794 return true;
777 795
778 HideInternal(); 796 HideInternal();
779 loader_.reset(new InstantLoader(this, instant_url)); 797 loader_.reset(new InstantLoader(this, instant_url));
780 loader_->InitContents(active_tab); 798 loader_->InitContents(active_tab);
781 799
782 // Ensure the searchbox API has the correct initial state. 800 // Ensure the searchbox API has the correct initial state.
783 if (extended_enabled_) { 801 if (extended_enabled_) {
784 browser_->UpdateThemeInfoForPreview(); 802 browser_->UpdateThemeInfoForPreview();
785 loader_->SetDisplayInstantResults(instant_enabled_); 803 loader_->SetDisplayInstantResults(instant_enabled_);
786 loader_->SearchModeChanged(search_mode_); 804 loader_->SearchModeChanged(search_mode_);
805 loader_->KeyCaptureChanged(omnibox_focus_state_ == FOCUS_INVISIBLE);
787 } 806 }
788 807
789 // Restart the stale loader timer. 808 // Restart the stale loader timer.
790 stale_loader_timer_.Start(FROM_HERE, 809 stale_loader_timer_.Start(FROM_HERE,
791 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, 810 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this,
792 &InstantController::OnStaleLoader); 811 &InstantController::OnStaleLoader);
793 812
794 return true; 813 return true;
795 } 814 }
796 815
797 bool InstantController::CreateDefaultLoader() { 816 bool InstantController::CreateDefaultLoader() {
798 // If there's no active tab, the browser is closing. 817 // If there's no active tab, the browser is closing.
799 const content::WebContents* active_tab = browser_->GetActiveWebContents(); 818 const content::WebContents* active_tab = browser_->GetActiveWebContents();
800 if (!active_tab) 819 if (!active_tab)
801 return false; 820 return false;
802 821
803 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( 822 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
804 Profile::FromBrowserContext(active_tab->GetBrowserContext()))-> 823 Profile::FromBrowserContext(active_tab->GetBrowserContext()))->
805 GetDefaultSearchProvider(); 824 GetDefaultSearchProvider();
806 825
807 return ResetLoader(template_url, active_tab); 826 return ResetLoader(template_url, active_tab);
808 } 827 }
809 828
810 void InstantController::OnStaleLoader() { 829 void InstantController::OnStaleLoader() {
811 // If the preview is showing or the omnibox has focus, don't delete the 830 // If the preview is showing or the omnibox has focus, don't delete the
812 // loader. It will get refreshed the next time the preview is hidden or the 831 // loader. It will get refreshed the next time the preview is hidden or the
813 // omnibox loses focus. 832 // omnibox loses focus.
814 if (!stale_loader_timer_.IsRunning() && !is_omnibox_focused_ && 833 if (!stale_loader_timer_.IsRunning() && omnibox_focus_state_ == FOCUS_NONE &&
815 model_.mode().is_default()) { 834 model_.mode().is_default()) {
816 loader_.reset(); 835 loader_.reset();
817 CreateDefaultLoader(); 836 CreateDefaultLoader();
818 } 837 }
819 } 838 }
820 839
821 void InstantController::ResetInstantTab() { 840 void InstantController::ResetInstantTab() {
822 if (search_mode_.is_origin_search()) { 841 if (search_mode_.is_origin_search()) {
823 content::WebContents* active_tab = browser_->GetActiveWebContents(); 842 content::WebContents* active_tab = browser_->GetActiveWebContents();
824 if (!instant_tab_ || active_tab != instant_tab_->contents()) { 843 if (!instant_tab_ || active_tab != instant_tab_->contents()) {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 } 1024 }
1006 1025
1007 std::map<std::string, int>::const_iterator iter = 1026 std::map<std::string, int>::const_iterator iter =
1008 blacklisted_urls_.find(*instant_url); 1027 blacklisted_urls_.find(*instant_url);
1009 if (iter != blacklisted_urls_.end() && 1028 if (iter != blacklisted_urls_.end() &&
1010 iter->second > kMaxInstantSupportFailures) 1029 iter->second > kMaxInstantSupportFailures)
1011 return false; 1030 return false;
1012 1031
1013 return true; 1032 return true;
1014 } 1033 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698