| 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 base::FileUtilProxy::CreateOrOpenCallback callback_; | 160 base::FileUtilProxy::CreateOrOpenCallback callback_; |
| 161 base::PlatformFile file_handle_; | 161 base::PlatformFile file_handle_; |
| 162 bool created_; | 162 bool created_; |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 class RelayCreateTemporary : public MessageLoopRelay { | 165 class RelayCreateTemporary : public MessageLoopRelay { |
| 166 public: | 166 public: |
| 167 RelayCreateTemporary( | 167 RelayCreateTemporary( |
| 168 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 168 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 169 int additional_file_flags, | 169 int additional_file_flags, |
| 170 base::FileUtilProxy::CreateTemporaryCallback* callback) | 170 const base::FileUtilProxy::CreateTemporaryCallback& callback) |
| 171 : message_loop_proxy_(message_loop_proxy), | 171 : message_loop_proxy_(message_loop_proxy), |
| 172 additional_file_flags_(additional_file_flags), | 172 additional_file_flags_(additional_file_flags), |
| 173 callback_(callback), | 173 callback_(callback), |
| 174 file_handle_(base::kInvalidPlatformFileValue) { | 174 file_handle_(base::kInvalidPlatformFileValue) { |
| 175 DCHECK(callback); | 175 DCHECK_EQ(false, callback.is_null()); |
| 176 } | 176 } |
| 177 | 177 |
| 178 protected: | 178 protected: |
| 179 virtual ~RelayCreateTemporary() { | 179 virtual ~RelayCreateTemporary() { |
| 180 if (file_handle_ != base::kInvalidPlatformFileValue) | 180 if (file_handle_ != base::kInvalidPlatformFileValue) |
| 181 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); | 181 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 182 } | 182 } |
| 183 | 183 |
| 184 virtual void RunWork() { | 184 virtual void RunWork() { |
| 185 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 185 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| 186 // that returns a FilePath and a PlatformFile. | 186 // that returns a FilePath and a PlatformFile. |
| 187 file_util::CreateTemporaryFile(&file_path_); | 187 file_util::CreateTemporaryFile(&file_path_); |
| 188 | 188 |
| 189 int file_flags = | 189 int file_flags = |
| 190 base::PLATFORM_FILE_WRITE | | 190 base::PLATFORM_FILE_WRITE | |
| 191 base::PLATFORM_FILE_TEMPORARY | | 191 base::PLATFORM_FILE_TEMPORARY | |
| 192 base::PLATFORM_FILE_CREATE_ALWAYS | | 192 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 193 additional_file_flags_; | 193 additional_file_flags_; |
| 194 | 194 |
| 195 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 195 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 196 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, | 196 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, |
| 197 NULL, &error_code); | 197 NULL, &error_code); |
| 198 set_error_code(error_code); | 198 set_error_code(error_code); |
| 199 } | 199 } |
| 200 | 200 |
| 201 virtual void RunCallback() { | 201 virtual void RunCallback() { |
| 202 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 202 callback_.Run(error_code(), base::PassPlatformFile(&file_handle_), |
| 203 file_path_); | 203 file_path_); |
| 204 delete callback_; | |
| 205 } | 204 } |
| 206 | 205 |
| 207 private: | 206 private: |
| 208 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 207 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 209 int additional_file_flags_; | 208 int additional_file_flags_; |
| 210 base::FileUtilProxy::CreateTemporaryCallback* callback_; | 209 base::FileUtilProxy::CreateTemporaryCallback callback_; |
| 211 base::PlatformFile file_handle_; | 210 base::PlatformFile file_handle_; |
| 212 FilePath file_path_; | 211 FilePath file_path_; |
| 213 }; | 212 }; |
| 214 | 213 |
| 215 class RelayWithStatusCallback : public MessageLoopRelay { | 214 class RelayWithStatusCallback : public MessageLoopRelay { |
| 216 public: | 215 public: |
| 217 explicit RelayWithStatusCallback( | 216 explicit RelayWithStatusCallback( |
| 218 base::FileUtilProxy::StatusCallback* callback) | 217 base::FileUtilProxy::StatusCallback* callback) |
| 219 : callback_(callback) { | 218 : callback_(callback) { |
| 220 // It is OK for callback to be NULL. | 219 // It is OK for callback to be NULL. |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 const FilePath& file_path, int file_flags, | 739 const FilePath& file_path, int file_flags, |
| 741 const CreateOrOpenCallback& callback) { | 740 const CreateOrOpenCallback& callback) { |
| 742 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 741 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
| 743 message_loop_proxy, file_path, file_flags, callback)); | 742 message_loop_proxy, file_path, file_flags, callback)); |
| 744 } | 743 } |
| 745 | 744 |
| 746 // static | 745 // static |
| 747 bool FileUtilProxy::CreateTemporary( | 746 bool FileUtilProxy::CreateTemporary( |
| 748 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 747 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 749 int additional_file_flags, | 748 int additional_file_flags, |
| 750 CreateTemporaryCallback* callback) { | 749 const CreateTemporaryCallback& callback) { |
| 751 return Start(FROM_HERE, message_loop_proxy, | 750 return Start(FROM_HERE, message_loop_proxy, |
| 752 new RelayCreateTemporary(message_loop_proxy, | 751 new RelayCreateTemporary(message_loop_proxy, |
| 753 additional_file_flags, | 752 additional_file_flags, |
| 754 callback)); | 753 callback)); |
| 755 } | 754 } |
| 756 | 755 |
| 757 // static | 756 // static |
| 758 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 757 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 759 base::PlatformFile file_handle, | 758 base::PlatformFile file_handle, |
| 760 StatusCallback* callback) { | 759 StatusCallback* callback) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 | 922 |
| 924 // static | 923 // static |
| 925 bool FileUtilProxy::Flush( | 924 bool FileUtilProxy::Flush( |
| 926 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 925 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 927 PlatformFile file, | 926 PlatformFile file, |
| 928 StatusCallback* callback) { | 927 StatusCallback* callback) { |
| 929 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 928 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| 930 } | 929 } |
| 931 | 930 |
| 932 } // namespace base | 931 } // namespace base |
| OLD | NEW |