OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "content/browser/download/download_request_handle.h" | 5 #include "content/browser/download/download_request_handle.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
10 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | |
11 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
12 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
14 | 13 |
15 using content::BrowserThread; | 14 using content::BrowserThread; |
16 using content::DownloadManager; | 15 using content::DownloadManager; |
17 using content::RenderViewHostImpl; | 16 using content::RenderViewHostImpl; |
18 using content::ResourceDispatcherHostImpl; | |
19 | 17 |
20 // IO Thread indirections to resource dispatcher host. | 18 DownloadRequestHandle::~DownloadRequestHandle() { |
21 // Provided as targets for PostTask from within this object | |
22 // only. | |
23 static void DoPauseRequest( | |
24 int process_unique_id, | |
25 int request_id, | |
26 bool pause) { | |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
28 ResourceDispatcherHostImpl::Get()->PauseRequest(process_unique_id, | |
29 request_id, | |
30 pause); | |
31 } | |
32 | |
33 static void DoCancelRequest( | |
34 int process_unique_id, | |
35 int request_id) { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
37 ResourceDispatcherHostImpl::Get()->CancelRequest(process_unique_id, | |
38 request_id, | |
39 false); | |
40 } | 19 } |
41 | 20 |
42 DownloadRequestHandle::DownloadRequestHandle() | 21 DownloadRequestHandle::DownloadRequestHandle() |
43 : child_id_(-1), | 22 : child_id_(-1), |
44 render_view_id_(-1), | 23 render_view_id_(-1), |
45 request_id_(-1) { | 24 request_id_(-1) { |
46 } | 25 } |
47 | 26 |
48 DownloadRequestHandle::DownloadRequestHandle(int child_id, | 27 DownloadRequestHandle::DownloadRequestHandle(DownloadResourceHandler* handler, |
| 28 int child_id, |
49 int render_view_id, | 29 int render_view_id, |
50 int request_id) | 30 int request_id) |
51 : child_id_(child_id), | 31 : handler_(handler), |
| 32 child_id_(child_id), |
52 render_view_id_(render_view_id), | 33 render_view_id_(render_view_id), |
53 request_id_(request_id) { | 34 request_id_(request_id) { |
54 // ResourceDispatcherHostImpl should not be null for non-default instances of | 35 DCHECK(handler_); |
55 // DownloadRequestHandle. | |
56 DCHECK(ResourceDispatcherHostImpl::Get()); | |
57 } | 36 } |
58 | 37 |
59 content::WebContents* DownloadRequestHandle::GetWebContents() const { | 38 content::WebContents* DownloadRequestHandle::GetWebContents() const { |
60 RenderViewHostImpl* render_view_host = | 39 RenderViewHostImpl* render_view_host = |
61 RenderViewHostImpl::FromID(child_id_, render_view_id_); | 40 RenderViewHostImpl::FromID(child_id_, render_view_id_); |
62 if (!render_view_host) | 41 if (!render_view_host) |
63 return NULL; | 42 return NULL; |
64 | 43 |
65 return render_view_host->GetDelegate()->GetAsWebContents(); | 44 return render_view_host->GetDelegate()->GetAsWebContents(); |
66 } | 45 } |
67 | 46 |
68 DownloadManager* DownloadRequestHandle::GetDownloadManager() const { | 47 DownloadManager* DownloadRequestHandle::GetDownloadManager() const { |
69 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID( | 48 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID( |
70 child_id_, render_view_id_); | 49 child_id_, render_view_id_); |
71 if (rvh == NULL) | 50 if (rvh == NULL) |
72 return NULL; | 51 return NULL; |
73 content::RenderProcessHost* rph = rvh->GetProcess(); | 52 content::RenderProcessHost* rph = rvh->GetProcess(); |
74 if (rph == NULL) | 53 if (rph == NULL) |
75 return NULL; | 54 return NULL; |
76 content::BrowserContext* context = rph->GetBrowserContext(); | 55 content::BrowserContext* context = rph->GetBrowserContext(); |
77 if (context == NULL) | 56 if (context == NULL) |
78 return NULL; | 57 return NULL; |
79 return context->GetDownloadManager(); | 58 return context->GetDownloadManager(); |
80 } | 59 } |
81 | 60 |
82 void DownloadRequestHandle::PauseRequest() const { | 61 void DownloadRequestHandle::PauseRequest() const { |
83 // The post is safe because ResourceDispatcherHostImpl is guaranteed | 62 if (handler_) { |
84 // to outlive the IO thread. | |
85 if (ResourceDispatcherHostImpl::Get()) { | |
86 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
87 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
88 base::Bind(&DoPauseRequest, child_id_, request_id_, true)); | 65 base::Bind(&DownloadResourceHandler::PauseRequest, handler_)); |
89 } | 66 } |
90 } | 67 } |
91 | 68 |
92 void DownloadRequestHandle::ResumeRequest() const { | 69 void DownloadRequestHandle::ResumeRequest() const { |
93 // The post is safe because ResourceDispatcherHostImpl is guaranteed | 70 if (handler_) { |
94 // to outlive the IO thread. | |
95 if (ResourceDispatcherHostImpl::Get()) { | |
96 BrowserThread::PostTask( | 71 BrowserThread::PostTask( |
97 BrowserThread::IO, FROM_HERE, | 72 BrowserThread::IO, FROM_HERE, |
98 base::Bind(&DoPauseRequest, child_id_, request_id_, false)); | 73 base::Bind(&DownloadResourceHandler::ResumeRequest, handler_)); |
99 } | 74 } |
100 } | 75 } |
101 | 76 |
102 void DownloadRequestHandle::CancelRequest() const { | 77 void DownloadRequestHandle::CancelRequest() const { |
103 // The post is safe because ResourceDispatcherHostImpl is guaranteed | 78 if (handler_) { |
104 // to outlive the IO thread. | |
105 if (ResourceDispatcherHostImpl::Get()) { | |
106 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
107 BrowserThread::IO, FROM_HERE, | 80 BrowserThread::IO, FROM_HERE, |
108 base::Bind(&DoCancelRequest, child_id_, request_id_)); | 81 base::Bind(&DownloadResourceHandler::CancelRequest, handler_)); |
109 } | 82 } |
110 } | 83 } |
111 | 84 |
112 std::string DownloadRequestHandle::DebugString() const { | 85 std::string DownloadRequestHandle::DebugString() const { |
113 return base::StringPrintf("{" | 86 return base::StringPrintf("{" |
114 " child_id = %d" | 87 " child_id = %d" |
115 " render_view_id = %d" | 88 " render_view_id = %d" |
116 " request_id = %d" | 89 " request_id = %d" |
117 "}", | 90 "}", |
118 child_id_, | 91 child_id_, |
119 render_view_id_, | 92 render_view_id_, |
120 request_id_); | 93 request_id_); |
121 } | 94 } |
OLD | NEW |