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/search_types.h"
19 #include "ui/gfx/native_widget_types.h"
21 class BrowserInstantController
;
26 struct EmbeddedSearchRequestParams
;
32 // Macro used for logging debug events. |message| should be a std::string.
33 #define LOG_INSTANT_DEBUG_EVENT(controller, message) \
34 controller->LogDebugEvent(message)
36 // InstantController drives Chrome Instant, i.e., the browser implementation of
37 // the Embedded Search API (see http://dev.chromium.org/embeddedsearch).
39 // In extended mode, InstantController maintains and coordinates an InstantTab
40 // instance of InstantPage. An InstantTab instance points to the currently
41 // active tab, if it supports the Embedded Search API. InstantTab is backed by a
42 // WebContents and it does not own that WebContents.
44 // InstantController is owned by Browser via BrowserInstantController.
45 class InstantController
: public InstantPage::Delegate
{
47 explicit InstantController(BrowserInstantController
* browser
);
48 ~InstantController() override
;
50 // Called if the browser is navigating to a search URL for |search_terms| with
51 // search-term-replacement enabled. If |instant_tab_| can be used to process
52 // the search, this does so and returns true. Else, returns false.
53 bool SubmitQuery(const base::string16
& search_terms
,
54 const EmbeddedSearchRequestParams
& params
);
56 // The search mode in the active tab has changed. Bind |instant_tab_| if the
57 // |new_mode| reflects an Instant search results page.
58 void SearchModeChanged(const SearchMode
& old_mode
,
59 const SearchMode
& new_mode
);
61 // The user switched tabs. Bind |instant_tab_| if the newly active tab is an
62 // Instant search results page.
63 void ActiveTabChanged();
65 // The user is about to switch tabs.
66 void TabDeactivated(content::WebContents
* contents
);
68 // Adds a new event to |debug_events_| and also DVLOG's it. Ensures that
69 // |debug_events_| doesn't get too large.
70 void LogDebugEvent(const std::string
& info
) const;
72 // Resets list of debug events.
73 void ClearDebugEvents();
75 // See comments for |debug_events_| below.
76 const std::list
<std::pair
<int64
, std::string
> >& debug_events() {
80 // Used by BrowserInstantController to notify InstantController about the
81 // instant support change event for the active web contents.
82 void InstantSupportChanged(InstantSupportState instant_support
);
85 // Accessors are made protected for testing purposes.
86 virtual InstantTab
* instant_tab() const;
88 virtual Profile
* profile() const;
91 friend class InstantExtendedManualTest
;
92 friend class InstantTestBase
;
94 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ExtendedModeIsOn
);
95 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, MostVisited
);
96 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ProcessIsolation
);
97 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, UnrelatedSiteInstance
);
98 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, OnDefaultSearchProviderChanged
);
99 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
100 AcceptingURLSearchDoesNotNavigate
);
101 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, AcceptingJSSearchDoesNotRunJS
);
102 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
103 ReloadSearchAfterBackReloadsCorrectQuery
);
104 FRIEND_TEST_ALL_PREFIXES(InstantExtendedFirstTabTest
,
105 RedirectToLocalOnLoadFailure
);
106 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, KeyboardTogglesVoiceSearch
);
107 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, HomeButtonAffectsMargin
);
108 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, SearchReusesInstantTab
);
109 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
110 SearchDoesntReuseInstantTabWithoutSupport
);
111 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
112 TypedSearchURLDoesntReuseInstantTab
);
113 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
,
114 DispatchMVChangeEventWhileNavigatingBackToNTP
);
116 // Overridden from InstantPage::Delegate:
117 // TODO(shishir): We assume that the WebContent's current RenderViewHost is
118 // the RenderViewHost being created which is not always true. Fix this.
119 void InstantSupportDetermined(const content::WebContents
* contents
,
120 bool supports_instant
) override
;
121 void InstantPageAboutToNavigateMainFrame(const content::WebContents
* contents
,
122 const GURL
& url
) override
;
124 // Helper function to navigate the given contents to the local fallback
125 // Instant URL and trim the history correctly.
126 void RedirectToLocalNTP(content::WebContents
* contents
);
128 // Helper for OmniboxFocusChanged. Commit or discard the overlay.
129 void OmniboxLostFocus(gfx::NativeView view_gaining_focus
);
131 // If the active tab is an Instant search results page, sets |instant_tab_| to
132 // point to it. Else, deletes any existing |instant_tab_|.
133 void ResetInstantTab();
135 // Sends theme info, omnibox bounds, etc. down to the Instant tab.
136 void UpdateInfoForInstantTab();
138 // Returns the InstantService for the browser profile.
139 InstantService
* GetInstantService() const;
141 BrowserInstantController
* const browser_
;
143 // The instance of InstantPage maintained by InstantController.
144 scoped_ptr
<InstantTab
> instant_tab_
;
146 // The search model mode for the active tab.
147 SearchMode search_mode_
;
149 // List of events and their timestamps, useful in debugging Instant behaviour.
150 mutable std::list
<std::pair
<int64
, std::string
> > debug_events_
;
152 DISALLOW_COPY_AND_ASSIGN(InstantController
);
155 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_CONTROLLER_H_