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

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: Rearranged structures for greater consistency. Created 9 years, 1 month 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) 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 const std::string& hash_state,
196 const linked_ptr<net::FileStream>& file_stream) 198 const linked_ptr<net::FileStream>& file_stream)
197 : full_path_(full_path), 199 : full_path_(full_path),
198 source_url_(source_url), 200 source_url_(source_url),
199 referrer_url_(referrer_url), 201 referrer_url_(referrer_url),
200 file_stream_(file_stream), 202 file_stream_(file_stream),
201 bytes_so_far_(received_bytes), 203 bytes_so_far_(received_bytes),
202 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep), 204 power_save_blocker_(PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep),
203 calculate_hash_(false), 205 calculate_hash_(false),
206 initial_hash_state_(hash_state),
204 detached_(false) { 207 detached_(false) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
206 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); 209 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen);
207 if (file_stream_.get()) 210 if (file_stream_.get())
208 file_stream_->EnableErrorStatistics(); 211 file_stream_->EnableErrorStatistics();
209 } 212 }
210 213
211 BaseFile::~BaseFile() { 214 BaseFile::~BaseFile() {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
213 if (detached_) 216 if (detached_)
214 Close(); 217 Close();
215 else 218 else
216 Cancel(); // Will delete the file. 219 Cancel(); // Will delete the file.
217 } 220 }
218 221
219 net::Error BaseFile::Initialize(bool calculate_hash) { 222 net::Error BaseFile::Initialize(bool calculate_hash) {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
221 DCHECK(!detached_); 224 DCHECK(!detached_);
222 225
223 calculate_hash_ = calculate_hash; 226 calculate_hash_ = calculate_hash;
224 227
225 if (calculate_hash_) 228 if (calculate_hash_) {
226 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); 229 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256));
230 if (!initial_hash_state_.empty() && (bytes_so_far_ > 0))
231 SetSha256HashState(initial_hash_state_);
232 }
227 233
228 if (full_path_.empty()) { 234 if (full_path_.empty()) {
229 FilePath temp_file; 235 FilePath temp_file;
230 FilePath download_dir = 236 FilePath download_dir =
231 content::GetContentClient()->browser()->GetDefaultDownloadDirectory(); 237 content::GetContentClient()->browser()->GetDefaultDownloadDirectory();
232 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) && 238 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) &&
233 !file_util::CreateTemporaryFile(&temp_file)) { 239 !file_util::CreateTemporaryFile(&temp_file)) {
234 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); 240 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND);
235 } 241 }
236 full_path_ = temp_file; 242 full_path_ = temp_file;
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 Close(); 389 Close();
384 } 390 }
385 391
386 bool BaseFile::GetSha256Hash(std::string* hash) { 392 bool BaseFile::GetSha256Hash(std::string* hash) {
387 DCHECK(!detached_); 393 DCHECK(!detached_);
388 hash->assign(reinterpret_cast<const char*>(sha256_hash_), 394 hash->assign(reinterpret_cast<const char*>(sha256_hash_),
389 sizeof(sha256_hash_)); 395 sizeof(sha256_hash_));
390 return (calculate_hash_ && !in_progress()); 396 return (calculate_hash_ && !in_progress());
391 } 397 }
392 398
399 std::string BaseFile::GetSha256HashState() {
400 if (!calculate_hash_)
401 return "";
402
403 Pickle hash_state;
404 if (!secure_hash_->Serialize(&hash_state))
405 return "";
406
407 return std::string(reinterpret_cast<const char*>(hash_state.data()),
408 hash_state.size());
409 }
410
411 bool BaseFile::SetSha256HashState(const std::string& hash_state_bytes) {
412 if (!calculate_hash_)
413 return false;
414
415 Pickle hash_state(hash_state_bytes.c_str(), hash_state_bytes.size());
416 void* data_iterator = NULL;
417
418 return secure_hash_->Deserialize(&data_iterator, &hash_state);
419 }
420
393 bool BaseFile::IsEmptySha256Hash(const std::string& hash) { 421 bool BaseFile::IsEmptySha256Hash(const std::string& hash) {
394 return (hash.size() == kSha256HashLen && 422 return (hash.size() == kSha256HashLen &&
395 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen))); 423 0 == memcmp(hash.data(), kEmptySha256Hash, sizeof(kSha256HashLen)));
396 } 424 }
397 425
398 void BaseFile::AnnotateWithSourceInformation() { 426 void BaseFile::AnnotateWithSourceInformation() {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
400 DCHECK(!detached_); 428 DCHECK(!detached_);
401 429
402 #if defined(OS_WIN) 430 #if defined(OS_WIN)
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 std::string BaseFile::DebugString() const { 492 std::string BaseFile::DebugString() const {
465 return base::StringPrintf("{ source_url_ = \"%s\"" 493 return base::StringPrintf("{ source_url_ = \"%s\""
466 " full_path_ = \"%" PRFilePath "\"" 494 " full_path_ = \"%" PRFilePath "\""
467 " bytes_so_far_ = %" PRId64 495 " bytes_so_far_ = %" PRId64
468 " detached_ = %c }", 496 " detached_ = %c }",
469 source_url_.spec().c_str(), 497 source_url_.spec().c_str(),
470 full_path_.value().c_str(), 498 full_path_.value().c_str(),
471 bytes_so_far_, 499 bytes_so_far_,
472 detached_ ? 'T' : 'F'); 500 detached_ ? 'T' : 'F');
473 } 501 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698