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 int file_flags_; | 160 int file_flags_; |
161 base::FileUtilProxy::CreateOrOpenCallback* callback_; | 161 base::FileUtilProxy::CreateOrOpenCallback* callback_; |
162 base::PlatformFile file_handle_; | 162 base::PlatformFile file_handle_; |
163 bool created_; | 163 bool created_; |
164 }; | 164 }; |
165 | 165 |
166 class RelayCreateTemporary : public MessageLoopRelay { | 166 class RelayCreateTemporary : public MessageLoopRelay { |
167 public: | 167 public: |
168 RelayCreateTemporary( | 168 RelayCreateTemporary( |
169 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 169 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 170 bool async, |
170 base::FileUtilProxy::CreateTemporaryCallback* callback) | 171 base::FileUtilProxy::CreateTemporaryCallback* callback) |
171 : message_loop_proxy_(message_loop_proxy), | 172 : message_loop_proxy_(message_loop_proxy), |
| 173 async_(async), |
172 callback_(callback), | 174 callback_(callback), |
173 file_handle_(base::kInvalidPlatformFileValue) { | 175 file_handle_(base::kInvalidPlatformFileValue) { |
174 DCHECK(callback); | 176 DCHECK(callback); |
175 } | 177 } |
176 | 178 |
177 protected: | 179 protected: |
178 virtual ~RelayCreateTemporary() { | 180 virtual ~RelayCreateTemporary() { |
179 if (file_handle_ != base::kInvalidPlatformFileValue) | 181 if (file_handle_ != base::kInvalidPlatformFileValue) |
180 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); | 182 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
181 } | 183 } |
182 | 184 |
183 virtual void RunWork() { | 185 virtual void RunWork() { |
184 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 186 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
185 // that returns a FilePath and a PlatformFile. | 187 // that returns a FilePath and a PlatformFile. |
186 file_util::CreateTemporaryFile(&file_path_); | 188 file_util::CreateTemporaryFile(&file_path_); |
187 | 189 |
188 // Use a fixed set of flags that are appropriate for writing to a temporary | 190 // Use a fixed set of flags that are appropriate for writing to a temporary |
189 // file from the IO thread using a net::FileStream. | 191 // file from the IO thread using a net::FileStream. |
190 int file_flags = | 192 int file_flags = |
191 base::PLATFORM_FILE_CREATE_ALWAYS | | 193 base::PLATFORM_FILE_CREATE_ALWAYS | |
192 base::PLATFORM_FILE_WRITE | | 194 base::PLATFORM_FILE_WRITE | |
193 base::PLATFORM_FILE_ASYNC | | |
194 base::PLATFORM_FILE_TEMPORARY; | 195 base::PLATFORM_FILE_TEMPORARY; |
| 196 if (async_) { |
| 197 file_flags |= base::PLATFORM_FILE_ASYNC; |
| 198 } |
195 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 199 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
196 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, | 200 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, |
197 NULL, &error_code); | 201 NULL, &error_code); |
198 set_error_code(error_code); | 202 set_error_code(error_code); |
199 } | 203 } |
200 | 204 |
201 virtual void RunCallback() { | 205 virtual void RunCallback() { |
202 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 206 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), |
203 file_path_); | 207 file_path_); |
204 delete callback_; | 208 delete callback_; |
205 } | 209 } |
206 | 210 |
207 private: | 211 private: |
208 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 212 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 213 bool async_; |
209 base::FileUtilProxy::CreateTemporaryCallback* callback_; | 214 base::FileUtilProxy::CreateTemporaryCallback* callback_; |
210 base::PlatformFile file_handle_; | 215 base::PlatformFile file_handle_; |
211 FilePath file_path_; | 216 FilePath file_path_; |
212 }; | 217 }; |
213 | 218 |
214 class RelayWithStatusCallback : public MessageLoopRelay { | 219 class RelayWithStatusCallback : public MessageLoopRelay { |
215 public: | 220 public: |
216 explicit RelayWithStatusCallback( | 221 explicit RelayWithStatusCallback( |
217 base::FileUtilProxy::StatusCallback* callback) | 222 base::FileUtilProxy::StatusCallback* callback) |
218 : callback_(callback) { | 223 : callback_(callback) { |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 740 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
736 const FilePath& file_path, int file_flags, | 741 const FilePath& file_path, int file_flags, |
737 CreateOrOpenCallback* callback) { | 742 CreateOrOpenCallback* callback) { |
738 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 743 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
739 message_loop_proxy, file_path, file_flags, callback)); | 744 message_loop_proxy, file_path, file_flags, callback)); |
740 } | 745 } |
741 | 746 |
742 // static | 747 // static |
743 bool FileUtilProxy::CreateTemporary( | 748 bool FileUtilProxy::CreateTemporary( |
744 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 749 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 750 bool async, |
745 CreateTemporaryCallback* callback) { | 751 CreateTemporaryCallback* callback) { |
746 return Start(FROM_HERE, message_loop_proxy, | 752 return Start(FROM_HERE, message_loop_proxy, |
747 new RelayCreateTemporary(message_loop_proxy, callback)); | 753 new RelayCreateTemporary(message_loop_proxy, async, 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 |