diff options
Diffstat (limited to 'chrome/browser/instant')
-rw-r--r-- | chrome/browser/instant/instant_browsertest.cc | 18 | ||||
-rw-r--r-- | chrome/browser/instant/instant_client.cc | 5 | ||||
-rw-r--r-- | chrome/browser/instant/instant_client.h | 3 | ||||
-rw-r--r-- | chrome/browser/instant/instant_controller.cc | 103 | ||||
-rw-r--r-- | chrome/browser/instant/instant_controller.h | 20 | ||||
-rw-r--r-- | chrome/browser/instant/instant_loader.cc | 4 | ||||
-rw-r--r-- | chrome/browser/instant/instant_loader.h | 1 |
7 files changed, 98 insertions, 56 deletions
diff --git a/chrome/browser/instant/instant_browsertest.cc b/chrome/browser/instant/instant_browsertest.cc index e97b25f..797688c 100644 --- a/chrome/browser/instant/instant_browsertest.cc +++ b/chrome/browser/instant/instant_browsertest.cc @@ -102,10 +102,13 @@ class InstantTest : public InProcessBrowserTest { void FocusOmnibox() { // If the omnibox already has focus, just notify Instant. - if (omnibox()->model()->has_focus()) - instant()->OmniboxGotFocus(); - else + if (omnibox()->model()->has_focus()) { + instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, + OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); + } + else { browser()->window()->GetLocationBar()->FocusLocation(false); + } } void FocusOmniboxAndWaitForInstantSupport() { @@ -678,7 +681,8 @@ IN_PROC_BROWSER_TEST_F(InstantTest, DoesNotCommitURLsTwo) { // Pretend the omnibox got focus. It already had focus, so we are just trying // to tickle a different code path. - instant()->OmniboxGotFocus(); + instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, + OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); // Commit the URL. As before, check that Instant wasn't committed. browser()->window()->GetLocationBar()->AcceptInput(); @@ -897,7 +901,8 @@ IN_PROC_BROWSER_TEST_F(InstantTest, InstantLoaderRefresh) { EXPECT_TRUE(instant()->loader_->supports_instant()); instant()->HideLoader(); EXPECT_TRUE(instant()->loader_->supports_instant()); - instant()->OmniboxLostFocus(NULL); + instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_NONE, + OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); EXPECT_FALSE(instant()->loader_->supports_instant()); // Try with a different ordering. @@ -905,7 +910,8 @@ IN_PROC_BROWSER_TEST_F(InstantTest, InstantLoaderRefresh) { instant()->stale_loader_timer_.Stop(); instant()->OnStaleLoader(); EXPECT_TRUE(instant()->loader_->supports_instant()); - instant()->OmniboxLostFocus(NULL); + instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_NONE, + OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); // TODO(sreeram): Currently, OmniboxLostFocus() calls HideLoader(). When it // stops hiding the preview eventually, uncomment these two lines: // EXPECT_TRUE(instant()->loader_->supports_instant()); diff --git a/chrome/browser/instant/instant_client.cc b/chrome/browser/instant/instant_client.cc index 50d7852..6f23d9a 100644 --- a/chrome/browser/instant/instant_client.cc +++ b/chrome/browser/instant/instant_client.cc @@ -71,6 +71,11 @@ void InstantClient::SetDisplayInstantResults(bool display_instant_results) { display_instant_results)); } +void InstantClient::KeyCaptureChanged(bool is_key_capture_enabled) { + Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged(routing_id(), + is_key_capture_enabled)); +} + void InstantClient::DidFinishLoad( int64 /* frame_id */, const GURL& /* validated_url */, diff --git a/chrome/browser/instant/instant_client.h b/chrome/browser/instant/instant_client.h index 821c36c..486ae8d 100644 --- a/chrome/browser/instant/instant_client.h +++ b/chrome/browser/instant/instant_client.h @@ -121,6 +121,9 @@ class InstantClient : public content::WebContentsObserver { // Tells the page whether it is allowed to display Instant results. void SetDisplayInstantResults(bool display_instant_results); + // Tells the page whether the browser is capturing user key strokes. + void KeyCaptureChanged(bool is_key_capture_enabled); + private: // Overridden from content::WebContentsObserver: virtual void DidFinishLoad( diff --git a/chrome/browser/instant/instant_controller.cc b/chrome/browser/instant/instant_controller.cc index d3e6545..9f40c85 100644 --- a/chrome/browser/instant/instant_controller.cc +++ b/chrome/browser/instant/instant_controller.cc @@ -156,7 +156,7 @@ InstantController::InstantController(chrome::BrowserInstantController* browser, last_verbatim_(false), last_transition_type_(content::PAGE_TRANSITION_LINK), last_match_was_search_(false), - is_omnibox_focused_(false), + omnibox_focus_state_(OMNIBOX_FOCUS_NONE), allow_preview_to_show_search_suggestions_(false) { } @@ -547,50 +547,37 @@ bool InstantController::CommitIfPossible(InstantCommitType type) { return true; } -void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { - DVLOG(1) << "OmniboxLostFocus"; - is_omnibox_focused_ = false; - - if (!extended_enabled_ && !instant_enabled_) - return; - - // If the preview is showing custom NTP content, don't hide it, commit it - // (no matter where the user clicked) or try to recreate it. - if (model_.mode().is_ntp()) - return; - - // If the preview is not showing at all, recreate it if it's stale. - if (model_.mode().is_default()) { - OnStaleLoader(); - return; - } - - // The preview is showing search suggestions. If GetPreviewContents() is NULL, - // we are in the commit path. Don't do anything. - if (!GetPreviewContents()) - return; - -#if defined(OS_MACOSX) - // TODO(sreeram): See if Mac really needs this special treatment. - if (!loader_->is_pointer_down_from_activate()) - HideLoader(); -#else - if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), - loader_->contents())) - CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); - else - HideLoader(); -#endif -} - -void InstantController::OmniboxGotFocus() { - DVLOG(1) << "OmniboxGotFocus"; - is_omnibox_focused_ = true; +void InstantController::OmniboxFocusChanged( + OmniboxFocusState state, + OmniboxFocusChangeReason reason, + gfx::NativeView view_gaining_focus) { + DVLOG(1) << "OmniboxFocusChanged: " << omnibox_focus_state_ << " to " + << state << " for reason " << reason; + OmniboxFocusState old_focus_state = omnibox_focus_state_; + omnibox_focus_state_ = state; if (!extended_enabled_ && !instant_enabled_) return; - CreateDefaultLoader(); + // Tell the page if the key capture mode changed unless the focus state + // changed because of TYPING. This is because in that case, the browser hasn't + // really stopped capturing key strokes. + // + // (More practically, if we don't do this check, the page would receive + // onkeycapturechange before the corresponding onchange, and the page would + // have no way of telling whether the keycapturechange happened because of + // some actual user action or just because they started typing.) + if (extended_enabled_ && GetPreviewContents() && + reason != OMNIBOX_FOCUS_CHANGE_TYPING) + loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); + + // If focus went from outside the omnibox to the omnibox, preload the default + // search engine, in anticipation of the user typing a query. If the reverse + // happened, commit or discard the preview. + if (state != OMNIBOX_FOCUS_NONE && old_focus_state == OMNIBOX_FOCUS_NONE) + CreateDefaultLoader(); + else if (state == OMNIBOX_FOCUS_NONE && old_focus_state != OMNIBOX_FOCUS_NONE) + OmniboxLostFocus(view_gaining_focus); } void InstantController::SearchModeChanged( @@ -810,6 +797,36 @@ void InstantController::InstantLoaderAboutToNavigateMainFrame(const GURL& url) { CommitIfPossible(INSTANT_COMMIT_NAVIGATED); } +void InstantController::OmniboxLostFocus(gfx::NativeView view_gaining_focus) { + // If the preview is showing custom NTP content, don't hide it, commit it + // (no matter where the user clicked) or try to recreate it. + if (model_.mode().is_ntp()) + return; + + // If the preview is not showing at all, recreate it if it's stale. + if (model_.mode().is_default()) { + OnStaleLoader(); + return; + } + + // The preview is showing search suggestions. If GetPreviewContents() is NULL, + // we are in the commit path. Don't do anything. + if (!GetPreviewContents()) + return; + +#if defined(OS_MACOSX) + // TODO(sreeram): See if Mac really needs this special treatment. + if (!loader_->is_pointer_down_from_activate()) + HideLoader(); +#else + if (IsViewInContents(GetViewGainingFocus(view_gaining_focus), + loader_->contents())) + CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); + else + HideLoader(); +#endif +} + bool InstantController::ResetLoader(const TemplateURL* template_url, const content::WebContents* active_tab) { std::string instant_url; @@ -828,6 +845,7 @@ bool InstantController::ResetLoader(const TemplateURL* template_url, browser_->UpdateThemeInfoForPreview(); loader_->SetDisplayInstantResults(instant_enabled_); loader_->SearchModeChanged(search_mode_); + loader_->KeyCaptureChanged(omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); } // Restart the stale loader timer. @@ -855,7 +873,8 @@ void InstantController::OnStaleLoader() { // If the preview is showing or the omnibox has focus, don't delete the // loader. It will get refreshed the next time the preview is hidden or the // omnibox loses focus. - if (!stale_loader_timer_.IsRunning() && !is_omnibox_focused_ && + if (!stale_loader_timer_.IsRunning() && + omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) { loader_.reset(); CreateDefaultLoader(); diff --git a/chrome/browser/instant/instant_controller.h b/chrome/browser/instant/instant_controller.h index 0f37e86..452e434 100644 --- a/chrome/browser/instant/instant_controller.h +++ b/chrome/browser/instant/instant_controller.h @@ -17,6 +17,7 @@ #include "base/timer.h" #include "chrome/browser/instant/instant_commit_type.h" #include "chrome/browser/instant/instant_model.h" +#include "chrome/browser/ui/omnibox/omnibox_edit_model.h" #include "chrome/common/instant_types.h" #include "chrome/common/search_types.h" #include "content/public/common/page_transition_types.h" @@ -84,12 +85,12 @@ class InstantController { // CommitInstant() on the browser, and returns true. Else, returns false. bool CommitIfPossible(InstantCommitType type); - // The omnibox has lost focus. Commit or discard the preview accordingly. - void OmniboxLostFocus(gfx::NativeView view_gaining_focus); - - // The omnibox has gained focus. Preload the default search engine, in - // anticipation of the user typing a query. - void OmniboxGotFocus(); + // Called to indicate that the omnibox focus state changed with the given + // |reason|. If |focus_state| is FOCUS_NONE, |view_gaining_focus| is set to + // the view gaining focus. + void OmniboxFocusChanged(OmniboxFocusState focus_state, + OmniboxFocusChangeReason reason, + gfx::NativeView view_gaining_focus); // The search mode in the active tab has changed. Pass the message down to // the loader which will notify the renderer. Create |instant_tab_| if the @@ -153,6 +154,9 @@ class InstantController { FRIEND_TEST_ALL_PREFIXES(InstantTest, NonInstantSearchProvider); FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); + // Helper for OmniboxFocusChanged. Commit or discard the preview. + void OmniboxLostFocus(gfx::NativeView view_gaining_focus); + // Creates a new loader if necessary, using the instant_url property of the // |template_url| (for example, if the Instant URL has changed since the last // time the loader was created). Returns false if the |template_url| doesn't @@ -243,8 +247,8 @@ class InstantController { // Used to ensure that the preview page is committable. bool last_match_was_search_; - // True if the omnibox is focused, false otherwise. - bool is_omnibox_focused_; + // Omnibox focus state. + OmniboxFocusState omnibox_focus_state_; // The search model mode for the active tab. chrome::search::Mode search_mode_; diff --git a/chrome/browser/instant/instant_loader.cc b/chrome/browser/instant/instant_loader.cc index 7561ffd..06e61d0 100644 --- a/chrome/browser/instant/instant_loader.cc +++ b/chrome/browser/instant/instant_loader.cc @@ -274,6 +274,10 @@ void InstantLoader::SetDisplayInstantResults(bool display_instant_results) { client_.SetDisplayInstantResults(display_instant_results); } +void InstantLoader::KeyCaptureChanged(bool is_key_capture_enabled) { + client_.KeyCaptureChanged(is_key_capture_enabled); +} + void InstantLoader::SetSuggestions( const std::vector<InstantSuggestion>& suggestions) { InstantSupportDetermined(true); diff --git a/chrome/browser/instant/instant_loader.h b/chrome/browser/instant/instant_loader.h index 25257c7..23b6f06 100644 --- a/chrome/browser/instant/instant_loader.h +++ b/chrome/browser/instant/instant_loader.h @@ -101,6 +101,7 @@ class InstantLoader : public InstantClient::Delegate, void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); void SendThemeAreaHeight(int height); void SetDisplayInstantResults(bool display_instant_results); + void KeyCaptureChanged(bool is_key_capture_enabled); private: class WebContentsDelegateImpl; |