| 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/sha2.h" |
| 9 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 10 #include "base/third_party/nss/blapi.h" | |
| 11 #include "base/third_party/nss/sha256.h" | |
| 12 #include "net/base/file_stream.h" | 11 #include "net/base/file_stream.h" |
| 13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 14 #include "chrome/browser/browser_thread.h" | 13 #include "chrome/browser/browser_thread.h" |
| 15 #include "chrome/browser/download/download_util.h" | 14 #include "chrome/browser/download/download_util.h" |
| 16 | 15 |
| 17 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 18 #include "app/win/win_util.h" | 17 #include "app/win/win_util.h" |
| 19 #include "chrome/common/win_safe_util.h" | 18 #include "chrome/common/win_safe_util.h" |
| 20 #elif defined(OS_MACOSX) | 19 #elif defined(OS_MACOSX) |
| 21 #include "chrome/browser/ui/cocoa/file_metadata.h" | 20 #include "chrome/browser/ui/cocoa/file_metadata.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 43 if (in_progress()) | 42 if (in_progress()) |
| 44 Cancel(); | 43 Cancel(); |
| 45 Close(); | 44 Close(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 bool BaseFile::Initialize(bool calculate_hash) { | 47 bool BaseFile::Initialize(bool calculate_hash) { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 50 | 49 |
| 51 calculate_hash_ = calculate_hash; | 50 calculate_hash_ = calculate_hash; |
| 52 | 51 |
| 53 if (calculate_hash_) { | 52 if (calculate_hash_) |
| 54 sha_context_.reset(new SHA256Context); | 53 sha_context_.reset(base::SHA256Context::Create()); |
| 55 SHA256_Begin(sha_context_.get()); | |
| 56 } | |
| 57 | 54 |
| 58 if (!full_path_.empty() || | 55 if (!full_path_.empty() || |
| 59 download_util::CreateTemporaryFileForDownload(&full_path_)) | 56 download_util::CreateTemporaryFileForDownload(&full_path_)) |
| 60 return Open(); | 57 return Open(); |
| 61 return false; | 58 return false; |
| 62 } | 59 } |
| 63 | 60 |
| 64 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) { | 61 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 66 | 63 |
| 67 if (!file_stream_.get()) | 64 if (!file_stream_.get()) |
| 68 return false; | 65 return false; |
| 69 | 66 |
| 70 // TODO(phajdan.jr): get rid of this check. | 67 // TODO(phajdan.jr): get rid of this check. |
| 71 if (data_len == 0) | 68 if (data_len == 0) |
| 72 return true; | 69 return true; |
| 73 | 70 |
| 74 bytes_so_far_ += data_len; | 71 bytes_so_far_ += data_len; |
| 75 | 72 |
| 76 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 | 73 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 |
| 77 size_t written = file_stream_->Write(data, data_len, NULL); | 74 size_t written = file_stream_->Write(data, data_len, NULL); |
| 78 if (written != data_len) | 75 if (written != data_len) |
| 79 return false; | 76 return false; |
| 80 | 77 |
| 81 if (calculate_hash_) { | 78 if (calculate_hash_) |
| 82 SHA256_Update(sha_context_.get(), | 79 sha_context_->Update(std::string(data, data_len)); |
| 83 reinterpret_cast<const unsigned char*>(data), | |
| 84 data_len); | |
| 85 } | |
| 86 | 80 |
| 87 return true; | 81 return true; |
| 88 } | 82 } |
| 89 | 83 |
| 90 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) { | 84 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 92 | 86 |
| 93 // Save the information whether the download is in progress because | 87 // Save the information whether the download is in progress because |
| 94 // it will be overwritten by closing the file. | 88 // it will be overwritten by closing the file. |
| 95 bool saved_in_progress = in_progress(); | 89 bool saved_in_progress = in_progress(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 156 Close(); | 150 Close(); |
| 157 if (!full_path_.empty()) | 151 if (!full_path_.empty()) |
| 158 file_util::Delete(full_path_, false); | 152 file_util::Delete(full_path_, false); |
| 159 } | 153 } |
| 160 | 154 |
| 161 void BaseFile::Finish() { | 155 void BaseFile::Finish() { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 163 | 157 |
| 164 if (calculate_hash_) | 158 if (calculate_hash_) |
| 165 SHA256_End(sha_context_.get(), sha256_hash_, NULL, kSha256HashLen); | 159 sha_context_->Finish(sha256_hash_, kSha256HashLen); |
| 166 | 160 |
| 167 Close(); | 161 Close(); |
| 168 } | 162 } |
| 169 | 163 |
| 170 bool BaseFile::GetSha256Hash(std::string* hash) { | 164 bool BaseFile::GetSha256Hash(std::string* hash) { |
| 171 if (!calculate_hash_ || in_progress()) | 165 if (!calculate_hash_ || in_progress()) |
| 172 return false; | 166 return false; |
| 173 hash->assign(reinterpret_cast<const char*>(sha256_hash_), | 167 hash->assign(reinterpret_cast<const char*>(sha256_hash_), |
| 174 sizeof(sha256_hash_)); | 168 sizeof(sha256_hash_)); |
| 175 return true; | 169 return true; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 file_stream_->Close(); | 222 file_stream_->Close(); |
| 229 file_stream_.reset(); | 223 file_stream_.reset(); |
| 230 } | 224 } |
| 231 } | 225 } |
| 232 | 226 |
| 233 std::string BaseFile::DebugString() const { | 227 std::string BaseFile::DebugString() const { |
| 234 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }", | 228 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }", |
| 235 source_url_.spec().c_str(), | 229 source_url_.spec().c_str(), |
| 236 full_path_.value().c_str()); | 230 full_path_.value().c_str()); |
| 237 } | 231 } |
| OLD | NEW |