Chromium Code Reviews| Index: chrome/browser/download/base_file.cc |
| =================================================================== |
| --- chrome/browser/download/base_file.cc (revision 70630) |
| +++ chrome/browser/download/base_file.cc (working copy) |
| @@ -6,7 +6,11 @@ |
| #include "base/file_util.h" |
| #include "base/logging.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/string_util.h" |
| #include "base/stringprintf.h" |
| +#include "base/third_party/nss/blapi.h" |
| +#include "base/third_party/nss/sha256.h" |
| #include "net/base/file_stream.h" |
| #include "net/base/net_errors.h" |
| #include "chrome/browser/browser_thread.h" |
| @@ -23,14 +27,17 @@ |
| const GURL& source_url, |
| const GURL& referrer_url, |
| int64 received_bytes, |
| - const linked_ptr<net::FileStream>& file_stream) |
| + const linked_ptr<net::FileStream>& file_stream, |
| + bool calculate_hash) |
| : full_path_(full_path), |
| path_renamed_(false), |
| source_url_(source_url), |
| referrer_url_(referrer_url), |
| file_stream_(file_stream), |
| bytes_so_far_(received_bytes), |
| - power_save_blocker_(true) { |
| + power_save_blocker_(true), |
| + calculate_hash_(calculate_hash), |
| + sha_context_(NULL) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| } |
| @@ -43,6 +50,12 @@ |
| bool BaseFile::Initialize() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + if (calculate_hash_) { |
| + sha_context_.reset(new SHA256Context); |
| + SHA256_Begin(sha_context_.get()); |
| + } |
| + |
| if (!full_path_.empty() || |
| download_util::CreateTemporaryFileForDownload(&full_path_)) |
| return Open(); |
| @@ -59,6 +72,12 @@ |
| if (data_len == 0) |
| return true; |
| + if (calculate_hash_) { |
| + SHA256_Update(sha_context_.get(), |
|
Paweł Hajdan Jr.
2011/01/11 08:28:21
Hmm, I wonder if we should somehow check result of
lzheng
2011/01/12 02:11:54
Done.
|
| + reinterpret_cast<const unsigned char*>(data), |
| + data_len); |
| + } |
| + |
| bytes_so_far_ += data_len; |
| // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 |
| @@ -140,8 +159,19 @@ |
| void BaseFile::Finish() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| Close(); |
| + if (calculate_hash_) { |
| + SHA256_End(sha_context_.get(), sha256_hash_, NULL, kSha256HashLen_); |
| + } |
| } |
| +bool BaseFile::GetSha256Hash(std::string* hash) { |
| + if (!calculate_hash_) |
| + return false; |
| + *hash = |
| + StringToLowerASCII(base::HexEncode(sha256_hash_, sizeof(sha256_hash_))); |
|
Randy Smith (Not in Mondays)
2011/01/11 18:29:51
This means that the hash can be grabbed at any tim
|
| + return true; |
| +} |
| + |
| void BaseFile::AnnotateWithSourceInformation() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| #if defined(OS_WIN) |