OLD | NEW |
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 "base/file_util_proxy.h" | 5 #include "base/file_util_proxy.h" |
6 | 6 |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 | 8 |
9 // TODO(jianli): Move the code from anonymous namespace to base namespace so | 9 // TODO(jianli): Move the code from anonymous namespace to base namespace so |
10 // that all of the base:: prefixes would be unnecessary. | 10 // that all of the base:: prefixes would be unnecessary. |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 base::FileUtilProxy::ReadCallback callback_; | 557 base::FileUtilProxy::ReadCallback callback_; |
558 int bytes_read_; | 558 int bytes_read_; |
559 }; | 559 }; |
560 | 560 |
561 class RelayWrite : public MessageLoopRelay { | 561 class RelayWrite : public MessageLoopRelay { |
562 public: | 562 public: |
563 RelayWrite(base::PlatformFile file, | 563 RelayWrite(base::PlatformFile file, |
564 int64 offset, | 564 int64 offset, |
565 const char* buffer, | 565 const char* buffer, |
566 int bytes_to_write, | 566 int bytes_to_write, |
567 base::FileUtilProxy::WriteCallback* callback) | 567 const base::FileUtilProxy::WriteCallback& callback) |
568 : file_(file), | 568 : file_(file), |
569 offset_(offset), | 569 offset_(offset), |
570 buffer_(new char[bytes_to_write]), | 570 buffer_(new char[bytes_to_write]), |
571 bytes_to_write_(bytes_to_write), | 571 bytes_to_write_(bytes_to_write), |
572 callback_(callback), | 572 callback_(callback), |
573 bytes_written_(0) { | 573 bytes_written_(0) { |
574 memcpy(buffer_.get(), buffer, bytes_to_write); | 574 memcpy(buffer_.get(), buffer, bytes_to_write); |
575 } | 575 } |
576 | 576 |
577 protected: | 577 protected: |
578 virtual void RunWork() { | 578 virtual void RunWork() { |
579 bytes_written_ = base::WritePlatformFile(file_, offset_, buffer_.get(), | 579 bytes_written_ = base::WritePlatformFile(file_, offset_, buffer_.get(), |
580 bytes_to_write_); | 580 bytes_to_write_); |
581 if (bytes_written_ < 0) | 581 if (bytes_written_ < 0) |
582 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); | 582 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
583 } | 583 } |
584 | 584 |
585 virtual void RunCallback() { | 585 virtual void RunCallback() { |
586 if (callback_) { | 586 if (!callback_.is_null()) |
587 callback_->Run(error_code(), bytes_written_); | 587 callback_.Run(error_code(), bytes_written_); |
588 delete callback_; | |
589 } | |
590 } | 588 } |
591 | 589 |
592 private: | 590 private: |
593 base::PlatformFile file_; | 591 base::PlatformFile file_; |
594 int64 offset_; | 592 int64 offset_; |
595 scoped_array<char> buffer_; | 593 scoped_array<char> buffer_; |
596 int bytes_to_write_; | 594 int bytes_to_write_; |
597 base::FileUtilProxy::WriteCallback* callback_; | 595 base::FileUtilProxy::WriteCallback callback_; |
598 int bytes_written_; | 596 int bytes_written_; |
599 }; | 597 }; |
600 | 598 |
601 class RelayTouch : public RelayWithStatusCallback { | 599 class RelayTouch : public RelayWithStatusCallback { |
602 public: | 600 public: |
603 RelayTouch(base::PlatformFile file, | 601 RelayTouch(base::PlatformFile file, |
604 const base::Time& last_access_time, | 602 const base::Time& last_access_time, |
605 const base::Time& last_modified_time, | 603 const base::Time& last_modified_time, |
606 base::FileUtilProxy::StatusCallback* callback) | 604 base::FileUtilProxy::StatusCallback* callback) |
607 : RelayWithStatusCallback(callback), | 605 : RelayWithStatusCallback(callback), |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 new RelayRead(file, offset, bytes_to_read, callback)); | 853 new RelayRead(file, offset, bytes_to_read, callback)); |
856 } | 854 } |
857 | 855 |
858 // static | 856 // static |
859 bool FileUtilProxy::Write( | 857 bool FileUtilProxy::Write( |
860 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 858 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
861 PlatformFile file, | 859 PlatformFile file, |
862 int64 offset, | 860 int64 offset, |
863 const char* buffer, | 861 const char* buffer, |
864 int bytes_to_write, | 862 int bytes_to_write, |
865 WriteCallback* callback) { | 863 const WriteCallback& callback) { |
866 if (bytes_to_write <= 0) { | 864 if (bytes_to_write <= 0) |
867 delete callback; | |
868 return false; | 865 return false; |
869 } | 866 |
870 return Start(FROM_HERE, message_loop_proxy, | 867 return Start(FROM_HERE, message_loop_proxy, |
871 new RelayWrite(file, offset, buffer, bytes_to_write, callback)); | 868 new RelayWrite(file, offset, buffer, bytes_to_write, callback)); |
872 } | 869 } |
873 | 870 |
874 // static | 871 // static |
875 bool FileUtilProxy::Touch( | 872 bool FileUtilProxy::Touch( |
876 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 873 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
877 PlatformFile file, | 874 PlatformFile file, |
878 const base::Time& last_access_time, | 875 const base::Time& last_access_time, |
879 const base::Time& last_modified_time, | 876 const base::Time& last_modified_time, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 | 914 |
918 // static | 915 // static |
919 bool FileUtilProxy::Flush( | 916 bool FileUtilProxy::Flush( |
920 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 917 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
921 PlatformFile file, | 918 PlatformFile file, |
922 StatusCallback* callback) { | 919 StatusCallback* callback) { |
923 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 920 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
924 } | 921 } |
925 | 922 |
926 } // namespace base | 923 } // namespace base |
OLD | NEW |