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 "chrome/browser/download/download_file.h" | 7 #include "chrome/browser/download/download_file.h" |
8 #include "chrome/browser/download/download_manager.h" | 8 #include "chrome/browser/download/download_manager.h" |
9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 10 #include "net/base/io_buffer.h" |
10 | 11 |
11 DownloadResourceHandler::DownloadResourceHandler(ResourceDispatcherHost* rdh, | 12 DownloadResourceHandler::DownloadResourceHandler(ResourceDispatcherHost* rdh, |
12 int render_process_host_id, | 13 int render_process_host_id, |
13 int render_view_id, | 14 int render_view_id, |
14 int request_id, | 15 int request_id, |
15 const std::string& url, | 16 const std::string& url, |
16 DownloadFileManager* manager, | 17 DownloadFileManager* manager, |
17 URLRequest* request, | 18 URLRequest* request, |
18 bool save_as) | 19 bool save_as) |
19 : download_id_(-1), | 20 : download_id_(-1), |
20 global_id_(ResourceDispatcherHost::GlobalRequestID(render_process_host_id, | 21 global_id_(ResourceDispatcherHost::GlobalRequestID(render_process_host_id, |
21 request_id)), | 22 request_id)), |
22 render_view_id_(render_view_id), | 23 render_view_id_(render_view_id), |
23 read_buffer_(NULL), | |
24 url_(UTF8ToWide(url)), | 24 url_(UTF8ToWide(url)), |
25 content_length_(0), | 25 content_length_(0), |
26 download_manager_(manager), | 26 download_manager_(manager), |
27 request_(request), | 27 request_(request), |
28 save_as_(save_as), | 28 save_as_(save_as), |
29 buffer_(new DownloadBuffer), | 29 buffer_(new DownloadBuffer), |
30 rdh_(rdh), | 30 rdh_(rdh), |
31 is_paused_(false) { | 31 is_paused_(false) { |
32 } | 32 } |
33 | 33 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 info->is_dangerous = false; | 65 info->is_dangerous = false; |
66 download_manager_->file_loop()->PostTask(FROM_HERE, | 66 download_manager_->file_loop()->PostTask(FROM_HERE, |
67 NewRunnableMethod(download_manager_, | 67 NewRunnableMethod(download_manager_, |
68 &DownloadFileManager::StartDownload, | 68 &DownloadFileManager::StartDownload, |
69 info)); | 69 info)); |
70 return true; | 70 return true; |
71 } | 71 } |
72 | 72 |
73 // Create a new buffer, which will be handed to the download thread for file | 73 // Create a new buffer, which will be handed to the download thread for file |
74 // writing and deletion. | 74 // writing and deletion. |
75 bool DownloadResourceHandler::OnWillRead(int request_id, | 75 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
76 char** buf, int* buf_size, | 76 int* buf_size, int min_size) { |
77 int min_size) { | |
78 DCHECK(buf && buf_size); | 77 DCHECK(buf && buf_size); |
79 if (!read_buffer_) { | 78 if (!read_buffer_) { |
80 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 79 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
81 read_buffer_ = new char[*buf_size]; | 80 read_buffer_ = new net::IOBuffer(*buf_size); |
82 } | 81 } |
83 *buf = read_buffer_; | 82 *buf = read_buffer_.get(); |
84 return true; | 83 return true; |
85 } | 84 } |
86 | 85 |
87 // Pass the buffer to the download file writer. | 86 // Pass the buffer to the download file writer. |
88 bool DownloadResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { | 87 bool DownloadResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { |
89 if (!*bytes_read) | 88 if (!*bytes_read) |
90 return true; | 89 return true; |
91 DCHECK(read_buffer_); | 90 DCHECK(read_buffer_); |
92 AutoLock auto_lock(buffer_->lock); | 91 AutoLock auto_lock(buffer_->lock); |
93 bool need_update = buffer_->contents.empty(); | 92 bool need_update = buffer_->contents.empty(); |
94 buffer_->contents.push_back(std::make_pair(read_buffer_, *bytes_read)); | 93 |
| 94 // We are passing ownership of this buffer to the download file manager. |
| 95 net::IOBuffer* buffer = NULL; |
| 96 read_buffer_.swap(&buffer); |
| 97 buffer_->contents.push_back(std::make_pair(buffer, *bytes_read)); |
95 if (need_update) { | 98 if (need_update) { |
96 download_manager_->file_loop()->PostTask(FROM_HERE, | 99 download_manager_->file_loop()->PostTask(FROM_HERE, |
97 NewRunnableMethod(download_manager_, | 100 NewRunnableMethod(download_manager_, |
98 &DownloadFileManager::UpdateDownload, | 101 &DownloadFileManager::UpdateDownload, |
99 download_id_, | 102 download_id_, |
100 buffer_)); | 103 buffer_)); |
101 } | 104 } |
102 read_buffer_ = NULL; | |
103 | 105 |
104 // We schedule a pause outside of the read loop if there is too much file | 106 // We schedule a pause outside of the read loop if there is too much file |
105 // writing work to do. | 107 // writing work to do. |
106 if (buffer_->contents.size() > kLoadsToWrite) | 108 if (buffer_->contents.size() > kLoadsToWrite) |
107 StartPauseTimer(); | 109 StartPauseTimer(); |
108 | 110 |
109 return true; | 111 return true; |
110 } | 112 } |
111 | 113 |
112 bool DownloadResourceHandler::OnResponseCompleted( | 114 bool DownloadResourceHandler::OnResponseCompleted( |
113 int request_id, | 115 int request_id, |
114 const URLRequestStatus& status) { | 116 const URLRequestStatus& status) { |
115 download_manager_->file_loop()->PostTask(FROM_HERE, | 117 download_manager_->file_loop()->PostTask(FROM_HERE, |
116 NewRunnableMethod(download_manager_, | 118 NewRunnableMethod(download_manager_, |
117 &DownloadFileManager::DownloadFinished, | 119 &DownloadFileManager::DownloadFinished, |
118 download_id_, | 120 download_id_, |
119 buffer_)); | 121 buffer_)); |
120 delete [] read_buffer_; | 122 read_buffer_ = NULL; |
121 | 123 |
122 // 'buffer_' is deleted by the DownloadFileManager. | 124 // 'buffer_' is deleted by the DownloadFileManager. |
123 buffer_ = NULL; | 125 buffer_ = NULL; |
124 return true; | 126 return true; |
125 } | 127 } |
126 | 128 |
127 // If the content-length header is not present (or contains something other | 129 // If the content-length header is not present (or contains something other |
128 // than numbers), the incoming content_length is -1 (unknown size). | 130 // than numbers), the incoming content_length is -1 (unknown size). |
129 // Set the content length to 0 to indicate unknown size to DownloadManager. | 131 // Set the content length to 0 to indicate unknown size to DownloadManager. |
130 void DownloadResourceHandler::set_content_length(const int64& content_length) { | 132 void DownloadResourceHandler::set_content_length(const int64& content_length) { |
(...skipping 29 matching lines...) Expand all Loading... |
160 should_pause); | 162 should_pause); |
161 is_paused_ = should_pause; | 163 is_paused_ = should_pause; |
162 } | 164 } |
163 } | 165 } |
164 | 166 |
165 void DownloadResourceHandler::StartPauseTimer() { | 167 void DownloadResourceHandler::StartPauseTimer() { |
166 if (!pause_timer_.IsRunning()) | 168 if (!pause_timer_.IsRunning()) |
167 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, | 169 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, |
168 &DownloadResourceHandler::CheckWriteProgress); | 170 &DownloadResourceHandler::CheckWriteProgress); |
169 } | 171 } |
OLD | NEW |