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" |
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 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 file_flags_(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 | 191 if (!(file_flags_ & base::PLATFORM_FILE_WRITE) || |
189 // file from the IO thread using a net::FileStream. | 192 !(file_flags_ & base::PLATFORM_FILE_TEMPORARY) || |
190 int file_flags = | 193 (!(file_flags_ & base::PLATFORM_FILE_CREATE_ALWAYS) && |
191 base::PLATFORM_FILE_CREATE_ALWAYS | | 194 !(file_flags_ & base::PLATFORM_FILE_CREATE) && |
192 base::PLATFORM_FILE_WRITE | | 195 !(file_flags_ & base::PLATFORM_FILE_OPEN_ALWAYS))) { |
193 base::PLATFORM_FILE_ASYNC | | 196 LOG(DFATAL) << "Invalid file_flags for CreateTemporary: " << file_flags_; |
194 base::PLATFORM_FILE_TEMPORARY; | 197 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 198 return; |
| 199 } |
195 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 200 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
196 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, | 201 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, |
197 NULL, &error_code); | 202 NULL, &error_code); |
198 set_error_code(error_code); | 203 set_error_code(error_code); |
199 } | 204 } |
200 | 205 |
201 virtual void RunCallback() { | 206 virtual void RunCallback() { |
202 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 207 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), |
203 file_path_); | 208 file_path_); |
204 delete callback_; | 209 delete callback_; |
205 } | 210 } |
206 | 211 |
207 private: | 212 private: |
208 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 213 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 214 int file_flags_; |
209 base::FileUtilProxy::CreateTemporaryCallback* callback_; | 215 base::FileUtilProxy::CreateTemporaryCallback* callback_; |
210 base::PlatformFile file_handle_; | 216 base::PlatformFile file_handle_; |
211 FilePath file_path_; | 217 FilePath file_path_; |
212 }; | 218 }; |
213 | 219 |
214 class RelayWithStatusCallback : public MessageLoopRelay { | 220 class RelayWithStatusCallback : public MessageLoopRelay { |
215 public: | 221 public: |
216 explicit RelayWithStatusCallback( | 222 explicit RelayWithStatusCallback( |
217 base::FileUtilProxy::StatusCallback* callback) | 223 base::FileUtilProxy::StatusCallback* callback) |
218 : callback_(callback) { | 224 : callback_(callback) { |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 741 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
736 const FilePath& file_path, int file_flags, | 742 const FilePath& file_path, int file_flags, |
737 CreateOrOpenCallback* callback) { | 743 CreateOrOpenCallback* callback) { |
738 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 744 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
739 message_loop_proxy, file_path, file_flags, callback)); | 745 message_loop_proxy, file_path, file_flags, callback)); |
740 } | 746 } |
741 | 747 |
742 // static | 748 // static |
743 bool FileUtilProxy::CreateTemporary( | 749 bool FileUtilProxy::CreateTemporary( |
744 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 750 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 751 int file_flags, |
745 CreateTemporaryCallback* callback) { | 752 CreateTemporaryCallback* callback) { |
746 return Start(FROM_HERE, message_loop_proxy, | 753 return Start(FROM_HERE, message_loop_proxy, |
747 new RelayCreateTemporary(message_loop_proxy, callback)); | 754 new RelayCreateTemporary(message_loop_proxy, |
| 755 file_flags, |
| 756 callback)); |
748 } | 757 } |
749 | 758 |
750 // static | 759 // static |
751 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 760 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
752 base::PlatformFile file_handle, | 761 base::PlatformFile file_handle, |
753 StatusCallback* callback) { | 762 StatusCallback* callback) { |
754 return Start(FROM_HERE, message_loop_proxy, | 763 return Start(FROM_HERE, message_loop_proxy, |
755 new RelayClose(file_handle, callback)); | 764 new RelayClose(file_handle, callback)); |
756 } | 765 } |
757 | 766 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 | 917 |
909 // static | 918 // static |
910 bool FileUtilProxy::Flush( | 919 bool FileUtilProxy::Flush( |
911 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 920 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
912 PlatformFile file, | 921 PlatformFile file, |
913 StatusCallback* callback) { | 922 StatusCallback* callback) { |
914 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 923 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
915 } | 924 } |
916 | 925 |
917 } // namespace base | 926 } // namespace base |
OLD | NEW |