| 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/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/pickle.h" |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 bound_net_log_.BeginEvent( | 495 bound_net_log_.BeginEvent( |
| 496 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, | 496 net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, |
| 497 make_scoped_refptr( | 497 make_scoped_refptr( |
| 498 new download_net_logs::FileOpenedParameters( | 498 new download_net_logs::FileOpenedParameters( |
| 499 full_path_.AsUTF8Unsafe(), bytes_so_far_))); | 499 full_path_.AsUTF8Unsafe(), bytes_so_far_))); |
| 500 | 500 |
| 501 // Create a new file stream if it is not provided. | 501 // Create a new file stream if it is not provided. |
| 502 if (!file_stream_.get()) { | 502 if (!file_stream_.get()) { |
| 503 CreateFileStream(); | 503 CreateFileStream(); |
| 504 file_stream_->EnableErrorStatistics(); | 504 file_stream_->EnableErrorStatistics(); |
| 505 int open_result = file_stream_->Open( | 505 int open_result = file_stream_->OpenSync( |
| 506 full_path_, | 506 full_path_, |
| 507 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE); | 507 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE); |
| 508 if (open_result != net::OK) | 508 if (open_result != net::OK) |
| 509 return ClearStream(LOG_ERROR("Open", open_result)); | 509 return ClearStream(LOG_ERROR("Open", open_result)); |
| 510 | 510 |
| 511 // We may be re-opening the file after rename. Always make sure we're | 511 // We may be re-opening the file after rename. Always make sure we're |
| 512 // writing at the end of the file. | 512 // writing at the end of the file. |
| 513 int64 seek_result = file_stream_->Seek(net::FROM_END, 0); | 513 int64 seek_result = file_stream_->Seek(net::FROM_END, 0); |
| 514 if (seek_result < 0) | 514 if (seek_result < 0) |
| 515 return ClearStream(LOG_ERROR("Seek", seek_result)); | 515 return ClearStream(LOG_ERROR("Seek", seek_result)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 529 | 529 |
| 530 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED, NULL); | 530 bound_net_log_.AddEvent(net::NetLog::TYPE_DOWNLOAD_FILE_CLOSED, NULL); |
| 531 | 531 |
| 532 if (file_stream_.get()) { | 532 if (file_stream_.get()) { |
| 533 #if defined(OS_CHROMEOS) | 533 #if defined(OS_CHROMEOS) |
| 534 // Currently we don't really care about the return value, since if it fails | 534 // Currently we don't really care about the return value, since if it fails |
| 535 // theres not much we can do. But we might in the future. | 535 // theres not much we can do. But we might in the future. |
| 536 file_stream_->Flush(); | 536 file_stream_->Flush(); |
| 537 #endif | 537 #endif |
| 538 file_stream_->Close(); | 538 file_stream_->CloseSync(); |
| 539 ClearStream(net::OK); | 539 ClearStream(net::OK); |
| 540 } | 540 } |
| 541 } | 541 } |
| 542 | 542 |
| 543 net::Error BaseFile::ClearStream(net::Error net_error) { | 543 net::Error BaseFile::ClearStream(net::Error net_error) { |
| 544 // This should only be called when we have a stream. | 544 // This should only be called when we have a stream. |
| 545 DCHECK(file_stream_.get() != NULL); | 545 DCHECK(file_stream_.get() != NULL); |
| 546 file_stream_.reset(); | 546 file_stream_.reset(); |
| 547 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, NULL); | 547 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_OPENED, NULL); |
| 548 return net_error; | 548 return net_error; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 562 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { | 562 int64 BaseFile::CurrentSpeedAtTime(base::TimeTicks current_time) const { |
| 563 base::TimeDelta diff = current_time - start_tick_; | 563 base::TimeDelta diff = current_time - start_tick_; |
| 564 int64 diff_ms = diff.InMilliseconds(); | 564 int64 diff_ms = diff.InMilliseconds(); |
| 565 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; | 565 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; |
| 566 } | 566 } |
| 567 | 567 |
| 568 int64 BaseFile::CurrentSpeed() const { | 568 int64 BaseFile::CurrentSpeed() const { |
| 569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 570 return CurrentSpeedAtTime(base::TimeTicks::Now()); | 570 return CurrentSpeedAtTime(base::TimeTicks::Now()); |
| 571 } | 571 } |
| OLD | NEW |