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.
5 #ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_
13 #include "base/basictypes.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string16.h"
17 #include "chrome/browser/ui/search/instant_page.h"
18 #include "chrome/common/instant_types.h"
19 #include "chrome/common/omnibox_focus_state.h"
20 #include "chrome/common/search_types.h"
21 #include "ui/gfx/native_widget_types.h"
23 class BrowserInstantController
;
33 // Macro used for logging debug events. |message| should be a std::string.
34 #define LOG_INSTANT_DEBUG_EVENT(controller, message) \
35 controller->LogDebugEvent(message)
37 // InstantController drives Chrome Instant, i.e., the browser implementation of
38 // the Embedded Search API (see http://dev.chromium.org/embeddedsearch).
40 // In extended mode, InstantController maintains and coordinates an InstantTab
41 // instance of InstantPage. An InstantTab instance points to the currently
42 // active tab, if it supports the Embedded Search API. InstantTab is backed by a
43 // WebContents and it does not own that WebContents.
45 // InstantController is owned by Browser via BrowserInstantController.
46 class InstantController
: public InstantPage::Delegate
{
48 explicit InstantController(BrowserInstantController
* browser
);
49 virtual ~InstantController();
51 // Sends the current SearchProvider suggestion to the Instant page if any.
52 void SetSuggestionToPrefetch(const InstantSuggestion
& suggestion
);
54 // Called if the browser is navigating to a search URL for |search_terms| with
55 // search-term-replacement enabled. If |instant_tab_| can be used to process
56 // the search, this does so and returns true. Else, returns false.
57 bool SubmitQuery(const base::string16
& search_terms
);
59 // Called to indicate that the omnibox focus state changed with the given
60 // |reason|. If |focus_state| is FOCUS_NONE, |view_gaining_focus| is set to
61 // the view gaining focus.
62 void OmniboxFocusChanged(OmniboxFocusState focus_state
,
63 OmniboxFocusChangeReason reason
,
64 gfx::NativeView view_gaining_focus
);
66 // The search mode in the active tab has changed. Bind |instant_tab_| if the
67 // |new_mode| reflects an Instant search results page.
68 void SearchModeChanged(const SearchMode
& old_mode
,
69 const SearchMode
& new_mode
);
71 // The user switched tabs. Bind |instant_tab_| if the newly active tab is an
72 // Instant search results page.
73 void ActiveTabChanged();
75 // The user is about to switch tabs.
76 void TabDeactivated(content::WebContents
* contents
);
78 // Adds a new event to |debug_events_| and also DVLOG's it. Ensures that
79 // |debug_events_| doesn't get too large.
80 void LogDebugEvent(const std::string
& info
) const;
82 // Resets list of debug events.
83 void ClearDebugEvents();
85 // See comments for |debug_events_| below.
86 const std::list
<std::pair
<int64
, std::string
> >& debug_events() {
90 // Used by BrowserInstantController to notify InstantController about the
91 // instant support change event for the active web contents.
92 void InstantSupportChanged(InstantSupportState instant_support
);
95 // Accessors are made protected for testing purposes.
96 virtual InstantTab
* instant_tab() const;
98 virtual Profile
* profile() const;
101 friend class InstantExtendedManualTest
;
102 friend class InstantTestBase
;
104 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ExtendedModeIsOn
);
105 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, MostVisited
);
106 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ProcessIsolation
);
107 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, UnrelatedSiteInstance
);
108 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, OnDefaultSearchProviderChanged
);
109 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
110 AcceptingURLSearchDoesNotNavigate
);
111 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, AcceptingJSSearchDoesNotRunJS
);
112 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
113 ReloadSearchAfterBackReloadsCorrectQuery
);
114 FRIEND_TEST_ALL_PREFIXES(InstantExtendedFirstTabTest
,
115 RedirectToLocalOnLoadFailure
);
116 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, KeyboardTogglesVoiceSearch
);
117 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, HomeButtonAffectsMargin
);
118 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, SearchReusesInstantTab
);
119 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
120 SearchDoesntReuseInstantTabWithoutSupport
);
121 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
122 TypedSearchURLDoesntReuseInstantTab
);
123 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
124 DispatchMVChangeEventWhileNavigatingBackToNTP
);
126 // Overridden from InstantPage::Delegate:
127 // TODO(shishir): We assume that the WebContent's current RenderViewHost is
128 // the RenderViewHost being created which is not always true. Fix this.
129 virtual void InstantSupportDetermined(
130 const content::WebContents
* contents
,
131 bool supports_instant
) OVERRIDE
;
132 virtual void InstantPageAboutToNavigateMainFrame(
133 const content::WebContents
* contents
,
134 const GURL
& url
) OVERRIDE
;
136 // Helper function to navigate the given contents to the local fallback
137 // Instant URL and trim the history correctly.
138 void RedirectToLocalNTP(content::WebContents
* contents
);
140 // Helper for OmniboxFocusChanged. Commit or discard the overlay.
141 void OmniboxLostFocus(gfx::NativeView view_gaining_focus
);
143 // If the active tab is an Instant search results page, sets |instant_tab_| to
144 // point to it. Else, deletes any existing |instant_tab_|.
145 void ResetInstantTab();
147 // Sends theme info, omnibox bounds, etc. down to the Instant tab.
148 void UpdateInfoForInstantTab();
150 // Returns whether input is in progress, i.e. if the omnibox has focus and the
151 // active tab is in mode SEARCH_SUGGESTIONS.
152 bool IsInputInProgress() const;
154 // Returns the InstantService for the browser profile.
155 InstantService
* GetInstantService() const;
157 BrowserInstantController
* const browser_
;
159 // The instance of InstantPage maintained by InstantController.
160 scoped_ptr
<InstantTab
> instant_tab_
;
162 // Omnibox focus state.
163 OmniboxFocusState omnibox_focus_state_
;
165 // The reason for the most recent omnibox focus change.
166 OmniboxFocusChangeReason omnibox_focus_change_reason_
;
168 // The search model mode for the active tab.
169 SearchMode search_mode_
;
171 // List of events and their timestamps, useful in debugging Instant behaviour.
172 mutable std::list
<std::pair
<int64
, std::string
> > debug_events_
;
174 DISALLOW_COPY_AND_ASSIGN(InstantController
);
177 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_