Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: chrome/browser/ui/search/instant_page.cc

Issue 16035020: Move instant support to SearchTabHelper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add missing unit_test Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "chrome/browser/ui/search/instant_page.h" 5 #include "chrome/browser/ui/search/instant_page.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/search/search.h"
9 #include "chrome/browser/ui/search/search_model.h"
10 #include "chrome/browser/ui/search/search_tab_helper.h"
8 #include "chrome/common/render_messages.h" 11 #include "chrome/common/render_messages.h"
9 #include "chrome/common/url_constants.h" 12 #include "chrome/common/url_constants.h"
10 #include "content/public/browser/navigation_controller.h" 13 #include "content/public/browser/navigation_controller.h"
11 #include "content/public/browser/navigation_details.h" 14 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/navigation_entry.h" 15 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/frame_navigate_params.h" 17 #include "content/public/common/frame_navigate_params.h"
15 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/font.h" 19 #include "ui/gfx/font.h"
17 20
18 InstantPage::Delegate::~Delegate() { 21 InstantPage::Delegate::~Delegate() {
19 } 22 }
20 23
21 InstantPage::~InstantPage() { 24 InstantPage::~InstantPage() {
25 if (contents())
26 GetSearchModel()->RemoveObserver(this);
22 } 27 }
23 28
24 bool InstantPage::supports_instant() const { 29 bool InstantPage::supports_instant() const {
25 return supports_instant_; 30 return contents() ?
31 SearchTabHelper::FromWebContents(contents())->SupportsInstant() : false;
26 } 32 }
27 33
28 bool InstantPage::IsLocal() const { 34 bool InstantPage::IsLocal() const {
29 return contents() && 35 return contents() &&
30 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || 36 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) ||
31 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); 37 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl));
32 } 38 }
33 39
34 void InstantPage::Update(const string16& text, 40 void InstantPage::Update(const string16& text,
35 size_t selection_start, 41 size_t selection_start,
(...skipping 28 matching lines...) Expand all
64 const gfx::Font& omnibox_font = 70 const gfx::Font& omnibox_font =
65 ui::ResourceBundle::GetSharedInstance().GetFont( 71 ui::ResourceBundle::GetSharedInstance().GetFont(
66 ui::ResourceBundle::MediumFont); 72 ui::ResourceBundle::MediumFont);
67 #endif 73 #endif
68 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); 74 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName());
69 size_t omnibox_font_size = omnibox_font.GetFontSize(); 75 size_t omnibox_font_size = omnibox_font.GetFontSize();
70 Send(new ChromeViewMsg_SearchBoxFontInformation( 76 Send(new ChromeViewMsg_SearchBoxFontInformation(
71 routing_id(), omnibox_font_name, omnibox_font_size)); 77 routing_id(), omnibox_font_name, omnibox_font_size));
72 } 78 }
73 79
74 void InstantPage::DetermineIfPageSupportsInstant() {
75 if (IsLocal()) {
76 // Local pages always support Instant. That's why we keep them around.
77 int page_id = contents()->GetController().GetActiveEntry()->GetPageID();
78 OnInstantSupportDetermined(page_id, true);
79 } else {
80 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
81 }
82 }
83
84 void InstantPage::SendAutocompleteResults( 80 void InstantPage::SendAutocompleteResults(
85 const std::vector<InstantAutocompleteResult>& results) { 81 const std::vector<InstantAutocompleteResult>& results) {
86 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); 82 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results));
87 } 83 }
88 84
89 void InstantPage::UpOrDownKeyPressed(int count) { 85 void InstantPage::UpOrDownKeyPressed(int count) {
90 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); 86 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count));
91 } 87 }
92 88
93 void InstantPage::EscKeyPressed() { 89 void InstantPage::EscKeyPressed() {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 const std::vector<InstantMostVisitedItem>& items) { 122 const std::vector<InstantMostVisitedItem>& items) {
127 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); 123 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
128 } 124 }
129 125
130 void InstantPage::ToggleVoiceSearch() { 126 void InstantPage::ToggleVoiceSearch() {
131 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id())); 127 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
132 } 128 }
133 129
134 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) 130 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url)
135 : delegate_(delegate), 131 : delegate_(delegate),
136 instant_url_(instant_url), 132 instant_url_(instant_url) {
137 supports_instant_(false),
138 instant_support_determined_(false) {
139 } 133 }
140 134
141 void InstantPage::SetContents(content::WebContents* contents) { 135 void InstantPage::SetContents(content::WebContents* web_contents) {
142 Observe(contents); 136 if (contents())
137 GetSearchModel()->RemoveObserver(this);
138
139 Observe(web_contents);
140
141 if (!web_contents)
142 return;
143
144 SearchModel* model = GetSearchModel();
145 model->AddObserver(this);
146
147 // Already know whether the page supports instant.
148 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
149 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES);
143 } 150 }
144 151
145 bool InstantPage::ShouldProcessRenderViewCreated() { 152 bool InstantPage::ShouldProcessRenderViewCreated() {
146 return false; 153 return false;
147 } 154 }
148 155
149 bool InstantPage::ShouldProcessRenderViewGone() { 156 bool InstantPage::ShouldProcessRenderViewGone() {
150 return false; 157 return false;
151 } 158 }
152 159
(...skipping 27 matching lines...) Expand all
180 187
181 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { 188 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() {
182 return false; 189 return false;
183 } 190 }
184 191
185 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { 192 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) {
186 if (ShouldProcessRenderViewCreated()) 193 if (ShouldProcessRenderViewCreated())
187 delegate_->InstantPageRenderViewCreated(contents()); 194 delegate_->InstantPageRenderViewCreated(contents());
188 } 195 }
189 196
190 void InstantPage::DidFinishLoad(
191 int64 /* frame_id */,
192 const GURL& /* validated_url */,
193 bool is_main_frame,
194 content::RenderViewHost* /* render_view_host */) {
195 if (is_main_frame && !supports_instant_)
196 DetermineIfPageSupportsInstant();
197 }
198
199 bool InstantPage::OnMessageReceived(const IPC::Message& message) { 197 bool InstantPage::OnMessageReceived(const IPC::Message& message) {
200 bool handled = true; 198 bool handled = true;
201 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) 199 IPC_BEGIN_MESSAGE_MAP(InstantPage, message)
202 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) 200 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
203 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
204 OnInstantSupportDetermined)
205 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, 201 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay,
206 OnShowInstantOverlay) 202 OnShowInstantOverlay)
207 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) 203 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox)
208 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, 204 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
209 OnSearchBoxNavigate); 205 OnSearchBoxNavigate);
210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, 206 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
211 OnDeleteMostVisitedItem); 207 OnDeleteMostVisitedItem);
212 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, 208 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
213 OnUndoMostVisitedDeletion); 209 OnUndoMostVisitedDeletion);
214 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, 210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 int64 /* frame_id */, 243 int64 /* frame_id */,
248 bool is_main_frame, 244 bool is_main_frame,
249 const GURL& /* validated_url */, 245 const GURL& /* validated_url */,
250 int /* error_code */, 246 int /* error_code */,
251 const string16& /* error_description */, 247 const string16& /* error_description */,
252 content::RenderViewHost* /* render_view_host */) { 248 content::RenderViewHost* /* render_view_host */) {
253 if (is_main_frame) 249 if (is_main_frame)
254 delegate_->InstantPageLoadFailed(contents()); 250 delegate_->InstantPageLoadFailed(contents());
255 } 251 }
256 252
253 void InstantPage::ModelChanged(const SearchModel::State& old_state,
254 const SearchModel::State& new_state) {
255 if (old_state.instant_support != new_state.instant_support)
256 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES);
257 }
258
259 SearchModel* InstantPage::GetSearchModel() {
260 return contents() ?
261 SearchTabHelper::FromWebContents(contents())->model() : NULL;
262 }
263
264 void InstantPage::InstantSupportDetermined(bool supports_instant) {
265 delegate_->InstantSupportDetermined(contents(), supports_instant);
266
267 // If the page doesn't support Instant, stop listening to it.
268 if (!supports_instant)
269 SetContents(NULL);
270 }
271
257 void InstantPage::OnSetSuggestions( 272 void InstantPage::OnSetSuggestions(
258 int page_id, 273 int page_id,
259 const std::vector<InstantSuggestion>& suggestions) { 274 const std::vector<InstantSuggestion>& suggestions) {
260 if (!contents()->IsActiveEntry(page_id)) 275 if (!contents()->IsActiveEntry(page_id))
261 return; 276 return;
262 277
263 OnInstantSupportDetermined(page_id, true); 278 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
264 if (!ShouldProcessSetSuggestions()) 279 if (!ShouldProcessSetSuggestions())
265 return; 280 return;
266 281
267 delegate_->SetSuggestions(contents(), suggestions); 282 delegate_->SetSuggestions(contents(), suggestions);
268 } 283 }
269 284
270 void InstantPage::OnInstantSupportDetermined(int page_id,
271 bool supports_instant) {
272 if (!contents()->IsActiveEntry(page_id) || supports_instant_) {
273 // Nothing to do if the page already supports Instant.
274 return;
275 }
276
277 instant_support_determined_ = true;
278 supports_instant_ = supports_instant;
279 delegate_->InstantSupportDetermined(contents(), supports_instant);
280
281 // If the page doesn't support Instant, stop listening to it.
282 if (!supports_instant)
283 Observe(NULL);
284 }
285
286 void InstantPage::OnShowInstantOverlay(int page_id, 285 void InstantPage::OnShowInstantOverlay(int page_id,
287 int height, 286 int height,
288 InstantSizeUnits units) { 287 InstantSizeUnits units) {
289 if (!contents()->IsActiveEntry(page_id)) 288 if (!contents()->IsActiveEntry(page_id))
290 return; 289 return;
291 290
292 OnInstantSupportDetermined(page_id, true); 291 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
293 delegate_->LogDropdownShown(); 292 delegate_->LogDropdownShown();
294 if (!ShouldProcessShowInstantOverlay()) 293 if (!ShouldProcessShowInstantOverlay())
295 return; 294 return;
296 295
297 delegate_->ShowInstantOverlay(contents(), height, units); 296 delegate_->ShowInstantOverlay(contents(), height, units);
298 } 297 }
299 298
300 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { 299 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) {
301 if (!contents()->IsActiveEntry(page_id)) 300 if (!contents()->IsActiveEntry(page_id))
302 return; 301 return;
303 302
304 OnInstantSupportDetermined(page_id, true); 303 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
305 if (!ShouldProcessFocusOmnibox()) 304 if (!ShouldProcessFocusOmnibox())
306 return; 305 return;
307 306
308 delegate_->FocusOmnibox(contents(), state); 307 delegate_->FocusOmnibox(contents(), state);
309 } 308 }
310 309
311 void InstantPage::OnSearchBoxNavigate(int page_id, 310 void InstantPage::OnSearchBoxNavigate(int page_id,
312 const GURL& url, 311 const GURL& url,
313 content::PageTransition transition, 312 content::PageTransition transition,
314 WindowOpenDisposition disposition, 313 WindowOpenDisposition disposition,
315 bool is_search_type) { 314 bool is_search_type) {
316 if (!contents()->IsActiveEntry(page_id)) 315 if (!contents()->IsActiveEntry(page_id))
317 return; 316 return;
318 317
319 OnInstantSupportDetermined(page_id, true); 318 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
320 if (!ShouldProcessNavigateToURL()) 319 if (!ShouldProcessNavigateToURL())
321 return; 320 return;
322 321
323 delegate_->NavigateToURL( 322 delegate_->NavigateToURL(
324 contents(), url, transition, disposition, is_search_type); 323 contents(), url, transition, disposition, is_search_type);
325 } 324 }
326 325
327 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { 326 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) {
328 if (!contents()->IsActiveEntry(page_id)) 327 if (!contents()->IsActiveEntry(page_id))
329 return; 328 return;
330 329
331 OnInstantSupportDetermined(page_id, true); 330 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
332 if (!ShouldProcessDeleteMostVisitedItem()) 331 if (!ShouldProcessDeleteMostVisitedItem())
333 return; 332 return;
334 333
335 delegate_->DeleteMostVisitedItem(url); 334 delegate_->DeleteMostVisitedItem(url);
336 } 335 }
337 336
338 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { 337 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) {
339 if (!contents()->IsActiveEntry(page_id)) 338 if (!contents()->IsActiveEntry(page_id))
340 return; 339 return;
341 340
342 OnInstantSupportDetermined(page_id, true); 341 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
343 if (!ShouldProcessUndoMostVisitedDeletion()) 342 if (!ShouldProcessUndoMostVisitedDeletion())
344 return; 343 return;
345 344
346 delegate_->UndoMostVisitedDeletion(url); 345 delegate_->UndoMostVisitedDeletion(url);
347 } 346 }
348 347
349 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { 348 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) {
350 if (!contents()->IsActiveEntry(page_id)) 349 if (!contents()->IsActiveEntry(page_id))
351 return; 350 return;
352 351
353 OnInstantSupportDetermined(page_id, true); 352 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
354 if (!ShouldProcessUndoAllMostVisitedDeletions()) 353 if (!ShouldProcessUndoAllMostVisitedDeletions())
355 return; 354 return;
356 355
357 delegate_->UndoAllMostVisitedDeletions(); 356 delegate_->UndoAllMostVisitedDeletions();
358 } 357 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698