Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/download/base_file.cc

Issue 6023006: Add support to sha256 hash the downloaded file.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/third_party/nss/blapi.h"
11 #include "base/third_party/nss/sha256.h"
10 #include "net/base/file_stream.h" 12 #include "net/base/file_stream.h"
11 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
12 #include "chrome/browser/browser_thread.h" 14 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/download/download_util.h" 15 #include "chrome/browser/download/download_util.h"
14 16
15 #if defined(OS_WIN) 17 #if defined(OS_WIN)
16 #include "app/win/win_util.h" 18 #include "app/win/win_util.h"
17 #include "chrome/common/win_safe_util.h" 19 #include "chrome/common/win_safe_util.h"
18 #elif defined(OS_MACOSX) 20 #elif defined(OS_MACOSX)
19 #include "chrome/browser/ui/cocoa/file_metadata.h" 21 #include "chrome/browser/ui/cocoa/file_metadata.h"
20 #endif 22 #endif
21 23
22 BaseFile::BaseFile(const FilePath& full_path, 24 BaseFile::BaseFile(const FilePath& full_path,
23 const GURL& source_url, 25 const GURL& source_url,
24 const GURL& referrer_url, 26 const GURL& referrer_url,
25 int64 received_bytes, 27 int64 received_bytes,
26 const linked_ptr<net::FileStream>& file_stream) 28 const linked_ptr<net::FileStream>& file_stream)
27 : full_path_(full_path), 29 : full_path_(full_path),
28 path_renamed_(false), 30 path_renamed_(false),
29 source_url_(source_url), 31 source_url_(source_url),
30 referrer_url_(referrer_url), 32 referrer_url_(referrer_url),
31 file_stream_(file_stream), 33 file_stream_(file_stream),
32 bytes_so_far_(received_bytes), 34 bytes_so_far_(received_bytes),
33 power_save_blocker_(true) { 35 power_save_blocker_(true),
36 calculate_hash_(false),
37 sha_context_(NULL) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
35 } 39 }
36 40
37 BaseFile::~BaseFile() { 41 BaseFile::~BaseFile() {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
39 if (in_progress()) 43 if (in_progress())
40 Cancel(); 44 Cancel();
41 Close(); 45 Close();
42 } 46 }
43 47
44 bool BaseFile::Initialize() { 48 bool BaseFile::Initialize(bool calculate_hash) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
50
51 calculate_hash_ = calculate_hash;
52
53 if (calculate_hash_) {
54 sha_context_.reset(new SHA256Context);
55 SHA256_Begin(sha_context_.get());
56 }
57
46 if (!full_path_.empty() || 58 if (!full_path_.empty() ||
47 download_util::CreateTemporaryFileForDownload(&full_path_)) 59 download_util::CreateTemporaryFileForDownload(&full_path_))
48 return Open(); 60 return Open();
49 return false; 61 return false;
50 } 62 }
51 63
52 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) { 64 bool BaseFile::AppendDataToFile(const char* data, size_t data_len) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
54 66
55 if (!file_stream_.get()) 67 if (!file_stream_.get())
56 return false; 68 return false;
57 69
58 // TODO(phajdan.jr): get rid of this check. 70 // TODO(phajdan.jr): get rid of this check.
59 if (data_len == 0) 71 if (data_len == 0)
60 return true; 72 return true;
61 73
62 bytes_so_far_ += data_len; 74 bytes_so_far_ += data_len;
63 75
64 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355 76 // TODO(phajdan.jr): handle errors on file writes. http://crbug.com/58355
65 size_t written = file_stream_->Write(data, data_len, NULL); 77 size_t written = file_stream_->Write(data, data_len, NULL);
66 return (written == data_len); 78 if (written != data_len)
79 return false;
80
81 if (calculate_hash_) {
82 SHA256_Update(sha_context_.get(),
83 reinterpret_cast<const unsigned char*>(data),
84 data_len);
85 }
86
87 return true;
67 } 88 }
68 89
69 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) { 90 bool BaseFile::Rename(const FilePath& new_path, bool is_final_rename) {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
71 92
72 // Save the information whether the download is in progress because 93 // Save the information whether the download is in progress because
73 // it will be overwritten by closing the file. 94 // it will be overwritten by closing the file.
74 bool saved_in_progress = in_progress(); 95 bool saved_in_progress = in_progress();
75 96
76 // If the new path is same as the old one, there is no need to perform the 97 // If the new path is same as the old one, there is no need to perform the
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 153
133 void BaseFile::Cancel() { 154 void BaseFile::Cancel() {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
135 Close(); 156 Close();
136 if (!full_path_.empty()) 157 if (!full_path_.empty())
137 file_util::Delete(full_path_, false); 158 file_util::Delete(full_path_, false);
138 } 159 }
139 160
140 void BaseFile::Finish() { 161 void BaseFile::Finish() {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
163
164 if (calculate_hash_)
165 SHA256_End(sha_context_.get(), sha256_hash_, NULL, kSha256HashLen);
166
142 Close(); 167 Close();
143 } 168 }
144 169
170 bool BaseFile::GetSha256Hash(std::string* hash) {
171 if (!calculate_hash_ || in_progress())
172 return false;
173 hash->assign(reinterpret_cast<const char*>(sha256_hash_),
174 sizeof(sha256_hash_));
175 return true;
176 }
177
145 void BaseFile::AnnotateWithSourceInformation() { 178 void BaseFile::AnnotateWithSourceInformation() {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
147 #if defined(OS_WIN) 180 #if defined(OS_WIN)
148 // Sets the Zone to tell Windows that this file comes from the internet. 181 // 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. 182 // We ignore the return value because a failure is not fatal.
150 win_util::SetInternetZoneIdentifier(full_path_); 183 win_util::SetInternetZoneIdentifier(full_path_);
151 #elif defined(OS_MACOSX) 184 #elif defined(OS_MACOSX)
152 file_metadata::AddQuarantineMetadataToFile(full_path_, source_url_, 185 file_metadata::AddQuarantineMetadataToFile(full_path_, source_url_,
153 referrer_url_); 186 referrer_url_);
154 file_metadata::AddOriginMetadataToFile(full_path_, source_url_, 187 file_metadata::AddOriginMetadataToFile(full_path_, source_url_,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 file_stream_->Close(); 228 file_stream_->Close();
196 file_stream_.reset(); 229 file_stream_.reset();
197 } 230 }
198 } 231 }
199 232
200 std::string BaseFile::DebugString() const { 233 std::string BaseFile::DebugString() const {
201 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }", 234 return base::StringPrintf("{ source_url_ = \"%s\" full_path_ = \"%s\" }",
202 source_url_.spec().c_str(), 235 source_url_.spec().c_str(),
203 full_path_.value().c_str()); 236 full_path_.value().c_str());
204 } 237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698