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 "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 rv = RecordAndMapError(error, | 580 rv = RecordAndMapError(error, |
581 FILE_ERROR_SOURCE_WRITE, | 581 FILE_ERROR_SOURCE_WRITE, |
582 record_uma_, | 582 record_uma_, |
583 bound_net_log_); | 583 bound_net_log_); |
584 } else { | 584 } else { |
585 rv = static_cast<int>(bytes_written); | 585 rv = static_cast<int>(bytes_written); |
586 } | 586 } |
587 return rv; | 587 return rv; |
588 } | 588 } |
589 | 589 |
590 int FileStreamWin::Flush() { | 590 int FileStreamWin::Flush(const CompletionCallback& callback) { |
| 591 if (!IsOpen()) |
| 592 return ERR_UNEXPECTED; |
| 593 |
| 594 // Make sure we're async and we have no other in-flight async operations. |
| 595 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| 596 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 597 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
| 598 DCHECK(!on_io_complete_.get()); |
| 599 |
| 600 on_io_complete_.reset(new base::WaitableEvent( |
| 601 false /* manual_reset */, false /* initially_signaled */)); |
| 602 |
| 603 int* result = new int(OK); |
| 604 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 605 FROM_HERE, |
| 606 base::Bind(&InvokeAndSignal, |
| 607 // Unretained should be fine as we wait for a signal on |
| 608 // on_io_complete_ at the destructor. |
| 609 base::Bind(&FileStreamWin::FlushFile, base::Unretained(this), |
| 610 result), |
| 611 on_io_complete_.get()), |
| 612 base::Bind(&FileStreamWin::OnFlushed, |
| 613 weak_ptr_factory_.GetWeakPtr(), |
| 614 callback, base::Owned(result)), |
| 615 true /* task is slow */); |
| 616 DCHECK(posted); |
| 617 return ERR_IO_PENDING; |
| 618 } |
| 619 |
| 620 int FileStreamWin::FlushSync() { |
591 base::ThreadRestrictions::AssertIOAllowed(); | 621 base::ThreadRestrictions::AssertIOAllowed(); |
592 | 622 |
593 if (!IsOpen()) | 623 if (!IsOpen()) |
594 return ERR_UNEXPECTED; | 624 return ERR_UNEXPECTED; |
595 | 625 |
596 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 626 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
597 if (FlushFileBuffers(file_)) { | 627 if (FlushFileBuffers(file_)) { |
598 return OK; | 628 return OK; |
599 } | 629 } |
600 | 630 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 bound_net_log_); | 715 bound_net_log_); |
686 return; | 716 return; |
687 } | 717 } |
688 if (async_context_.get()) { | 718 if (async_context_.get()) { |
689 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); | 719 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); |
690 SetOffset(async_context_->overlapped(), res); | 720 SetOffset(async_context_->overlapped(), res); |
691 } | 721 } |
692 *result = res.QuadPart; | 722 *result = res.QuadPart; |
693 } | 723 } |
694 | 724 |
| 725 void FileStreamWin::FlushFile(int* result) { |
| 726 if (FlushFileBuffers(file_)) { |
| 727 *result = OK; |
| 728 } else { |
| 729 *result = RecordAndMapError(GetLastError(), |
| 730 FILE_ERROR_SOURCE_FLUSH, |
| 731 record_uma_, |
| 732 bound_net_log_); |
| 733 } |
| 734 } |
| 735 |
695 void FileStreamWin::OnOpened(const CompletionCallback& callback, int* result) { | 736 void FileStreamWin::OnOpened(const CompletionCallback& callback, int* result) { |
696 if (*result == OK) { | 737 if (*result == OK) { |
697 async_context_.reset(new AsyncContext(bound_net_log_)); | 738 async_context_.reset(new AsyncContext(bound_net_log_)); |
698 if (record_uma_) | 739 if (record_uma_) |
699 async_context_->EnableErrorStatistics(); | 740 async_context_->EnableErrorStatistics(); |
700 MessageLoopForIO::current()->RegisterIOHandler(file_, | 741 MessageLoopForIO::current()->RegisterIOHandler(file_, |
701 async_context_.get()); | 742 async_context_.get()); |
702 } | 743 } |
703 | 744 |
704 // Reset this before Run() as Run() may issue a new async operation. | 745 // Reset this before Run() as Run() may issue a new async operation. |
705 ResetOnIOComplete(); | 746 ResetOnIOComplete(); |
706 callback.Run(*result); | 747 callback.Run(*result); |
707 } | 748 } |
708 | 749 |
709 void FileStreamWin::OnSeeked( | 750 void FileStreamWin::OnSeeked( |
710 const Int64CompletionCallback& callback, | 751 const Int64CompletionCallback& callback, |
711 int64* result) { | 752 int64* result) { |
712 // Reset this before Run() as Run() may issue a new async operation. | 753 // Reset this before Run() as Run() may issue a new async operation. |
713 ResetOnIOComplete(); | 754 ResetOnIOComplete(); |
714 callback.Run(*result); | 755 callback.Run(*result); |
715 } | 756 } |
716 | 757 |
| 758 void FileStreamWin::OnFlushed(const CompletionCallback& callback, int* result) { |
| 759 // Reset this before Run() as Run() may issue a new async operation. |
| 760 ResetOnIOComplete(); |
| 761 callback.Run(*result); |
| 762 } |
| 763 |
717 void FileStreamWin::ResetOnIOComplete() { | 764 void FileStreamWin::ResetOnIOComplete() { |
718 on_io_complete_.reset(); | 765 on_io_complete_.reset(); |
719 weak_ptr_factory_.InvalidateWeakPtrs(); | 766 weak_ptr_factory_.InvalidateWeakPtrs(); |
720 } | 767 } |
721 | 768 |
722 void FileStreamWin::WaitForIOCompletion() { | 769 void FileStreamWin::WaitForIOCompletion() { |
723 // http://crbug.com/115067 | 770 // http://crbug.com/115067 |
724 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 771 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
725 if (on_io_complete_.get()) { | 772 if (on_io_complete_.get()) { |
726 on_io_complete_->Wait(); | 773 on_io_complete_->Wait(); |
727 on_io_complete_.reset(); | 774 on_io_complete_.reset(); |
728 } | 775 } |
729 } | 776 } |
730 | 777 |
731 } // namespace net | 778 } // namespace net |
OLD | NEW |