| 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 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 base::FileUtilProxy::GetFileInfoCallback callback_; | 520 base::FileUtilProxy::GetFileInfoCallback callback_; |
| 521 base::PlatformFile file_; | 521 base::PlatformFile file_; |
| 522 base::PlatformFileInfo file_info_; | 522 base::PlatformFileInfo file_info_; |
| 523 }; | 523 }; |
| 524 | 524 |
| 525 class RelayRead : public MessageLoopRelay { | 525 class RelayRead : public MessageLoopRelay { |
| 526 public: | 526 public: |
| 527 RelayRead(base::PlatformFile file, | 527 RelayRead(base::PlatformFile file, |
| 528 int64 offset, | 528 int64 offset, |
| 529 int bytes_to_read, | 529 int bytes_to_read, |
| 530 base::FileUtilProxy::ReadCallback* callback) | 530 const base::FileUtilProxy::ReadCallback& callback) |
| 531 : file_(file), | 531 : file_(file), |
| 532 offset_(offset), | 532 offset_(offset), |
| 533 buffer_(new char[bytes_to_read]), | 533 buffer_(new char[bytes_to_read]), |
| 534 bytes_to_read_(bytes_to_read), | 534 bytes_to_read_(bytes_to_read), |
| 535 callback_(callback), | 535 callback_(callback), |
| 536 bytes_read_(0) { | 536 bytes_read_(0) { |
| 537 } | 537 } |
| 538 | 538 |
| 539 protected: | 539 protected: |
| 540 virtual void RunWork() { | 540 virtual void RunWork() { |
| 541 bytes_read_ = base::ReadPlatformFile(file_, offset_, buffer_.get(), | 541 bytes_read_ = base::ReadPlatformFile(file_, offset_, buffer_.get(), |
| 542 bytes_to_read_); | 542 bytes_to_read_); |
| 543 if (bytes_read_ < 0) | 543 if (bytes_read_ < 0) |
| 544 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); | 544 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 545 } | 545 } |
| 546 | 546 |
| 547 virtual void RunCallback() { | 547 virtual void RunCallback() { |
| 548 if (callback_) { | 548 if (!callback_.is_null()) |
| 549 callback_->Run(error_code(), buffer_.get(), bytes_read_); | 549 callback_.Run(error_code(), buffer_.get(), bytes_read_); |
| 550 delete callback_; | |
| 551 } | |
| 552 } | 550 } |
| 553 | 551 |
| 554 private: | 552 private: |
| 555 base::PlatformFile file_; | 553 base::PlatformFile file_; |
| 556 int64 offset_; | 554 int64 offset_; |
| 557 scoped_array<char> buffer_; | 555 scoped_array<char> buffer_; |
| 558 int bytes_to_read_; | 556 int bytes_to_read_; |
| 559 base::FileUtilProxy::ReadCallback* callback_; | 557 base::FileUtilProxy::ReadCallback callback_; |
| 560 int bytes_read_; | 558 int bytes_read_; |
| 561 }; | 559 }; |
| 562 | 560 |
| 563 class RelayWrite : public MessageLoopRelay { | 561 class RelayWrite : public MessageLoopRelay { |
| 564 public: | 562 public: |
| 565 RelayWrite(base::PlatformFile file, | 563 RelayWrite(base::PlatformFile file, |
| 566 int64 offset, | 564 int64 offset, |
| 567 const char* buffer, | 565 const char* buffer, |
| 568 int bytes_to_write, | 566 int bytes_to_write, |
| 569 base::FileUtilProxy::WriteCallback* callback) | 567 base::FileUtilProxy::WriteCallback* callback) |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 return Start(FROM_HERE, message_loop_proxy, | 840 return Start(FROM_HERE, message_loop_proxy, |
| 843 new RelayDelete(file_path, true, callback)); | 841 new RelayDelete(file_path, true, callback)); |
| 844 } | 842 } |
| 845 | 843 |
| 846 // static | 844 // static |
| 847 bool FileUtilProxy::Read( | 845 bool FileUtilProxy::Read( |
| 848 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 846 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 849 PlatformFile file, | 847 PlatformFile file, |
| 850 int64 offset, | 848 int64 offset, |
| 851 int bytes_to_read, | 849 int bytes_to_read, |
| 852 ReadCallback* callback) { | 850 const ReadCallback& callback) { |
| 853 if (bytes_to_read < 0) { | 851 if (bytes_to_read < 0) |
| 854 delete callback; | |
| 855 return false; | 852 return false; |
| 856 } | 853 |
| 857 return Start(FROM_HERE, message_loop_proxy, | 854 return Start(FROM_HERE, message_loop_proxy, |
| 858 new RelayRead(file, offset, bytes_to_read, callback)); | 855 new RelayRead(file, offset, bytes_to_read, callback)); |
| 859 } | 856 } |
| 860 | 857 |
| 861 // static | 858 // static |
| 862 bool FileUtilProxy::Write( | 859 bool FileUtilProxy::Write( |
| 863 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 860 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 864 PlatformFile file, | 861 PlatformFile file, |
| 865 int64 offset, | 862 int64 offset, |
| 866 const char* buffer, | 863 const char* buffer, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 | 917 |
| 921 // static | 918 // static |
| 922 bool FileUtilProxy::Flush( | 919 bool FileUtilProxy::Flush( |
| 923 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 920 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 924 PlatformFile file, | 921 PlatformFile file, |
| 925 StatusCallback* callback) { | 922 StatusCallback* callback) { |
| 926 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 923 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| 927 } | 924 } |
| 928 | 925 |
| 929 } // namespace base | 926 } // namespace base |
| OLD | NEW |