blob: 845032c9365dfec559b002a7b8c38fd530d6e0bf [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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "chrome/browser/ui/search/instant_page.h"
18#include "chrome/common/instant_types.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010019#include "chrome/common/omnibox_focus_state.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000020#include "chrome/common/search_types.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010021#include "content/public/common/page_transition_types.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022#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)c2e0dbd2013-05-09 18:35:53 +010026class BrowserInstantController;
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010027class GURL;
Ben Murdocheb525c52013-07-10 11:40:50 +010028class InstantService;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029class InstantTab;
Ben Murdocheb525c52013-07-10 11:40:50 +010030class Profile;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000031
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032namespace content {
33class WebContents;
34}
35
Ben Murdochba5b9a62013-08-12 14:20:17 +010036namespace gfx {
37class Rect;
38}
39
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040// 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 Murdochca12bfa2013-07-23 11:17:05 +010047// 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)2a99a7e2013-03-28 15:31:22 +000051//
52// InstantController is owned by Browser via BrowserInstantController.
Ben Murdochca12bfa2013-07-23 11:17:05 +010053class InstantController : public InstantPage::Delegate {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000054 public:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010055 explicit InstantController(BrowserInstantController* browser);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056 virtual ~InstantController();
57
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058 // Sets the stored start-edge margin and width of the omnibox.
59 void SetOmniboxBounds(const gfx::Rect& bounds);
60
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010061 // Sends the current SearchProvider suggestion to the Instant page if any.
62 void SetSuggestionToPrefetch(const InstantSuggestion& suggestion);
63
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010064 // Notifies |instant_Tab_| to toggle voice search.
65 void ToggleVoiceSearch();
66
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010067 // 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)2a99a7e2013-03-28 15:31:22 +000072 // 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 Murdocheb525c52013-07-10 11:40:50 +010079 // The search mode in the active tab has changed. Bind |instant_tab_| if the
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080 // |new_mode| reflects an Instant search results page.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010081 void SearchModeChanged(const SearchMode& old_mode,
82 const SearchMode& new_mode);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083
Ben Murdocheb525c52013-07-10 11:40:50 +010084 // The user switched tabs. Bind |instant_tab_| if the newly active tab is an
85 // Instant search results page.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000086 void ActiveTabChanged();
87
Ben Murdocheb525c52013-07-10 11:40:50 +010088 // The user is about to switch tabs.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000089 void TabDeactivated(content::WebContents* contents);
90
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000091 // 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 Murdochba5b9a62013-08-12 14:20:17 +0100103 // Gets the stored start-edge margin and width of the omnibox.
104 const gfx::Rect omnibox_bounds() {
105 return omnibox_bounds_;
106 }
107
Ben Murdocheb525c52013-07-10 11:40:50 +0100108 // 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)2a99a7e2013-03-28 15:31:22 +0000111
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100112 protected:
113 // Accessors are made protected for testing purposes.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100114 virtual InstantTab* instant_tab() const;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100115
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100116 virtual Profile* profile() const;
117
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118 private:
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100119 friend class InstantExtendedManualTest;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100120 friend class InstantTestBase;
Ben Murdochca12bfa2013-07-23 11:17:05 +0100121
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000122 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ExtendedModeIsOn);
123 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, MostVisited);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000124 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 Murdoch7dbb3d52013-07-17 14:55:54 +0100128 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, PreloadedNTPRenderProcessGone);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100129 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
130 PreloadedNTPDoesntSupportInstant);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000131 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
132 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, UnrelatedSiteInstance);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100133 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, OnDefaultSearchProviderChanged);
Ben Murdocheb525c52013-07-10 11:40:50 +0100134 FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
135 NTPReactsToNetworkChanges);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100136 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
137 AcceptingURLSearchDoesNotNavigate);
138 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, AcceptingJSSearchDoesNotRunJS);
139 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
140 ReloadSearchAfterBackReloadsCorrectQuery);
Ben Murdocheb525c52013-07-10 11:40:50 +0100141 FRIEND_TEST_ALL_PREFIXES(InstantExtendedFirstTabTest,
142 RedirectToLocalOnLoadFailure);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100143 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, KeyboardTogglesVoiceSearch);
Ben Murdocheb525c52013-07-10 11:40:50 +0100144 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, HomeButtonAffectsMargin);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100145 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, SearchReusesInstantTab);
146 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
147 SearchDoesntReuseInstantTabWithoutSupport);
148 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
149 TypedSearchURLDoesntReuseInstantTab);
Ben Murdocheb525c52013-07-10 11:40:50 +0100150 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest,
151 DispatchMVChangeEventWhileNavigatingBackToNTP);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100152
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000153 // 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)2a99a7e2013-03-28 15:31:22 +0000156 virtual void InstantSupportDetermined(
157 const content::WebContents* contents,
158 bool supports_instant) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000159 virtual void InstantPageAboutToNavigateMainFrame(
160 const content::WebContents* contents,
161 const GURL& url) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000162 virtual void NavigateToURL(
163 const content::WebContents* contents,
164 const GURL& url,
165 content::PageTransition transition,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100166 WindowOpenDisposition disposition,
167 bool is_search_type) OVERRIDE;
Ben Murdochba5b9a62013-08-12 14:20:17 +0100168 virtual void PasteIntoOmnibox(const content::WebContents* contents,
169 const string16& text) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100170 virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000171
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100172 // 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)2a99a7e2013-03-28 15:31:22 +0000176 // Helper for OmniboxFocusChanged. Commit or discard the overlay.
177 void OmniboxLostFocus(gfx::NativeView view_gaining_focus);
178
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000179 // 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)4e180b62013-10-18 15:46:22 +0100183 // Sends theme info, omnibox bounds, etc. down to the Instant tab.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100184 void UpdateInfoForInstantTab();
185
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100186 // 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)c2e0dbd2013-05-09 18:35:53 +0100190 // Returns true if the local page is being used.
191 bool UsingLocalPage() const;
192
Ben Murdocheb525c52013-07-10 11:40:50 +0100193 // Returns the InstantService for the browser profile.
194 InstantService* GetInstantService() const;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100195
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100196 BrowserInstantController* const browser_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000197
Ben Murdochca12bfa2013-07-23 11:17:05 +0100198 // The instance of InstantPage maintained by InstantController.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000199 scoped_ptr<InstantTab> instant_tab_;
200
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000201 // Omnibox focus state.
202 OmniboxFocusState omnibox_focus_state_;
203
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100204 // The reason for the most recent omnibox focus change.
205 OmniboxFocusChangeReason omnibox_focus_change_reason_;
206
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000207 // The search model mode for the active tab.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100208 SearchMode search_mode_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000209
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000210 // 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)2a99a7e2013-03-28 15:31:22 +0000214 // 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)2a99a7e2013-03-28 15:31:22 +0000217 DISALLOW_COPY_AND_ASSIGN(InstantController);
218};
219
220#endif // CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_