Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_ |
| 6 | #define CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_ |
| 7 | |
| 8 | #include <list> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/basictypes.h" |
| 14 | #include "base/gtest_prod_util.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "chrome/browser/ui/search/instant_page.h" |
| 18 | #include "chrome/common/instant_types.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 19 | #include "chrome/common/omnibox_focus_state.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 20 | #include "chrome/common/search_types.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 21 | #include "content/public/common/page_transition_types.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | #include "ui/base/window_open_disposition.h" |
| 23 | #include "ui/gfx/native_widget_types.h" |
| 24 | #include "ui/gfx/rect.h" |
| 25 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 26 | class BrowserInstantController; |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 27 | class GURL; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 28 | class InstantService; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 29 | class InstantTab; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 30 | class Profile; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 31 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | namespace content { |
| 33 | class WebContents; |
| 34 | } |
| 35 | |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 36 | namespace gfx { |
| 37 | class Rect; |
| 38 | } |
| 39 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 40 | // Macro used for logging debug events. |message| should be a std::string. |
| 41 | #define LOG_INSTANT_DEBUG_EVENT(controller, message) \ |
| 42 | controller->LogDebugEvent(message) |
| 43 | |
| 44 | // InstantController drives Chrome Instant, i.e., the browser implementation of |
| 45 | // the Embedded Search API (see http://dev.chromium.org/embeddedsearch). |
| 46 | // |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 47 | // In extended mode, InstantController maintains and coordinates an InstantTab |
| 48 | // instance of InstantPage. An InstantTab instance points to the currently |
| 49 | // active tab, if it supports the Embedded Search API. InstantTab is backed by a |
| 50 | // WebContents and it does not own that WebContents. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | // |
| 52 | // InstantController is owned by Browser via BrowserInstantController. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 53 | class InstantController : public InstantPage::Delegate { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 54 | public: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 55 | explicit InstantController(BrowserInstantController* browser); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 56 | virtual ~InstantController(); |
| 57 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 58 | // Sets the stored start-edge margin and width of the omnibox. |
| 59 | void SetOmniboxBounds(const gfx::Rect& bounds); |
| 60 | |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 61 | // Sends the current SearchProvider suggestion to the Instant page if any. |
| 62 | void SetSuggestionToPrefetch(const InstantSuggestion& suggestion); |
| 63 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 64 | // Notifies |instant_Tab_| to toggle voice search. |
| 65 | void ToggleVoiceSearch(); |
| 66 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 67 | // Called if the browser is navigating to a search URL for |search_terms| with |
| 68 | // search-term-replacement enabled. If |instant_tab_| can be used to process |
| 69 | // the search, this does so and returns true. Else, returns false. |
| 70 | bool SubmitQuery(const string16& search_terms); |
| 71 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | // Called to indicate that the omnibox focus state changed with the given |
| 73 | // |reason|. If |focus_state| is FOCUS_NONE, |view_gaining_focus| is set to |
| 74 | // the view gaining focus. |
| 75 | void OmniboxFocusChanged(OmniboxFocusState focus_state, |
| 76 | OmniboxFocusChangeReason reason, |
| 77 | gfx::NativeView view_gaining_focus); |
| 78 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 79 | // The search mode in the active tab has changed. Bind |instant_tab_| if the |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 80 | // |new_mode| reflects an Instant search results page. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 81 | void SearchModeChanged(const SearchMode& old_mode, |
| 82 | const SearchMode& new_mode); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 84 | // The user switched tabs. Bind |instant_tab_| if the newly active tab is an |
| 85 | // Instant search results page. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 86 | void ActiveTabChanged(); |
| 87 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 88 | // The user is about to switch tabs. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 89 | void TabDeactivated(content::WebContents* contents); |
| 90 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 91 | // Adds a new event to |debug_events_| and also DVLOG's it. Ensures that |
| 92 | // |debug_events_| doesn't get too large. |
| 93 | void LogDebugEvent(const std::string& info) const; |
| 94 | |
| 95 | // Resets list of debug events. |
| 96 | void ClearDebugEvents(); |
| 97 | |
| 98 | // See comments for |debug_events_| below. |
| 99 | const std::list<std::pair<int64, std::string> >& debug_events() { |
| 100 | return debug_events_; |
| 101 | } |
| 102 | |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 103 | // Gets the stored start-edge margin and width of the omnibox. |
| 104 | const gfx::Rect omnibox_bounds() { |
| 105 | return omnibox_bounds_; |
| 106 | } |
| 107 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 108 | // Used by BrowserInstantController to notify InstantController about the |
| 109 | // instant support change event for the active web contents. |
| 110 | void InstantSupportChanged(InstantSupportState instant_support); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 111 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 112 | protected: |
| 113 | // Accessors are made protected for testing purposes. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 114 | virtual InstantTab* instant_tab() const; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 115 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 116 | virtual Profile* profile() const; |
| 117 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | private: |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 119 | friend class InstantExtendedManualTest; |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 120 | friend class InstantTestBase; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 121 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 122 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ExtendedModeIsOn); |
| 123 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, MostVisited); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 124 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, NTPIsPreloaded); |
| 125 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPIsUsedInNewTab); |
| 126 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPIsUsedInSameTab); |
| 127 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPForWrongProvider); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 128 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPRenderProcessGone); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 129 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 130 | PreloadedNTPDoesntSupportInstant); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 131 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
| 132 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, UnrelatedSiteInstance); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 133 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, OnDefaultSearchProviderChanged); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 134 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest, |
| 135 | NTPReactsToNetworkChanges); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 136 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 137 | AcceptingURLSearchDoesNotNavigate); |
| 138 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, AcceptingJSSearchDoesNotRunJS); |
| 139 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 140 | ReloadSearchAfterBackReloadsCorrectQuery); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 141 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedFirstTabTest, |
| 142 | RedirectToLocalOnLoadFailure); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 143 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, KeyboardTogglesVoiceSearch); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 144 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, HomeButtonAffectsMargin); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 145 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, SearchReusesInstantTab); |
| 146 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 147 | SearchDoesntReuseInstantTabWithoutSupport); |
| 148 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 149 | TypedSearchURLDoesntReuseInstantTab); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 150 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, |
| 151 | DispatchMVChangeEventWhileNavigatingBackToNTP); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 152 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 153 | // Overridden from InstantPage::Delegate: |
| 154 | // TODO(shishir): We assume that the WebContent's current RenderViewHost is |
| 155 | // the RenderViewHost being created which is not always true. Fix this. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 156 | virtual void InstantSupportDetermined( |
| 157 | const content::WebContents* contents, |
| 158 | bool supports_instant) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 159 | virtual void InstantPageAboutToNavigateMainFrame( |
| 160 | const content::WebContents* contents, |
| 161 | const GURL& url) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 162 | virtual void NavigateToURL( |
| 163 | const content::WebContents* contents, |
| 164 | const GURL& url, |
| 165 | content::PageTransition transition, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 166 | WindowOpenDisposition disposition, |
| 167 | bool is_search_type) OVERRIDE; |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 168 | virtual void PasteIntoOmnibox(const content::WebContents* contents, |
| 169 | const string16& text) OVERRIDE; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 170 | virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 171 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 172 | // Helper function to navigate the given contents to the local fallback |
| 173 | // Instant URL and trim the history correctly. |
| 174 | void RedirectToLocalNTP(content::WebContents* contents); |
| 175 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 176 | // Helper for OmniboxFocusChanged. Commit or discard the overlay. |
| 177 | void OmniboxLostFocus(gfx::NativeView view_gaining_focus); |
| 178 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 179 | // If the active tab is an Instant search results page, sets |instant_tab_| to |
| 180 | // point to it. Else, deletes any existing |instant_tab_|. |
| 181 | void ResetInstantTab(); |
| 182 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 183 | // Sends theme info, omnibox bounds, etc. down to the Instant tab. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 184 | void UpdateInfoForInstantTab(); |
| 185 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 186 | // Returns whether input is in progress, i.e. if the omnibox has focus and the |
| 187 | // active tab is in mode SEARCH_SUGGESTIONS. |
| 188 | bool IsInputInProgress() const; |
| 189 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 190 | // Returns true if the local page is being used. |
| 191 | bool UsingLocalPage() const; |
| 192 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 193 | // Returns the InstantService for the browser profile. |
| 194 | InstantService* GetInstantService() const; |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 195 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 196 | BrowserInstantController* const browser_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 197 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 198 | // The instance of InstantPage maintained by InstantController. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 199 | scoped_ptr<InstantTab> instant_tab_; |
| 200 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 201 | // Omnibox focus state. |
| 202 | OmniboxFocusState omnibox_focus_state_; |
| 203 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 204 | // The reason for the most recent omnibox focus change. |
| 205 | OmniboxFocusChangeReason omnibox_focus_change_reason_; |
| 206 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 207 | // The search model mode for the active tab. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 208 | SearchMode search_mode_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 209 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 210 | // The start-edge margin and width of the omnibox, used by the page to align |
| 211 | // its suggestions with the omnibox. |
| 212 | gfx::Rect omnibox_bounds_; |
| 213 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 214 | // List of events and their timestamps, useful in debugging Instant behaviour. |
| 215 | mutable std::list<std::pair<int64, std::string> > debug_events_; |
| 216 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(InstantController); |
| 218 | }; |
| 219 | |
| 220 | #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_ |