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/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/chrome_thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
9 #include "chrome/browser/download/download_item.h" | 9 #include "chrome/browser/download/download_item.h" |
10 #include "chrome/browser/download/download_file_manager.h" | 10 #include "chrome/browser/download/download_file_manager.h" |
11 #include "chrome/browser/history/download_types.h" | 11 #include "chrome/browser/history/download_types.h" |
12 #include "chrome/browser/renderer_host/global_request_id.h" | 12 #include "chrome/browser/renderer_host/global_request_id.h" |
13 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 13 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
14 #include "chrome/common/resource_response.h" | 14 #include "chrome/common/resource_response.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
17 | 17 |
18 DownloadResourceHandler::DownloadResourceHandler( | 18 DownloadResourceHandler::DownloadResourceHandler( |
19 ResourceDispatcherHost* rdh, | 19 ResourceDispatcherHost* rdh, |
20 int render_process_host_id, | 20 int render_process_host_id, |
21 int render_view_id, | 21 int render_view_id, |
22 int request_id, | 22 int request_id, |
23 const GURL& url, | 23 const GURL& url, |
24 DownloadFileManager* manager, | 24 DownloadFileManager* download_file_manager, |
25 URLRequest* request, | 25 URLRequest* request, |
26 bool save_as, | 26 bool save_as, |
27 const DownloadSaveInfo& save_info) | 27 const DownloadSaveInfo& save_info) |
28 : download_id_(-1), | 28 : download_id_(-1), |
29 global_id_(render_process_host_id, request_id), | 29 global_id_(render_process_host_id, request_id), |
30 render_view_id_(render_view_id), | 30 render_view_id_(render_view_id), |
31 url_(url), | 31 url_(url), |
32 content_length_(0), | 32 content_length_(0), |
33 download_manager_(manager), | 33 download_file_manager_(download_file_manager), |
34 request_(request), | 34 request_(request), |
35 save_as_(save_as), | 35 save_as_(save_as), |
36 save_info_(save_info), | 36 save_info_(save_info), |
37 buffer_(new DownloadBuffer), | 37 buffer_(new DownloadBuffer), |
38 rdh_(rdh), | 38 rdh_(rdh), |
39 is_paused_(false) { | 39 is_paused_(false) { |
40 } | 40 } |
41 | 41 |
42 bool DownloadResourceHandler::OnUploadProgress(int request_id, | 42 bool DownloadResourceHandler::OnUploadProgress(int request_id, |
43 uint64 position, | 43 uint64 position, |
(...skipping 12 matching lines...) Expand all Loading... |
56 | 56 |
57 // Send the download creation information to the download thread. | 57 // Send the download creation information to the download thread. |
58 bool DownloadResourceHandler::OnResponseStarted(int request_id, | 58 bool DownloadResourceHandler::OnResponseStarted(int request_id, |
59 ResourceResponse* response) { | 59 ResourceResponse* response) { |
60 std::string content_disposition; | 60 std::string content_disposition; |
61 request_->GetResponseHeaderByName("content-disposition", | 61 request_->GetResponseHeaderByName("content-disposition", |
62 &content_disposition); | 62 &content_disposition); |
63 set_content_disposition(content_disposition); | 63 set_content_disposition(content_disposition); |
64 set_content_length(response->response_head.content_length); | 64 set_content_length(response->response_head.content_length); |
65 | 65 |
66 download_id_ = download_manager_->GetNextId(); | 66 download_id_ = download_file_manager_->GetNextId(); |
67 // |download_manager_| consumes (deletes): | 67 // |download_file_manager_| consumes (deletes): |
68 DownloadCreateInfo* info = new DownloadCreateInfo; | 68 DownloadCreateInfo* info = new DownloadCreateInfo; |
69 info->url = url_; | 69 info->url = url_; |
70 info->referrer_url = GURL(request_->referrer()); | 70 info->referrer_url = GURL(request_->referrer()); |
71 info->start_time = base::Time::Now(); | 71 info->start_time = base::Time::Now(); |
72 info->received_bytes = 0; | 72 info->received_bytes = 0; |
73 info->total_bytes = content_length_; | 73 info->total_bytes = content_length_; |
74 info->state = DownloadItem::IN_PROGRESS; | 74 info->state = DownloadItem::IN_PROGRESS; |
75 info->download_id = download_id_; | 75 info->download_id = download_id_; |
76 info->child_id = global_id_.child_id; | 76 info->child_id = global_id_.child_id; |
77 info->render_view_id = render_view_id_; | 77 info->render_view_id = render_view_id_; |
78 info->request_id = global_id_.request_id; | 78 info->request_id = global_id_.request_id; |
79 info->content_disposition = content_disposition_; | 79 info->content_disposition = content_disposition_; |
80 info->mime_type = response->response_head.mime_type; | 80 info->mime_type = response->response_head.mime_type; |
81 | 81 |
82 std::string content_type_header; | 82 std::string content_type_header; |
83 if (!response->response_head.headers || | 83 if (!response->response_head.headers || |
84 !response->response_head.headers->GetMimeType(&content_type_header)) | 84 !response->response_head.headers->GetMimeType(&content_type_header)) |
85 content_type_header = ""; | 85 content_type_header = ""; |
86 info->original_mime_type = content_type_header; | 86 info->original_mime_type = content_type_header; |
87 | 87 |
88 info->prompt_user_for_save_location = | 88 info->prompt_user_for_save_location = |
89 save_as_ && save_info_.file_path.empty(); | 89 save_as_ && save_info_.file_path.empty(); |
90 info->is_dangerous = false; | 90 info->is_dangerous = false; |
91 info->referrer_charset = request_->context()->referrer_charset(); | 91 info->referrer_charset = request_->context()->referrer_charset(); |
92 info->save_info = save_info_; | 92 info->save_info = save_info_; |
93 ChromeThread::PostTask( | 93 ChromeThread::PostTask( |
94 ChromeThread::FILE, FROM_HERE, | 94 ChromeThread::FILE, FROM_HERE, |
95 NewRunnableMethod( | 95 NewRunnableMethod( |
96 download_manager_, &DownloadFileManager::StartDownload, info)); | 96 download_file_manager_, &DownloadFileManager::StartDownload, info)); |
97 return true; | 97 return true; |
98 } | 98 } |
99 | 99 |
100 bool DownloadResourceHandler::OnWillStart(int request_id, | 100 bool DownloadResourceHandler::OnWillStart(int request_id, |
101 const GURL& url, | 101 const GURL& url, |
102 bool* defer) { | 102 bool* defer) { |
103 return true; | 103 return true; |
104 } | 104 } |
105 | 105 |
106 // Create a new buffer, which will be handed to the download thread for file | 106 // Create a new buffer, which will be handed to the download thread for file |
(...skipping 19 matching lines...) Expand all Loading... |
126 AutoLock auto_lock(buffer_->lock); | 126 AutoLock auto_lock(buffer_->lock); |
127 bool need_update = buffer_->contents.empty(); | 127 bool need_update = buffer_->contents.empty(); |
128 | 128 |
129 // We are passing ownership of this buffer to the download file manager. | 129 // We are passing ownership of this buffer to the download file manager. |
130 net::IOBuffer* buffer = NULL; | 130 net::IOBuffer* buffer = NULL; |
131 read_buffer_.swap(&buffer); | 131 read_buffer_.swap(&buffer); |
132 buffer_->contents.push_back(std::make_pair(buffer, *bytes_read)); | 132 buffer_->contents.push_back(std::make_pair(buffer, *bytes_read)); |
133 if (need_update) { | 133 if (need_update) { |
134 ChromeThread::PostTask( | 134 ChromeThread::PostTask( |
135 ChromeThread::FILE, FROM_HERE, | 135 ChromeThread::FILE, FROM_HERE, |
136 NewRunnableMethod(download_manager_, | 136 NewRunnableMethod(download_file_manager_, |
137 &DownloadFileManager::UpdateDownload, | 137 &DownloadFileManager::UpdateDownload, |
138 download_id_, | 138 download_id_, |
139 buffer_)); | 139 buffer_)); |
140 } | 140 } |
141 | 141 |
142 // We schedule a pause outside of the read loop if there is too much file | 142 // We schedule a pause outside of the read loop if there is too much file |
143 // writing work to do. | 143 // writing work to do. |
144 if (buffer_->contents.size() > kLoadsToWrite) | 144 if (buffer_->contents.size() > kLoadsToWrite) |
145 StartPauseTimer(); | 145 StartPauseTimer(); |
146 | 146 |
147 return true; | 147 return true; |
148 } | 148 } |
149 | 149 |
150 bool DownloadResourceHandler::OnResponseCompleted( | 150 bool DownloadResourceHandler::OnResponseCompleted( |
151 int request_id, | 151 int request_id, |
152 const URLRequestStatus& status, | 152 const URLRequestStatus& status, |
153 const std::string& security_info) { | 153 const std::string& security_info) { |
154 ChromeThread::PostTask( | 154 ChromeThread::PostTask( |
155 ChromeThread::FILE, FROM_HERE, | 155 ChromeThread::FILE, FROM_HERE, |
156 NewRunnableMethod(download_manager_, | 156 NewRunnableMethod(download_file_manager_, |
157 &DownloadFileManager::DownloadFinished, | 157 &DownloadFileManager::DownloadFinished, |
158 download_id_, | 158 download_id_, |
159 buffer_)); | 159 buffer_)); |
160 read_buffer_ = NULL; | 160 read_buffer_ = NULL; |
161 | 161 |
162 // 'buffer_' is deleted by the DownloadFileManager. | 162 // 'buffer_' is deleted by the DownloadFileManager. |
163 buffer_ = NULL; | 163 buffer_ = NULL; |
164 return true; | 164 return true; |
165 } | 165 } |
166 | 166 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 should_pause); | 202 should_pause); |
203 is_paused_ = should_pause; | 203 is_paused_ = should_pause; |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 void DownloadResourceHandler::StartPauseTimer() { | 207 void DownloadResourceHandler::StartPauseTimer() { |
208 if (!pause_timer_.IsRunning()) | 208 if (!pause_timer_.IsRunning()) |
209 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, | 209 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, |
210 &DownloadResourceHandler::CheckWriteProgress); | 210 &DownloadResourceHandler::CheckWriteProgress); |
211 } | 211 } |
OLD | NEW |