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 "chrome/browser/renderer_host/download_resource_handler.h" | 5 #include "chrome/browser/renderer_host/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" |
11 #include "base/metrics/stats_counters.h" | 11 #include "base/metrics/stats_counters.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "chrome/browser/download/download_create_info.h" | 13 #include "chrome/browser/download/download_create_info.h" |
14 #include "chrome/browser/download/download_item.h" | 14 #include "chrome/browser/download/download_item.h" |
15 #include "chrome/browser/download/download_file_manager.h" | 15 #include "chrome/browser/download/download_file_manager.h" |
| 16 #include "chrome/browser/download/download_manager.h" |
| 17 #include "chrome/browser/download/download_prefs.h" |
16 #include "chrome/browser/download/download_request_handle.h" | 18 #include "chrome/browser/download/download_request_handle.h" |
17 #include "chrome/browser/download/download_util.h" | 19 #include "chrome/browser/download/download_util.h" |
18 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
19 #include "content/browser/renderer_host/global_request_id.h" | 21 #include "content/browser/renderer_host/global_request_id.h" |
20 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 22 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
21 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
22 #include "content/common/resource_response.h" | 24 #include "content/common/resource_response.h" |
23 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
24 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
25 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
26 | 28 |
27 DownloadResourceHandler::DownloadResourceHandler( | 29 DownloadResourceHandler::DownloadResourceHandler( |
28 ResourceDispatcherHost* rdh, | 30 ResourceDispatcherHost* rdh, |
29 int render_process_host_id, | 31 int render_process_host_id, |
30 int render_view_id, | 32 int render_view_id, |
31 int request_id, | 33 int request_id, |
32 const GURL& url, | 34 const GURL& url, |
33 DownloadFileManager* download_file_manager, | 35 DownloadFileManager* download_file_manager, |
34 net::URLRequest* request, | 36 net::URLRequest* request, |
35 bool save_as, | 37 bool save_as, |
| 38 OnStartedCallback started_cb, |
36 const DownloadSaveInfo& save_info) | 39 const DownloadSaveInfo& save_info) |
37 : download_id_(-1), | 40 : download_id_(-1), |
38 global_id_(render_process_host_id, request_id), | 41 global_id_(render_process_host_id, request_id), |
39 render_view_id_(render_view_id), | 42 render_view_id_(render_view_id), |
40 content_length_(0), | 43 content_length_(0), |
41 download_file_manager_(download_file_manager), | 44 download_file_manager_(download_file_manager), |
42 request_(request), | 45 request_(request), |
43 save_as_(save_as), | 46 save_as_(save_as), |
| 47 started_cb_(started_cb), |
44 save_info_(save_info), | 48 save_info_(save_info), |
45 buffer_(new DownloadBuffer), | 49 buffer_(new DownloadBuffer), |
46 rdh_(rdh), | 50 rdh_(rdh), |
47 is_paused_(false) { | 51 is_paused_(false) { |
48 download_util::RecordDownloadCount(download_util::UNTHROTTLED_COUNT); | 52 download_util::RecordDownloadCount(download_util::UNTHROTTLED_COUNT); |
49 } | 53 } |
50 | 54 |
51 bool DownloadResourceHandler::OnUploadProgress(int request_id, | 55 bool DownloadResourceHandler::OnUploadProgress(int request_id, |
52 uint64 position, | 56 uint64 position, |
53 uint64 size) { | 57 uint64 size) { |
(...skipping 16 matching lines...) Expand all Loading... |
70 download_start_time_ = base::TimeTicks::Now(); | 74 download_start_time_ = base::TimeTicks::Now(); |
71 std::string content_disposition; | 75 std::string content_disposition; |
72 request_->GetResponseHeaderByName("content-disposition", | 76 request_->GetResponseHeaderByName("content-disposition", |
73 &content_disposition); | 77 &content_disposition); |
74 set_content_disposition(content_disposition); | 78 set_content_disposition(content_disposition); |
75 set_content_length(response->response_head.content_length); | 79 set_content_length(response->response_head.content_length); |
76 | 80 |
77 const ResourceDispatcherHostRequestInfo* request_info = | 81 const ResourceDispatcherHostRequestInfo* request_info = |
78 ResourceDispatcherHost::InfoForRequest(request_); | 82 ResourceDispatcherHost::InfoForRequest(request_); |
79 | 83 |
80 download_id_ = download_file_manager_->GetNextId(); | |
81 | |
82 // Deleted in DownloadManager. | 84 // Deleted in DownloadManager. |
83 DownloadCreateInfo* info = new DownloadCreateInfo; | 85 DownloadCreateInfo* info = new DownloadCreateInfo; |
84 info->url_chain = request_->url_chain(); | 86 info->url_chain = request_->url_chain(); |
85 info->referrer_url = GURL(request_->referrer()); | 87 info->referrer_url = GURL(request_->referrer()); |
86 info->start_time = base::Time::Now(); | 88 info->start_time = base::Time::Now(); |
87 info->received_bytes = 0; | 89 info->received_bytes = 0; |
88 info->total_bytes = content_length_; | 90 info->total_bytes = content_length_; |
89 info->state = DownloadItem::IN_PROGRESS; | 91 info->state = DownloadItem::IN_PROGRESS; |
90 info->download_id = download_id_; | |
91 info->has_user_gesture = request_info->has_user_gesture(); | 92 info->has_user_gesture = request_info->has_user_gesture(); |
92 info->request_handle = DownloadRequestHandle(rdh_, | 93 info->request_handle = DownloadRequestHandle(rdh_, |
93 global_id_.child_id, | 94 global_id_.child_id, |
94 render_view_id_, | 95 render_view_id_, |
95 global_id_.request_id); | 96 global_id_.request_id); |
96 info->content_disposition = content_disposition_; | 97 info->content_disposition = content_disposition_; |
97 info->mime_type = response->response_head.mime_type; | 98 info->mime_type = response->response_head.mime_type; |
98 // TODO(ahendrickson) -- Get the last modified time and etag, so we can | 99 // TODO(ahendrickson) -- Get the last modified time and etag, so we can |
99 // resume downloading. | 100 // resume downloading. |
100 | 101 |
101 std::string content_type_header; | 102 std::string content_type_header; |
102 if (!response->response_head.headers || | 103 if (!response->response_head.headers || |
103 !response->response_head.headers->GetMimeType(&content_type_header)) | 104 !response->response_head.headers->GetMimeType(&content_type_header)) |
104 content_type_header = ""; | 105 content_type_header = ""; |
105 info->original_mime_type = content_type_header; | 106 info->original_mime_type = content_type_header; |
106 | 107 |
107 info->prompt_user_for_save_location = | 108 info->prompt_user_for_save_location = |
108 save_as_ && save_info_.file_path.empty(); | 109 save_as_ && save_info_.file_path.empty(); |
109 info->referrer_charset = request_->context()->referrer_charset(); | 110 info->referrer_charset = request_->context()->referrer_charset(); |
110 info->save_info = save_info_; | 111 info->save_info = save_info_; |
| 112 |
111 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
112 BrowserThread::UI, FROM_HERE, | 114 BrowserThread::UI, FROM_HERE, |
113 NewRunnableMethod( | 115 NewRunnableMethod( |
114 download_file_manager_, &DownloadFileManager::StartDownload, info)); | 116 this, &DownloadResourceHandler::OnResponseStartedOnUIThread, info)); |
115 | 117 |
116 // We can't start saving the data before we create the file on disk. | 118 // We can't start saving the data before we create the file on disk. |
117 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. | 119 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. |
118 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); | 120 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); |
119 | 121 |
120 return true; | 122 return true; |
121 } | 123 } |
122 | 124 |
| 125 void DownloadResourceHandler::OnResponseStartedOnUIThread( |
| 126 DownloadCreateInfo* info) { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 128 download_id_ = info->request_handle.GetDownloadManager()-> |
| 129 download_prefs()->GetNextId(); |
| 130 VLOG(1) << __PRETTY_FUNCTION__ << " " << download_id_; |
| 131 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
| 132 this, &DownloadResourceHandler::CallStartedCB, 0)); |
| 133 info->download_id = download_id_; |
| 134 download_file_manager_->StartDownload(info); |
| 135 } |
| 136 |
| 137 void DownloadResourceHandler::CallStartedCB(int error) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 139 if (started_cb_.is_null()) return; |
| 140 started_cb_.Run(download_id_, error); |
| 141 started_cb_.Reset(); |
| 142 } |
| 143 |
123 bool DownloadResourceHandler::OnWillStart(int request_id, | 144 bool DownloadResourceHandler::OnWillStart(int request_id, |
124 const GURL& url, | 145 const GURL& url, |
125 bool* defer) { | 146 bool* defer) { |
126 return true; | 147 return true; |
127 } | 148 } |
128 | 149 |
129 // Create a new buffer, which will be handed to the download thread for file | 150 // Create a new buffer, which will be handed to the download thread for file |
130 // writing and deletion. | 151 // writing and deletion. |
131 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 152 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
132 int* buf_size, int min_size) { | 153 int* buf_size, int min_size) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 bool DownloadResourceHandler::OnResponseCompleted( | 192 bool DownloadResourceHandler::OnResponseCompleted( |
172 int request_id, | 193 int request_id, |
173 const net::URLRequestStatus& status, | 194 const net::URLRequestStatus& status, |
174 const std::string& security_info) { | 195 const std::string& security_info) { |
175 VLOG(20) << __FUNCTION__ << "()" << DebugString() | 196 VLOG(20) << __FUNCTION__ << "()" << DebugString() |
176 << " request_id = " << request_id | 197 << " request_id = " << request_id |
177 << " status.status() = " << status.status() | 198 << " status.status() = " << status.status() |
178 << " status.os_error() = " << status.os_error(); | 199 << " status.os_error() = " << status.os_error(); |
179 int error_code = (status.status() == net::URLRequestStatus::FAILED) ? | 200 int error_code = (status.status() == net::URLRequestStatus::FAILED) ? |
180 status.os_error() : 0; | 201 status.os_error() : 0; |
| 202 if (download_id_ == -1) { |
| 203 CallStartedCB(error_code); |
| 204 } |
181 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, | 205 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, |
182 // so that any functions queued up on the FILE thread are executed | 206 // so that any functions queued up on the FILE thread are executed |
183 // before deletion. | 207 // before deletion. |
184 BrowserThread::PostTask( | 208 BrowserThread::PostTask( |
185 BrowserThread::FILE, FROM_HERE, | 209 BrowserThread::FILE, FROM_HERE, |
186 NewRunnableMethod(download_file_manager_, | 210 NewRunnableMethod(download_file_manager_, |
187 &DownloadFileManager::OnResponseCompleted, | 211 &DownloadFileManager::OnResponseCompleted, |
188 download_id_, | 212 download_id_, |
189 buffer_.release(), | 213 buffer_.release(), |
190 error_code, | 214 error_code, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 " render_view_id_ = " "%d" | 279 " render_view_id_ = " "%d" |
256 " save_info_.file_path = \"%" PRFilePath "\"" | 280 " save_info_.file_path = \"%" PRFilePath "\"" |
257 " }", | 281 " }", |
258 request_->url().spec().c_str(), | 282 request_->url().spec().c_str(), |
259 download_id_, | 283 download_id_, |
260 global_id_.child_id, | 284 global_id_.child_id, |
261 global_id_.request_id, | 285 global_id_.request_id, |
262 render_view_id_, | 286 render_view_id_, |
263 save_info_.file_path.value().c_str()); | 287 save_info_.file_path.value().c_str()); |
264 } | 288 } |
OLD | NEW |