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.
5 #include "content/browser/download/download_request_handle.h"
8 #include "base/command_line.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/browser/frame_host/navigator.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/common/content_switches.h"
20 DownloadRequestHandle::~DownloadRequestHandle() {
23 DownloadRequestHandle::DownloadRequestHandle()
27 frame_tree_node_id_(-1) {
30 DownloadRequestHandle::DownloadRequestHandle(
31 const base::WeakPtr
<DownloadResourceHandler
>& handler
,
35 int frame_tree_node_id
)
38 render_view_id_(render_view_id
),
39 request_id_(request_id
),
40 frame_tree_node_id_(frame_tree_node_id
) {
41 DCHECK(handler_
.get());
44 WebContents
* DownloadRequestHandle::GetWebContents() const {
45 // PlzNavigate: if a FrameTreeNodeID was provided, use it to return the
47 // TODO(davidben): This logic should be abstracted within the ResourceLoader
48 // stack. https://crbug.com/376003
49 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
50 switches::kEnableBrowserSideNavigation
)) {
51 FrameTreeNode
* frame_tree_node
=
52 FrameTreeNode::GloballyFindByID(frame_tree_node_id_
);
53 if (frame_tree_node
) {
54 return WebContentsImpl::FromFrameTreeNode(frame_tree_node
);
58 RenderViewHostImpl
* render_view_host
=
59 RenderViewHostImpl::FromID(child_id_
, render_view_id_
);
60 if (!render_view_host
)
63 return render_view_host
->GetDelegate()->GetAsWebContents();
66 DownloadManager
* DownloadRequestHandle::GetDownloadManager() const {
67 // PlzNavigate: if a FrameTreeNodeID was provided, use it to return the
69 // TODO(davidben): This logic should be abstracted within the ResourceLoader
70 // stack. https://crbug.com/376003
71 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
72 switches::kEnableBrowserSideNavigation
)) {
73 FrameTreeNode
* frame_tree_node
=
74 FrameTreeNode::GloballyFindByID(frame_tree_node_id_
);
75 if (frame_tree_node
) {
76 BrowserContext
* context
=
77 frame_tree_node
->navigator()->GetController()->GetBrowserContext();
79 return BrowserContext::GetDownloadManager(context
);
83 RenderViewHostImpl
* rvh
= RenderViewHostImpl::FromID(
84 child_id_
, render_view_id_
);
87 RenderProcessHost
* rph
= rvh
->GetProcess();
90 BrowserContext
* context
= rph
->GetBrowserContext();
93 return BrowserContext::GetDownloadManager(context
);
96 void DownloadRequestHandle::PauseRequest() const {
97 BrowserThread::PostTask(
98 BrowserThread::IO
, FROM_HERE
,
99 base::Bind(&DownloadResourceHandler::PauseRequest
, handler_
));
102 void DownloadRequestHandle::ResumeRequest() const {
103 BrowserThread::PostTask(
104 BrowserThread::IO
, FROM_HERE
,
105 base::Bind(&DownloadResourceHandler::ResumeRequest
, handler_
));
108 void DownloadRequestHandle::CancelRequest() const {
109 BrowserThread::PostTask(
110 BrowserThread::IO
, FROM_HERE
,
111 base::Bind(&DownloadResourceHandler::CancelRequest
, handler_
));
114 std::string
DownloadRequestHandle::DebugString() const {
115 return base::StringPrintf("{"
117 " render_view_id = %d"
125 } // namespace content