| 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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 248 |
| 249 private: | 249 private: |
| 250 base::PlatformFile file_handle_; | 250 base::PlatformFile file_handle_; |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 class RelayEnsureFileExists : public MessageLoopRelay { | 253 class RelayEnsureFileExists : public MessageLoopRelay { |
| 254 public: | 254 public: |
| 255 RelayEnsureFileExists( | 255 RelayEnsureFileExists( |
| 256 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 256 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 257 const FilePath& file_path, | 257 const FilePath& file_path, |
| 258 base::FileUtilProxy::EnsureFileExistsCallback* callback) | 258 const base::FileUtilProxy::EnsureFileExistsCallback& callback) |
| 259 : message_loop_proxy_(message_loop_proxy), | 259 : message_loop_proxy_(message_loop_proxy), |
| 260 file_path_(file_path), | 260 file_path_(file_path), |
| 261 callback_(callback), | 261 callback_(callback), |
| 262 created_(false) { | 262 created_(false) { |
| 263 DCHECK(callback); | 263 DCHECK_EQ(false, callback.is_null()); |
| 264 } | 264 } |
| 265 | 265 |
| 266 protected: | 266 protected: |
| 267 virtual void RunWork() { | 267 virtual void RunWork() { |
| 268 if (!file_util::DirectoryExists(file_path_.DirName())) { | 268 if (!file_util::DirectoryExists(file_path_.DirName())) { |
| 269 // If its parent does not exist, should return NOT_FOUND error. | 269 // If its parent does not exist, should return NOT_FOUND error. |
| 270 set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); | 270 set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 271 return; | 271 return; |
| 272 } | 272 } |
| 273 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 273 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 274 // Tries to create the |file_path_| exclusively. This should fail | 274 // Tries to create the |file_path_| exclusively. This should fail |
| 275 // with PLATFORM_FILE_ERROR_EXISTS if the path already exists. | 275 // with PLATFORM_FILE_ERROR_EXISTS if the path already exists. |
| 276 base::PlatformFile handle = base::CreatePlatformFile( | 276 base::PlatformFile handle = base::CreatePlatformFile( |
| 277 file_path_, | 277 file_path_, |
| 278 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 278 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 279 &created_, &error_code); | 279 &created_, &error_code); |
| 280 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { | 280 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { |
| 281 // Make sure created_ is false. | 281 // Make sure created_ is false. |
| 282 created_ = false; | 282 created_ = false; |
| 283 error_code = base::PLATFORM_FILE_OK; | 283 error_code = base::PLATFORM_FILE_OK; |
| 284 } | 284 } |
| 285 if (handle != base::kInvalidPlatformFileValue) | 285 if (handle != base::kInvalidPlatformFileValue) |
| 286 base::ClosePlatformFile(handle); | 286 base::ClosePlatformFile(handle); |
| 287 set_error_code(error_code); | 287 set_error_code(error_code); |
| 288 } | 288 } |
| 289 | 289 |
| 290 virtual void RunCallback() { | 290 virtual void RunCallback() { |
| 291 callback_->Run(error_code(), created_); | 291 callback_.Run(error_code(), created_); |
| 292 delete callback_; | |
| 293 } | 292 } |
| 294 | 293 |
| 295 private: | 294 private: |
| 296 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 295 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 297 FilePath file_path_; | 296 FilePath file_path_; |
| 298 base::FileUtilProxy::EnsureFileExistsCallback* callback_; | 297 base::FileUtilProxy::EnsureFileExistsCallback callback_; |
| 299 bool created_; | 298 bool created_; |
| 300 }; | 299 }; |
| 301 | 300 |
| 302 class RelayDelete : public RelayWithStatusCallback { | 301 class RelayDelete : public RelayWithStatusCallback { |
| 303 public: | 302 public: |
| 304 RelayDelete(const FilePath& file_path, | 303 RelayDelete(const FilePath& file_path, |
| 305 bool recursive, | 304 bool recursive, |
| 306 base::FileUtilProxy::StatusCallback* callback) | 305 base::FileUtilProxy::StatusCallback* callback) |
| 307 : RelayWithStatusCallback(callback), | 306 : RelayWithStatusCallback(callback), |
| 308 file_path_(file_path), | 307 file_path_(file_path), |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 base::PlatformFile file_handle, | 757 base::PlatformFile file_handle, |
| 759 StatusCallback* callback) { | 758 StatusCallback* callback) { |
| 760 return Start(FROM_HERE, message_loop_proxy, | 759 return Start(FROM_HERE, message_loop_proxy, |
| 761 new RelayClose(file_handle, callback)); | 760 new RelayClose(file_handle, callback)); |
| 762 } | 761 } |
| 763 | 762 |
| 764 // static | 763 // static |
| 765 bool FileUtilProxy::EnsureFileExists( | 764 bool FileUtilProxy::EnsureFileExists( |
| 766 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 765 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 767 const FilePath& file_path, | 766 const FilePath& file_path, |
| 768 EnsureFileExistsCallback* callback) { | 767 const EnsureFileExistsCallback& callback) { |
| 769 return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( | 768 return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( |
| 770 message_loop_proxy, file_path, callback)); | 769 message_loop_proxy, file_path, callback)); |
| 771 } | 770 } |
| 772 | 771 |
| 773 // Retrieves the information about a file. It is invalid to pass NULL for the | 772 // Retrieves the information about a file. It is invalid to pass NULL for the |
| 774 // callback. | 773 // callback. |
| 775 bool FileUtilProxy::GetFileInfo( | 774 bool FileUtilProxy::GetFileInfo( |
| 776 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 775 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 777 const FilePath& file_path, | 776 const FilePath& file_path, |
| 778 GetFileInfoCallback* callback) { | 777 GetFileInfoCallback* callback) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 922 | 921 |
| 923 // static | 922 // static |
| 924 bool FileUtilProxy::Flush( | 923 bool FileUtilProxy::Flush( |
| 925 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 924 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 926 PlatformFile file, | 925 PlatformFile file, |
| 927 StatusCallback* callback) { | 926 StatusCallback* callback) { |
| 928 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 927 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| 929 } | 928 } |
| 930 | 929 |
| 931 } // namespace base | 930 } // namespace base |
| OLD | NEW |