OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ |
6 #define CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ | 6 #define CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "base/timer.h" | 17 #include "base/timer.h" |
18 #include "chrome/browser/instant/instant_commit_type.h" | 18 #include "chrome/browser/instant/instant_commit_type.h" |
19 #include "chrome/browser/instant/instant_model.h" | 19 #include "chrome/browser/instant/instant_model.h" |
20 #include "chrome/common/instant_types.h" | 20 #include "chrome/common/instant_types.h" |
21 #include "chrome/common/search_types.h" | 21 #include "chrome/common/search_types.h" |
22 #include "content/public/common/page_transition_types.h" | 22 #include "content/public/common/page_transition_types.h" |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
24 #include "ui/gfx/native_widget_types.h" | 24 #include "ui/gfx/native_widget_types.h" |
25 #include "ui/gfx/rect.h" | 25 #include "ui/gfx/rect.h" |
26 | 26 |
27 struct AutocompleteMatch; | 27 struct AutocompleteMatch; |
28 class AutocompleteProvider; | 28 class AutocompleteProvider; |
29 class InstantLoader; | 29 class InstantLoader; |
30 class TabContents; | 30 class InstantTab; |
31 class TemplateURL; | 31 class TemplateURL; |
32 struct ThemeBackgroundInfo; | 32 |
33 | 33 |
34 namespace chrome { | 34 namespace chrome { |
35 class BrowserInstantController; | 35 class BrowserInstantController; |
36 } | 36 } |
37 | 37 |
| 38 namespace content { |
| 39 class WebContents; |
| 40 } |
| 41 |
38 // InstantController maintains a WebContents that is intended to give a preview | 42 // InstantController maintains a WebContents that is intended to give a preview |
39 // of search suggestions and results. InstantController is owned by Browser via | 43 // of search suggestions and results. InstantController is owned by Browser via |
40 // BrowserInstantController. | 44 // BrowserInstantController. |
41 class InstantController { | 45 class InstantController { |
42 public: | 46 public: |
43 InstantController(chrome::BrowserInstantController* browser, | 47 InstantController(chrome::BrowserInstantController* browser, |
44 bool extended_enabled); | 48 bool extended_enabled); |
45 ~InstantController(); | 49 ~InstantController(); |
46 | 50 |
47 // Invoked as the user types into the omnibox. |user_text| is what the user | 51 // Invoked as the user types into the omnibox. |user_text| is what the user |
(...skipping 14 matching lines...) Expand all Loading... |
62 | 66 |
63 // Send autocomplete results from |providers| to the preview page. | 67 // Send autocomplete results from |providers| to the preview page. |
64 void HandleAutocompleteResults( | 68 void HandleAutocompleteResults( |
65 const std::vector<AutocompleteProvider*>& providers); | 69 const std::vector<AutocompleteProvider*>& providers); |
66 | 70 |
67 // Called when the user presses up or down. |count| is a repeat count, | 71 // Called when the user presses up or down. |count| is a repeat count, |
68 // negative for moving up, positive for moving down. Returns true if Instant | 72 // negative for moving up, positive for moving down. Returns true if Instant |
69 // handled the key press. | 73 // handled the key press. |
70 bool OnUpOrDownKeyPressed(int count); | 74 bool OnUpOrDownKeyPressed(int count); |
71 | 75 |
72 // The preview TabContents. May be NULL if ReleasePreviewContents() has been | 76 // The preview WebContents. May be NULL if CommitIfCurrent() has been called, |
73 // called, with no subsequent successful call to Update(). InstantController | 77 // with no subsequent successful call to Update(). InstantController retains |
74 // retains ownership of the object. | 78 // ownership of the object. |
75 TabContents* GetPreviewContents() const; | 79 content::WebContents* GetPreviewContents() const; |
76 | 80 |
77 // Returns true if the Instant preview can be committed now. | 81 // Returns true if the Instant preview can be committed now. |
78 bool IsCurrent() const; | 82 bool IsCurrent() const; |
79 | 83 |
80 // If the preview is showing search results, commits the preview, calling | 84 // If the preview is showing search results, commits the preview, calling |
81 // CommitInstant() on the browser, and returns true. Else, returns false. | 85 // CommitInstant() on the browser, and returns true. Else, returns false. |
82 bool CommitIfCurrent(InstantCommitType type); | 86 bool CommitIfCurrent(InstantCommitType type); |
83 | 87 |
84 // The omnibox has lost focus. Commit or discard the preview accordingly. | 88 // The omnibox has lost focus. Commit or discard the preview accordingly. |
85 void OmniboxLostFocus(gfx::NativeView view_gaining_focus); | 89 void OmniboxLostFocus(gfx::NativeView view_gaining_focus); |
86 | 90 |
87 // The omnibox has gained focus. Preload the default search engine, in | 91 // The omnibox has gained focus. Preload the default search engine, in |
88 // anticipation of the user typing a query. | 92 // anticipation of the user typing a query. |
89 void OmniboxGotFocus(); | 93 void OmniboxGotFocus(); |
90 | 94 |
91 // The search mode in the active tab has changed. Pass the message down to | 95 // The search mode in the active tab has changed. Pass the message down to |
92 // the loader which will notify the renderer. | 96 // the loader which will notify the renderer. Create |instant_tab_| if the |
| 97 // |new_mode| reflects an Instant search results page. |
93 void SearchModeChanged(const chrome::search::Mode& old_mode, | 98 void SearchModeChanged(const chrome::search::Mode& old_mode, |
94 const chrome::search::Mode& new_mode); | 99 const chrome::search::Mode& new_mode); |
95 | 100 |
96 // The user switched tabs. Hide the preview if needed. | 101 // The user switched tabs. Hide the preview if needed. Create |instant_tab_| |
| 102 // if the newly active tab is an Instant search results page. |
97 void ActiveTabChanged(); | 103 void ActiveTabChanged(); |
98 | 104 |
99 // Sets whether Instant should show result previews. | 105 // Sets whether Instant should show result previews. |
100 void SetInstantEnabled(bool instant_enabled); | 106 void SetInstantEnabled(bool instant_enabled); |
101 | 107 |
102 // The theme has changed. Pass the message down to the loader which will | 108 // The theme has changed. Pass the message down to the loader which will |
103 // notify the renderer. | 109 // notify the renderer. |
104 void ThemeChanged(const ThemeBackgroundInfo& theme_info); | 110 void ThemeChanged(const ThemeBackgroundInfo& theme_info); |
105 | 111 |
106 // The theme area height has changed. Pass the message down to the loader | 112 // The theme area height has changed. Pass the message down to the loader |
107 // which will notify the renderer. | 113 // which will notify the renderer. |
108 void ThemeAreaHeightChanged(int height); | 114 void ThemeAreaHeightChanged(int height); |
109 | 115 |
110 // Returns the transition type of the last AutocompleteMatch passed to Update. | 116 // Returns the transition type of the last AutocompleteMatch passed to Update. |
111 content::PageTransition last_transition_type() const { | 117 content::PageTransition last_transition_type() const { |
112 return last_transition_type_; | 118 return last_transition_type_; |
113 } | 119 } |
114 | 120 |
115 const InstantModel* model() const { return &model_; } | 121 const InstantModel* model() const { return &model_; } |
116 | 122 |
117 // Invoked by InstantLoader when it has suggested text. | 123 // Invoked by the page when it has suggested text. |
118 void SetSuggestions(InstantLoader* loader, | 124 void SetSuggestions(const content::WebContents* contents, |
119 const std::vector<InstantSuggestion>& suggestions); | 125 const std::vector<InstantSuggestion>& suggestions); |
120 | 126 |
121 // Invoked by InstantLoader to commit the preview. | 127 // Invoked by the page when its support for the Instant API is determined. |
122 void CommitInstantLoader(InstantLoader* loader); | 128 void InstantSupportDetermined(const content::WebContents* contents, |
| 129 bool supports_instant); |
123 | 130 |
124 // Invoked by InstantLoader to request that the preview be shown. | 131 // Invoked by InstantLoader to request that the preview be shown. |
125 void ShowInstantPreview(InstantLoader* loader, | 132 void ShowInstantPreview(InstantShownReason reason, |
126 InstantShownReason reason, | |
127 int height, | 133 int height, |
128 InstantSizeUnits units); | 134 InstantSizeUnits units); |
129 | 135 |
130 // Invoked by InstantLoader when it has determined whether or not the page | 136 // Invoked by InstantLoader when it has swapped a different WebContents into |
131 // supports the Instant API. | |
132 void InstantSupportDetermined(InstantLoader* loader, bool supports_instant); | |
133 | |
134 // Invoked by InstantLoader when it has swapped a different TabContents into | |
135 // the preview, usually because a prerendered page was navigated to. | 137 // the preview, usually because a prerendered page was navigated to. |
136 void SwappedTabContents(InstantLoader* loader); | 138 void SwappedWebContents(); |
137 | 139 |
138 // Invoked by InstantLoader when the preview gains focus, usually due to the | 140 // Invoked by InstantLoader when the preview gains focus, usually due to the |
139 // user clicking on it. | 141 // user clicking on it. |
140 void InstantLoaderContentsFocused(InstantLoader* loader); | 142 void InstantLoaderContentsFocused(); |
141 | |
142 #if defined(UNIT_TEST) | |
143 // Accessors used only in tests. | |
144 InstantLoader* loader() const { return loader_.get(); } | |
145 #endif | |
146 | 143 |
147 private: | 144 private: |
| 145 FRIEND_TEST_ALL_PREFIXES(InstantTest, OmniboxFocusLoadsInstant); |
| 146 FRIEND_TEST_ALL_PREFIXES(InstantTest, NonInstantSearchProvider); |
148 FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); | 147 FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); |
149 | 148 |
150 // Creates a new loader if necessary, using the instant_url property of the | 149 // Creates a new loader if necessary, using the instant_url property of the |
151 // |template_url| (for example, if the Instant URL has changed since the last | 150 // |template_url| (for example, if the Instant URL has changed since the last |
152 // time the loader was created). Returns false if the |template_url| doesn't | 151 // time the loader was created). Returns false if the |template_url| doesn't |
153 // have a valid Instant URL; true otherwise. | 152 // have a valid Instant URL; true otherwise. |
154 bool ResetLoader(const TemplateURL* template_url, | 153 bool ResetLoader(const TemplateURL* template_url, |
155 const TabContents* active_tab); | 154 const content::WebContents* active_tab); |
156 | 155 |
157 // Ensures that the |loader_| uses the default Instant URL, recreating it if | 156 // Ensures that the |loader_| uses the default Instant URL, recreating it if |
158 // necessary, and returns true. Returns false if the Instant URL could not be | 157 // necessary, and returns true. Returns false if the Instant URL could not be |
159 // determined or the active tab is NULL (browser is shutting down). | 158 // determined or the active tab is NULL (browser is shutting down). |
160 bool CreateDefaultLoader(); | 159 bool CreateDefaultLoader(); |
161 | 160 |
162 // Called when the |loader_| might be stale. If it's actually stale, and the | 161 // Called when the |loader_| might be stale. If it's actually stale, and the |
163 // omnibox doesn't have focus, and the preview isn't showing, the |loader_| is | 162 // omnibox doesn't have focus, and the preview isn't showing, the |loader_| is |
164 // deleted and recreated. Else the refresh is skipped. | 163 // deleted and recreated. Else the refresh is skipped. |
165 void OnStaleLoader(); | 164 void OnStaleLoader(); |
166 | 165 |
167 // Destroys the |loader_| and its preview contents. | 166 // If the active tab is an Instant search results page, sets |instant_tab_| to |
168 void DeleteLoader(); | 167 // point to it. Else, deletes any existing |instant_tab_|. |
| 168 void ResetInstantTab(); |
169 | 169 |
170 // Hide the preview. If |clear_query| is true, clears query text and sends a | 170 // Hide the preview. Also sends an onchange event (with blank query) to the |
171 // an onchange event (with blank query) to the preview, telling it to clear | 171 // preview, telling it to clear out results for any old queries. |
172 // out results for any old queries. | 172 void Hide(); |
173 void Hide(bool clear_query); | |
174 | 173 |
175 // Counterpart to Hide(). Asks the |browser_| to display the preview with | 174 // Like Hide(), but doesn't call OnStaleLoader(). You'll almost always want |
176 // the given |height|. | 175 // Hide(). Use this only if you are going to call loader_.reset() afterwards. |
| 176 void HideInternal(); |
| 177 |
| 178 // Counterpart to Hide(). Asks the |browser_| to display the preview with the |
| 179 // given |height|. |
177 void Show(InstantShownReason reason, int height, InstantSizeUnits units); | 180 void Show(InstantShownReason reason, int height, InstantSizeUnits units); |
178 | 181 |
179 // Send the omnibox dropdown bounds to the page. | 182 // Send the omnibox dropdown bounds to the page. |
180 void SendBoundsToPage(); | 183 void SendBoundsToPage(); |
181 | 184 |
182 // If |template_url| is a valid TemplateURL for use with Instant, fills in | 185 // If |template_url| is a valid TemplateURL for use with Instant, fills in |
183 // |instant_url| and returns true; returns false otherwise. | 186 // |instant_url| and returns true; returns false otherwise. |
184 // Note: If the command-line switch kInstantURL is set, this method uses its | 187 // Note: If the command-line switch kInstantURL is set, this method uses its |
185 // value for |instant_url| and returns true without examining |template_url|. | 188 // value for |instant_url| and returns true without examining |template_url|. |
186 bool GetInstantURL(const TemplateURL* template_url, | 189 bool GetInstantURL(const TemplateURL* template_url, |
187 std::string* instant_url) const; | 190 std::string* instant_url) const; |
188 | 191 |
189 chrome::BrowserInstantController* const browser_; | 192 chrome::BrowserInstantController* const browser_; |
190 | 193 |
191 // Whether the extended API and regular API are enabled. If both are false, | 194 // Whether the extended API and regular API are enabled. If both are false, |
192 // Instant is effectively disabled. | 195 // Instant is effectively disabled. |
193 const bool extended_enabled_; | 196 const bool extended_enabled_; |
194 bool instant_enabled_; | 197 bool instant_enabled_; |
195 | 198 |
| 199 // The state of the preview page, i.e., the page owned by |loader_|. Ignored |
| 200 // if |instant_tab_| is in use. |
196 InstantModel model_; | 201 InstantModel model_; |
197 | 202 |
| 203 // The preview WebContents. |
198 scoped_ptr<InstantLoader> loader_; | 204 scoped_ptr<InstantLoader> loader_; |
199 | 205 |
| 206 // A committed WebContents that supports Instant. If non-NULL, messages will |
| 207 // be sent to this, instead of |loader_| (which will be hidden). |
| 208 scoped_ptr<InstantTab> instant_tab_; |
| 209 |
200 // The most recent user_text passed to Update(). | 210 // The most recent user_text passed to Update(). |
201 string16 last_user_text_; | 211 string16 last_user_text_; |
202 | 212 |
203 // The most recent full_text passed to Update(). | 213 // The most recent full_text passed to Update(). |
204 string16 last_full_text_; | 214 string16 last_full_text_; |
205 | 215 |
206 // The most recent verbatim passed to Update(). | 216 // The most recent verbatim passed to Update(). |
207 bool last_verbatim_; | 217 bool last_verbatim_; |
208 | 218 |
209 // The most recent suggestion received from the page, minus any prefix that | 219 // The most recent suggestion received from the page, minus any prefix that |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // Instant API (or we assume it does, since we haven't determined it doesn't). | 251 // Instant API (or we assume it does, since we haven't determined it doesn't). |
242 std::map<std::string, int> blacklisted_urls_; | 252 std::map<std::string, int> blacklisted_urls_; |
243 | 253 |
244 // Search terms extraction (for autocomplete history matches) doesn't work | 254 // Search terms extraction (for autocomplete history matches) doesn't work |
245 // on Instant URLs. So, whenever the user commits an Instant search, we add | 255 // on Instant URLs. So, whenever the user commits an Instant search, we add |
246 // an equivalent non-Instant search URL to history, so that the search shows | 256 // an equivalent non-Instant search URL to history, so that the search shows |
247 // up in autocomplete history matches. | 257 // up in autocomplete history matches. |
248 GURL url_for_history_; | 258 GURL url_for_history_; |
249 | 259 |
250 // The timestamp at which query editing began. This value is used when the | 260 // The timestamp at which query editing began. This value is used when the |
251 // first set of suggestions is processed and cleared when the overlay is | 261 // preview is showed and cleared when the preview is hidden. |
252 // hidden. | |
253 base::Time first_interaction_time_; | 262 base::Time first_interaction_time_; |
254 | 263 |
255 DISALLOW_COPY_AND_ASSIGN(InstantController); | 264 DISALLOW_COPY_AND_ASSIGN(InstantController); |
256 }; | 265 }; |
257 | 266 |
258 #endif // CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ | 267 #endif // CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ |
OLD | NEW |