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

Side by Side Diff: chrome/browser/prerender/prerender_contents.cc

Issue 7004039: Cancel downloads earlier when prerendering. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Remove outdated comment Created 9 years, 7 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
« no previous file with comments | « chrome/browser/prerender/prerender_contents.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/prerender/prerender_contents.h" 5 #include "chrome/browser/prerender/prerender_contents.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/process_util.h" 10 #include "base/process_util.h"
11 #include "base/task.h" 11 #include "base/task.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/background_contents_service.h" 13 #include "chrome/browser/background_contents_service.h"
14 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/history/history_marshaling.h" 15 #include "chrome/browser/history/history_marshaling.h"
16 #include "chrome/browser/prerender/prerender_final_status.h" 16 #include "chrome/browser/prerender/prerender_final_status.h"
17 #include "chrome/browser/prerender/prerender_manager.h" 17 #include "chrome/browser/prerender/prerender_manager.h"
18 #include "chrome/browser/prerender/prerender_render_widget_host_view.h" 18 #include "chrome/browser/prerender/prerender_render_widget_host_view.h"
19 #include "chrome/browser/prerender/prerender_tracker.h" 19 #include "chrome/browser/prerender/prerender_tracker.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/renderer_preferences_util.h" 21 #include "chrome/browser/renderer_preferences_util.h"
22 #include "chrome/browser/ui/download/download_tab_helper.h"
22 #include "chrome/browser/ui/login/login_prompt.h" 23 #include "chrome/browser/ui/login/login_prompt.h"
23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 24 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
24 #include "chrome/common/extensions/extension_constants.h" 25 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/extensions/extension_messages.h" 26 #include "chrome/common/extensions/extension_messages.h"
26 #include "chrome/common/icon_messages.h" 27 #include "chrome/common/icon_messages.h"
27 #include "chrome/common/render_messages.h" 28 #include "chrome/common/render_messages.h"
28 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
29 #include "content/browser/browsing_instance.h" 30 #include "content/browser/browsing_instance.h"
30 #include "content/browser/renderer_host/render_view_host.h" 31 #include "content/browser/renderer_host/render_view_host.h"
31 #include "content/browser/renderer_host/resource_dispatcher_host.h" 32 #include "content/browser/renderer_host/resource_dispatcher_host.h"
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 DCHECK(!prerendering_has_started_); 247 DCHECK(!prerendering_has_started_);
247 DCHECK(prerender_contents_.get() == NULL); 248 DCHECK(prerender_contents_.get() == NULL);
248 DCHECK(source_render_view_host != NULL); 249 DCHECK(source_render_view_host != NULL);
249 DCHECK(source_render_view_host->view() != NULL); 250 DCHECK(source_render_view_host->view() != NULL);
250 251
251 prerendering_has_started_ = true; 252 prerendering_has_started_ = true;
252 TabContents* new_contents = new TabContents(profile_, NULL, MSG_ROUTING_NONE, 253 TabContents* new_contents = new TabContents(profile_, NULL, MSG_ROUTING_NONE,
253 NULL, NULL); 254 NULL, NULL);
254 prerender_contents_.reset(new TabContentsWrapper(new_contents)); 255 prerender_contents_.reset(new TabContentsWrapper(new_contents));
255 tab_contents_observer_registrar_.Observe(new_contents); 256 tab_contents_observer_registrar_.Observe(new_contents);
257 prerender_contents_->download_tab_helper()->set_delegate(this);
256 258
257 TabContents* source_tc = 259 TabContents* source_tc =
258 source_render_view_host->delegate()->GetAsTabContents(); 260 source_render_view_host->delegate()->GetAsTabContents();
259 if (source_tc) { 261 if (source_tc) {
260 // So that history merging will work, get the max page ID 262 // So that history merging will work, get the max page ID
261 // of the old page, and add a safety margin of 10 to it (for things 263 // of the old page, and add a safety margin of 10 to it (for things
262 // such as redirects). 264 // such as redirects).
263 int32 max_page_id = source_tc->GetMaxPageID(); 265 int32 max_page_id = source_tc->GetMaxPageID();
264 if (max_page_id != -1) { 266 if (max_page_id != -1) {
265 prerender_contents_->controller().set_max_restored_page_id( 267 prerender_contents_->controller().set_max_restored_page_id(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, 302 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
301 Source<Profile>(profile_)); 303 Source<Profile>(profile_));
302 304
303 // Register to cancel if Authentication is required. 305 // Register to cancel if Authentication is required.
304 notification_registrar_.Add(this, NotificationType::AUTH_NEEDED, 306 notification_registrar_.Add(this, NotificationType::AUTH_NEEDED,
305 NotificationService::AllSources()); 307 NotificationService::AllSources());
306 308
307 notification_registrar_.Add(this, NotificationType::AUTH_CANCELLED, 309 notification_registrar_.Add(this, NotificationType::AUTH_CANCELLED,
308 NotificationService::AllSources()); 310 NotificationService::AllSources());
309 311
310 // Register all responses to see if we should cancel.
311 notification_registrar_.Add(this, NotificationType::DOWNLOAD_INITIATED,
312 NotificationService::AllSources());
313
314 // Register to inform new RenderViews that we're prerendering. 312 // Register to inform new RenderViews that we're prerendering.
315 notification_registrar_.Add( 313 notification_registrar_.Add(
316 this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, 314 this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
317 Source<TabContents>(new_contents)); 315 Source<TabContents>(new_contents));
318 316
319 // Register for redirect notifications sourced from |this|. 317 // Register for redirect notifications sourced from |this|.
320 notification_registrar_.Add( 318 notification_registrar_.Add(
321 this, NotificationType::RESOURCE_RECEIVED_REDIRECT, 319 this, NotificationType::RESOURCE_RECEIVED_REDIRECT,
322 Source<RenderViewHostDelegate>(GetRenderViewHostDelegate())); 320 Source<RenderViewHostDelegate>(GetRenderViewHostDelegate()));
323 321
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 RecordFinalStatus(final_status_); 371 RecordFinalStatus(final_status_);
374 372
375 // Only delete the RenderViewHost if we own it. 373 // Only delete the RenderViewHost if we own it.
376 if (render_view_host_) 374 if (render_view_host_)
377 render_view_host_->Shutdown(); 375 render_view_host_->Shutdown();
378 376
379 if (child_id_ != -1 && route_id_ != -1) { 377 if (child_id_ != -1 && route_id_ != -1) {
380 PrerenderTracker::GetInstance()->OnPrerenderingFinished( 378 PrerenderTracker::GetInstance()->OnPrerenderingFinished(
381 child_id_, route_id_); 379 child_id_, route_id_);
382 } 380 }
381
382 // If we still have a TabContents, clean up anything we need to and then
383 // destroy it.
384 if (prerender_contents_.get())
385 delete ReleasePrerenderContents();
383 } 386 }
384 387
385 RenderViewHostDelegate::View* PrerenderContents::GetViewDelegate() { 388 RenderViewHostDelegate::View* PrerenderContents::GetViewDelegate() {
386 return this; 389 return this;
387 } 390 }
388 391
389 const GURL& PrerenderContents::GetURL() const { 392 const GURL& PrerenderContents::GetURL() const {
390 return url_; 393 return url_;
391 } 394 }
392 395
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 LoginHandler* handler = details_ptr->handler(); 458 LoginHandler* handler = details_ptr->handler();
456 DCHECK(handler != NULL); 459 DCHECK(handler != NULL);
457 RenderViewHostDelegate* delegate = handler->GetRenderViewHostDelegate(); 460 RenderViewHostDelegate* delegate = handler->GetRenderViewHostDelegate();
458 if (delegate == GetRenderViewHostDelegate()) { 461 if (delegate == GetRenderViewHostDelegate()) {
459 Destroy(FINAL_STATUS_AUTH_NEEDED); 462 Destroy(FINAL_STATUS_AUTH_NEEDED);
460 return; 463 return;
461 } 464 }
462 break; 465 break;
463 } 466 }
464 467
468 // TODO(mmenke): Once we get rid of the old RenderViewHost code, remove
469 // this.
465 case NotificationType::DOWNLOAD_INITIATED: { 470 case NotificationType::DOWNLOAD_INITIATED: {
466 // If the download is started from a RenderViewHost that we are 471 // If the download is started from a RenderViewHost that we are
467 // delegating, kill the prerender. This cancels any pending requests 472 // delegating, kill the prerender. This cancels any pending requests
468 // though the download never actually started thanks to the 473 // though the download never actually started thanks to the
469 // DownloadRequestLimiter. 474 // DownloadRequestLimiter.
470 DCHECK(NotificationService::NoDetails() == details); 475 DCHECK(NotificationService::NoDetails() == details);
471 RenderViewHost* render_view_host = Source<RenderViewHost>(source).ptr(); 476 RenderViewHost* render_view_host = Source<RenderViewHost>(source).ptr();
472 CHECK(render_view_host != NULL); 477 CHECK(render_view_host != NULL);
473 if (render_view_host->delegate() == GetRenderViewHostDelegate()) { 478 if (render_view_host->delegate() == GetRenderViewHostDelegate()) {
474 Destroy(FINAL_STATUS_DOWNLOAD); 479 Destroy(FINAL_STATUS_DOWNLOAD);
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 if (render_view_host_observer_.get()) 738 if (render_view_host_observer_.get())
734 render_view_host_observer_->set_prerender_contents(NULL); 739 render_view_host_observer_->set_prerender_contents(NULL);
735 } 740 }
736 741
737 void PrerenderContents::RendererUnresponsive(RenderViewHost* render_view_host, 742 void PrerenderContents::RendererUnresponsive(RenderViewHost* render_view_host,
738 bool is_during_unload) { 743 bool is_during_unload) {
739 DCHECK_EQ(render_view_host_, render_view_host); 744 DCHECK_EQ(render_view_host_, render_view_host);
740 Destroy(FINAL_STATUS_RENDERER_UNRESPONSIVE); 745 Destroy(FINAL_STATUS_RENDERER_UNRESPONSIVE);
741 } 746 }
742 747
748 bool PrerenderContents::CanDownload(int request_id) {
749 Destroy(FINAL_STATUS_DOWNLOAD);
750 // Cancel the download.
751 return false;
752 }
753
754 void PrerenderContents::OnStartDownload(DownloadItem* download,
755 TabContentsWrapper* tab) {
756 // Prerendered pages should never be able to download files.
757 NOTREACHED();
758 }
759
743 base::ProcessMetrics* PrerenderContents::MaybeGetProcessMetrics() { 760 base::ProcessMetrics* PrerenderContents::MaybeGetProcessMetrics() {
744 if (process_metrics_.get() == NULL) { 761 if (process_metrics_.get() == NULL) {
745 // If a PrenderContents hasn't started prerending, don't be fully formed. 762 // If a PrenderContents hasn't started prerending, don't be fully formed.
746 if (!render_view_host() || !render_view_host()->process()) 763 if (!render_view_host() || !render_view_host()->process())
747 return NULL; 764 return NULL;
748 base::ProcessHandle handle = render_view_host()->process()->GetHandle(); 765 base::ProcessHandle handle = render_view_host()->process()->GetHandle();
749 if (handle == base::kNullProcessHandle) 766 if (handle == base::kNullProcessHandle)
750 return NULL; 767 return NULL;
751 #if !defined(OS_MACOSX) 768 #if !defined(OS_MACOSX)
752 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(handle)); 769 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(handle));
(...skipping 16 matching lines...) Expand all
769 if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) { 786 if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
770 if (private_bytes > 787 if (private_bytes >
771 prerender_manager_->max_prerender_memory_mb() * 1024 * 1024) { 788 prerender_manager_->max_prerender_memory_mb() * 1024 * 1024) {
772 Destroy(FINAL_STATUS_MEMORY_LIMIT_EXCEEDED); 789 Destroy(FINAL_STATUS_MEMORY_LIMIT_EXCEEDED);
773 } 790 }
774 } 791 }
775 } 792 }
776 793
777 TabContentsWrapper* PrerenderContents::ReleasePrerenderContents() { 794 TabContentsWrapper* PrerenderContents::ReleasePrerenderContents() {
778 render_view_host_observer_.reset(); 795 render_view_host_observer_.reset();
796 prerender_contents_->download_tab_helper()->set_delegate(NULL);
779 return prerender_contents_.release(); 797 return prerender_contents_.release();
780 } 798 }
781 799
782 RenderViewHostDelegate* PrerenderContents::GetRenderViewHostDelegate() { 800 RenderViewHostDelegate* PrerenderContents::GetRenderViewHostDelegate() {
783 if (UseTabContents()) { 801 if (UseTabContents()) {
784 if (!prerender_contents_.get()) 802 if (!prerender_contents_.get())
785 return NULL; 803 return NULL;
786 return prerender_contents_->tab_contents(); 804 return prerender_contents_->tab_contents();
787 } else { 805 } else {
788 return this; 806 return this;
(...skipping 14 matching lines...) Expand all
803 } 821 }
804 return render_view_host_; 822 return render_view_host_;
805 } 823 }
806 824
807 void PrerenderContents::CommitHistory(TabContents* tc) { 825 void PrerenderContents::CommitHistory(TabContents* tc) {
808 if (tab_contents_delegate_.get()) 826 if (tab_contents_delegate_.get())
809 tab_contents_delegate_->CommitHistory(tc); 827 tab_contents_delegate_->CommitHistory(tc);
810 } 828 }
811 829
812 } // namespace prerender 830 } // namespace prerender
OLDNEW
« no previous file with comments | « chrome/browser/prerender/prerender_contents.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698