Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop_proxy.h" | |
| 12 #include "content/browser/download/byte_stream.h" | |
| 10 #include "content/browser/download/download_create_info.h" | 13 #include "content/browser/download/download_create_info.h" |
| 14 #include "content/browser/download/download_interrupt_reasons_impl.h" | |
| 15 #include "content/browser/download/download_net_log_parameters.h" | |
| 11 #include "content/browser/power_save_blocker.h" | 16 #include "content/browser/power_save_blocker.h" |
| 12 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/download_manager.h" | 18 #include "content/public/browser/download_manager.h" |
| 19 #include "content/browser/download/download_stats.h" | |
| 20 #include "net/base/io_buffer.h" | |
| 14 | 21 |
| 15 using content::BrowserThread; | 22 using content::BrowserThread; |
| 16 using content::DownloadId; | 23 using content::DownloadId; |
| 17 using content::DownloadManager; | 24 using content::DownloadManager; |
| 18 | 25 |
| 19 const int kUpdatePeriodMs = 500; | 26 const int kUpdatePeriodMs = 500; |
| 20 | 27 |
| 21 DownloadFileImpl::DownloadFileImpl( | 28 DownloadFileImpl::DownloadFileImpl( |
| 22 const DownloadCreateInfo* info, | 29 const DownloadCreateInfo* info, |
| 30 scoped_ptr<content::ByteStreamOutput> pipe, | |
| 23 DownloadRequestHandleInterface* request_handle, | 31 DownloadRequestHandleInterface* request_handle, |
| 24 DownloadManager* download_manager, | 32 DownloadManager* download_manager, |
| 25 bool calculate_hash, | 33 bool calculate_hash, |
| 26 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 34 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 27 const net::BoundNetLog& bound_net_log) | 35 const net::BoundNetLog& bound_net_log) |
| 28 : file_(info->save_info.file_path, | 36 : file_(info->save_info.file_path, |
| 29 info->url(), | 37 info->url(), |
| 30 info->referrer_url, | 38 info->referrer_url, |
| 31 info->received_bytes, | 39 info->received_bytes, |
| 32 calculate_hash, | 40 calculate_hash, |
| 33 info->save_info.hash_state, | 41 info->save_info.hash_state, |
| 34 info->save_info.file_stream, | 42 info->save_info.file_stream, |
| 35 bound_net_log), | 43 bound_net_log), |
| 44 input_pipe_(pipe.Pass()), | |
| 36 id_(info->download_id), | 45 id_(info->download_id), |
| 37 request_handle_(request_handle), | 46 request_handle_(request_handle), |
| 38 download_manager_(download_manager), | 47 download_manager_(download_manager), |
| 48 bound_net_log_(bound_net_log), | |
| 49 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 39 power_save_blocker_(power_save_blocker.Pass()) { | 50 power_save_blocker_(power_save_blocker.Pass()) { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 41 } | 52 } |
| 42 | 53 |
| 43 DownloadFileImpl::~DownloadFileImpl() { | 54 DownloadFileImpl::~DownloadFileImpl() { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 56 // Prevent any future callbacks from reaching us. | |
| 57 if (input_pipe_.get()) | |
| 58 input_pipe_->RegisterCallback(base::Closure()); | |
| 45 } | 59 } |
| 46 | 60 |
| 47 // BaseFile delegated functions. | 61 // BaseFile delegated functions. |
| 48 net::Error DownloadFileImpl::Initialize() { | 62 net::Error DownloadFileImpl::Initialize() { |
| 49 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); | 63 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); |
| 50 return file_.Initialize(); | 64 net::Error result = file_.Initialize(); |
| 65 if (result != net::OK) | |
| 66 return result; | |
| 67 | |
| 68 input_pipe_->RegisterCallback( | |
| 69 // Unretained is safe because the callback is nulled in the | |
|
benjhayden
2012/05/21 13:44:14
Is there any particular reason not to use a weak p
Randy Smith (Not in Mondays)
2012/05/23 03:33:15
Given that we already have the factory, no. Done.
| |
| 70 // destructor. | |
| 71 base::Bind(&DownloadFileImpl::PipeActive, base::Unretained(this))); | |
| 72 | |
| 73 // Initial pull from the straw. | |
| 74 PipeActive(); | |
| 75 | |
| 76 return result; | |
| 51 } | 77 } |
| 52 | 78 |
| 53 net::Error DownloadFileImpl::AppendDataToFile(const char* data, | 79 net::Error DownloadFileImpl::AppendDataToFile(const char* data, |
| 54 size_t data_len) { | 80 size_t data_len) { |
| 55 if (!update_timer_->IsRunning()) { | 81 if (!update_timer_->IsRunning()) { |
| 56 update_timer_->Start(FROM_HERE, | 82 update_timer_->Start(FROM_HERE, |
| 57 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 83 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
| 58 this, &DownloadFileImpl::SendUpdate); | 84 this, &DownloadFileImpl::SendUpdate); |
| 59 } | 85 } |
| 60 return file_.AppendDataToFile(data, data_len); | 86 return file_.AppendDataToFile(data, data_len); |
| 61 } | 87 } |
| 62 | 88 |
| 63 net::Error DownloadFileImpl::Rename(const FilePath& full_path) { | 89 net::Error DownloadFileImpl::Rename(const FilePath& full_path) { |
| 64 return file_.Rename(full_path); | 90 return file_.Rename(full_path); |
| 65 } | 91 } |
| 66 | 92 |
| 67 void DownloadFileImpl::Detach() { | 93 void DownloadFileImpl::Detach() { |
| 68 file_.Detach(); | 94 file_.Detach(); |
| 69 } | 95 } |
| 70 | 96 |
| 71 void DownloadFileImpl::Cancel() { | 97 void DownloadFileImpl::Cancel() { |
| 72 file_.Cancel(); | 98 file_.Cancel(); |
| 73 } | 99 } |
| 74 | 100 |
| 75 void DownloadFileImpl::Finish() { | |
| 76 file_.Finish(); | |
| 77 update_timer_.reset(); | |
| 78 } | |
| 79 | |
| 80 void DownloadFileImpl::AnnotateWithSourceInformation() { | 101 void DownloadFileImpl::AnnotateWithSourceInformation() { |
| 81 file_.AnnotateWithSourceInformation(); | 102 file_.AnnotateWithSourceInformation(); |
| 82 } | 103 } |
| 83 | 104 |
| 84 FilePath DownloadFileImpl::FullPath() const { | 105 FilePath DownloadFileImpl::FullPath() const { |
| 85 return file_.full_path(); | 106 return file_.full_path(); |
| 86 } | 107 } |
| 87 | 108 |
| 88 bool DownloadFileImpl::InProgress() const { | 109 bool DownloadFileImpl::InProgress() const { |
| 89 return file_.in_progress(); | 110 return file_.in_progress(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 return base::StringPrintf("{" | 149 return base::StringPrintf("{" |
| 129 " id_ = " "%d" | 150 " id_ = " "%d" |
| 130 " request_handle = %s" | 151 " request_handle = %s" |
| 131 " Base File = %s" | 152 " Base File = %s" |
| 132 " }", | 153 " }", |
| 133 id_.local(), | 154 id_.local(), |
| 134 request_handle_->DebugString().c_str(), | 155 request_handle_->DebugString().c_str(), |
| 135 file_.DebugString().c_str()); | 156 file_.DebugString().c_str()); |
| 136 } | 157 } |
| 137 | 158 |
| 159 void DownloadFileImpl::PipeActive() { | |
| 160 base::Time start(base::Time::Now()); | |
| 161 base::Time now; | |
| 162 scoped_refptr<net::IOBuffer> incoming_data; | |
| 163 size_t length = 0; | |
| 164 size_t total_length = 0; | |
| 165 size_t num_buffers = 0; | |
| 166 content::ByteStreamOutput::StreamState state( | |
| 167 content::ByteStreamOutput::STREAM_EMPTY); | |
| 168 content::DownloadInterruptReason reason = | |
| 169 content::DOWNLOAD_INTERRUPT_REASON_NONE; | |
| 170 base::TimeDelta delta(base::TimeDelta::FromMilliseconds(1000)); | |
|
benjhayden
2012/05/21 13:44:14
kConstant, please.
Also, why 1s?
Randy Smith (Not in Mondays)
2012/05/23 03:33:15
Done.
| |
| 171 | |
| 172 // Take care of any file local activity required. | |
| 173 do { | |
| 174 state = input_pipe_->Read(&incoming_data, &length); | |
|
benjhayden
2012/05/21 13:44:14
Would you mind changing |length| to |incoming_data
Randy Smith (Not in Mondays)
2012/05/23 03:33:15
Done.
| |
| 175 | |
| 176 net::Error result = net::OK; | |
| 177 switch (state) { | |
| 178 case content::ByteStreamOutput::STREAM_EMPTY: | |
| 179 break; | |
| 180 case content::ByteStreamOutput::STREAM_HAS_DATA: | |
| 181 ++num_buffers; | |
| 182 result = AppendDataToFile(incoming_data.get()->data(), length); | |
| 183 total_length += length; | |
| 184 reason = content::ConvertNetErrorToInterruptReason( | |
| 185 result, content::DOWNLOAD_INTERRUPT_FROM_DISK); | |
| 186 break; | |
| 187 case content::ByteStreamOutput::STREAM_COMPLETE: | |
| 188 reason = input_pipe_->GetStatus(); | |
| 189 SendUpdate(); | |
| 190 file_.Finish(); | |
| 191 update_timer_.reset(); | |
| 192 break; | |
| 193 default: | |
| 194 NOTREACHED(); | |
| 195 break; | |
| 196 } | |
| 197 now = base::Time::Now(); | |
| 198 } while (state == content::ByteStreamOutput::STREAM_HAS_DATA && | |
| 199 reason == content::DOWNLOAD_INTERRUPT_REASON_NONE && | |
| 200 now - start <= delta); | |
| 201 | |
| 202 // If we're stopping to yield the thread, post a task so we come back. | |
|
benjhayden
2012/05/21 13:44:14
Why is it necessary to yield the thread?
Randy Smith (Not in Mondays)
2012/05/23 03:33:15
That's a complex question, to which the answer is
benjhayden
2012/05/24 15:35:56
I don't quite understand why the file thread doesn
| |
| 203 if (state == content::ByteStreamOutput::STREAM_HAS_DATA && | |
| 204 now - start > delta) { | |
| 205 BrowserThread::PostTask( | |
| 206 BrowserThread::FILE, FROM_HERE, | |
| 207 base::Bind(&DownloadFileImpl::PipeActive, weak_factory_.GetWeakPtr())); | |
| 208 } | |
| 209 | |
| 210 if (total_length) | |
| 211 download_stats::RecordFileThreadReceiveBuffers(total_length); | |
| 212 | |
| 213 download_stats::RecordContiguousWriteTime(now - start); | |
| 214 | |
| 215 // Take care of communication with our controller. | |
| 216 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 217 // Error case for both upstream source and file write. | |
| 218 // Shut down processing and signal an error to our controller. | |
| 219 // Our controller will clean us up. | |
| 220 input_pipe_->RegisterCallback(base::Closure()); | |
| 221 weak_factory_.InvalidateWeakPtrs(); | |
| 222 if (download_manager_.get()) { | |
| 223 BrowserThread::PostTask( | |
| 224 BrowserThread::UI, FROM_HERE, | |
| 225 base::Bind(&DownloadManager::OnDownloadInterrupted, | |
| 226 download_manager_, id_.local(), | |
| 227 BytesSoFar(), GetHashState(), reason)); | |
| 228 } | |
| 229 } else if (state == content::ByteStreamOutput::STREAM_COMPLETE) { | |
| 230 // Signal successful completion and shut down processing. | |
| 231 input_pipe_->RegisterCallback(base::Closure()); | |
| 232 weak_factory_.InvalidateWeakPtrs(); | |
| 233 if (download_manager_.get()) { | |
| 234 std::string hash; | |
| 235 if (!GetHash(&hash) || file_.IsEmptyHash(hash)) | |
| 236 hash.clear(); | |
| 237 BrowserThread::PostTask( | |
| 238 BrowserThread::UI, FROM_HERE, | |
| 239 base::Bind(&DownloadManager::OnResponseCompleted, | |
| 240 download_manager_, id_.local(), | |
| 241 BytesSoFar(), hash)); | |
| 242 } | |
| 243 } | |
| 244 if (bound_net_log_.IsLoggingAllEvents()) { | |
| 245 bound_net_log_.AddEvent( | |
| 246 net::NetLog::TYPE_DOWNLOAD_PIPE_DRAINED, | |
| 247 make_scoped_refptr(new download_net_logs::FilePipeDrainedParameters( | |
| 248 total_length, num_buffers))); | |
| 249 } | |
| 250 } | |
| 251 | |
| 138 void DownloadFileImpl::SendUpdate() { | 252 void DownloadFileImpl::SendUpdate() { |
| 139 if (download_manager_.get()) { | 253 if (download_manager_.get()) { |
| 140 BrowserThread::PostTask( | 254 BrowserThread::PostTask( |
| 141 BrowserThread::UI, FROM_HERE, | 255 BrowserThread::UI, FROM_HERE, |
| 142 base::Bind(&DownloadManager::UpdateDownload, | 256 base::Bind(&DownloadManager::UpdateDownload, |
| 143 download_manager_, id_.local(), | 257 download_manager_, id_.local(), |
| 144 BytesSoFar(), CurrentSpeed(), GetHashState())); | 258 BytesSoFar(), CurrentSpeed(), GetHashState())); |
| 145 } | 259 } |
| 146 } | 260 } |
| OLD | NEW |