| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 186 |
| 187 } // namespace | 187 } // namespace |
| 188 | 188 |
| 189 // This will initialize the entire array to zero. | 189 // This will initialize the entire array to zero. |
| 190 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; | 190 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; |
| 191 | 191 |
| 192 BaseFile::BaseFile(const FilePath& full_path, | 192 BaseFile::BaseFile(const FilePath& full_path, |
| 193 const GURL& source_url, | 193 const GURL& source_url, |
| 194 const GURL& referrer_url, | 194 const GURL& referrer_url, |
| 195 int64 received_bytes, | 195 int64 received_bytes, |
| 196 const Pickle& hash_state, |
| 196 const linked_ptr<net::FileStream>& file_stream) | 197 const linked_ptr<net::FileStream>& file_stream) |
| 197 : full_path_(full_path), | 198 : full_path_(full_path), |
| 198 source_url_(source_url), | 199 source_url_(source_url), |
| 199 referrer_url_(referrer_url), | 200 referrer_url_(referrer_url), |
| 200 file_stream_(file_stream), | 201 file_stream_(file_stream), |
| 201 bytes_so_far_(received_bytes), | 202 bytes_so_far_(received_bytes), |
| 202 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), | 203 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), |
| 203 calculate_hash_(false), | 204 calculate_hash_(false), |
| 205 initial_hash_state_(hash_state), |
| 204 detached_(false) { | 206 detached_(false) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 206 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); | 208 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); |
| 207 if (file_stream_.get()) | 209 if (file_stream_.get()) |
| 208 file_stream_->EnableErrorStatistics(); | 210 file_stream_->EnableErrorStatistics(); |
| 209 } | 211 } |
| 210 | 212 |
| 211 BaseFile::~BaseFile() { | 213 BaseFile::~BaseFile() { |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 213 if (detached_) | 215 if (detached_) |
| 214 Close(); | 216 Close(); |
| 215 else | 217 else |
| 216 Cancel(); // Will delete the file. | 218 Cancel(); // Will delete the file. |
| 217 } | 219 } |
| 218 | 220 |
| 219 net::Error BaseFile::Initialize(bool calculate_hash) { | 221 net::Error BaseFile::Initialize(bool calculate_hash) { |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 221 DCHECK(!detached_); | 223 DCHECK(!detached_); |
| 222 | 224 |
| 223 calculate_hash_ = calculate_hash; | 225 calculate_hash_ = calculate_hash; |
| 224 | 226 |
| 225 if (calculate_hash_) | 227 if (calculate_hash_) { |
| 226 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 228 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 229 if ((initial_hash_state_.size() != 0) && (bytes_so_far_ > 0)) |
| 230 SetSha256HashState(&initial_hash_state_); |
| 231 } |
| 227 | 232 |
| 228 if (full_path_.empty()) { | 233 if (full_path_.empty()) { |
| 229 FilePath temp_file; | 234 FilePath temp_file; |
| 230 FilePath download_dir = | 235 FilePath download_dir = |
| 231 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); | 236 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
| 232 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) && | 237 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) && |
| 233 !file_util::CreateTemporaryFile(&temp_file)) { | 238 !file_util::CreateTemporaryFile(&temp_file)) { |
| 234 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); | 239 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); |
| 235 } | 240 } |
| 236 full_path_ = temp_file; | 241 full_path_ = temp_file; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 if (calculate_hash_) | 385 if (calculate_hash_) |
| 381 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 386 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
| 382 | 387 |
| 383 Close(); | 388 Close(); |
| 384 } | 389 } |
| 385 | 390 |
| 386 bool BaseFile::GetSha256Hash(std::string* hash) { | 391 bool BaseFile::GetSha256Hash(std::string* hash) { |
| 387 DCHECK(!detached_); | 392 DCHECK(!detached_); |
| 388 hash->assign(reinterpret_cast<const char*>(sha256_hash_), | 393 hash->assign(reinterpret_cast<const char*>(sha256_hash_), |
| 389 sizeof(sha256_hash_)); | 394 sizeof(sha256_hash_)); |
| 390 return (calculate_hash_ && !in_progress()); | 395 return (calculate_hash_); |
| 396 } |
| 397 |
| 398 bool BaseFile::GetSha256HashState(Pickle* hash_state) { |
| 399 return secure_hash_->Serialize(hash_state); |
| 400 } |
| 401 |
| 402 bool BaseFile::SetSha256HashState(Pickle* hash_state) { |
| 403 void* data_iterator = NULL; |
| 404 return secure_hash_->Deserialize(&data_iterator, hash_state); |
| 391 } | 405 } |
| 392 | 406 |
| 393 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { | 407 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { |
| 394 return (hash.size() == kSha256HashLen && | 408 return (hash.size() == kSha256HashLen && |
| 395 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); | 409 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); |
| 396 } | 410 } |
| 397 | 411 |
| 398 void BaseFile::AnnotateWithSourceInformation() { | 412 void BaseFile::AnnotateWithSourceInformation() { |
| 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 400 DCHECK(!detached_); | 414 DCHECK(!detached_); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 std::string BaseFile::DebugString() const { | 478 std::string BaseFile::DebugString() const { |
| 465 return base::StringPrintf("{ source_url_ = \"%s\"" | 479 return base::StringPrintf("{ source_url_ = \"%s\"" |
| 466 " full_path_ = \"%" PRFilePath "\"" | 480 " full_path_ = \"%" PRFilePath "\"" |
| 467 " bytes_so_far_ = %" PRId64 | 481 " bytes_so_far_ = %" PRId64 |
| 468 " detached_ = %c }", | 482 " detached_ = %c }", |
| 469 source_url_.spec().c_str(), | 483 source_url_.spec().c_str(), |
| 470 full_path_.value().c_str(), | 484 full_path_.value().c_str(), |
| 471 bytes_so_far_, | 485 bytes_so_far_, |
| 472 detached_ ? 'T' : 'F'); | 486 detached_ ? 'T' : 'F'); |
| 473 } | 487 } |
| OLD | NEW |