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; | |
33 | 32 |
34 namespace chrome { | 33 namespace chrome { |
35 class BrowserInstantController; | 34 class BrowserInstantController; |
36 } | 35 } |
37 | 36 |
| 37 namespace content { |
| 38 class WebContents; |
| 39 } |
| 40 |
38 // InstantController maintains a WebContents that is intended to give a preview | 41 // InstantController maintains a WebContents that is intended to give a preview |
39 // of search suggestions and results. InstantController is owned by Browser via | 42 // of search suggestions and results. InstantController is owned by Browser via |
40 // BrowserInstantController. | 43 // BrowserInstantController. |
41 class InstantController { | 44 class InstantController { |
42 public: | 45 public: |
43 InstantController(chrome::BrowserInstantController* browser, | 46 InstantController(chrome::BrowserInstantController* browser, |
44 bool extended_enabled); | 47 bool extended_enabled); |
45 ~InstantController(); | 48 ~InstantController(); |
46 | 49 |
47 // Invoked as the user types into the omnibox. |user_text| is what the user | 50 // Invoked as the user types into the omnibox. |user_text| is what the user |
48 // has typed. |full_text| is what the omnibox is showing. These may differ if | 51 // has typed. |full_text| is what the omnibox is showing. These may differ if |
49 // the user typed only some text, and the rest was inline autocompleted. If | 52 // the user typed only some text, and the rest was inline autocompleted. If |
50 // |verbatim| is true, search results are shown for the exact omnibox text, | 53 // |verbatim| is true, search results are shown for the exact omnibox text, |
51 // rather than the best guess as to what the user means. Returns true if the | 54 // rather than the best guess as to what the user means. Returns true if the |
52 // update is accepted (i.e., if |match| is a search rather than a URL). | 55 // update is accepted (i.e., if |match| is a search rather than a URL). |
53 bool Update(const AutocompleteMatch& match, | 56 bool Update(const AutocompleteMatch& match, |
54 const string16& user_text, | 57 const string16& user_text, |
55 const string16& full_text, | 58 const string16& full_text, |
| 59 size_t selection_start, |
| 60 size_t selection_end, |
56 bool verbatim, | 61 bool verbatim, |
57 bool user_input_in_progress, | 62 bool user_input_in_progress, |
58 bool omnibox_popup_is_open); | 63 bool omnibox_popup_is_open); |
59 | 64 |
60 // Sets the bounds of the omnibox dropdown, in screen coordinates. | 65 // Sets the bounds of the omnibox dropdown, in screen coordinates. |
61 void SetOmniboxBounds(const gfx::Rect& bounds); | 66 void SetOmniboxBounds(const gfx::Rect& bounds); |
62 | 67 |
63 // Send autocomplete results from |providers| to the preview page. | 68 // Send autocomplete results from |providers| to the preview page. |
64 void HandleAutocompleteResults( | 69 void HandleAutocompleteResults( |
65 const std::vector<AutocompleteProvider*>& providers); | 70 const std::vector<AutocompleteProvider*>& providers); |
66 | 71 |
67 // Called when the user presses up or down. |count| is a repeat count, | 72 // 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 | 73 // negative for moving up, positive for moving down. Returns true if Instant |
69 // handled the key press. | 74 // handled the key press. |
70 bool OnUpOrDownKeyPressed(int count); | 75 bool OnUpOrDownKeyPressed(int count); |
71 | 76 |
72 // The preview TabContents. May be NULL if ReleasePreviewContents() has been | 77 // The preview WebContents. May be NULL. InstantController retains ownership. |
73 // called, with no subsequent successful call to Update(). InstantController | 78 content::WebContents* GetPreviewContents() const; |
74 // retains ownership of the object. | |
75 TabContents* GetPreviewContents() const; | |
76 | 79 |
77 // Returns true if the Instant preview can be committed now. | 80 // Returns true if the Instant preview can be committed now. |
78 bool IsCurrent() const; | 81 bool IsCurrent() const; |
79 | 82 |
80 // If the preview is showing search results, commits the preview, calling | 83 // If the preview is showing search results, commits the preview, calling |
81 // CommitInstant() on the browser, and returns true. Else, returns false. | 84 // CommitInstant() on the browser, and returns true. Else, returns false. |
82 bool CommitIfCurrent(InstantCommitType type); | 85 bool CommitIfCurrent(InstantCommitType type); |
83 | 86 |
84 // The omnibox has lost focus. Commit or discard the preview accordingly. | 87 // The omnibox has lost focus. Commit or discard the preview accordingly. |
85 void OmniboxLostFocus(gfx::NativeView view_gaining_focus); | 88 void OmniboxLostFocus(gfx::NativeView view_gaining_focus); |
86 | 89 |
87 // The omnibox has gained focus. Preload the default search engine, in | 90 // The omnibox has gained focus. Preload the default search engine, in |
88 // anticipation of the user typing a query. | 91 // anticipation of the user typing a query. |
89 void OmniboxGotFocus(); | 92 void OmniboxGotFocus(); |
90 | 93 |
91 // The search mode in the active tab has changed. Pass the message down to | 94 // The search mode in the active tab has changed. Pass the message down to |
92 // the loader which will notify the renderer. | 95 // the loader which will notify the renderer. Create |instant_tab_| if the |
| 96 // |new_mode| reflects an Instant search results page. |
93 void SearchModeChanged(const chrome::search::Mode& old_mode, | 97 void SearchModeChanged(const chrome::search::Mode& old_mode, |
94 const chrome::search::Mode& new_mode); | 98 const chrome::search::Mode& new_mode); |
95 | 99 |
96 // The user switched tabs. Hide the preview if needed. | 100 // The user switched tabs. Hide the preview. Create |instant_tab_| if the |
| 101 // newly active tab is an Instant search results page. |
97 void ActiveTabChanged(); | 102 void ActiveTabChanged(); |
98 | 103 |
99 // Sets whether Instant should show result previews. | 104 // Sets whether Instant should show result previews. |
100 void SetInstantEnabled(bool instant_enabled); | 105 void SetInstantEnabled(bool instant_enabled); |
101 | 106 |
102 // The theme has changed. Pass the message down to the loader which will | 107 // The theme has changed. Pass the message to the preview page. |
103 // notify the renderer. | |
104 void ThemeChanged(const ThemeBackgroundInfo& theme_info); | 108 void ThemeChanged(const ThemeBackgroundInfo& theme_info); |
105 | 109 |
106 // The theme area height has changed. Pass the message down to the loader | 110 // The theme area height has changed. Pass the message to the preview page. |
107 // which will notify the renderer. | |
108 void ThemeAreaHeightChanged(int height); | 111 void ThemeAreaHeightChanged(int height); |
109 | 112 |
110 // Returns the transition type of the last AutocompleteMatch passed to Update. | 113 // Returns the transition type of the last AutocompleteMatch passed to Update. |
111 content::PageTransition last_transition_type() const { | 114 content::PageTransition last_transition_type() const { |
112 return last_transition_type_; | 115 return last_transition_type_; |
113 } | 116 } |
114 | 117 |
115 const InstantModel* model() const { return &model_; } | 118 const InstantModel* model() const { return &model_; } |
116 | 119 |
117 // Invoked by InstantLoader when it has suggested text. | 120 // Invoked by the page when it has suggested text. |
118 void SetSuggestions(InstantLoader* loader, | 121 void SetSuggestions(const content::WebContents* contents, |
119 const std::vector<InstantSuggestion>& suggestions); | 122 const std::vector<InstantSuggestion>& suggestions); |
120 | 123 |
121 // Invoked by InstantLoader to commit the preview. | 124 // Invoked by the page when its support for the Instant API is determined. |
122 void CommitInstantLoader(InstantLoader* loader); | 125 void InstantSupportDetermined(const content::WebContents* contents, |
| 126 bool supports_instant); |
123 | 127 |
124 // Invoked by InstantLoader to request that the preview be shown. | 128 // Invoked by InstantLoader to request that the preview be shown. |
125 void ShowInstantPreview(InstantLoader* loader, | 129 void ShowInstantPreview(InstantShownReason reason, |
126 InstantShownReason reason, | |
127 int height, | 130 int height, |
128 InstantSizeUnits units); | 131 InstantSizeUnits units); |
129 | 132 |
130 // Invoked by InstantLoader when it has determined whether or not the page | 133 // 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. | 134 // the preview, usually because a prerendered page was navigated to. |
136 void SwappedTabContents(InstantLoader* loader); | 135 void SwappedWebContents(); |
137 | 136 |
138 // Invoked by InstantLoader when the preview gains focus, usually due to the | 137 // Invoked by InstantLoader when the preview gains focus, usually due to the |
139 // user clicking on it. | 138 // user clicking on it. |
140 void InstantLoaderContentsFocused(InstantLoader* loader); | 139 void InstantLoaderContentsFocused(); |
141 | |
142 #if defined(UNIT_TEST) | |
143 // Accessors used only in tests. | |
144 InstantLoader* loader() const { return loader_.get(); } | |
145 #endif | |
146 | 140 |
147 private: | 141 private: |
| 142 FRIEND_TEST_ALL_PREFIXES(InstantTest, OmniboxFocusLoadsInstant); |
| 143 FRIEND_TEST_ALL_PREFIXES(InstantTest, NonInstantSearchProvider); |
148 FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); | 144 FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); |
149 | 145 |
150 // Creates a new loader if necessary, using the instant_url property of the | 146 // 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 | 147 // |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 | 148 // time the loader was created). Returns false if the |template_url| doesn't |
153 // have a valid Instant URL; true otherwise. | 149 // have a valid Instant URL; true otherwise. |
154 bool ResetLoader(const TemplateURL* template_url, | 150 bool ResetLoader(const TemplateURL* template_url, |
155 const TabContents* active_tab); | 151 const content::WebContents* active_tab); |
156 | 152 |
157 // Ensures that the |loader_| uses the default Instant URL, recreating it if | 153 // 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 | 154 // 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). | 155 // determined or the active tab is NULL (browser is shutting down). |
160 bool CreateDefaultLoader(); | 156 bool CreateDefaultLoader(); |
161 | 157 |
162 // Called when the |loader_| might be stale. If it's actually stale, and the | 158 // 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 | 159 // omnibox doesn't have focus, and the preview isn't showing, the |loader_| is |
164 // deleted and recreated. Else the refresh is skipped. | 160 // deleted and recreated. Else the refresh is skipped. |
165 void OnStaleLoader(); | 161 void OnStaleLoader(); |
166 | 162 |
167 // Destroys the |loader_| and its preview contents. | 163 // If the active tab is an Instant search results page, sets |instant_tab_| to |
168 void DeleteLoader(); | 164 // point to it. Else, deletes any existing |instant_tab_|. |
| 165 void ResetInstantTab(); |
169 | 166 |
170 // Hide the preview. If |clear_query| is true, clears query text and sends a | 167 // Called by Update() to ensure we have an Instant page that can process |
171 // an onchange event (with blank query) to the preview, telling it to clear | 168 // |match|. Returns true if we should continue with the Update(). |
172 // out results for any old queries. | 169 bool ResetLoaderForMatch(const AutocompleteMatch& match); |
173 void Hide(bool clear_query); | |
174 | 170 |
175 // Counterpart to Hide(). Asks the |browser_| to display the preview with | 171 // Hide the preview. Also sends an onchange event (with blank query) to the |
176 // the given |height|. | 172 // preview, telling it to clear out results for any old queries. |
177 void Show(InstantShownReason reason, int height, InstantSizeUnits units); | 173 void HideLoader(); |
| 174 |
| 175 // Like HideLoader(), but doesn't call OnStaleLoader(). Use HideLoader() |
| 176 // unless you are going to call loader_.reset() yourself subsequently. |
| 177 void HideInternal(); |
| 178 |
| 179 // Counterpart to HideLoader(). Asks the |browser_| to display the preview |
| 180 // with the given |height|. |
| 181 void ShowLoader(InstantShownReason reason, |
| 182 int height, |
| 183 InstantSizeUnits units); |
178 | 184 |
179 // Send the omnibox dropdown bounds to the page. | 185 // Send the omnibox dropdown bounds to the page. |
180 void SendBoundsToPage(); | 186 void SendBoundsToPage(); |
181 | 187 |
182 // If |template_url| is a valid TemplateURL for use with Instant, fills in | 188 // If |template_url| is a valid TemplateURL for use with Instant, fills in |
183 // |instant_url| and returns true; returns false otherwise. | 189 // |instant_url| and returns true; returns false otherwise. |
184 // Note: If the command-line switch kInstantURL is set, this method uses its | 190 // 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|. | 191 // value for |instant_url| and returns true without examining |template_url|. |
186 bool GetInstantURL(const TemplateURL* template_url, | 192 bool GetInstantURL(const TemplateURL* template_url, |
187 std::string* instant_url) const; | 193 std::string* instant_url) const; |
188 | 194 |
189 chrome::BrowserInstantController* const browser_; | 195 chrome::BrowserInstantController* const browser_; |
190 | 196 |
191 // Whether the extended API and regular API are enabled. If both are false, | 197 // Whether the extended API and regular API are enabled. If both are false, |
192 // Instant is effectively disabled. | 198 // Instant is effectively disabled. |
193 const bool extended_enabled_; | 199 const bool extended_enabled_; |
194 bool instant_enabled_; | 200 bool instant_enabled_; |
195 | 201 |
| 202 // The state of the preview page, i.e., the page owned by |loader_|. Ignored |
| 203 // if |instant_tab_| is in use. |
196 InstantModel model_; | 204 InstantModel model_; |
197 | 205 |
| 206 // The preview WebContents. |
198 scoped_ptr<InstantLoader> loader_; | 207 scoped_ptr<InstantLoader> loader_; |
199 | 208 |
200 // The most recent user_text passed to Update(). | 209 // A committed WebContents that supports Instant. If non-NULL, the |loader_| |
201 string16 last_user_text_; | 210 // is guaranteed to be hidden and messages will be sent to this instead. |
| 211 scoped_ptr<InstantTab> instant_tab_; |
202 | 212 |
203 // The most recent full_text passed to Update(). | 213 // The most recent full_text passed to Update(). If empty, we'll not accept |
204 string16 last_full_text_; | 214 // search suggestions from |loader_| or |instant_tab_|. |
| 215 string16 last_omnibox_text_; |
205 | 216 |
206 // The most recent verbatim passed to Update(). | 217 // True if the last Update() had an inline autocompletion. Used only to make |
| 218 // sure that we don't accidentally suggest gray text suggestion in that case. |
| 219 bool last_omnibox_text_has_inline_autocompletion_; |
| 220 |
| 221 // The most recent verbatim passed to Update(). Used only to ensure that we |
| 222 // don't accidentally suggest an inline autocompletion. |
207 bool last_verbatim_; | 223 bool last_verbatim_; |
208 | 224 |
209 // The most recent suggestion received from the page, minus any prefix that | 225 // The most recent suggestion received from the page, minus any prefix that |
210 // the user has typed. | 226 // the user has typed. |
211 InstantSuggestion last_suggestion_; | 227 InstantSuggestion last_suggestion_; |
212 | 228 |
213 // See comments on the getter above. | 229 // See comments on the getter above. |
214 content::PageTransition last_transition_type_; | 230 content::PageTransition last_transition_type_; |
215 | 231 |
216 // True if the last match passed to Update() was a search (versus a URL). | 232 // True if the last match passed to Update() was a search (versus a URL). |
| 233 // Used to ensure that the preview page is committable. |
217 bool last_match_was_search_; | 234 bool last_match_was_search_; |
218 | 235 |
219 // True if the omnibox is focused, false otherwise. | 236 // True if the omnibox is focused, false otherwise. |
220 bool is_omnibox_focused_; | 237 bool is_omnibox_focused_; |
221 | 238 |
222 // The search model mode for the active tab. | 239 // The search model mode for the active tab. |
223 chrome::search::Mode search_mode_; | 240 chrome::search::Mode search_mode_; |
224 | 241 |
225 // Current omnibox bounds. | 242 // Current omnibox bounds. |
226 gfx::Rect omnibox_bounds_; | 243 gfx::Rect omnibox_bounds_; |
(...skipping 11 matching lines...) Expand all Loading... |
238 // engine identified by Instant URL K didn't support the Instant API in each | 255 // engine identified by Instant URL K didn't support the Instant API in each |
239 // of the last N times that we loaded it. If an Instant URL isn't present in | 256 // of the last N times that we loaded it. If an Instant URL isn't present in |
240 // the map at all or has a value 0, it means that search engine supports the | 257 // the map at all or has a value 0, it means that search engine supports the |
241 // Instant API (or we assume it does, since we haven't determined it doesn't). | 258 // Instant API (or we assume it does, since we haven't determined it doesn't). |
242 std::map<std::string, int> blacklisted_urls_; | 259 std::map<std::string, int> blacklisted_urls_; |
243 | 260 |
244 // Search terms extraction (for autocomplete history matches) doesn't work | 261 // Search terms extraction (for autocomplete history matches) doesn't work |
245 // on Instant URLs. So, whenever the user commits an Instant search, we add | 262 // 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 | 263 // an equivalent non-Instant search URL to history, so that the search shows |
247 // up in autocomplete history matches. | 264 // up in autocomplete history matches. |
| 265 // TODO(sreeram): Remove when http://crbug.com/155373 is fixed. |
248 GURL url_for_history_; | 266 GURL url_for_history_; |
249 | 267 |
250 // The timestamp at which query editing began. This value is used when the | 268 // 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 | 269 // preview is showed and cleared when the preview is hidden. |
252 // hidden. | |
253 base::Time first_interaction_time_; | 270 base::Time first_interaction_time_; |
254 | 271 |
| 272 // Whether to allow the preview to show search suggestions. In general, the |
| 273 // preview is allowed to show search suggestions whenever |search_mode_| is |
| 274 // MODE_SEARCH_SUGGESTIONS, except in those cases where this is false. |
| 275 bool allow_preview_to_show_search_suggestions_; |
| 276 |
255 DISALLOW_COPY_AND_ASSIGN(InstantController); | 277 DISALLOW_COPY_AND_ASSIGN(InstantController); |
256 }; | 278 }; |
257 | 279 |
258 #endif // CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ | 280 #endif // CHROME_BROWSER_INSTANT_INSTANT_CONTROLLER_H_ |
OLD | NEW |