OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/http/http_response_headers.h" | 25 #include "net/http/http_response_headers.h" |
26 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
27 | 27 |
28 DownloadResourceHandler::DownloadResourceHandler( | 28 DownloadResourceHandler::DownloadResourceHandler( |
29 ResourceDispatcherHost* rdh, | 29 ResourceDispatcherHost* rdh, |
30 int render_process_host_id, | 30 int render_process_host_id, |
31 int render_view_id, | 31 int render_view_id, |
32 int request_id, | 32 int request_id, |
33 const GURL& url, | 33 const GURL& url, |
| 34 DownloadId dl_id, |
34 DownloadFileManager* download_file_manager, | 35 DownloadFileManager* download_file_manager, |
35 net::URLRequest* request, | 36 net::URLRequest* request, |
36 bool save_as, | 37 bool save_as, |
37 const DownloadResourceHandler::OnStartedCallback& started_cb, | 38 const DownloadResourceHandler::OnStartedCallback& started_cb, |
38 const DownloadSaveInfo& save_info) | 39 const DownloadSaveInfo& save_info) |
39 : download_id_(-1), | 40 : download_id_(dl_id), |
40 global_id_(render_process_host_id, request_id), | 41 global_id_(render_process_host_id, request_id), |
41 render_view_id_(render_view_id), | 42 render_view_id_(render_view_id), |
42 content_length_(0), | 43 content_length_(0), |
43 download_file_manager_(download_file_manager), | 44 download_file_manager_(download_file_manager), |
44 request_(request), | 45 request_(request), |
45 save_as_(save_as), | 46 save_as_(save_as), |
46 started_cb_(started_cb), | 47 started_cb_(started_cb), |
47 save_info_(save_info), | 48 save_info_(save_info), |
48 buffer_(new DownloadBuffer), | 49 buffer_(new DownloadBuffer), |
49 rdh_(rdh), | 50 rdh_(rdh), |
50 is_paused_(false) { | 51 is_paused_(false) { |
| 52 DCHECK(dl_id.IsValid()); |
51 download_stats::RecordDownloadCount(download_stats::UNTHROTTLED_COUNT); | 53 download_stats::RecordDownloadCount(download_stats::UNTHROTTLED_COUNT); |
52 } | 54 } |
53 | 55 |
54 bool DownloadResourceHandler::OnUploadProgress(int request_id, | 56 bool DownloadResourceHandler::OnUploadProgress(int request_id, |
55 uint64 position, | 57 uint64 position, |
56 uint64 size) { | 58 uint64 size) { |
57 return true; | 59 return true; |
58 } | 60 } |
59 | 61 |
60 // Not needed, as this event handler ought to be the final resource. | 62 // Not needed, as this event handler ought to be the final resource. |
61 bool DownloadResourceHandler::OnRequestRedirected(int request_id, | 63 bool DownloadResourceHandler::OnRequestRedirected(int request_id, |
62 const GURL& url, | 64 const GURL& url, |
63 ResourceResponse* response, | 65 ResourceResponse* response, |
64 bool* defer) { | 66 bool* defer) { |
65 return true; | 67 return true; |
66 } | 68 } |
67 | 69 |
68 // Send the download creation information to the download thread. | 70 // Send the download creation information to the download thread. |
69 bool DownloadResourceHandler::OnResponseStarted(int request_id, | 71 bool DownloadResourceHandler::OnResponseStarted(int request_id, |
70 ResourceResponse* response) { | 72 ResourceResponse* response) { |
| 73 DCHECK(download_id_.IsValid()); |
71 VLOG(20) << __FUNCTION__ << "()" << DebugString() | 74 VLOG(20) << __FUNCTION__ << "()" << DebugString() |
72 << " request_id = " << request_id; | 75 << " request_id = " << request_id; |
73 download_start_time_ = base::TimeTicks::Now(); | 76 download_start_time_ = base::TimeTicks::Now(); |
74 std::string content_disposition; | 77 std::string content_disposition; |
75 request_->GetResponseHeaderByName("content-disposition", | 78 request_->GetResponseHeaderByName("content-disposition", |
76 &content_disposition); | 79 &content_disposition); |
77 set_content_disposition(content_disposition); | 80 set_content_disposition(content_disposition); |
78 set_content_length(response->response_head.content_length); | 81 set_content_length(response->response_head.content_length); |
79 | 82 |
80 const ResourceDispatcherHostRequestInfo* request_info = | 83 const ResourceDispatcherHostRequestInfo* request_info = |
81 ResourceDispatcherHost::InfoForRequest(request_); | 84 ResourceDispatcherHost::InfoForRequest(request_); |
82 | 85 |
83 download_id_ = download_file_manager_->GetNextId(); | |
84 | |
85 // Deleted in DownloadManager. | 86 // Deleted in DownloadManager. |
86 DownloadCreateInfo* info = new DownloadCreateInfo(FilePath(), GURL(), | 87 DownloadCreateInfo* info = new DownloadCreateInfo(FilePath(), GURL(), |
87 base::Time::Now(), 0, content_length_, DownloadItem::IN_PROGRESS, | 88 base::Time::Now(), 0, content_length_, DownloadItem::IN_PROGRESS, |
88 download_id_, request_info->has_user_gesture(), | 89 download_id_.local(), request_info->has_user_gesture(), |
89 request_info->transition_type()); | 90 request_info->transition_type()); |
90 info->url_chain = request_->url_chain(); | 91 info->url_chain = request_->url_chain(); |
91 info->referrer_url = GURL(request_->referrer()); | 92 info->referrer_url = GURL(request_->referrer()); |
| 93 info->start_time = base::Time::Now(); |
| 94 info->received_bytes = 0; |
| 95 info->total_bytes = content_length_; |
| 96 info->state = DownloadItem::IN_PROGRESS; |
| 97 info->download_id = download_id_.local(); |
| 98 info->has_user_gesture = request_info->has_user_gesture(); |
92 info->request_handle = DownloadRequestHandle(rdh_, | 99 info->request_handle = DownloadRequestHandle(rdh_, |
93 global_id_.child_id, | 100 global_id_.child_id, |
94 render_view_id_, | 101 render_view_id_, |
95 global_id_.request_id); | 102 global_id_.request_id); |
96 info->content_disposition = content_disposition_; | 103 info->content_disposition = content_disposition_; |
97 info->mime_type = response->response_head.mime_type; | 104 info->mime_type = response->response_head.mime_type; |
98 download_stats::RecordDownloadMimeType(info->mime_type); | 105 download_stats::RecordDownloadMimeType(info->mime_type); |
99 // TODO(ahendrickson) -- Get the last modified time and etag, so we can | 106 // TODO(ahendrickson) -- Get the last modified time and etag, so we can |
100 // resume downloading. | 107 // resume downloading. |
101 | 108 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 bool DownloadResourceHandler::OnResponseCompleted( | 189 bool DownloadResourceHandler::OnResponseCompleted( |
183 int request_id, | 190 int request_id, |
184 const net::URLRequestStatus& status, | 191 const net::URLRequestStatus& status, |
185 const std::string& security_info) { | 192 const std::string& security_info) { |
186 VLOG(20) << __FUNCTION__ << "()" << DebugString() | 193 VLOG(20) << __FUNCTION__ << "()" << DebugString() |
187 << " request_id = " << request_id | 194 << " request_id = " << request_id |
188 << " status.status() = " << status.status() | 195 << " status.status() = " << status.status() |
189 << " status.error() = " << status.error(); | 196 << " status.error() = " << status.error(); |
190 net::Error error_code = (status.status() == net::URLRequestStatus::FAILED) ? | 197 net::Error error_code = (status.status() == net::URLRequestStatus::FAILED) ? |
191 static_cast<net::Error>(status.error()) : net::OK; | 198 static_cast<net::Error>(status.error()) : net::OK; |
192 if (download_id_ == -1) | 199 if (!download_id_.IsValid()) |
193 CallStartedCB(error_code); | 200 CallStartedCB(error_code); |
194 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, | 201 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, |
195 // so that any functions queued up on the FILE thread are executed | 202 // so that any functions queued up on the FILE thread are executed |
196 // before deletion. | 203 // before deletion. |
197 BrowserThread::PostTask( | 204 BrowserThread::PostTask( |
198 BrowserThread::FILE, FROM_HERE, | 205 BrowserThread::FILE, FROM_HERE, |
199 NewRunnableMethod(download_file_manager_, | 206 NewRunnableMethod(download_file_manager_, |
200 &DownloadFileManager::OnResponseCompleted, | 207 &DownloadFileManager::OnResponseCompleted, |
201 download_id_, | 208 download_id_, |
202 buffer_.release(), | 209 buffer_.release(), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 " url_ = " "\"%s\"" | 270 " url_ = " "\"%s\"" |
264 " download_id_ = " "%d" | 271 " download_id_ = " "%d" |
265 " global_id_ = {" | 272 " global_id_ = {" |
266 " child_id = " "%d" | 273 " child_id = " "%d" |
267 " request_id = " "%d" | 274 " request_id = " "%d" |
268 " }" | 275 " }" |
269 " render_view_id_ = " "%d" | 276 " render_view_id_ = " "%d" |
270 " save_info_.file_path = \"%" PRFilePath "\"" | 277 " save_info_.file_path = \"%" PRFilePath "\"" |
271 " }", | 278 " }", |
272 request_->url().spec().c_str(), | 279 request_->url().spec().c_str(), |
273 download_id_, | 280 download_id_.local(), |
274 global_id_.child_id, | 281 global_id_.child_id, |
275 global_id_.request_id, | 282 global_id_.request_id, |
276 render_view_id_, | 283 render_view_id_, |
277 save_info_.file_path.value().c_str()); | 284 save_info_.file_path.value().c_str()); |
278 } | 285 } |
OLD | NEW |