dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 5 | #ifndef BASE_FILE_UTIL_PROXY_H_ |
| 6 | #define BASE_FILE_UTIL_PROXY_H_ |
| 7 | |
| 8 | #include <vector> |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 9 | |
| 10 | #include "base/callback.h" |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 11 | #include "base/file_path.h" |
| 12 | #include "base/file_util.h" |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 13 | #include "base/platform_file.h" |
| 14 | #include "base/ref_counted.h" |
| 15 | #include "base/tracked_objects.h" |
| 16 | |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 17 | namespace file_util { |
| 18 | struct FileInfo; |
| 19 | } |
| 20 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 21 | namespace base { |
| 22 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 23 | namespace file_util_proxy { |
| 24 | // Holds metadata for file or directory entry. |
| 25 | struct Entry { |
| 26 | FilePath::StringType name; |
| 27 | bool isDirectory; |
| 28 | }; |
| 29 | } // namespace file_util_proxy |
| 30 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 31 | class MessageLoopProxy; |
| 32 | |
| 33 | // This class provides asynchronous access to common file routines. |
| 34 | class FileUtilProxy { |
| 35 | public: |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 36 | // This callback is used by methods that report only an error code. It is |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 37 | // valid to pass NULL as the callback parameter to any function that takes a |
| 38 | // StatusCallback, in which case the operation will complete silently. |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 39 | typedef Callback1<base::PlatformFileError /* error code */ |
| 40 | >::Type StatusCallback; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 41 | |
| 42 | // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 43 | // for the callback. |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 44 | typedef Callback3<base::PlatformFileError /* error code */, |
| 45 | base::PassPlatformFile, |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 46 | bool /* created */>::Type CreateOrOpenCallback; |
| 47 | static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 48 | const FilePath& file_path, |
| 49 | int file_flags, |
| 50 | CreateOrOpenCallback* callback); |
| 51 | |
| 52 | // Creates a temporary file for writing. The path and an open file handle |
| 53 | // are returned. It is invalid to pass NULL for the callback. |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 54 | typedef Callback3<base::PlatformFileError /* error code */, |
| 55 | base::PassPlatformFile, |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 56 | FilePath>::Type CreateTemporaryCallback; |
| 57 | static bool CreateTemporary( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 58 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 59 | CreateTemporaryCallback* callback); |
| 60 | |
| 61 | // Close the given file handle. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 62 | static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 63 | base::PlatformFile, |
| 64 | StatusCallback* callback); |
| 65 | |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 66 | // Retrieves the information about a file. It is invalid to pass NULL for the |
| 67 | // callback. |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 68 | typedef Callback2<base::PlatformFileError /* error code */, |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 69 | const file_util::FileInfo& /*file_info*/ |
| 70 | >::Type GetFileInfoCallback; |
| 71 | static bool GetFileInfo( |
| 72 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 73 | const FilePath& file_path, |
| 74 | GetFileInfoCallback* callback); |
| 75 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 76 | typedef Callback2<base::PlatformFileError /* error code */, |
| 77 | const std::vector<base::file_util_proxy::Entry>& |
| 78 | >::Type ReadDirectoryCallback; |
| 79 | static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 80 | const FilePath& file_path, |
| 81 | ReadDirectoryCallback* callback); |
| 82 | |
| 83 | // Copies a file or a directory from |src_file_path| to |dest_file_path| |
| 84 | // Error cases: |
| 85 | // If destination file doesn't exist or destination's parent |
| 86 | // doesn't exists. |
| 87 | // If source dir exists but destination path is an existing file. |
| 88 | // If source is a parent of destination. |
| 89 | // If source doesn't exists. |
| 90 | static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 91 | const FilePath& src_file_path, |
| 92 | const FilePath& dest_file_path, |
| 93 | StatusCallback* callback); |
| 94 | |
| 95 | // Creates directory at given path. It's an error to create |
| 96 | // if |exclusive| is true and dir already exists. |
| 97 | static bool CreateDirectory( |
| 98 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 99 | const FilePath& file_path, |
| 100 | bool exclusive, |
| 101 | StatusCallback* callback); |
| 102 | |
| 103 | // Deletes a file or empty directory. |
| 104 | static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 105 | const FilePath& file_path, |
| 106 | StatusCallback* callback); |
| 107 | |
| 108 | // Moves a file or a directory from src_file_path to dest_file_path. |
| 109 | // Error cases are similar to Copy method's error cases. |
| 110 | static bool Move( |
| 111 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 112 | const FilePath& src_file_path, |
| 113 | const FilePath& dest_file_path, |
| 114 | StatusCallback* callback); |
| 115 | |
| 116 | // Deletes a directory and all of its contents. |
| 117 | static bool RecursiveDelete( |
| 118 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 119 | const FilePath& file_path, |
| 120 | StatusCallback* callback); |
| 121 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 122 | private: |
| 123 | DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 124 | }; |
| 125 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 126 | } // namespace base |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 127 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame^] | 128 | #endif // BASE_FILE_UTIL_PROXY_H_ |