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_resource_handler.h" | 5 #include "content/browser/download/download_resource_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "content/browser/byte_stream.h" | 14 #include "content/browser/byte_stream.h" |
15 #include "content/browser/download/download_create_info.h" | 15 #include "content/browser/download/download_create_info.h" |
16 #include "content/browser/download/download_interrupt_reasons_impl.h" | 16 #include "content/browser/download/download_interrupt_reasons_impl.h" |
17 #include "content/browser/download/download_manager_impl.h" | 17 #include "content/browser/download/download_manager_impl.h" |
18 #include "content/browser/download/download_request_handle.h" | 18 #include "content/browser/download/download_request_handle.h" |
19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
20 #include "content/browser/loader/resource_request_info_impl.h" | 20 #include "content/browser/loader/resource_request_info_impl.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/download_interrupt_reasons.h" | 22 #include "content/public/browser/download_interrupt_reasons.h" |
23 #include "content/public/browser/navigation_entry.h" | 23 #include "content/public/browser/navigation_entry.h" |
| 24 #include "content/public/browser/render_frame_host.h" |
24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/resource_response.h" | 26 #include "content/public/common/resource_response.h" |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 | 29 |
29 struct DownloadResourceHandler::DownloadTabInfo { | 30 struct DownloadResourceHandler::DownloadTabInfo { |
30 GURL tab_url; | 31 GURL tab_url; |
31 GURL tab_referrer_url; | 32 GURL tab_referrer_url; |
32 }; | 33 }; |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // Static function in order to prevent any accidental accesses to | 37 // Static function in order to prevent any accidental accesses to |
37 // DownloadResourceHandler members from the UI thread. | 38 // DownloadResourceHandler members from the UI thread. |
38 static void StartOnUIThread( | 39 static void StartOnUIThread( |
39 std::unique_ptr<DownloadCreateInfo> info, | 40 std::unique_ptr<DownloadCreateInfo> info, |
40 std::unique_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info, | 41 std::unique_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info, |
41 std::unique_ptr<ByteStreamReader> stream, | 42 std::unique_ptr<ByteStreamReader> stream, |
| 43 int render_process_id, |
| 44 int render_frame_id, |
42 const DownloadUrlParameters::OnStartedCallback& started_cb) { | 45 const DownloadUrlParameters::OnStartedCallback& started_cb) { |
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
44 | 47 |
| 48 RenderFrameHost* frame_host = |
| 49 RenderFrameHost::FromID(render_process_id, render_frame_id); |
45 DownloadManager* download_manager = | 50 DownloadManager* download_manager = |
46 info->request_handle->GetDownloadManager(); | 51 info->request_handle->GetDownloadManager(); |
47 if (!download_manager) { | 52 if (!download_manager || !frame_host) { |
48 // NULL in unittests or if the page closed right after starting the | 53 // NULL in unittests or if the page closed right after starting the |
49 // download. | 54 // download. |
50 if (!started_cb.is_null()) | 55 if (!started_cb.is_null()) |
51 started_cb.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); | 56 started_cb.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
52 | 57 |
53 if (stream) | 58 if (stream) |
54 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, | 59 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
55 stream.release()); | 60 stream.release()); |
56 return; | 61 return; |
57 } | 62 } |
58 | 63 |
59 info->tab_url = tab_info->tab_url; | 64 info->tab_url = tab_info->tab_url; |
60 info->tab_referrer_url = tab_info->tab_referrer_url; | 65 info->tab_referrer_url = tab_info->tab_referrer_url; |
| 66 info->site_instance_url = frame_host->GetSiteInstance()->GetSiteURL(); |
61 | 67 |
62 download_manager->StartDownload(std::move(info), std::move(stream), | 68 download_manager->StartDownload(std::move(info), std::move(stream), |
63 started_cb); | 69 started_cb); |
64 } | 70 } |
65 | 71 |
66 void InitializeDownloadTabInfoOnUIThread( | 72 void InitializeDownloadTabInfoOnUIThread( |
67 const DownloadRequestHandle& request_handle, | 73 const DownloadRequestHandle& request_handle, |
68 DownloadResourceHandler::DownloadTabInfo* tab_info) { | 74 DownloadResourceHandler::DownloadTabInfo* tab_info) { |
69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 75 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
70 | 76 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 return; | 188 return; |
183 } | 189 } |
184 | 190 |
185 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | 191 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); |
186 create_info->has_user_gesture = request_info->HasUserGesture(); | 192 create_info->has_user_gesture = request_info->HasUserGesture(); |
187 create_info->transition_type = request_info->GetPageTransition(); | 193 create_info->transition_type = request_info->GetPageTransition(); |
188 | 194 |
189 create_info->request_handle.reset(new DownloadRequestHandle( | 195 create_info->request_handle.reset(new DownloadRequestHandle( |
190 AsWeakPtr(), request_info->GetWebContentsGetterForRequest())); | 196 AsWeakPtr(), request_info->GetWebContentsGetterForRequest())); |
191 | 197 |
| 198 int render_process_id = -1; |
| 199 int render_frame_id = -1; |
| 200 request_info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id); |
| 201 |
192 BrowserThread::PostTask( | 202 BrowserThread::PostTask( |
193 BrowserThread::UI, FROM_HERE, | 203 BrowserThread::UI, FROM_HERE, |
194 base::Bind(&StartOnUIThread, base::Passed(&create_info), | 204 base::Bind(&StartOnUIThread, base::Passed(&create_info), |
195 base::Passed(&tab_info_), base::Passed(&stream_reader), | 205 base::Passed(&tab_info_), base::Passed(&stream_reader), |
196 callback)); | 206 render_process_id, render_frame_id, callback)); |
197 } | 207 } |
198 | 208 |
199 void DownloadResourceHandler::OnReadyToRead() { | 209 void DownloadResourceHandler::OnReadyToRead() { |
200 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
201 controller()->Resume(); | 211 controller()->Resume(); |
202 } | 212 } |
203 | 213 |
204 void DownloadResourceHandler::CancelRequest() { | 214 void DownloadResourceHandler::CancelRequest() { |
205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 215 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
206 | 216 |
(...skipping 16 matching lines...) Expand all Loading... |
223 " }", | 233 " }", |
224 request() ? | 234 request() ? |
225 request()->url().spec().c_str() : | 235 request()->url().spec().c_str() : |
226 "<NULL request>", | 236 "<NULL request>", |
227 info->GetChildID(), | 237 info->GetChildID(), |
228 info->GetRequestID(), | 238 info->GetRequestID(), |
229 info->GetRouteID()); | 239 info->GetRouteID()); |
230 } | 240 } |
231 | 241 |
232 } // namespace content | 242 } // namespace content |
OLD | NEW |