OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/download/download_request_handle.h" |
| 6 |
| 7 #include "base/stringprintf.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/tab_contents/tab_util.h" |
| 10 #include "content/browser/browser_thread.h" |
| 11 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 12 #include "content/browser/tab_contents/tab_contents.h" |
| 13 |
| 14 // IO Thread indirections to resource dispatcher host. |
| 15 // Provided as targets for PostTask from within this object |
| 16 // only. |
| 17 static void ResourceDispatcherHostPauseRequest( |
| 18 ResourceDispatcherHost* rdh, |
| 19 int process_unique_id, |
| 20 int request_id, |
| 21 bool pause) { |
| 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 23 rdh->PauseRequest(process_unique_id, request_id, pause); |
| 24 } |
| 25 |
| 26 static void ResourceDispatcherHostCancelRequest( |
| 27 ResourceDispatcherHost* rdh, |
| 28 int process_unique_id, |
| 29 int request_id) { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 31 rdh->CancelRequest(process_unique_id, request_id, false); |
| 32 } |
| 33 |
| 34 DownloadRequestHandle::DownloadRequestHandle() |
| 35 : rdh_(NULL), |
| 36 child_id_(-1), |
| 37 render_view_id_(-1), |
| 38 request_id_(-1) { |
| 39 } |
| 40 |
| 41 DownloadRequestHandle::DownloadRequestHandle(ResourceDispatcherHost* rdh, |
| 42 int child_id, |
| 43 int render_view_id, |
| 44 int request_id) |
| 45 : rdh_(rdh), |
| 46 child_id_(child_id), |
| 47 render_view_id_(render_view_id), |
| 48 request_id_(request_id) { |
| 49 // ResourceDispatcherHost should not be null for non-default instances |
| 50 // of DownloadRequestHandle. |
| 51 DCHECK(rdh); |
| 52 } |
| 53 |
| 54 TabContents* DownloadRequestHandle::GetTabContents() const{ |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 56 return tab_util::GetTabContentsByID(child_id_, render_view_id_); |
| 57 } |
| 58 |
| 59 DownloadManager* DownloadRequestHandle::GetDownloadManager() const { |
| 60 TabContents* contents = GetTabContents(); |
| 61 if (!contents) |
| 62 return NULL; |
| 63 |
| 64 Profile* profile = contents->profile(); |
| 65 if (!profile) |
| 66 return NULL; |
| 67 |
| 68 return profile->GetDownloadManager(); |
| 69 } |
| 70 |
| 71 void DownloadRequestHandle::PauseRequest() { |
| 72 // The post is safe because ResourceDispatcherHost is guaranteed |
| 73 // to outlive the IO thread. |
| 74 if (rdh_) { |
| 75 BrowserThread::PostTask( |
| 76 BrowserThread::IO, FROM_HERE, |
| 77 NewRunnableFunction(&ResourceDispatcherHostPauseRequest, |
| 78 rdh_, child_id_, request_id_, true)); |
| 79 } |
| 80 } |
| 81 |
| 82 void DownloadRequestHandle::ResumeRequest() { |
| 83 // The post is safe because ResourceDispatcherHost is guaranteed |
| 84 // to outlive the IO thread. |
| 85 if (rdh_) { |
| 86 BrowserThread::PostTask( |
| 87 BrowserThread::IO, FROM_HERE, |
| 88 NewRunnableFunction(&ResourceDispatcherHostPauseRequest, |
| 89 rdh_, child_id_, request_id_, false)); |
| 90 } |
| 91 } |
| 92 |
| 93 void DownloadRequestHandle::CancelRequest() { |
| 94 // The post is safe because ResourceDispatcherHost is guaranteed |
| 95 // to outlive the IO thread. |
| 96 if (rdh_) { |
| 97 BrowserThread::PostTask( |
| 98 BrowserThread::IO, FROM_HERE, |
| 99 NewRunnableFunction(&ResourceDispatcherHostCancelRequest, |
| 100 rdh_, child_id_, request_id_)); |
| 101 } |
| 102 } |
| 103 |
| 104 std::string DownloadRequestHandle::DebugString() const { |
| 105 return base::StringPrintf("{" |
| 106 " child_id = %d" |
| 107 " render_view_id = %d" |
| 108 " request_id = %d" |
| 109 "}", |
| 110 child_id_, |
| 111 render_view_id_, |
| 112 request_id_); |
| 113 } |
OLD | NEW |