blob: 7964d8717df60a0b574c19a60ac152d6f3112bde [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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)2a99a7e2013-03-28 15:31:22 +00009#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)868fa2f2013-06-11 10:57:03 +010016#include "base/strings/string16.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010017#include "chrome/browser/search/instant_service_observer.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "chrome/browser/ui/search/instant_page.h"
19#include "chrome/common/instant_types.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010020#include "chrome/common/omnibox_focus_state.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000021#include "chrome/common/search_types.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010022#include "content/public/common/page_transition_types.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010023#include "net/base/network_change_notifier.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000024#include "ui/base/window_open_disposition.h"
25#include "ui/gfx/native_widget_types.h"
26#include "ui/gfx/rect.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010027#include "url/gurl.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010029class BrowserInstantController;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030class InstantNTP;
Ben Murdocheb525c52013-07-10 11:40:50 +010031class InstantService;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032class InstantTab;
Ben Murdocheb525c52013-07-10 11:40:50 +010033class Profile;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000034
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035namespace content {
36class WebContents;
37}
38
39// Macro used for logging debug events. |message| should be a std::string.
40#define LOG_INSTANT_DEBUG_EVENT(controller, message) \
41 controller->LogDebugEvent(message)
42
43// InstantController drives Chrome Instant, i.e., the browser implementation of
44// the Embedded Search API (see http://dev.chromium.org/embeddedsearch).
45//
Ben Murdocheb525c52013-07-10 11:40:50 +010046// In extended mode, InstantController maintains and coordinates two
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047// instances of InstantPage:
Ben Murdocheb525c52013-07-10 11:40:50 +010048// (1) An InstantNTP instance which is a preloaded search page that will be
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000049// swapped-in the next time the user navigates to the New Tab Page. It is
50// never shown to the user in an uncommitted state.
Ben Murdocheb525c52013-07-10 11:40:50 +010051// (2) An InstantTab instance which points to the currently active tab, if it
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000052// supports the Embedded Search API.
53//
Ben Murdocheb525c52013-07-10 11:40:50 +010054// Both are backed by a WebContents. InstantNTP owns its WebContents and
55// InstantTab does not.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056//
57// InstantController is owned by Browser via BrowserInstantController.
Ben Murdocheb525c52013-07-10 11:40:50 +010058class InstantController : public InstantPage::Delegate,
59 public InstantServiceObserver {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000060 public:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010061 InstantController(BrowserInstantController* browser,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062 bool extended_enabled);
63 virtual ~InstantController();
64
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000065 // Releases and returns the NTP WebContents. May be NULL. Loads a new
66 // WebContents for the NTP.
67 scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT;
68
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 // Sets the stored start-edge margin and width of the omnibox.
70 void SetOmniboxBounds(const gfx::Rect& bounds);
71
Ben Murdocheb525c52013-07-10 11:40:50 +010072 // Called when the default search provider changes. Resets InstantNTP.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010073 void OnDefaultSearchProviderChanged();
74
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010075 // Notifies |instant_Tab_| to toggle voice search.
76 void ToggleVoiceSearch();
77
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010078 // The ntp WebContents. May be NULL. InstantController retains ownership.
79 content::WebContents* GetNTPContents() const;
80
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010081 // Called if the browser is navigating to a search URL for |search_terms| with
82 // search-term-replacement enabled. If |instant_tab_| can be used to process
83 // the search, this does so and returns true. Else, returns false.
84 bool SubmitQuery(const string16& search_terms);
85
Ben Murdocheb525c52013-07-10 11:40:50 +010086 // If the network status changes, try to reset NTP and Overlay.
87 void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000088
89 // Called to indicate that the omnibox focus state changed with the given
90 // |reason|. If |focus_state| is FOCUS_NONE, |view_gaining_focus| is set to
91 // the view gaining focus.
92 void OmniboxFocusChanged(OmniboxFocusState focus_state,
93 OmniboxFocusChangeReason reason,
94 gfx::NativeView view_gaining_focus);
95
Ben Murdocheb525c52013-07-10 11:40:50 +010096 // The search mode in the active tab has changed. Bind |instant_tab_| if the
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000097 // |new_mode| reflects an Instant search results page.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010098 void SearchModeChanged(const SearchMode& old_mode,
99 const SearchMode& new_mode);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000100
Ben Murdocheb525c52013-07-10 11:40:50 +0100101 // The user switched tabs. Bind |instant_tab_| if the newly active tab is an
102 // Instant search results page.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000103 void ActiveTabChanged();
104
Ben Murdocheb525c52013-07-10 11:40:50 +0100105 // The user is about to switch tabs.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106 void TabDeactivated(content::WebContents* contents);
107
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108 // Adds a new event to |debug_events_| and also DVLOG's it. Ensures that
109 // |debug_events_| doesn't get too large.
110 void LogDebugEvent(const std::string& info) const;
111
112 // Resets list of debug events.
113 void ClearDebugEvents();
114
Ben Murdocheb525c52013-07-10 11:40:50 +0100115 // Loads a new NTP to replace |ntp_|.
116 void ReloadStaleNTP();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100117
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100118 // Returns the correct Instant URL to use from the following possibilities:
119 // o The default search engine's Instant URL
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100120 // o The local page (see GetLocalInstantURL())
121 // Returns empty string if no valid Instant URL is available (this is only
122 // possible in non-extended mode where we don't have a local page fall-back).
123 virtual std::string GetInstantURL() const;
124
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000125 // See comments for |debug_events_| below.
126 const std::list<std::pair<int64, std::string> >& debug_events() {
127 return debug_events_;
128 }
129
Ben Murdocheb525c52013-07-10 11:40:50 +0100130 // Used by BrowserInstantController to notify InstantController about the
131 // instant support change event for the active web contents.
132 void InstantSupportChanged(InstantSupportState instant_support);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000133
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100134 protected:
135 // Accessors are made protected for testing purposes.
136 virtual bool extended_enabled() const;
137
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100138 virtual InstantTab* instant_tab() const;
139 virtual InstantNTP* ntp() const;
140
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100141 virtual Profile* profile() const;
142
143 // Returns true if Javascript is enabled and false otherwise.
144 virtual bool IsJavascriptEnabled() const;
145
146 // Returns true if the browser is in startup.
147 virtual bool InStartup() const;
148
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000149 private:
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100150 friend class InstantExtendedManualTest;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100151 friend class InstantTestBase;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100152#define UNIT_F(test) FRIEND_TEST_ALL_PREFIXES(InstantControllerTest, test)
153 UNIT_F(DoesNotSwitchToLocalNTPIfOnCurrentNTP);
154 UNIT_F(DoesNotSwitchToLocalNTPIfOnLocalNTP);
155 UNIT_F(IsJavascriptEnabled);
156 UNIT_F(IsJavascriptEnabledChecksContentSettings);
157 UNIT_F(IsJavascriptEnabledChecksPrefs);
158 UNIT_F(PrefersRemoteNTPOnStartup);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100159 UNIT_F(SwitchesToLocalNTPIfJSDisabled);
160 UNIT_F(SwitchesToLocalNTPIfNoInstantSupport);
161 UNIT_F(SwitchesToLocalNTPIfNoNTPReady);
162 UNIT_F(SwitchesToLocalNTPIfPathBad);
163#undef UNIT_F
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000164 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ExtendedModeIsOn);
165 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, MostVisited);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000166 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, NTPIsPreloaded);
167 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPIsUsedInNewTab);
168 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPIsUsedInSameTab);
169 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPForWrongProvider);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100170 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPRenderProcessGone);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100171 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
172 PreloadedNTPDoesntSupportInstant);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000173 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
174 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, UnrelatedSiteInstance);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100175 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, OnDefaultSearchProviderChanged);
Ben Murdocheb525c52013-07-10 11:40:50 +0100176 FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
177 NTPReactsToNetworkChanges);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100178 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
179 AcceptingURLSearchDoesNotNavigate);
180 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, AcceptingJSSearchDoesNotRunJS);
181 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
182 ReloadSearchAfterBackReloadsCorrectQuery);
Ben Murdocheb525c52013-07-10 11:40:50 +0100183 FRIEND_TEST_ALL_PREFIXES(InstantExtendedFirstTabTest,
184 RedirectToLocalOnLoadFailure);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100185 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, KeyboardTogglesVoiceSearch);
Ben Murdocheb525c52013-07-10 11:40:50 +0100186 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, HomeButtonAffectsMargin);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100187 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, SearchReusesInstantTab);
188 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
189 SearchDoesntReuseInstantTabWithoutSupport);
190 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
191 TypedSearchURLDoesntReuseInstantTab);
Ben Murdocheb525c52013-07-10 11:40:50 +0100192 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
193 DispatchMVChangeEventWhileNavigatingBackToNTP);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100194
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000195 // Overridden from InstantPage::Delegate:
196 // TODO(shishir): We assume that the WebContent's current RenderViewHost is
197 // the RenderViewHost being created which is not always true. Fix this.
198 virtual void InstantPageRenderViewCreated(
199 const content::WebContents* contents) OVERRIDE;
200 virtual void InstantSupportDetermined(
201 const content::WebContents* contents,
202 bool supports_instant) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100203 virtual void InstantPageRenderProcessGone(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000204 const content::WebContents* contents) OVERRIDE;
205 virtual void InstantPageAboutToNavigateMainFrame(
206 const content::WebContents* contents,
207 const GURL& url) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100208 virtual void FocusOmnibox(const content::WebContents* contents,
209 OmniboxFocusState state) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000210 virtual void NavigateToURL(
211 const content::WebContents* contents,
212 const GURL& url,
213 content::PageTransition transition,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100214 WindowOpenDisposition disposition,
215 bool is_search_type) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100216 virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000217
Ben Murdocheb525c52013-07-10 11:40:50 +0100218 // Overridden from InstantServiceObserver:
219 virtual void ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) OVERRIDE;
220 virtual void MostVisitedItemsChanged(
221 const std::vector<InstantMostVisitedItem>& items) OVERRIDE;
222
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000223 // Invoked by the InstantLoader when the Instant page wants to delete a
224 // Most Visited item.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100225 virtual void DeleteMostVisitedItem(const GURL& url) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000226
227 // Invoked by the InstantLoader when the Instant page wants to undo a
228 // Most Visited deletion.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100229 virtual void UndoMostVisitedDeletion(const GURL& url) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000230
231 // Invoked by the InstantLoader when the Instant page wants to undo all
232 // Most Visited deletions.
233 virtual void UndoAllMostVisitedDeletions() OVERRIDE;
234
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100235 // Helper function to navigate the given contents to the local fallback
236 // Instant URL and trim the history correctly.
237 void RedirectToLocalNTP(content::WebContents* contents);
238
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000239 // Helper for OmniboxFocusChanged. Commit or discard the overlay.
240 void OmniboxLostFocus(gfx::NativeView view_gaining_focus);
241
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100242 // Returns the local Instant URL. (Just a convenience wrapper around
243 // chrome::GetLocalInstantURL.)
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100244 virtual std::string GetLocalInstantURL() const;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000245
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100246 // Returns true if |page| has an up-to-date Instant URL and supports Instant.
247 // Note that local URLs will not pass this check.
248 bool PageIsCurrent(const InstantPage* page) const;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000249
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100250 // Recreates |ntp_| using |instant_url|.
251 void ResetNTP(const std::string& instant_url);
252
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100253 // Returns true if we should switch to using the local NTP.
254 bool ShouldSwitchToLocalNTP() const;
255
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000256 // If the active tab is an Instant search results page, sets |instant_tab_| to
257 // point to it. Else, deletes any existing |instant_tab_|.
258 void ResetInstantTab();
259
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100260 // Sends theme info, omnibox bounds, font info, etc. down to the Instant tab.
261 void UpdateInfoForInstantTab();
262
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100263 // Returns whether input is in progress, i.e. if the omnibox has focus and the
264 // active tab is in mode SEARCH_SUGGESTIONS.
265 bool IsInputInProgress() const;
266
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100267 // Returns true if the local page is being used.
268 bool UsingLocalPage() const;
269
Ben Murdocheb525c52013-07-10 11:40:50 +0100270 // Returns the InstantService for the browser profile.
271 InstantService* GetInstantService() const;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100272
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100273 BrowserInstantController* const browser_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000274
275 // Whether the extended API and regular API are enabled. If both are false,
276 // Instant is effectively disabled.
277 const bool extended_enabled_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000278
Ben Murdocheb525c52013-07-10 11:40:50 +0100279 // The instances of InstantPage maintained by InstantController.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000280 scoped_ptr<InstantNTP> ntp_;
281 scoped_ptr<InstantTab> instant_tab_;
282
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000283 // Omnibox focus state.
284 OmniboxFocusState omnibox_focus_state_;
285
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100286 // The reason for the most recent omnibox focus change.
287 OmniboxFocusChangeReason omnibox_focus_change_reason_;
288
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000289 // The search model mode for the active tab.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100290 SearchMode search_mode_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000291
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000292 // The start-edge margin and width of the omnibox, used by the page to align
293 // its suggestions with the omnibox.
294 gfx::Rect omnibox_bounds_;
295
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000296 // List of events and their timestamps, useful in debugging Instant behaviour.
297 mutable std::list<std::pair<int64, std::string> > debug_events_;
298
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000299 DISALLOW_COPY_AND_ASSIGN(InstantController);
300};
301
302#endif // CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_