Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/download/base_file.h" | 5 #include "chrome/browser/download/base_file.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/string_util.h" | |
| 9 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/third_party/nss/blapi.h" | |
| 13 #include "base/third_party/nss/sha256.h" | |
| 10 #include "net/base/file_stream.h" | 14 #include "net/base/file_stream.h" |
| 11 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 12 #include "chrome/browser/browser_thread.h" | 16 #include "chrome/browser/browser_thread.h" |
| 13 #include "chrome/browser/download/download_util.h" | 17 #include "chrome/browser/download/download_util.h" |
| 14 | 18 |
| 15 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 16 #include "app/win/win_util.h" | 20 #include "app/win/win_util.h" |
| 17 #include "chrome/common/win_safe_util.h" | 21 #include "chrome/common/win_safe_util.h" |
| 18 #elif defined(OS_MACOSX) | 22 #elif defined(OS_MACOSX) |
| 19 #include "chrome/browser/ui/cocoa/file_metadata.h" | 23 #include "chrome/browser/ui/cocoa/file_metadata.h" |
| 20 #endif | 24 #endif |
| 21 | 25 |
| 22 BaseFile::BaseFile(const FilePath& full_path, | 26 BaseFile::BaseFile(const FilePath& full_path, |
| 23 const GURL& source_url, | 27 const GURL& source_url, |
| 24 const GURL& referrer_url, | 28 const GURL& referrer_url, |
| 25 int64 received_bytes, | 29 int64 received_bytes, |
| 26 const linked_ptr<net::FileStream>& file_stream) | 30 const linked_ptr<net::FileStream>& file_stream, |
| 31 bool calculate_hash) | |
| 27 : full_path_(full_path), | 32 : full_path_(full_path), |
| 28 path_renamed_(false), | 33 path_renamed_(false), |
| 29 source_url_(source_url), | 34 source_url_(source_url), |
| 30 referrer_url_(referrer_url), | 35 referrer_url_(referrer_url), |
| 31 file_stream_(file_stream), | 36 file_stream_(file_stream), |
| 32 bytes_so_far_(received_bytes), | 37 bytes_so_far_(received_bytes), |
| 33 power_save_blocker_(true) { | 38 power_save_blocker_(true), |
| 39 calculate_hash_(calculate_hash), | |
| 40 sha_context_(NULL) { | |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 35 } | 42 } |
| 36 | 43 |
| 37 BaseFile::~BaseFile() { | 44 BaseFile::~BaseFile() { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 39 if (in_progress()) | 46 if (in_progress()) |
| 40 Cancel(); | 47 Cancel(); |
| 41 Close(); | 48 Close(); |
| 42 } | 49 } |
| 43 | 50 |
| 44 bool BaseFile::Initialize() { | 51 bool BaseFile::Initialize() { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 53 | |
| 54 if (calculate_hash_) { | |
| 55 sha_context_.reset(new SHA256Context); | |
| 56 SHA256_Begin(sha_context_.get()); | |
| 57 } | |
| 58 | |
| 46 if (!full_path_.empty() || | 59 if (!full_path_.empty() || |
| 47 download_util::CreateTemporaryFileForDownload(&full_path_)) | 60 download_util::CreateTemporaryFileForDownload(&full_path_)) |
| 48 return Open(); | 61 return Open(); |
| 49 return false; | 62 return false; |
| 50 } | 63 } |
| 51 | 64 |
| 52 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) { | 65 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) { |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 54 | 67 |
| 55 if (!file_stream_.get()) | 68 if (!file_stream_.get()) |
| 56 return false; | 69 return false; |
| 57 | 70 |
| 58 // TODO(phajdan.jr): get rid of this check. | 71 // TODO(phajdan.jr): get rid of this check. |
| 59 if (data_len == 0) | 72 if (data_len == 0) |
| 60 return true; | 73 return true; |
| 61 | 74 |
| 75 if (calculate_hash_) { | |
| 76 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.
| |
| 77 reinterpret_cast<const unsigned char*>(data), | |
| 78 data_len); | |
| 79 } | |
| 80 | |
| 62 bytes_so_far_ += data_len; | 81 bytes_so_far_ += data_len; |
| 63 | 82 |
| 64 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 | 83 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 |
| 65 size_t written = file_stream_->Write(data, data_len, NULL); | 84 size_t written = file_stream_->Write(data, data_len, NULL); |
| 66 return (written == data_len); | 85 return (written == data_len); |
| 67 } | 86 } |
| 68 | 87 |
| 69 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) { | 88 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 71 | 90 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 void BaseFile::Cancel() { | 152 void BaseFile::Cancel() { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 Close(); | 154 Close(); |
| 136 if (!full_path_.empty()) | 155 if (!full_path_.empty()) |
| 137 file_util::Delete(full_path_, false); | 156 file_util::Delete(full_path_, false); |
| 138 } | 157 } |
| 139 | 158 |
| 140 void BaseFile::Finish() { | 159 void BaseFile::Finish() { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 142 Close(); | 161 Close(); |
| 162 if (calculate_hash_) { | |
| 163 SHA256_End(sha_context_.get(), sha256_hash_, NULL, kSha256HashLen_); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 bool BaseFile::GetSha256Hash(std::string* hash) { | |
| 168 if (!calculate_hash_) | |
| 169 return false; | |
| 170 *hash = | |
| 171 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
| |
| 172 return true; | |
| 143 } | 173 } |
| 144 | 174 |
| 145 void BaseFile::AnnotateWithSourceInformation() { | 175 void BaseFile::AnnotateWithSourceInformation() { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 147 #if defined(OS_WIN) | 177 #if defined(OS_WIN) |
| 148 // Sets the Zone to tell Windows that this file comes from the internet. | 178 // Sets the Zone to tell Windows that this file comes from the internet. |
| 149 // We ignore the return value because a failure is not fatal. | 179 // We ignore the return value because a failure is not fatal. |
| 150 win_util::SetInternetZoneIdentifier(full_path_); | 180 win_util::SetInternetZoneIdentifier(full_path_); |
| 151 #elif defined(OS_MACOSX) | 181 #elif defined(OS_MACOSX) |
| 152 file_metadata::AddQuarantineMetadataToFile(full_path_, source_url_, | 182 file_metadata::AddQuarantineMetadataToFile(full_path_, source_url_, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 file_stream_->Close(); | 225 file_stream_->Close(); |
| 196 file_stream_.reset(); | 226 file_stream_.reset(); |
| 197 } | 227 } |
| 198 } | 228 } |
| 199 | 229 |
| 200 std::string BaseFile::DebugString() const { | 230 std::string BaseFile::DebugString() const { |
| 201 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }", | 231 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }", |
| 202 source_url_.spec().c_str(), | 232 source_url_.spec().c_str(), |
| 203 full_path_.value().c_str()); | 233 full_path_.value().c_str()); |
| 204 } | 234 } |
| OLD | NEW |