| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ | 5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ |
| 6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ | 6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ |
| 7 | 7 |
| 8 #include "base/file_util_proxy.h" | |
| 9 #include "base/platform_file.h" | |
| 10 #include "base/process.h" | 8 #include "base/process.h" |
| 11 | 9 |
| 12 namespace base { | 10 namespace base { |
| 13 class Time; | 11 class Time; |
| 14 } // namespace base | 12 } // namespace base |
| 15 | 13 |
| 16 namespace net { | 14 namespace net { |
| 17 class URLRequest; | 15 class URLRequest; |
| 18 class URLRequestContext; | 16 class URLRequestContext; |
| 19 } // namespace net | 17 } // namespace net |
| 20 | 18 |
| 21 class GURL; | 19 class GURL; |
| 22 | 20 |
| 23 namespace fileapi { | 21 namespace fileapi { |
| 24 | 22 |
| 23 class FileSystemCallbackDispatcher; |
| 25 class FileSystemOperation; | 24 class FileSystemOperation; |
| 26 | 25 |
| 27 // The interface class for FileSystemOperation implementations. | 26 // The interface class for FileSystemOperation implementations. |
| 28 // | 27 // |
| 29 // This interface defines file system operations required to implement | 28 // This interface defines file system operations required to implement |
| 30 // "File API: Directories and System" | 29 // "File API: Directories and System" |
| 31 // http://www.w3.org/TR/file-system-api/ | 30 // http://www.w3.org/TR/file-system-api/ |
| 32 // | 31 // |
| 33 // DESIGN NOTES | 32 // DESIGN NOTES |
| 34 // | 33 // |
| 35 // This class is designed to | 34 // This class is designed to |
| 36 // | 35 // |
| 37 // 1) Serve one-time file system operation per instance. Only one | 36 // 1) Serve one-time file system operation per instance. Only one |
| 38 // method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists, | 37 // method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists, |
| 39 // GetMetadata, ReadDirectory and Remove) may be called during the | 38 // GetMetadata, ReadDirectory and Remove) may be called during the |
| 40 // lifetime of this object and it should be called no more than once. | 39 // lifetime of this object and it should be called no more than once. |
| 41 // | 40 // |
| 42 // 2) Be self-destructed, or get deleted via base::Owned() after the | 41 // 2) Be self-destructed, or get deleted via base::Owned() after the |
| 43 // operation finishes and completion callback is called. | 42 // operation finishes and completion callback is called. |
| 44 // | 43 // |
| 45 // 3) Deliver the results of operations to the client via the callback function | 44 // 3) Deliver the results of operations to the client via |
| 46 // passed as the last parameter of the method. | 45 // FileSystemCallbackDispatcher. |
| 46 // TODO(kinuko): Change the functions to take callbacks instead. |
| 47 // | 47 // |
| 48 class FileSystemOperationInterface { | 48 class FileSystemOperationInterface { |
| 49 public: | 49 public: |
| 50 virtual ~FileSystemOperationInterface() {} | 50 virtual ~FileSystemOperationInterface() {} |
| 51 | 51 |
| 52 // Used for CreateFile(), etc. |result| is the return code of the operation. | |
| 53 typedef base::Callback<void(base::PlatformFileError result)> StatusCallback; | |
| 54 | |
| 55 // Used for GetMetadata(). |result| is the return code of the operation, | |
| 56 // |file_info| is the obtained file info, and |platform_path| is the path | |
| 57 // of the file. | |
| 58 typedef base::Callback<void( | |
| 59 base::PlatformFileError result, | |
| 60 const base::PlatformFileInfo& file_info, | |
| 61 const FilePath& platform_path)> GetMetadataCallback; | |
| 62 | |
| 63 // Used for OpenFile(). |result| is the return code of the operation. | |
| 64 typedef base::Callback<void( | |
| 65 base::PlatformFileError result, | |
| 66 base::PlatformFile file, | |
| 67 base::ProcessHandle peer_handle)> OpenFileCallback; | |
| 68 | |
| 69 // Used for ReadDirectory(). |result| is the return code of the operation, | |
| 70 // |file_list| is the list of files read, and |has_more| is true if some files | |
| 71 // are yet to be read. | |
| 72 typedef base::Callback<void( | |
| 73 base::PlatformFileError result, | |
| 74 const std::vector<base::FileUtilProxy::Entry>& file_list, | |
| 75 bool has_more)> ReadDirectoryCallback; | |
| 76 | |
| 77 // Used for Write(). | |
| 78 typedef base::Callback<void(base::PlatformFileError result, | |
| 79 int64 bytes, | |
| 80 bool complete)> WriteCallback; | |
| 81 | |
| 82 // Creates a file at |path|. If |exclusive| is true, an error is raised | 52 // Creates a file at |path|. If |exclusive| is true, an error is raised |
| 83 // in case a file is already present at the URL. | 53 // in case a file is already present at the URL. |
| 84 virtual void CreateFile(const GURL& path, | 54 virtual void CreateFile(const GURL& path, |
| 85 bool exclusive, | 55 bool exclusive) = 0; |
| 86 const StatusCallback& callback) = 0; | |
| 87 | 56 |
| 88 // Creates a directory at |path|. If |exclusive| is true, an error is | 57 // Creates a directory at |path|. If |exclusive| is true, an error is |
| 89 // raised in case a directory is already present at the URL. If | 58 // raised in case a directory is already present at the URL. If |
| 90 // |recursive| is true, create parent directories as needed just like | 59 // |recursive| is true, create parent directories as needed just like |
| 91 // mkdir -p does. | 60 // mkdir -p does. |
| 92 virtual void CreateDirectory(const GURL& path, | 61 virtual void CreateDirectory(const GURL& path, |
| 93 bool exclusive, | 62 bool exclusive, |
| 94 bool recursive, | 63 bool recursive) = 0; |
| 95 const StatusCallback& callback) = 0; | |
| 96 | 64 |
| 97 // Copes a file or directory from |src_path| to |dest_path|. If | 65 // Copes a file or directory from |src_path| to |dest_path|. If |
| 98 // |src_path| is a directory, the contents of |src_path| are copied to | 66 // |src_path| is a directory, the contents of |src_path| are copied to |
| 99 // |dest_path| recursively. A new file or directory is created at | 67 // |dest_path| recursively. A new file or directory is created at |
| 100 // |dest_path| as needed. | 68 // |dest_path| as needed. |
| 101 virtual void Copy(const GURL& src_path, | 69 virtual void Copy(const GURL& src_path, |
| 102 const GURL& dest_path, | 70 const GURL& dest_path) = 0; |
| 103 const StatusCallback& callback) = 0; | |
| 104 | 71 |
| 105 // Moves a file or directory from |src_path| to |dest_path|. A new file | 72 // Moves a file or directory from |src_path| to |dest_path|. A new file |
| 106 // or directory is created at |dest_path| as needed. | 73 // or directory is created at |dest_path| as needed. |
| 107 virtual void Move(const GURL& src_path, | 74 virtual void Move(const GURL& src_path, |
| 108 const GURL& dest_path, | 75 const GURL& dest_path) = 0; |
| 109 const StatusCallback& callback) = 0; | |
| 110 | 76 |
| 111 // Checks if a directory is present at |path|. | 77 // Checks if a directory is present at |path|. |
| 112 virtual void DirectoryExists(const GURL& path, | 78 virtual void DirectoryExists(const GURL& path) = 0; |
| 113 const StatusCallback& callback) = 0; | |
| 114 | 79 |
| 115 // Checks if a file is present at |path|. | 80 // Checks if a file is present at |path|. |
| 116 virtual void FileExists(const GURL& path, | 81 virtual void FileExists(const GURL& path) = 0; |
| 117 const StatusCallback& callback) = 0; | |
| 118 | 82 |
| 119 // Gets the metadata of a file or directory at |path|. | 83 // Gets the metadata of a file or directory at |path|. |
| 120 virtual void GetMetadata(const GURL& path, | 84 virtual void GetMetadata(const GURL& path) = 0; |
| 121 const GetMetadataCallback& callback) = 0; | |
| 122 | 85 |
| 123 // Reads contents of a directory at |path|. | 86 // Reads contents of a directory at |path|. |
| 124 virtual void ReadDirectory(const GURL& path, | 87 virtual void ReadDirectory(const GURL& path) = 0; |
| 125 const ReadDirectoryCallback& callback) = 0; | |
| 126 | 88 |
| 127 // Removes a file or directory at |path|. If |recursive| is true, remove | 89 // Removes a file or directory at |path|. If |recursive| is true, remove |
| 128 // all files and directories under the directory at |path| recursively. | 90 // all files and directories under the directory at |path| recursively. |
| 129 virtual void Remove(const GURL& path, bool recursive, | 91 virtual void Remove(const GURL& path, bool recursive) = 0; |
| 130 const StatusCallback& callback) = 0; | |
| 131 | 92 |
| 132 // Writes contents of |blob_url| to |path| at |offset|. | 93 // Writes contents of |blob_url| to |path| at |offset|. |
| 133 // |url_request_context| is used to read contents in |blob_url|. | 94 // |url_request_context| is used to read contents in |blob_url|. |
| 134 virtual void Write(const net::URLRequestContext* url_request_context, | 95 virtual void Write(const net::URLRequestContext* url_request_context, |
| 135 const GURL& path, | 96 const GURL& path, |
| 136 const GURL& blob_url, | 97 const GURL& blob_url, |
| 137 int64 offset, | 98 int64 offset) = 0; |
| 138 const WriteCallback& callback) = 0; | |
| 139 | 99 |
| 140 // Truncates a file at |path| to |length|. If |length| is larger than | 100 // Truncates a file at |path| to |length|. If |length| is larger than |
| 141 // the original file size, the file will be extended, and the extended | 101 // the original file size, the file will be extended, and the extended |
| 142 // part is filled with null bytes. | 102 // part is filled with null bytes. |
| 143 virtual void Truncate(const GURL& path, int64 length, | 103 virtual void Truncate(const GURL& path, int64 length) = 0; |
| 144 const StatusCallback& callback) = 0; | |
| 145 | 104 |
| 146 // Tries to cancel the current operation [we support cancelling write or | 105 // Tries to cancel the current operation [we support cancelling write or |
| 147 // truncate only]. Reports failure for the current operation, then reports | 106 // truncate only]. Reports failure for the current operation, then reports |
| 148 // success for the cancel operation itself via the |cancel_dispatcher|. | 107 // success for the cancel operation itself via the |cancel_dispatcher|. |
| 149 // | 108 // |
| 150 // E.g. a typical cancel implementation would look like: | 109 // E.g. a typical cancel implementation would look like: |
| 151 // | 110 // |
| 152 // virtual void SomeOperationImpl::Cancel( | 111 // virtual void SomeOperationImpl::Cancel( |
| 153 // const StatusCallback& cancel_callback) { | 112 // scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher) { |
| 154 // // Abort the current inflight operation first. | 113 // // Abort the current inflight operation first. |
| 155 // ... | 114 // ... |
| 156 // | 115 // |
| 157 // // Dispatch ABORT error for the current operation by invoking | 116 // // Dispatch ABORT error for the current operation by calling |
| 158 // // the callback function for the ongoing operation, | 117 // // DidFail() callback of the dispatcher attached to this operation. |
| 159 // operation_callback.Run(base::PLATFORM_FILE_ERROR_ABORT, ...); | 118 // // (dispatcher_ in this example) |
| 119 // dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_ABORT); |
| 160 // | 120 // |
| 161 // // Dispatch 'success' for the cancel (or dispatch appropriate | 121 // // Dispatch 'success' for the cancel (or dispatch appropriate |
| 162 // // error code with DidFail() if the cancel has somehow failed). | 122 // // error code with DidFail() if the cancel has somehow failed). |
| 163 // cancel_callback.Run(base::PLATFORM_FILE_OK); | 123 // cancel_dispatcher->DidSucceed(); |
| 164 // } | 124 // } |
| 165 // | 125 // |
| 166 // Note that, for reporting failure, the callback function passed to a | 126 virtual void Cancel( |
| 167 // cancellable operations are kept around with the operation instance | 127 scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher) = 0; |
| 168 // (as |operation_callback_| in the code example). | |
| 169 virtual void Cancel(const StatusCallback& cancel_callback) = 0; | |
| 170 | 128 |
| 171 // Modifies timestamps of a file or directory at |path| with | 129 // Modifies timestamps of a file or directory at |path| with |
| 172 // |last_access_time| and |last_modified_time|. The function DOES NOT | 130 // |last_access_time| and |last_modified_time|. The function DOES NOT |
| 173 // create a file unlike 'touch' command on Linux. | 131 // create a file unlike 'touch' command on Linux. |
| 174 // | 132 // |
| 175 // This function is used only by Pepper as of writing. | 133 // This function is used only by Pepper as of writing. |
| 176 virtual void TouchFile(const GURL& path, | 134 virtual void TouchFile(const GURL& path, |
| 177 const base::Time& last_access_time, | 135 const base::Time& last_access_time, |
| 178 const base::Time& last_modified_time, | 136 const base::Time& last_modified_time) = 0; |
| 179 const StatusCallback& callback) = 0; | |
| 180 | 137 |
| 181 // Opens a file at |path| with |file_flags|, where flags are OR'ed | 138 // Opens a file at |path| with |file_flags|, where flags are OR'ed |
| 182 // values of base::PlatformFileFlags. | 139 // values of base::PlatformFileFlags. |
| 183 // | 140 // |
| 184 // |peer_handle| is the process handle of a pepper plugin process, which | 141 // |peer_handle| is the process handle of a pepper plugin process, which |
| 185 // is necessary for underlying IPC calls with Pepper plugins. | 142 // is necessary for underlying IPC calls with Pepper plugins. |
| 186 // | 143 // |
| 187 // This function is used only by Pepper as of writing. | 144 // This function is used only by Pepper as of writing. |
| 188 virtual void OpenFile(const GURL& path, | 145 virtual void OpenFile( |
| 189 int file_flags, | 146 const GURL& path, |
| 190 base::ProcessHandle peer_handle, | 147 int file_flags, |
| 191 const OpenFileCallback& callback) = 0; | 148 base::ProcessHandle peer_handle) = 0; |
| 192 | 149 |
| 193 // For downcasting to FileSystemOperation. | 150 // For downcasting to FileSystemOperation. |
| 194 // TODO(kinuko): this hack should go away once appropriate upload-stream | 151 // TODO(kinuko): this hack should go away once appropriate upload-stream |
| 195 // handling based on element types is supported. | 152 // handling based on element types is supported. |
| 196 virtual FileSystemOperation* AsFileSystemOperation() = 0; | 153 virtual FileSystemOperation* AsFileSystemOperation() = 0; |
| 197 }; | 154 }; |
| 198 | 155 |
| 199 } // namespace fileapi | 156 } // namespace fileapi |
| 200 | 157 |
| 201 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ | 158 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ |
| OLD | NEW |