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/base_file.h" | 5 #include "content/browser/download/base_file.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 const net::BoundNetLog& bound_net_log) | 213 const net::BoundNetLog& bound_net_log) |
214 : full_path_(full_path), | 214 : full_path_(full_path), |
215 source_url_(source_url), | 215 source_url_(source_url), |
216 referrer_url_(referrer_url), | 216 referrer_url_(referrer_url), |
217 file_stream_(file_stream.Pass()), | 217 file_stream_(file_stream.Pass()), |
218 bytes_so_far_(received_bytes), | 218 bytes_so_far_(received_bytes), |
219 start_tick_(base::TimeTicks::Now()), | 219 start_tick_(base::TimeTicks::Now()), |
220 calculate_hash_(calculate_hash), | 220 calculate_hash_(calculate_hash), |
221 detached_(false), | 221 detached_(false), |
222 bound_net_log_(bound_net_log) { | 222 bound_net_log_(bound_net_log) { |
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
224 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); | 223 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); |
225 if (file_stream_.get()) { | |
226 file_stream_->SetBoundNetLogSource(bound_net_log_); | |
227 file_stream_->EnableErrorStatistics(); | |
228 } | |
229 | |
230 if (calculate_hash_) { | 224 if (calculate_hash_) { |
231 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 225 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
232 if ((bytes_so_far_ > 0) && // Not starting at the beginning. | 226 if ((bytes_so_far_ > 0) && // Not starting at the beginning. |
233 (hash_state != "") && // Reasonably sure we have a hash state. | 227 (hash_state != "") && // Reasonably sure we have a hash state. |
234 (!IsEmptyHash(hash_state))) { | 228 (!IsEmptyHash(hash_state))) { |
235 SetHashState(hash_state); | 229 SetHashState(hash_state); |
236 } | 230 } |
237 } | 231 } |
238 } | 232 } |
239 | 233 |
240 BaseFile::~BaseFile() { | 234 BaseFile::~BaseFile() { |
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
242 if (detached_) | 236 if (detached_) |
243 Close(); | 237 Close(); |
244 else | 238 else |
245 Cancel(); // Will delete the file. | 239 Cancel(); // Will delete the file. |
246 } | 240 } |
247 | 241 |
248 net::Error BaseFile::Initialize(const FilePath& default_directory) { | 242 net::Error BaseFile::Initialize(const FilePath& default_directory) { |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
250 DCHECK(!detached_); | 244 DCHECK(!detached_); |
251 | 245 |
| 246 if (file_stream_.get()) { |
| 247 file_stream_->SetBoundNetLogSource(bound_net_log_); |
| 248 file_stream_->EnableErrorStatistics(); |
| 249 } |
| 250 |
252 if (full_path_.empty()) { | 251 if (full_path_.empty()) { |
253 FilePath initial_directory(default_directory); | 252 FilePath initial_directory(default_directory); |
254 FilePath temp_file; | 253 FilePath temp_file; |
255 if (initial_directory.empty()) { | 254 if (initial_directory.empty()) { |
256 initial_directory = | 255 initial_directory = |
257 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); | 256 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
258 } | 257 } |
259 // |initial_directory| can still be empty if ContentBrowserClient returned | 258 // |initial_directory| can still be empty if ContentBrowserClient returned |
260 // an empty path for the downloads directory. | 259 // an empty path for the downloads directory. |
261 if ((initial_directory.empty() || | 260 if ((initial_directory.empty() || |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { | 555 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { |
557 base::TimeDelta diff = current_time - start_tick_; | 556 base::TimeDelta diff = current_time - start_tick_; |
558 int64 diff_ms = diff.InMilliseconds(); | 557 int64 diff_ms = diff.InMilliseconds(); |
559 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; | 558 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; |
560 } | 559 } |
561 | 560 |
562 int64 BaseFile::CurrentSpeed() const { | 561 int64 BaseFile::CurrentSpeed() const { |
563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
564 return CurrentSpeedAtTime(base::TimeTicks::Now()); | 563 return CurrentSpeedAtTime(base::TimeTicks::Now()); |
565 } | 564 } |
OLD | NEW |