Chromium Code Reviews| 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 #include "base/platform_file.h" | |
|
darin (slow to review)
2011/06/11 07:00:37
this include is unnecessary. it already comes fro
noelutz
2011/06/13 16:52:08
Done.
| |
| 8 | 9 |
| 9 // TODO(jianli): Move the code from anonymous namespace to base namespace so | 10 // TODO(jianli): Move the code from anonymous namespace to base namespace so |
| 10 // that all of the base:: prefixes would be unnecessary. | 11 // that all of the base:: prefixes would be unnecessary. |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Performs common checks for move and copy. | 16 // Performs common checks for move and copy. |
| 16 // This also removes the destination directory if it's non-empty and all other | 17 // This also removes the destination directory if it's non-empty and all other |
| 17 // checks are passed (so that the copy/move correctly overwrites the | 18 // checks are passed (so that the copy/move correctly overwrites the |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 int file_flags_; | 161 int file_flags_; |
| 161 base::FileUtilProxy::CreateOrOpenCallback* callback_; | 162 base::FileUtilProxy::CreateOrOpenCallback* callback_; |
| 162 base::PlatformFile file_handle_; | 163 base::PlatformFile file_handle_; |
| 163 bool created_; | 164 bool created_; |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 class RelayCreateTemporary : public MessageLoopRelay { | 167 class RelayCreateTemporary : public MessageLoopRelay { |
| 167 public: | 168 public: |
| 168 RelayCreateTemporary( | 169 RelayCreateTemporary( |
| 169 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 170 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 171 int additional_file_flags, | |
| 170 base::FileUtilProxy::CreateTemporaryCallback* callback) | 172 base::FileUtilProxy::CreateTemporaryCallback* callback) |
| 171 : message_loop_proxy_(message_loop_proxy), | 173 : message_loop_proxy_(message_loop_proxy), |
| 174 additional_file_flags_(additional_file_flags), | |
| 172 callback_(callback), | 175 callback_(callback), |
| 173 file_handle_(base::kInvalidPlatformFileValue) { | 176 file_handle_(base::kInvalidPlatformFileValue) { |
| 174 DCHECK(callback); | 177 DCHECK(callback); |
| 175 } | 178 } |
| 176 | 179 |
| 177 protected: | 180 protected: |
| 178 virtual ~RelayCreateTemporary() { | 181 virtual ~RelayCreateTemporary() { |
| 179 if (file_handle_ != base::kInvalidPlatformFileValue) | 182 if (file_handle_ != base::kInvalidPlatformFileValue) |
| 180 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); | 183 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 181 } | 184 } |
| 182 | 185 |
| 183 virtual void RunWork() { | 186 virtual void RunWork() { |
| 184 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 187 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| 185 // that returns a FilePath and a PlatformFile. | 188 // that returns a FilePath and a PlatformFile. |
| 186 file_util::CreateTemporaryFile(&file_path_); | 189 file_util::CreateTemporaryFile(&file_path_); |
| 187 | 190 |
| 188 // Use a fixed set of flags that are appropriate for writing to a temporary | |
| 189 // file from the IO thread using a net::FileStream. | |
| 190 int file_flags = | 191 int file_flags = |
| 192 base::PLATFORM_FILE_WRITE | | |
| 193 base::PLATFORM_FILE_TEMPORARY | | |
| 191 base::PLATFORM_FILE_CREATE_ALWAYS | | 194 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 192 base::PLATFORM_FILE_WRITE | | 195 additional_file_flags_; |
| 193 base::PLATFORM_FILE_ASYNC | | 196 |
| 194 base::PLATFORM_FILE_TEMPORARY; | |
| 195 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 197 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 196 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, | 198 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, |
| 197 NULL, &error_code); | 199 NULL, &error_code); |
| 198 set_error_code(error_code); | 200 set_error_code(error_code); |
| 199 } | 201 } |
| 200 | 202 |
| 201 virtual void RunCallback() { | 203 virtual void RunCallback() { |
| 202 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 204 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), |
| 203 file_path_); | 205 file_path_); |
| 204 delete callback_; | 206 delete callback_; |
| 205 } | 207 } |
| 206 | 208 |
| 207 private: | 209 private: |
| 208 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 210 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 211 int additional_file_flags_; | |
| 209 base::FileUtilProxy::CreateTemporaryCallback* callback_; | 212 base::FileUtilProxy::CreateTemporaryCallback* callback_; |
| 210 base::PlatformFile file_handle_; | 213 base::PlatformFile file_handle_; |
| 211 FilePath file_path_; | 214 FilePath file_path_; |
| 212 }; | 215 }; |
| 213 | 216 |
| 214 class RelayWithStatusCallback : public MessageLoopRelay { | 217 class RelayWithStatusCallback : public MessageLoopRelay { |
| 215 public: | 218 public: |
| 216 explicit RelayWithStatusCallback( | 219 explicit RelayWithStatusCallback( |
| 217 base::FileUtilProxy::StatusCallback* callback) | 220 base::FileUtilProxy::StatusCallback* callback) |
| 218 : callback_(callback) { | 221 : callback_(callback) { |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 738 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 736 const FilePath& file_path, int file_flags, | 739 const FilePath& file_path, int file_flags, |
| 737 CreateOrOpenCallback* callback) { | 740 CreateOrOpenCallback* callback) { |
| 738 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 741 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
| 739 message_loop_proxy, file_path, file_flags, callback)); | 742 message_loop_proxy, file_path, file_flags, callback)); |
| 740 } | 743 } |
| 741 | 744 |
| 742 // static | 745 // static |
| 743 bool FileUtilProxy::CreateTemporary( | 746 bool FileUtilProxy::CreateTemporary( |
| 744 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 747 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 748 int additional_file_flags, | |
| 745 CreateTemporaryCallback* callback) { | 749 CreateTemporaryCallback* callback) { |
| 746 return Start(FROM_HERE, message_loop_proxy, | 750 return Start(FROM_HERE, message_loop_proxy, |
| 747 new RelayCreateTemporary(message_loop_proxy, callback)); | 751 new RelayCreateTemporary(message_loop_proxy, |
| 752 additional_file_flags, | |
| 753 callback)); | |
| 748 } | 754 } |
| 749 | 755 |
| 750 // static | 756 // static |
| 751 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 757 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 752 base::PlatformFile file_handle, | 758 base::PlatformFile file_handle, |
| 753 StatusCallback* callback) { | 759 StatusCallback* callback) { |
| 754 return Start(FROM_HERE, message_loop_proxy, | 760 return Start(FROM_HERE, message_loop_proxy, |
| 755 new RelayClose(file_handle, callback)); | 761 new RelayClose(file_handle, callback)); |
| 756 } | 762 } |
| 757 | 763 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 | 914 |
| 909 // static | 915 // static |
| 910 bool FileUtilProxy::Flush( | 916 bool FileUtilProxy::Flush( |
| 911 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 917 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 912 PlatformFile file, | 918 PlatformFile file, |
| 913 StatusCallback* callback) { | 919 StatusCallback* callback) { |
| 914 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 920 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| 915 } | 921 } |
| 916 | 922 |
| 917 } // namespace base | 923 } // namespace base |
| OLD | NEW |