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 #ifndef BASE_FILE_UTIL_PROXY_H_ | 5 #ifndef BASE_FILE_UTIL_PROXY_H_ |
| 6 #define BASE_FILE_UTIL_PROXY_H_ | 6 #define BASE_FILE_UTIL_PROXY_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/callback_old.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/location.h" | |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
| 16 #include "base/tracked_objects.h" | 17 #include "base/tracked_objects.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 class MessageLoopProxy; | 21 class MessageLoopProxy; |
| 21 class Time; | 22 class Time; |
| 22 | 23 |
| 24 namespace internal { | |
| 25 template <typename R, typename R1> | |
| 26 struct ReturnValueAdaptor { | |
| 27 static R1 Value(const R& value); | |
| 28 }; | |
| 29 | |
| 30 template <typename R> | |
| 31 struct ReturnValueAdaptor<R, R> { | |
| 32 static PlatformFileError Value(const R& value) { return value; } | |
| 33 }; | |
| 34 | |
| 35 template <> | |
| 36 struct ReturnValueAdaptor<bool, PlatformFileError> { | |
| 37 static PlatformFileError Value(const bool& value) { | |
| 38 if (value) | |
| 39 return PLATFORM_FILE_OK; | |
| 40 return PLATFORM_FILE_ERROR_FAILED; | |
| 41 } | |
| 42 }; | |
| 43 } // namespace internal | |
| 44 | |
| 23 // This class provides asynchronous access to common file routines. | 45 // This class provides asynchronous access to common file routines. |
| 24 class BASE_EXPORT FileUtilProxy { | 46 class BASE_EXPORT FileUtilProxy { |
| 25 public: | 47 public: |
| 26 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback. | 48 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback. |
| 27 struct Entry { | 49 struct Entry { |
| 28 FilePath::StringType name; | 50 FilePath::StringType name; |
| 29 bool is_directory; | 51 bool is_directory; |
| 30 int64 size; | 52 int64 size; |
| 31 base::Time last_modified_time; | 53 base::Time last_modified_time; |
| 32 }; | 54 }; |
| 33 | 55 |
| 34 // This callback is used by methods that report only an error code. It is | 56 // This callback is used by methods that report only an error code. |
| 35 // valid to pass NULL as the callback parameter to any function that takes a | 57 typedef Callback<void(PlatformFileError)> StatusCallback; |
| 36 // StatusCallback, in which case the operation will complete silently. | |
| 37 // The ownership of |callback| is taken by the function and will always be | |
| 38 // deleted by the function even on failure. | |
| 39 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback; | |
| 40 | 58 |
| 41 typedef Callback3<PlatformFileError /* error code */, | 59 typedef Callback<void(PlatformFileError, |
| 42 PassPlatformFile, | 60 PassPlatformFile, |
| 43 bool /* created */>::Type CreateOrOpenCallback; | 61 bool /* created */)> CreateOrOpenCallback; |
| 44 typedef Callback3<PlatformFileError /* error code */, | 62 typedef Callback<void(PlatformFileError, |
| 45 PassPlatformFile, | 63 PassPlatformFile, |
| 46 FilePath>::Type CreateTemporaryCallback; | 64 FilePath)> CreateTemporaryCallback; |
| 47 typedef Callback2<PlatformFileError /* error code */, | 65 typedef Callback<void(PlatformFileError, |
| 48 bool /* created */>::Type EnsureFileExistsCallback; | 66 const PlatformFileInfo&)> GetFileInfoCallback; |
| 49 typedef Callback2<PlatformFileError /* error code */, | 67 typedef Callback<void(PlatformFileError, |
| 50 const PlatformFileInfo& /* file_info */ | 68 const std::vector<Entry>&)> ReadDirectoryCallback; |
| 51 >::Type GetFileInfoCallback; | 69 typedef Callback<void(PlatformFileError, |
| 52 typedef Callback2<PlatformFileError /* error code */, | 70 const char* /* data */, |
| 53 const std::vector<Entry>&>::Type ReadDirectoryCallback; | 71 int /* bytes read/written */)> ReadCallback; |
| 54 typedef Callback3<PlatformFileError /* error code */, | 72 typedef Callback<void(PlatformFileError, |
| 55 const char* /* data */, | 73 int /* bytes written */)> WriteCallback; |
| 56 int /* bytes read/written */>::Type ReadCallback; | 74 |
| 57 typedef Callback2<PlatformFileError /* error code */, | 75 class MessageLoopRelay : public RefCountedThreadSafe<MessageLoopRelay> { |
|
awong
2011/10/12 08:09:27
Is it possible to not have this class at All?
It l
kinuko
2011/10/12 11:23:38
Thanks for your early comments!
Hmm... I see. The
awong
2011/10/15 00:17:12
I think that'd work pretty well. About the memory
| |
| 58 int /* bytes written */>::Type WriteCallback; | 76 public: |
| 77 MessageLoopRelay() {} | |
| 78 bool Start(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 79 const tracked_objects::Location& from_here); | |
| 80 protected: | |
| 81 friend class RefCountedThreadSafe<MessageLoopRelay>; | |
| 82 virtual ~MessageLoopRelay() {} | |
| 83 virtual void RunWork() = 0; | |
| 84 virtual void RunCallback() = 0; | |
| 85 }; | |
| 86 | |
| 87 template <typename R, typename R1 = R> | |
| 88 class MessageLoopRelayWithReturn : public MessageLoopRelay { | |
| 89 public: | |
| 90 MessageLoopRelayWithReturn(const Callback<R(void)>& closure, | |
| 91 const Callback<void(R1)>& callback, | |
| 92 R initial_value) | |
| 93 : closure_(closure), callback_(callback), | |
| 94 result_(initial_value) {} | |
| 95 | |
| 96 protected: | |
| 97 virtual ~MessageLoopRelayWithReturn() {} | |
| 98 | |
| 99 virtual void RunWork() OVERRIDE { | |
| 100 result_ = closure_.Run(); | |
| 101 } | |
| 102 | |
| 103 virtual void RunCallback() OVERRIDE { | |
| 104 if (!callback_.is_null()) { | |
| 105 callback_.Run(internal::ReturnValueAdaptor<R, R1>::Value(result_)); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 Callback<R(void)> closure_; | |
| 111 Callback<void(R1)> callback_; | |
| 112 R result_; | |
| 113 }; | |
| 114 | |
| 115 // A helper function that runs the given |file_function| that returns R. | |
| 116 // It is valid to pass Callback<>() as the callback parameter, | |
| 117 // in which case the operation will complete silently (like you do | |
| 118 // with normal PostTask with IgnoreReturn). | |
| 119 template <typename R, typename A> | |
| 120 static bool PostFileTaskAndReplyStatus( | |
| 121 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 122 const tracked_objects::Location& from_here, | |
| 123 const Callback<R(void)>& file_function, | |
| 124 const Callback<void(A)>& callback) { | |
| 125 typedef MessageLoopRelayWithReturn<R, A> RelayType; | |
| 126 return make_scoped_refptr( | |
| 127 new RelayType(file_function, callback, PLATFORM_FILE_OK))->Start( | |
| 128 message_loop_proxy, from_here); | |
| 129 } | |
| 59 | 130 |
| 60 // Creates or opens a file with the given flags. It is invalid to pass NULL | 131 // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 61 // for the callback. | 132 // for the callback. |
| 62 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create | 133 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create |
| 63 // a new file at the given |file_path| and calls back with | 134 // a new file at the given |file_path| and calls back with |
| 64 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | 135 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 65 // Takes ownership of |callback| and will delete it even on failure. | 136 // Takes ownership of |callback| and will delete it even on failure. |
| 66 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 137 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 67 const FilePath& file_path, | 138 const FilePath& file_path, |
| 68 int file_flags, | 139 int file_flags, |
| 69 CreateOrOpenCallback* callback); | 140 const CreateOrOpenCallback& callback); |
| 70 | 141 |
| 71 // Creates a temporary file for writing. The path and an open file handle | 142 // Creates a temporary file for writing. The path and an open file handle |
| 72 // are returned. It is invalid to pass NULL for the callback. The additional | 143 // are returned. It is invalid to pass NULL for the callback. The additional |
| 73 // file flags will be added on top of the default file flags which are: | 144 // file flags will be added on top of the default file flags which are: |
| 74 // base::PLATFORM_FILE_CREATE_ALWAYS | 145 // base::PLATFORM_FILE_CREATE_ALWAYS |
| 75 // base::PLATFORM_FILE_WRITE | 146 // base::PLATFORM_FILE_WRITE |
| 76 // base::PLATFORM_FILE_TEMPORARY. | 147 // base::PLATFORM_FILE_TEMPORARY. |
| 77 // Set |additional_file_flags| to 0 for synchronous writes and set to | 148 // Set |additional_file_flags| to 0 for synchronous writes and set to |
| 78 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. | 149 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. |
| 79 static bool CreateTemporary( | 150 static bool CreateTemporary( |
| 80 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 151 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 81 int additional_file_flags, | 152 int additional_file_flags, |
| 82 CreateTemporaryCallback* callback); | 153 const CreateTemporaryCallback& callback); |
| 83 | |
| 84 // Close the given file handle. | |
| 85 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 86 PlatformFile, | |
| 87 StatusCallback* callback); | |
| 88 | |
| 89 // Ensures that the given |file_path| exist. This creates a empty new file | |
| 90 // at |file_path| if the |file_path| does not exist. | |
| 91 // If a new file han not existed and is created at the |file_path|, | |
| 92 // |created| of the callback argument is set true and |error code| | |
| 93 // is set PLATFORM_FILE_OK. | |
| 94 // If the file already exists, |created| is set false and |error code| | |
| 95 // is set PLATFORM_FILE_OK. | |
| 96 // If the file hasn't existed but it couldn't be created for some other | |
| 97 // reasons, |created| is set false and |error code| indicates the error. | |
| 98 static bool EnsureFileExists( | |
| 99 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 100 const FilePath& file_path, | |
| 101 EnsureFileExistsCallback* callback); | |
| 102 | 154 |
| 103 // Retrieves the information about a file. It is invalid to pass NULL for the | 155 // Retrieves the information about a file. It is invalid to pass NULL for the |
| 104 // callback. | 156 // callback. |
| 105 static bool GetFileInfo( | 157 static bool GetFileInfo( |
| 106 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 158 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 107 const FilePath& file_path, | 159 const FilePath& file_path, |
| 108 GetFileInfoCallback* callback); | 160 const GetFileInfoCallback& callback); |
| 109 | 161 |
| 110 static bool GetFileInfoFromPlatformFile( | 162 static bool GetFileInfoFromPlatformFile( |
| 111 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 163 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 112 PlatformFile file, | 164 PlatformFile file, |
| 113 GetFileInfoCallback* callback); | 165 const GetFileInfoCallback& callback); |
| 114 | |
| 115 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 116 const FilePath& file_path, | |
| 117 ReadDirectoryCallback* callback); | |
| 118 | |
| 119 // Creates directory at given path. It's an error to create | |
| 120 // if |exclusive| is true and dir already exists. | |
| 121 static bool CreateDirectory( | |
| 122 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 123 const FilePath& file_path, | |
| 124 bool exclusive, | |
| 125 bool recursive, | |
| 126 StatusCallback* callback); | |
| 127 | |
| 128 // Copies a file or a directory from |src_file_path| to |dest_file_path| | |
| 129 // Error cases: | |
| 130 // If destination file doesn't exist or destination's parent | |
| 131 // doesn't exists. | |
| 132 // If source dir exists but destination path is an existing file. | |
| 133 // If source file exists but destination path is an existing directory. | |
| 134 // If source is a parent of destination. | |
| 135 // If source doesn't exists. | |
| 136 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 137 const FilePath& src_file_path, | |
| 138 const FilePath& dest_file_path, | |
| 139 StatusCallback* callback); | |
| 140 | |
| 141 // Moves a file or a directory from src_file_path to dest_file_path. | |
| 142 // Error cases are similar to Copy method's error cases. | |
| 143 static bool Move( | |
| 144 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 145 const FilePath& src_file_path, | |
| 146 const FilePath& dest_file_path, | |
| 147 StatusCallback* callback); | |
| 148 | |
| 149 // Deletes a file or a directory. | |
| 150 // It is an error to delete a non-empty directory with recursive=false. | |
| 151 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 152 const FilePath& file_path, | |
| 153 bool recursive, | |
| 154 StatusCallback* callback); | |
| 155 | |
| 156 // Deletes a directory and all of its contents. | |
| 157 static bool RecursiveDelete( | |
| 158 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 159 const FilePath& file_path, | |
| 160 StatusCallback* callback); | |
| 161 | 166 |
| 162 // Reads from a file. On success, the file pointer is moved to position | 167 // Reads from a file. On success, the file pointer is moved to position |
| 163 // |offset + bytes_to_read| in the file. The callback can be NULL. | 168 // |offset + bytes_to_read| in the file. The callback can be NULL. |
| 164 static bool Read( | 169 static bool Read( |
| 165 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 170 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 166 PlatformFile file, | 171 PlatformFile file, |
| 167 int64 offset, | 172 int64 offset, |
| 168 int bytes_to_read, | 173 int bytes_to_read, |
| 169 ReadCallback* callback); | 174 const ReadCallback& callback); |
| 170 | 175 |
| 171 // Writes to a file. If |offset| is greater than the length of the file, | 176 // Writes to a file. If |offset| is greater than the length of the file, |
| 172 // |false| is returned. On success, the file pointer is moved to position | 177 // |false| is returned. On success, the file pointer is moved to position |
| 173 // |offset + bytes_to_write| in the file. The callback can be NULL. | 178 // |offset + bytes_to_write| in the file. The callback can be NULL. |
| 174 // |bytes_to_write| must be greater than zero. | 179 // |bytes_to_write| must be greater than zero. |
| 175 static bool Write( | 180 static bool Write( |
| 176 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 181 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 177 PlatformFile file, | 182 PlatformFile file, |
| 178 int64 offset, | 183 int64 offset, |
| 179 const char* buffer, | 184 const char* buffer, |
| 180 int bytes_to_write, | 185 int bytes_to_write, |
| 181 WriteCallback* callback); | 186 const WriteCallback& callback); |
| 182 | |
| 183 // Touches a file. The callback can be NULL. | |
| 184 static bool Touch( | |
| 185 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 186 PlatformFile file, | |
| 187 const Time& last_access_time, | |
| 188 const Time& last_modified_time, | |
| 189 StatusCallback* callback); | |
| 190 | |
| 191 // Touches a file. The callback can be NULL. | |
| 192 static bool Touch( | |
| 193 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 194 const FilePath& file_path, | |
| 195 const Time& last_access_time, | |
| 196 const Time& last_modified_time, | |
| 197 StatusCallback* callback); | |
| 198 | |
| 199 // Truncates a file to the given length. If |length| is greater than the | |
| 200 // current length of the file, the file will be extended with zeroes. | |
| 201 // The callback can be NULL. | |
| 202 static bool Truncate( | |
| 203 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 204 PlatformFile file, | |
| 205 int64 length, | |
| 206 StatusCallback* callback); | |
| 207 | |
| 208 // Truncates a file to the given length. If |length| is greater than the | |
| 209 // current length of the file, the file will be extended with zeroes. | |
| 210 // The callback can be NULL. | |
| 211 static bool Truncate( | |
| 212 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 213 const FilePath& path, | |
| 214 int64 length, | |
| 215 StatusCallback* callback); | |
| 216 | |
| 217 // Flushes a file. The callback can be NULL. | |
| 218 static bool Flush( | |
| 219 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 220 PlatformFile file, | |
| 221 StatusCallback* callback); | |
| 222 | 187 |
| 223 private: | 188 private: |
| 224 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); | 189 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 225 }; | 190 }; |
| 226 | 191 |
| 227 } // namespace base | 192 } // namespace base |
| 228 | 193 |
| 229 #endif // BASE_FILE_UTIL_PROXY_H_ | 194 #endif // BASE_FILE_UTIL_PROXY_H_ |
| OLD | NEW |