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

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: '' 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 27 matching lines...) Expand all
121 const std::vector<InstantMostVisitedItem>& items) { 117 const std::vector<InstantMostVisitedItem>& items) {
122 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); 118 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
123 } 119 }
124 120
125 void InstantPage::ToggleVoiceSearch() { 121 void InstantPage::ToggleVoiceSearch() {
126 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id())); 122 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id()));
127 } 123 }
128 124
129 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) 125 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url)
130 : delegate_(delegate), 126 : delegate_(delegate),
131 instant_url_(instant_url), 127 instant_url_(instant_url) {
132 supports_instant_(false),
133 instant_support_determined_(false) {
134 } 128 }
135 129
136 void InstantPage::SetContents(content::WebContents* contents) { 130 void InstantPage::SetContents(content::WebContents* web_contents) {
137 Observe(contents); 131 if (contents())
132 GetSearchModel()->RemoveObserver(this);
133
134 Observe(web_contents);
135
136 if (!web_contents)
137 return;
138
139 SearchModel* model = GetSearchModel();
140 model->AddObserver(this);
141
142 // Already know whether the page supports instant.
143 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
144 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES);
138 } 145 }
139 146
140 bool InstantPage::ShouldProcessRenderViewCreated() { 147 bool InstantPage::ShouldProcessRenderViewCreated() {
141 return false; 148 return false;
142 } 149 }
143 150
144 bool InstantPage::ShouldProcessRenderViewGone() { 151 bool InstantPage::ShouldProcessRenderViewGone() {
145 return false; 152 return false;
146 } 153 }
147 154
(...skipping 27 matching lines...) Expand all
175 182
176 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { 183 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() {
177 return false; 184 return false;
178 } 185 }
179 186
180 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { 187 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) {
181 if (ShouldProcessRenderViewCreated()) 188 if (ShouldProcessRenderViewCreated())
182 delegate_->InstantPageRenderViewCreated(contents()); 189 delegate_->InstantPageRenderViewCreated(contents());
183 } 190 }
184 191
185 void InstantPage::DidFinishLoad(
186 int64 /* frame_id */,
187 const GURL& /* validated_url */,
188 bool is_main_frame,
189 content::RenderViewHost* /* render_view_host */) {
190 if (is_main_frame && !supports_instant_)
191 DetermineIfPageSupportsInstant();
192 }
193
194 bool InstantPage::OnMessageReceived(const IPC::Message& message) { 192 bool InstantPage::OnMessageReceived(const IPC::Message& message) {
195 bool handled = true; 193 bool handled = true;
196 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) 194 IPC_BEGIN_MESSAGE_MAP(InstantPage, message)
197 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) 195 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
198 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
199 OnInstantSupportDetermined)
200 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, 196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay,
201 OnShowInstantOverlay) 197 OnShowInstantOverlay)
202 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) 198 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox)
203 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, 199 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
204 OnSearchBoxNavigate); 200 OnSearchBoxNavigate);
205 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, 201 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
206 OnDeleteMostVisitedItem); 202 OnDeleteMostVisitedItem);
207 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, 203 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
208 OnUndoMostVisitedDeletion); 204 OnUndoMostVisitedDeletion);
209 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, 205 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 int64 /* frame_id */, 238 int64 /* frame_id */,
243 bool is_main_frame, 239 bool is_main_frame,
244 const GURL& /* validated_url */, 240 const GURL& /* validated_url */,
245 int /* error_code */, 241 int /* error_code */,
246 const string16& /* error_description */, 242 const string16& /* error_description */,
247 content::RenderViewHost* /* render_view_host */) { 243 content::RenderViewHost* /* render_view_host */) {
248 if (is_main_frame) 244 if (is_main_frame)
249 delegate_->InstantPageLoadFailed(contents()); 245 delegate_->InstantPageLoadFailed(contents());
250 } 246 }
251 247
248 void InstantPage::ModelChanged(const SearchModel::State& old_state,
249 const SearchModel::State& new_state) {
250 if (old_state.instant_support != new_state.instant_support)
251 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES);
252 }
253
254 SearchModel* InstantPage::GetSearchModel() {
255 return contents() ?
256 SearchTabHelper::FromWebContents(contents())->model() : NULL;
257 }
258
259 void InstantPage::SetSupportsInstant(bool supports_instant) {
samarth 2013/06/14 00:17:10 Call this InstantSupportDetermined?
kmadhusu 2013/06/17 16:49:19 Done.
260 delegate_->InstantSupportDetermined(contents(), supports_instant);
261
262 // If the page doesn't support Instant, stop listening to it.
263 if (!supports_instant)
264 SetContents(NULL);
265 }
266
252 void InstantPage::OnSetSuggestions( 267 void InstantPage::OnSetSuggestions(
253 int page_id, 268 int page_id,
254 const std::vector<InstantSuggestion>& suggestions) { 269 const std::vector<InstantSuggestion>& suggestions) {
255 if (!contents()->IsActiveEntry(page_id)) 270 if (!contents()->IsActiveEntry(page_id))
256 return; 271 return;
257 272
258 OnInstantSupportDetermined(page_id, true); 273 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
259 if (!ShouldProcessSetSuggestions()) 274 if (!ShouldProcessSetSuggestions())
260 return; 275 return;
261 276
262 delegate_->SetSuggestions(contents(), suggestions); 277 delegate_->SetSuggestions(contents(), suggestions);
263 } 278 }
264 279
265 void InstantPage::OnInstantSupportDetermined(int page_id,
266 bool supports_instant) {
267 if (!contents()->IsActiveEntry(page_id) || supports_instant_) {
268 // Nothing to do if the page already supports Instant.
269 return;
270 }
271
272 instant_support_determined_ = true;
273 supports_instant_ = supports_instant;
274 delegate_->InstantSupportDetermined(contents(), supports_instant);
275
276 // If the page doesn't support Instant, stop listening to it.
277 if (!supports_instant)
278 Observe(NULL);
279 }
280
281 void InstantPage::OnShowInstantOverlay(int page_id, 280 void InstantPage::OnShowInstantOverlay(int page_id,
282 int height, 281 int height,
283 InstantSizeUnits units) { 282 InstantSizeUnits units) {
284 if (!contents()->IsActiveEntry(page_id)) 283 if (!contents()->IsActiveEntry(page_id))
285 return; 284 return;
286 285
287 OnInstantSupportDetermined(page_id, true); 286 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
288 delegate_->LogDropdownShown(); 287 delegate_->LogDropdownShown();
289 if (!ShouldProcessShowInstantOverlay()) 288 if (!ShouldProcessShowInstantOverlay())
290 return; 289 return;
291 290
292 delegate_->ShowInstantOverlay(contents(), height, units); 291 delegate_->ShowInstantOverlay(contents(), height, units);
293 } 292 }
294 293
295 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { 294 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) {
296 if (!contents()->IsActiveEntry(page_id)) 295 if (!contents()->IsActiveEntry(page_id))
297 return; 296 return;
298 297
299 OnInstantSupportDetermined(page_id, true); 298 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
300 if (!ShouldProcessFocusOmnibox()) 299 if (!ShouldProcessFocusOmnibox())
301 return; 300 return;
302 301
303 delegate_->FocusOmnibox(contents(), state); 302 delegate_->FocusOmnibox(contents(), state);
304 } 303 }
305 304
306 void InstantPage::OnSearchBoxNavigate(int page_id, 305 void InstantPage::OnSearchBoxNavigate(int page_id,
307 const GURL& url, 306 const GURL& url,
308 content::PageTransition transition, 307 content::PageTransition transition,
309 WindowOpenDisposition disposition, 308 WindowOpenDisposition disposition,
310 bool is_search_type) { 309 bool is_search_type) {
311 if (!contents()->IsActiveEntry(page_id)) 310 if (!contents()->IsActiveEntry(page_id))
312 return; 311 return;
313 312
314 OnInstantSupportDetermined(page_id, true); 313 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
315 if (!ShouldProcessNavigateToURL()) 314 if (!ShouldProcessNavigateToURL())
316 return; 315 return;
317 316
318 delegate_->NavigateToURL( 317 delegate_->NavigateToURL(
319 contents(), url, transition, disposition, is_search_type); 318 contents(), url, transition, disposition, is_search_type);
320 } 319 }
321 320
322 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { 321 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) {
323 if (!contents()->IsActiveEntry(page_id)) 322 if (!contents()->IsActiveEntry(page_id))
324 return; 323 return;
325 324
326 OnInstantSupportDetermined(page_id, true); 325 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
327 if (!ShouldProcessDeleteMostVisitedItem()) 326 if (!ShouldProcessDeleteMostVisitedItem())
328 return; 327 return;
329 328
330 delegate_->DeleteMostVisitedItem(url); 329 delegate_->DeleteMostVisitedItem(url);
331 } 330 }
332 331
333 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { 332 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) {
334 if (!contents()->IsActiveEntry(page_id)) 333 if (!contents()->IsActiveEntry(page_id))
335 return; 334 return;
336 335
337 OnInstantSupportDetermined(page_id, true); 336 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
338 if (!ShouldProcessUndoMostVisitedDeletion()) 337 if (!ShouldProcessUndoMostVisitedDeletion())
339 return; 338 return;
340 339
341 delegate_->UndoMostVisitedDeletion(url); 340 delegate_->UndoMostVisitedDeletion(url);
342 } 341 }
343 342
344 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { 343 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) {
345 if (!contents()->IsActiveEntry(page_id)) 344 if (!contents()->IsActiveEntry(page_id))
346 return; 345 return;
347 346
348 OnInstantSupportDetermined(page_id, true); 347 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
349 if (!ShouldProcessUndoAllMostVisitedDeletions()) 348 if (!ShouldProcessUndoAllMostVisitedDeletions())
350 return; 349 return;
351 350
352 delegate_->UndoAllMostVisitedDeletions(); 351 delegate_->UndoAllMostVisitedDeletions();
353 } 352 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698