| 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 WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_PROXY_H_ |
| 6 #define BASE_FILE_UTIL_PROXY_H_ | 6 #define WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_PROXY_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | |
| 10 #include "base/callback.h" | 9 #include "base/callback.h" |
| 11 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 11 #include "base/file_util_proxy.h" |
| 13 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 14 #include "base/ref_counted.h" | |
| 15 #include "base/tracked_objects.h" | |
| 16 | 13 |
| 17 namespace base { | 14 namespace base { |
| 18 | |
| 19 class MessageLoopProxy; | 15 class MessageLoopProxy; |
| 20 class Time; | 16 class Time; |
| 17 } |
| 21 | 18 |
| 22 // This class provides asynchronous access to common file routines. | 19 namespace fileapi { |
| 23 class FileUtilProxy { | 20 |
| 21 class SandboxedFileSystemContext; |
| 22 |
| 23 class ObfuscatedFileUtilProxy : public base::FileUtilProxy { |
| 24 public: | 24 public: |
| 25 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback. | 25 ObfuscatedFileUtilProxy(SandboxedFileSystemContext *file_system_context) : |
| 26 struct Entry { | 26 file_system_context_(file_system_context) { |
| 27 FilePath::StringType name; | 27 } |
| 28 bool is_directory; | 28 // Bridges to the file thread and does the series of operations necessary to |
| 29 }; | 29 // create or open the file, as needed. Creation requires updating [or |
| 30 | 30 // creating] a single dictionary file. Opening an existing file doesn't |
| 31 // This callback is used by methods that report only an error code. It is | 31 // require any dictionary lookup. |
| 32 // valid to pass NULL as the callback parameter to any function that takes a | 32 // All the interesting stuff, including translations, should happen on the |
| 33 // StatusCallback, in which case the operation will complete silently. | 33 // file thread. That way ObfuscatedFileUtil can be called directly from the |
| 34 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback; | 34 // file thread and it'll do all the work. Plus it removes synchronization |
| 35 | 35 // problems to do it all in one long-running operation over there. So these |
| 36 typedef Callback3<PlatformFileError /* error code */, | 36 // proxy functions are really thin wrappers. Over on the file thread, the |
| 37 PassPlatformFile, | 37 // implementation of e.g. CreateDirectory will look like: |
| 38 bool /* created */>::Type CreateOrOpenCallback; | 38 // * Read dictionary file; check for conflicts. |
| 39 typedef Callback3<PlatformFileError /* error code */, | 39 // * Update dictionary; write out dictionary.next. |
| 40 PassPlatformFile, | 40 // * Call FileUtil::CreateDirectory. |
| 41 FilePath>::Type CreateTemporaryCallback; | 41 // * Move dictionary.next onto dictionary. |
| 42 typedef Callback2<PlatformFileError /* error code */, | |
| 43 bool /* created */>::Type EnsureFileExistsCallback; | |
| 44 typedef Callback2<PlatformFileError /* error code */, | |
| 45 const PlatformFileInfo& /* file_info */ | |
| 46 >::Type GetFileInfoCallback; | |
| 47 typedef Callback2<PlatformFileError /* error code */, | |
| 48 const std::vector<Entry>&>::Type ReadDirectoryCallback; | |
| 49 typedef Callback2<PlatformFileError /* error code */, | |
| 50 int /* bytes read/written */>::Type ReadWriteCallback; | |
| 51 | 42 |
| 52 // Creates or opens a file with the given flags. It is invalid to pass NULL | 43 // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 53 // for the callback. | 44 // for the callback. |
| 54 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create | 45 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create |
| 55 // a new file at the given |file_path| and calls back with | 46 // a new file at the given |file_path| and calls back with |
| 56 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | 47 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 57 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 48 // bool CreateOrOpen( |
| 58 const FilePath& file_path, | 49 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 59 int file_flags, | 50 // const FilePath& file_path, |
| 60 CreateOrOpenCallback* callback); | 51 // int file_flags, |
| 52 // CreateOrOpenCallback* callback); |
| 61 | 53 |
| 62 // Creates a temporary file for writing. The path and an open file handle | 54 // Ensures that the given |file_path| exists. This creates a empty new file |
| 63 // are returned. It is invalid to pass NULL for the callback. | |
| 64 static bool CreateTemporary( | |
| 65 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 66 CreateTemporaryCallback* callback); | |
| 67 | |
| 68 // Close the given file handle. | |
| 69 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 70 PlatformFile, | |
| 71 StatusCallback* callback); | |
| 72 | |
| 73 // Ensures that the given |file_path| exist. This creates a empty new file | |
| 74 // at |file_path| if the |file_path| does not exist. | 55 // at |file_path| if the |file_path| does not exist. |
| 75 // If a new file han not existed and is created at the |file_path|, | 56 // If the file had not existed and is created at the |file_path|, |
| 76 // |created| of the callback argument is set true and |error code| | 57 // |created| of the callback argument is set to true and |error code| |
| 77 // is set PLATFORM_FILE_OK. | 58 // is set to PLATFORM_FILE_OK. |
| 78 // If the file already exists, |created| is set false and |error code| | 59 // If the file already exists, |created| is set to false and |error code| |
| 79 // is set PLATFORM_FILE_OK. | 60 // is set to PLATFORM_FILE_OK. |
| 80 // If the file hasn't existed but it couldn't be created for some other | 61 // If the file hadn't existed but it couldn't be created for any reason, |
| 81 // reasons, |created| is set false and |error code| indicates the error. | 62 // |created| is set to false and |error code| indicates the error. |
| 82 static bool EnsureFileExists( | 63 // bool EnsureFileExists( |
| 83 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 64 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 84 const FilePath& file_path, | 65 // const FilePath& file_path, |
| 85 EnsureFileExistsCallback* callback); | 66 // EnsureFileExistsCallback* callback); |
| 86 | 67 |
| 87 // Retrieves the information about a file. It is invalid to pass NULL for the | 68 // Retrieves the information about a file. It is invalid to pass NULL for the |
| 88 // callback. | 69 // callback. |
| 89 static bool GetFileInfo( | 70 // bool GetFileInfo( |
| 90 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 71 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 91 const FilePath& file_path, | 72 // const FilePath& file_path, |
| 92 GetFileInfoCallback* callback); | 73 // GetFileInfoCallback* callback); |
| 93 | 74 |
| 94 static bool GetFileInfoFromPlatformFile( | 75 // bool ReadDirectory( |
| 95 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 76 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 96 PlatformFile file, | 77 // const FilePath& file_path, |
| 97 GetFileInfoCallback* callback); | 78 // ReadDirectoryCallback* callback); |
| 98 | |
| 99 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 100 const FilePath& file_path, | |
| 101 ReadDirectoryCallback* callback); | |
| 102 | 79 |
| 103 // Creates directory at given path. It's an error to create | 80 // Creates directory at given path. It's an error to create |
| 104 // if |exclusive| is true and dir already exists. | 81 // if |exclusive| is true and dir already exists. |
| 105 static bool CreateDirectory( | 82 bool CreateDirectory( |
| 106 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 83 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 107 const FilePath& file_path, | 84 const FilePath& file_path, |
| 108 bool exclusive, | 85 bool exclusive, |
| 109 bool recursive, | 86 bool recursive, |
| 110 StatusCallback* callback); | 87 StatusCallback* callback); |
| 111 | 88 |
| 112 // Copies a file or a directory from |src_file_path| to |dest_file_path| | 89 // Copies a file or a directory from |src_file_path| to |dest_file_path| |
| 113 // Error cases: | 90 // Error cases: |
| 114 // If destination file doesn't exist or destination's parent | 91 // If destination file doesn't exist or destination's parent |
| 115 // doesn't exists. | 92 // doesn't exists. |
| 116 // If source dir exists but destination path is an existing file. | 93 // If source dir exists but destination path is an existing file. |
| 117 // If source file exists but destination path is an existing directory. | 94 // If source file exists but destination path is an existing directory. |
| 118 // If source is a parent of destination. | 95 // If source is a parent of destination. |
| 119 // If source doesn't exists. | 96 // If source doesn't exists. |
| 120 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 97 // bool Copy( |
| 121 const FilePath& src_file_path, | 98 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 122 const FilePath& dest_file_path, | 99 // const FilePath& src_file_path, |
| 123 StatusCallback* callback); | 100 // const FilePath& dest_file_path, |
| 101 // StatusCallback* callback); |
| 124 | 102 |
| 125 // Moves a file or a directory from src_file_path to dest_file_path. | 103 // Moves a file or a directory from src_file_path to dest_file_path. |
| 126 // Error cases are similar to Copy method's error cases. | 104 // Error cases are similar to Copy method's error cases. |
| 127 static bool Move( | 105 // bool Move( |
| 128 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 106 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 129 const FilePath& src_file_path, | 107 // const FilePath& src_file_path, |
| 130 const FilePath& dest_file_path, | 108 // const FilePath& dest_file_path, |
| 131 StatusCallback* callback); | 109 // StatusCallback* callback); |
| 132 | 110 |
| 133 // Deletes a file or a directory. | 111 // Deletes a file or a directory. |
| 134 // It is an error to delete a non-empty directory with recursive=false. | 112 // It is an error to delete a non-empty directory with recursive=false. |
| 135 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 113 // bool Delete( |
| 136 const FilePath& file_path, | 114 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 137 bool recursive, | 115 // const FilePath& file_path, |
| 138 StatusCallback* callback); | 116 // bool recursive, |
| 117 // StatusCallback* callback); |
| 139 | 118 |
| 140 // Deletes a directory and all of its contents. | 119 // Deletes a directory and all of its contents. |
| 141 static bool RecursiveDelete( | 120 // bool RecursiveDelete( |
| 142 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 121 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 143 const FilePath& file_path, | 122 // const FilePath& file_path, |
| 144 StatusCallback* callback); | 123 // StatusCallback* callback); |
| 145 | |
| 146 // Reads from a file. On success, the file pointer is moved to position | |
| 147 // |offset + bytes_to_read| in the file. The callback can be NULL. | |
| 148 static bool Read( | |
| 149 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 150 PlatformFile file, | |
| 151 int64 offset, | |
| 152 char* buffer, | |
| 153 int bytes_to_read, | |
| 154 ReadWriteCallback* callback); | |
| 155 | |
| 156 // Writes to a file. If |offset| is greater than the length of the file, | |
| 157 // |false| is returned. On success, the file pointer is moved to position | |
| 158 // |offset + bytes_to_write| in the file. The callback can be NULL. | |
| 159 static bool Write( | |
| 160 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 161 PlatformFile file, | |
| 162 int64 offset, | |
| 163 const char* buffer, | |
| 164 int bytes_to_write, | |
| 165 ReadWriteCallback* callback); | |
| 166 | 124 |
| 167 // Touches a file. The callback can be NULL. | 125 // Touches a file. The callback can be NULL. |
| 168 static bool Touch( | 126 // bool Touch( |
| 169 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 127 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 170 PlatformFile file, | 128 // const FilePath& file_path, |
| 171 const Time& last_access_time, | 129 // const base::Time& last_access_time, |
| 172 const Time& last_modified_time, | 130 // const base::Time& last_modified_time, |
| 173 StatusCallback* callback); | 131 // StatusCallback* callback); |
| 174 | |
| 175 // Touches a file. The callback can be NULL. | |
| 176 static bool Touch( | |
| 177 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 178 const FilePath& file_path, | |
| 179 const Time& last_access_time, | |
| 180 const Time& last_modified_time, | |
| 181 StatusCallback* callback); | |
| 182 | 132 |
| 183 // Truncates a file to the given length. If |length| is greater than the | 133 // Truncates a file to the given length. If |length| is greater than the |
| 184 // current length of the file, the file will be extended with zeroes. | 134 // current length of the file, the file will be extended with zeroes. |
| 185 // The callback can be NULL. | 135 // The callback can be NULL. |
| 186 static bool Truncate( | 136 // bool Truncate( |
| 187 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 137 // scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 188 PlatformFile file, | 138 // const FilePath& path, |
| 189 int64 length, | 139 // int64 length, |
| 190 StatusCallback* callback); | 140 // StatusCallback* callback); |
| 191 | |
| 192 // Truncates a file to the given length. If |length| is greater than the | |
| 193 // current length of the file, the file will be extended with zeroes. | |
| 194 // The callback can be NULL. | |
| 195 static bool Truncate( | |
| 196 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 197 const FilePath& path, | |
| 198 int64 length, | |
| 199 StatusCallback* callback); | |
| 200 | |
| 201 // Flushes a file. The callback can be NULL. | |
| 202 static bool Flush( | |
| 203 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
| 204 PlatformFile file, | |
| 205 StatusCallback* callback); | |
| 206 | 141 |
| 207 private: | 142 private: |
| 208 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); | 143 void CrackPath( |
| 144 const FilePath& file_path, FilePath* root, FilePath* virtual_path); |
| 145 SandboxedFileSystemContext* file_system_context_; |
| 146 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilProxy); |
| 209 }; | 147 }; |
| 210 | 148 |
| 211 } // namespace base | 149 } // namespace fileapi |
| 212 | 150 |
| 213 #endif // BASE_FILE_UTIL_PROXY_H_ | 151 #endif // WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_PROXY_H_ |
| OLD | NEW |