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 3 // found in the LICENSE file. 4 5 #include "content/browser/download/download_request_handle.h" 6 7 #include "base/bind.h" 8 #include "base/strings/stringprintf.h" 9 #include "content/browser/renderer_host/render_view_host_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h" 11 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_thread.h" 13 14 namespace content { 15 16 DownloadRequestHandle::~DownloadRequestHandle() { 17 } 18 19 DownloadRequestHandle::DownloadRequestHandle() 20 : child_id_(-1), 21 render_view_id_(-1), 22 request_id_(-1) { 23 } 24 25 DownloadRequestHandle::DownloadRequestHandle( 26 const base::WeakPtr<DownloadResourceHandler>& handler, 27 int child_id, 28 int render_view_id, 29 int request_id) 30 : handler_(handler), 31 child_id_(child_id), 32 render_view_id_(render_view_id), 33 request_id_(request_id) { 34 DCHECK(handler_.get()); 35 } 36 37 WebContents* DownloadRequestHandle::GetWebContents() const { 38 RenderViewHostImpl* render_view_host = 39 RenderViewHostImpl::FromID(child_id_, render_view_id_); 40 if (!render_view_host) 41 return NULL; 42 43 return render_view_host->GetDelegate()->GetAsWebContents(); 44 } 45 46 DownloadManager* DownloadRequestHandle::GetDownloadManager() const { 47 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID( 48 child_id_, render_view_id_); 49 if (rvh == NULL) 50 return NULL; 51 RenderProcessHost* rph = rvh->GetProcess(); 52 if (rph == NULL) 53 return NULL; 54 BrowserContext* context = rph->GetBrowserContext(); 55 if (context == NULL) 56 return NULL; 57 return BrowserContext::GetDownloadManager(context); 58 } 59 60 void DownloadRequestHandle::PauseRequest() const { 61 BrowserThread::PostTask( 62 BrowserThread::IO, FROM_HERE, 63 base::Bind(&DownloadResourceHandler::PauseRequest, handler_)); 64 } 65 66 void DownloadRequestHandle::ResumeRequest() const { 67 BrowserThread::PostTask( 68 BrowserThread::IO, FROM_HERE, 69 base::Bind(&DownloadResourceHandler::ResumeRequest, handler_)); 70 } 71 72 void DownloadRequestHandle::CancelRequest() const { 73 BrowserThread::PostTask( 74 BrowserThread::IO, FROM_HERE, 75 base::Bind(&DownloadResourceHandler::CancelRequest, handler_)); 76 } 77 78 std::string DownloadRequestHandle::DebugString() const { 79 return base::StringPrintf("{" 80 " child_id = %d" 81 " render_view_id = %d" 82 " request_id = %d" 83 "}", 84 child_id_, 85 render_view_id_, 86 request_id_); 87 } 88 89 } // namespace content 90