| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 12 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "content/browser/download/download_net_log_parameters.h" | 15 #include "content/browser/download/download_net_log_parameters.h" |
| 15 #include "content/browser/download/download_stats.h" | 16 #include "content/browser/download/download_stats.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 break; | 65 break; |
| 65 } | 66 } |
| 66 | 67 |
| 67 #undef NET_ERROR | 68 #undef NET_ERROR |
| 68 | 69 |
| 69 VLOG(1) << " " << func << "(): " << operation | 70 VLOG(1) << " " << func << "(): " << operation |
| 70 << "() returned error " << error << " (" << err_string << ")"; | 71 << "() returned error " << error << " (" << err_string << ")"; |
| 71 | 72 |
| 72 bound_net_log.AddEvent( | 73 bound_net_log.AddEvent( |
| 73 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, | 74 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, |
| 74 make_scoped_refptr( | 75 base::Bind(&download_net_logs::FileErrorCallback, operation, net_error)); |
| 75 new download_net_logs::FileErrorParameters(operation, net_error))); | |
| 76 | 76 |
| 77 return net_error; | 77 return net_error; |
| 78 } | 78 } |
| 79 | 79 |
| 80 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
| 81 | 81 |
| 82 #define SHFILE_TO_NET_ERROR(symbol, value, mapping, description) \ | 82 #define SHFILE_TO_NET_ERROR(symbol, value, mapping, description) \ |
| 83 case value: return net::ERR_##mapping; | 83 case value: return net::ERR_##mapping; |
| 84 | 84 |
| 85 // Maps the result of a call to |SHFileOperation()| onto a |net::Error|. | 85 // Maps the result of a call to |SHFileOperation()| onto a |net::Error|. |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 318 |
| 319 net::Error BaseFile::Rename(const FilePath& new_path) { | 319 net::Error BaseFile::Rename(const FilePath& new_path) { |
| 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 321 | 321 |
| 322 // Save the information whether the download is in progress because | 322 // Save the information whether the download is in progress because |
| 323 // it will be overwritten by closing the file. | 323 // it will be overwritten by closing the file. |
| 324 bool saved_in_progress = in_progress(); | 324 bool saved_in_progress = in_progress(); |
| 325 | 325 |
| 326 bound_net_log_.AddEvent( | 326 bound_net_log_.AddEvent( |
| 327 net::NetLog::TYPE_DOWNLOAD_FILE_RENAMED, | 327 net::NetLog::TYPE_DOWNLOAD_FILE_RENAMED, |
| 328 make_scoped_refptr( | 328 base::Bind(&download_net_logs::FileRenamedCallback, |
| 329 new download_net_logs::FileRenamedParameters( | 329 &full_path_, &new_path)); |
| 330 full_path_.AsUTF8Unsafe(), new_path.AsUTF8Unsafe()))); | |
| 331 | 330 |
| 332 // If the new path is same as the old one, there is no need to perform the | 331 // If the new path is same as the old one, there is no need to perform the |
| 333 // following renaming logic. | 332 // following renaming logic. |
| 334 if (new_path == full_path_) { | 333 if (new_path == full_path_) { |
| 335 // Don't close the file if we're not done (finished or canceled). | 334 // Don't close the file if we're not done (finished or canceled). |
| 336 if (!saved_in_progress) | 335 if (!saved_in_progress) |
| 337 Close(); | 336 Close(); |
| 338 | 337 |
| 339 return net::OK; | 338 return net::OK; |
| 340 } | 339 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 | 388 |
| 390 // We don't need to re-open the file if we're done (finished or canceled). | 389 // We don't need to re-open the file if we're done (finished or canceled). |
| 391 if (!saved_in_progress) | 390 if (!saved_in_progress) |
| 392 return net::OK; | 391 return net::OK; |
| 393 | 392 |
| 394 return Open(); | 393 return Open(); |
| 395 } | 394 } |
| 396 | 395 |
| 397 void BaseFile::Detach() { | 396 void BaseFile::Detach() { |
| 398 detached_ = true; | 397 detached_ = true; |
| 399 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED, NULL); | 398 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DETACHED); |
| 400 } | 399 } |
| 401 | 400 |
| 402 void BaseFile::Cancel() { | 401 void BaseFile::Cancel() { |
| 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 404 DCHECK(!detached_); | 403 DCHECK(!detached_); |
| 405 | 404 |
| 406 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED, NULL); | 405 bound_net_log_.AddEvent(net::NetLog::TYPE_CANCELLED); |
| 407 | 406 |
| 408 Close(); | 407 Close(); |
| 409 | 408 |
| 410 if (!full_path_.empty()) { | 409 if (!full_path_.empty()) { |
| 411 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED, NULL); | 410 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_DELETED); |
| 412 | 411 |
| 413 file_util::Delete(full_path_, false); | 412 file_util::Delete(full_path_, false); |
| 414 } | 413 } |
| 415 } | 414 } |
| 416 | 415 |
| 417 void BaseFile::Finish() { | 416 void BaseFile::Finish() { |
| 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 419 | 418 |
| 420 if (calculate_hash_) | 419 if (calculate_hash_) |
| 421 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 420 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 file_stream_->SetBoundNetLogSource(bound_net_log_); | 478 file_stream_->SetBoundNetLogSource(bound_net_log_); |
| 480 } | 479 } |
| 481 | 480 |
| 482 net::Error BaseFile::Open() { | 481 net::Error BaseFile::Open() { |
| 483 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 482 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 484 DCHECK(!detached_); | 483 DCHECK(!detached_); |
| 485 DCHECK(!full_path_.empty()); | 484 DCHECK(!full_path_.empty()); |
| 486 | 485 |
| 487 bound_net_log_.BeginEvent( | 486 bound_net_log_.BeginEvent( |
| 488 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, | 487 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, |
| 489 make_scoped_refptr( | 488 base::Bind(&download_net_logs::FileOpenedCallback, |
| 490 new download_net_logs::FileOpenedParameters( | 489 &full_path_, bytes_so_far_)); |
| 491 full_path_.AsUTF8Unsafe(), bytes_so_far_))); | |
| 492 | 490 |
| 493 // Create a new file stream if it is not provided. | 491 // Create a new file stream if it is not provided. |
| 494 if (!file_stream_.get()) { | 492 if (!file_stream_.get()) { |
| 495 CreateFileStream(); | 493 CreateFileStream(); |
| 496 file_stream_->EnableErrorStatistics(); | 494 file_stream_->EnableErrorStatistics(); |
| 497 int open_result = file_stream_->OpenSync( | 495 int open_result = file_stream_->OpenSync( |
| 498 full_path_, | 496 full_path_, |
| 499 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE); | 497 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE); |
| 500 if (open_result != net::OK) | 498 if (open_result != net::OK) |
| 501 return ClearStream(LOG_ERROR("Open", open_result)); | 499 return ClearStream(LOG_ERROR("Open", open_result)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 512 #if defined(OS_WIN) | 510 #if defined(OS_WIN) |
| 513 AnnotateWithSourceInformation(); | 511 AnnotateWithSourceInformation(); |
| 514 #endif | 512 #endif |
| 515 | 513 |
| 516 return net::OK; | 514 return net::OK; |
| 517 } | 515 } |
| 518 | 516 |
| 519 void BaseFile::Close() { | 517 void BaseFile::Close() { |
| 520 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 521 | 519 |
| 522 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED, NULL); | 520 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED); |
| 523 | 521 |
| 524 if (file_stream_.get()) { | 522 if (file_stream_.get()) { |
| 525 #if defined(OS_CHROMEOS) | 523 #if defined(OS_CHROMEOS) |
| 526 // Currently we don't really care about the return value, since if it fails | 524 // Currently we don't really care about the return value, since if it fails |
| 527 // theres not much we can do. But we might in the future. | 525 // theres not much we can do. But we might in the future. |
| 528 file_stream_->Flush(); | 526 file_stream_->Flush(); |
| 529 #endif | 527 #endif |
| 530 file_stream_->CloseSync(); | 528 file_stream_->CloseSync(); |
| 531 ClearStream(net::OK); | 529 ClearStream(net::OK); |
| 532 } | 530 } |
| 533 } | 531 } |
| 534 | 532 |
| 535 net::Error BaseFile::ClearStream(net::Error net_error) { | 533 net::Error BaseFile::ClearStream(net::Error net_error) { |
| 536 // This should only be called when we have a stream. | 534 // This should only be called when we have a stream. |
| 537 DCHECK(file_stream_.get() != NULL); | 535 DCHECK(file_stream_.get() != NULL); |
| 538 file_stream_.reset(); | 536 file_stream_.reset(); |
| 539 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, NULL); | 537 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_OPENED); |
| 540 return net_error; | 538 return net_error; |
| 541 } | 539 } |
| 542 | 540 |
| 543 std::string BaseFile::DebugString() const { | 541 std::string BaseFile::DebugString() const { |
| 544 return base::StringPrintf("{ source_url_ = \"%s\"" | 542 return base::StringPrintf("{ source_url_ = \"%s\"" |
| 545 " full_path_ = \"%" PRFilePath "\"" | 543 " full_path_ = \"%" PRFilePath "\"" |
| 546 " bytes_so_far_ = %" PRId64 | 544 " bytes_so_far_ = %" PRId64 |
| 547 " detached_ = %c }", | 545 " detached_ = %c }", |
| 548 source_url_.spec().c_str(), | 546 source_url_.spec().c_str(), |
| 549 full_path_.value().c_str(), | 547 full_path_.value().c_str(), |
| 550 bytes_so_far_, | 548 bytes_so_far_, |
| 551 detached_ ? 'T' : 'F'); | 549 detached_ ? 'T' : 'F'); |
| 552 } | 550 } |
| 553 | 551 |
| 554 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { | 552 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { |
| 555 base::TimeDelta diff = current_time - start_tick_; | 553 base::TimeDelta diff = current_time - start_tick_; |
| 556 int64 diff_ms = diff.InMilliseconds(); | 554 int64 diff_ms = diff.InMilliseconds(); |
| 557 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; | 555 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; |
| 558 } | 556 } |
| 559 | 557 |
| 560 int64 BaseFile::CurrentSpeed() const { | 558 int64 BaseFile::CurrentSpeed() const { |
| 561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 562 return CurrentSpeedAtTime(base::TimeTicks::Now()); | 560 return CurrentSpeedAtTime(base::TimeTicks::Now()); |
| 563 } | 561 } |
| OLD | NEW |