OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/file_util.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
9 #include "chrome/browser/download/download_item.h" | 10 #include "chrome/browser/download/download_item.h" |
10 #include "chrome/browser/download/download_file_manager.h" | 11 #include "chrome/browser/download/download_file_manager.h" |
| 12 #include "chrome/browser/download/download_util.h" |
11 #include "chrome/browser/history/download_create_info.h" | 13 #include "chrome/browser/history/download_create_info.h" |
12 #include "chrome/browser/renderer_host/global_request_id.h" | 14 #include "chrome/browser/renderer_host/global_request_id.h" |
13 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 15 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 16 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" |
14 #include "chrome/common/resource_response.h" | 17 #include "chrome/common/resource_response.h" |
15 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
16 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
17 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
18 | 21 |
19 DownloadResourceHandler::DownloadResourceHandler( | 22 DownloadResourceHandler::DownloadResourceHandler( |
20 ResourceDispatcherHost* rdh, | 23 ResourceDispatcherHost* rdh, |
21 int render_process_host_id, | 24 int render_process_host_id, |
22 int render_view_id, | 25 int render_view_id, |
23 int request_id, | 26 int request_id, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 60 |
58 // Send the download creation information to the download thread. | 61 // Send the download creation information to the download thread. |
59 bool DownloadResourceHandler::OnResponseStarted(int request_id, | 62 bool DownloadResourceHandler::OnResponseStarted(int request_id, |
60 ResourceResponse* response) { | 63 ResourceResponse* response) { |
61 std::string content_disposition; | 64 std::string content_disposition; |
62 request_->GetResponseHeaderByName("content-disposition", | 65 request_->GetResponseHeaderByName("content-disposition", |
63 &content_disposition); | 66 &content_disposition); |
64 set_content_disposition(content_disposition); | 67 set_content_disposition(content_disposition); |
65 set_content_length(response->response_head.content_length); | 68 set_content_length(response->response_head.content_length); |
66 | 69 |
67 download_id_ = download_file_manager_->GetNextId(); | 70 ResourceDispatcherHostRequestInfo* req_info = |
| 71 ResourceDispatcherHost::InfoForRequest(request_); |
| 72 download_id_ = -1; |
| 73 if (req_info->was_interrupted()) { |
| 74 FilePath orig_path = save_info_.file_path; |
| 75 download_id_ = download_file_manager_->GetDownloadId(url_, orig_path); |
| 76 if (download_id_ == -1) { |
| 77 orig_path = download_util::RemoveCrDownloadPath(save_info_.file_path); |
| 78 download_id_ = download_file_manager_->GetDownloadId(url_, orig_path); |
| 79 } |
| 80 } |
| 81 if (download_id_ == -1) |
| 82 download_id_ = download_file_manager_->GetNextId(); |
68 // |download_file_manager_| consumes (deletes): | 83 // |download_file_manager_| consumes (deletes): |
69 DownloadCreateInfo* info = new DownloadCreateInfo; | 84 DownloadCreateInfo* info = new DownloadCreateInfo; |
70 info->url = url_; | 85 info->url = url_; |
71 info->referrer_url = GURL(request_->referrer()); | 86 info->referrer_url = GURL(request_->referrer()); |
72 info->start_time = base::Time::Now(); | 87 info->start_time = base::Time::Now(); |
73 info->received_bytes = 0; | 88 info->received_bytes = |
| 89 req_info->was_interrupted() ? req_info->interrupted_bytes() : 0; |
74 info->total_bytes = content_length_; | 90 info->total_bytes = content_length_; |
75 info->state = DownloadItem::IN_PROGRESS; | 91 info->state = DownloadItem::IN_PROGRESS; |
76 info->download_id = download_id_; | 92 info->download_id = download_id_; |
77 info->child_id = global_id_.child_id; | 93 info->child_id = global_id_.child_id; |
78 info->render_view_id = render_view_id_; | 94 info->render_view_id = render_view_id_; |
79 info->request_id = global_id_.request_id; | 95 info->request_id = global_id_.request_id; |
80 info->content_disposition = content_disposition_; | 96 info->content_disposition = content_disposition_; |
81 info->mime_type = response->response_head.mime_type; | 97 info->mime_type = response->response_head.mime_type; |
| 98 info->is_partial_download = req_info->was_interrupted(); |
| 99 |
| 100 // Handle server's response codes. |
| 101 int status = request_->response_headers()->response_code(); |
| 102 switch (status) { |
| 103 // Continue with download: |
| 104 case 200: // OK |
| 105 // Downloading the full file, even if we asked for a range. |
| 106 info->received_bytes = 0; |
| 107 if (info->is_partial_download) { |
| 108 // Delete the crdownload file explicitly. |
| 109 FilePath crpath = |
| 110 download_util::GetCrDownloadPath(save_info_.file_path); |
| 111 file_util::Delete(crpath, false); |
| 112 } |
| 113 // Fall through. |
| 114 |
| 115 case 206: // Partial content. Leave alone. |
| 116 break; |
| 117 |
| 118 case 412: // Precondition failed. Fails 'If-Unmodified-Since'. |
| 119 case 204: // No content. File not present. |
| 120 default: // All other errors. |
| 121 // TODO(ahendrickson) -- Cancel, and notify user. |
| 122 break; |
| 123 |
| 124 // Retry by downloading from the start automatically: |
| 125 case 416: // Range not available. Fails 'Content-Range'. |
| 126 // TODO(ahendrickson) -- Submit another request for the whole file. |
| 127 break; |
| 128 } |
82 | 129 |
83 std::string content_type_header; | 130 std::string content_type_header; |
84 if (!response->response_head.headers || | 131 if (!response->response_head.headers || |
85 !response->response_head.headers->GetMimeType(&content_type_header)) | 132 !response->response_head.headers->GetMimeType(&content_type_header)) |
86 content_type_header = ""; | 133 content_type_header = ""; |
87 info->original_mime_type = content_type_header; | 134 info->original_mime_type = content_type_header; |
88 | 135 |
89 info->prompt_user_for_save_location = | 136 info->prompt_user_for_save_location = |
90 save_as_ && save_info_.file_path.empty(); | 137 save_as_ && save_info_.file_path.empty(); |
91 info->is_dangerous = false; | 138 info->is_dangerous = false; |
92 info->referrer_charset = request_->context()->referrer_charset(); | 139 info->referrer_charset = request_->context()->referrer_charset(); |
93 info->save_info = save_info_; | 140 info->save_info = save_info_; |
94 ChromeThread::PostTask( | 141 ChromeThread::PostTask( |
95 ChromeThread::UI, FROM_HERE, | 142 ChromeThread::UI, |
| 143 FROM_HERE, |
96 NewRunnableMethod( | 144 NewRunnableMethod( |
97 download_file_manager_, &DownloadFileManager::StartDownload, info)); | 145 download_file_manager_, &DownloadFileManager::StartDownload, info)); |
98 | 146 |
99 // We can't start saving the data before we create the file on disk. | 147 // We can't start saving the data before we create the file on disk. |
100 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. | 148 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. |
101 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); | 149 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); |
102 | 150 |
103 return true; | 151 return true; |
104 } | 152 } |
105 | 153 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 if (buffer_->contents.size() > kLoadsToWrite) | 196 if (buffer_->contents.size() > kLoadsToWrite) |
149 StartPauseTimer(); | 197 StartPauseTimer(); |
150 | 198 |
151 return true; | 199 return true; |
152 } | 200 } |
153 | 201 |
154 bool DownloadResourceHandler::OnResponseCompleted( | 202 bool DownloadResourceHandler::OnResponseCompleted( |
155 int request_id, | 203 int request_id, |
156 const URLRequestStatus& status, | 204 const URLRequestStatus& status, |
157 const std::string& security_info) { | 205 const std::string& security_info) { |
| 206 int error_code = |
| 207 (status.status() == URLRequestStatus::FAILED) ? status.os_error() : 0; |
158 ChromeThread::PostTask( | 208 ChromeThread::PostTask( |
159 ChromeThread::FILE, FROM_HERE, | 209 ChromeThread::FILE, |
| 210 FROM_HERE, |
160 NewRunnableMethod(download_file_manager_, | 211 NewRunnableMethod(download_file_manager_, |
161 &DownloadFileManager::OnResponseCompleted, | 212 &DownloadFileManager::OnResponseCompleted, |
162 download_id_, | 213 download_id_, |
163 buffer_)); | 214 buffer_, |
| 215 error_code, |
| 216 security_info)); |
164 read_buffer_ = NULL; | 217 read_buffer_ = NULL; |
165 | 218 |
166 // 'buffer_' is deleted by the DownloadFileManager. | 219 // 'buffer_' is deleted by the DownloadFileManager. |
167 buffer_ = NULL; | 220 buffer_ = NULL; |
168 return true; | 221 return true; |
169 } | 222 } |
170 | 223 |
171 void DownloadResourceHandler::OnRequestClosed() { | 224 void DownloadResourceHandler::OnRequestClosed() { |
172 } | 225 } |
173 | 226 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 262 } |
210 | 263 |
211 DownloadResourceHandler::~DownloadResourceHandler() { | 264 DownloadResourceHandler::~DownloadResourceHandler() { |
212 } | 265 } |
213 | 266 |
214 void DownloadResourceHandler::StartPauseTimer() { | 267 void DownloadResourceHandler::StartPauseTimer() { |
215 if (!pause_timer_.IsRunning()) | 268 if (!pause_timer_.IsRunning()) |
216 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, | 269 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, |
217 &DownloadResourceHandler::CheckWriteProgress); | 270 &DownloadResourceHandler::CheckWriteProgress); |
218 } | 271 } |
OLD | NEW |