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