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

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

Issue 8404049: Added member data to classes to support download resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with trunk Created 9 years 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
« no previous file with comments | « content/browser/download/base_file.h ('k') | content/browser/download/base_file_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/pickle.h"
10 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
11 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
12 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
13 #include "content/browser/download/download_stats.h" 14 #include "content/browser/download/download_stats.h"
14 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/content_browser_client.h" 16 #include "content/public/browser/content_browser_client.h"
16 #include "crypto/secure_hash.h" 17 #include "crypto/secure_hash.h"
17 #include "net/base/file_stream.h" 18 #include "net/base/file_stream.h"
18 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
19 20
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 187
187 } // namespace 188 } // namespace
188 189
189 // This will initialize the entire array to zero. 190 // This will initialize the entire array to zero.
190 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; 191 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 };
191 192
192 BaseFile::BaseFile(const FilePath& full_path, 193 BaseFile::BaseFile(const FilePath& full_path,
193 const GURL& source_url, 194 const GURL& source_url,
194 const GURL& referrer_url, 195 const GURL& referrer_url,
195 int64 received_bytes, 196 int64 received_bytes,
197 bool calculate_hash,
198 const std::string& hash_state,
196 const linked_ptr<net::FileStream>& file_stream) 199 const linked_ptr<net::FileStream>& file_stream)
197 : full_path_(full_path), 200 : full_path_(full_path),
198 source_url_(source_url), 201 source_url_(source_url),
199 referrer_url_(referrer_url), 202 referrer_url_(referrer_url),
200 file_stream_(file_stream), 203 file_stream_(file_stream),
201 bytes_so_far_(received_bytes), 204 bytes_so_far_(received_bytes),
202 start_tick_(base::TimeTicks::Now()), 205 start_tick_(base::TimeTicks::Now()),
203 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), 206 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep),
204 calculate_hash_(false), 207 calculate_hash_(calculate_hash),
205 detached_(false) { 208 detached_(false) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
207 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); 210 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen);
208 if (file_stream_.get()) 211 if (file_stream_.get())
209 file_stream_->EnableErrorStatistics(); 212 file_stream_->EnableErrorStatistics();
213
214 if (calculate_hash_) {
215 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256));
216 if ((bytes_so_far_ > 0) && // Not starting at the beginning.
217 (hash_state != "") && // Reasonably sure we have a hash state.
218 (!IsEmptyHash(hash_state))) {
219 SetHashState(hash_state);
220 }
221 }
210 } 222 }
211 223
212 BaseFile::~BaseFile() { 224 BaseFile::~BaseFile() {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
214 if (detached_) 226 if (detached_)
215 Close(); 227 Close();
216 else 228 else
217 Cancel(); // Will delete the file. 229 Cancel(); // Will delete the file.
218 } 230 }
219 231
220 net::Error BaseFile::Initialize(bool calculate_hash) { 232 net::Error BaseFile::Initialize() {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
222 DCHECK(!detached_); 234 DCHECK(!detached_);
223 235
224 calculate_hash_ = calculate_hash;
225
226 if (calculate_hash_)
227 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256));
228
229 if (full_path_.empty()) { 236 if (full_path_.empty()) {
230 FilePath temp_file; 237 FilePath temp_file;
231 FilePath download_dir = 238 FilePath download_dir =
232 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); 239 content::GetContentClient()->browser()->GetDefaultDownloadDirectory();
233 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) && 240 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) &&
234 !file_util::CreateTemporaryFile(&temp_file)) { 241 !file_util::CreateTemporaryFile(&temp_file)) {
235 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); 242 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND);
236 } 243 }
237 full_path_ = temp_file; 244 full_path_ = temp_file;
238 } 245 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 390
384 void BaseFile::Finish() { 391 void BaseFile::Finish() {
385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
386 393
387 if (calculate_hash_) 394 if (calculate_hash_)
388 secure_hash_->Finish(sha256_hash_, kSha256HashLen); 395 secure_hash_->Finish(sha256_hash_, kSha256HashLen);
389 396
390 Close(); 397 Close();
391 } 398 }
392 399
393 bool BaseFile::GetSha256Hash(std::string* hash) { 400 bool BaseFile::GetHash(std::string* hash) {
394 DCHECK(!detached_); 401 DCHECK(!detached_);
395 hash->assign(reinterpret_cast<const char*>(sha256_hash_), 402 hash->assign(reinterpret_cast<const char*>(sha256_hash_),
396 sizeof(sha256_hash_)); 403 sizeof(sha256_hash_));
397 return (calculate_hash_ && !in_progress()); 404 return (calculate_hash_ && !in_progress());
398 } 405 }
399 406
400 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { 407 std::string BaseFile::GetHashState() {
408 if (!calculate_hash_)
409 return "";
410
411 Pickle hash_state;
412 if (!secure_hash_->Serialize(&hash_state))
413 return "";
414
415 return std::string(reinterpret_cast<const char*>(hash_state.data()),
416 hash_state.size());
417 }
418
419 bool BaseFile::SetHashState(const std::string& hash_state_bytes) {
420 if (!calculate_hash_)
421 return false;
422
423 Pickle hash_state(hash_state_bytes.c_str(), hash_state_bytes.size());
424 void* data_iterator = NULL;
425
426 return secure_hash_->Deserialize(&data_iterator, &hash_state);
427 }
428
429 bool BaseFile::IsEmptyHash(const std::string& hash) {
401 return (hash.size() == kSha256HashLen && 430 return (hash.size() == kSha256HashLen &&
402 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); 431 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen)));
403 } 432 }
404 433
405 void BaseFile::AnnotateWithSourceInformation() { 434 void BaseFile::AnnotateWithSourceInformation() {
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
407 DCHECK(!detached_); 436 DCHECK(!detached_);
408 437
409 #if defined(OS_WIN) 438 #if defined(OS_WIN)
410 // Sets the Zone to tell Windows that this file comes from the internet. 439 // Sets the Zone to tell Windows that this file comes from the internet.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { 511 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const {
483 base::TimeDelta diff = current_time - start_tick_; 512 base::TimeDelta diff = current_time - start_tick_;
484 int64 diff_ms = diff.InMilliseconds(); 513 int64 diff_ms = diff.InMilliseconds();
485 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; 514 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms;
486 } 515 }
487 516
488 int64 BaseFile::CurrentSpeed() const { 517 int64 BaseFile::CurrentSpeed() const {
489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
490 return CurrentSpeedAtTime(base::TimeTicks::Now()); 519 return CurrentSpeedAtTime(base::TimeTicks::Now());
491 } 520 }
OLDNEW
« no previous file with comments | « content/browser/download/base_file.h ('k') | content/browser/download/base_file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698