chrome: bluetooth: hook up the AdapterAdded signal
[chromium-blink-merge.git] / webkit / fileapi / file_system_file_util.h
blobf96e532707726722a842e828122660113a22ed25
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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
8 #include "base/file_path.h"
9 #include "base/file_util_proxy.h"
10 #include "base/platform_file.h"
12 namespace base {
13 class Time;
16 namespace fileapi {
18 using base::PlatformFile;
19 using base::PlatformFileError;
20 class FileSystemOperationContext;
22 // A large part of this implementation is taken from base::FileUtilProxy.
24 // The default implementations of the virtual methods below (*1) assume the
25 // given paths are native ones for the host platform. The subclasses that
26 // perform local path translation/obfuscation must override them.
27 // (*1) All virtual methods which receive FilePath as their arguments:
28 // CreateOrOpen, EnsureFileExists, GetLocalFilePath, GetFileInfo,
29 // ReadDirectory, CreateDirectory, CopyOrMoveFile, DeleteFile,
30 // DeleteSingleDirectory, Touch, Truncate, PathExists, DirectoryExists,
31 // IsDirectoryEmpty and CreateFileEnumerator.
33 // The methods below (*2) assume the given paths may not be native ones for the
34 // host platform. The subclasses should not override them. They provide basic
35 // meta logic by using other virtual methods.
36 // (*2) All non-virtual methods: Copy, Move, Delete, DeleteDirectoryRecursive,
37 // PerformCommonCheckAndPreparationForMoveAndCopy and CopyOrMoveDirectory.
38 class FileSystemFileUtil {
39 public:
40 // It will be implemented by each subclass such as FileSystemFileEnumerator.
41 class AbstractFileEnumerator {
42 public:
43 virtual ~AbstractFileEnumerator() {}
45 // Returns an empty string if there are no more results.
46 virtual FilePath Next() = 0;
48 virtual int64 Size() = 0;
49 virtual bool IsDirectory() = 0;
52 class EmptyFileEnumerator : public AbstractFileEnumerator {
53 virtual FilePath Next() OVERRIDE { return FilePath(); }
54 virtual int64 Size() OVERRIDE { return 0; }
55 virtual bool IsDirectory() OVERRIDE { return false; }
58 virtual ~FileSystemFileUtil();
60 // Copies or moves a single file.
61 // Copies a file or a directory from |src_file_path| to |dest_file_path|.
63 // Error cases:
64 // If destination's parent doesn't exist.
65 // If source dir exists but destination path is an existing file.
66 // If source file exists but destination path is an existing directory.
67 // If source is a parent of destination.
68 // If source doesn't exist.
70 // This method calls one of the following methods depending on whether the
71 // target is a directory or not.
72 // - (virtual) CopyOrMoveFile or
73 // - (non-virtual) CopyOrMoveDirectory.
74 PlatformFileError Copy(
75 FileSystemOperationContext* context,
76 const FilePath& src_file_path,
77 const FilePath& dest_file_path);
79 // Moves a file or a directory from src_file_path to dest_file_path.
81 // Error cases are similar to Copy method's error cases.
83 // This method calls one of the following methods depending on whether the
84 // target is a directory or not.
85 // - (virtual) CopyOrMoveFile or
86 // - (non-virtual) CopyOrMoveDirectory.
87 PlatformFileError Move(
88 FileSystemOperationContext* context,
89 const FilePath& src_file_path,
90 const FilePath& dest_file_path);
92 // Deletes a file or a directory.
93 // It is an error to delete a non-empty directory with recursive=false.
95 // This method calls one of the following methods depending on whether the
96 // target is a directory or not, and whether the |recursive| flag is given or
97 // not.
98 // - (virtual) DeleteFile,
99 // - (virtual) DeleteSingleDirectory or
100 // - (non-virtual) DeleteDirectoryRecursive which calls two methods above.
101 PlatformFileError Delete(
102 FileSystemOperationContext* context,
103 const FilePath& file_path,
104 bool recursive);
106 // Creates or opens a file with the given flags. It is invalid to pass NULL
107 // for the callback.
108 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
109 // a new file at the given |file_path| and calls back with
110 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
111 virtual PlatformFileError CreateOrOpen(
112 FileSystemOperationContext* context,
113 const FilePath& file_path,
114 int file_flags,
115 PlatformFile* file_handle,
116 bool* created);
118 // Close the given file handle.
119 virtual PlatformFileError Close(
120 FileSystemOperationContext* context,
121 PlatformFile);
123 // Ensures that the given |file_path| exist. This creates a empty new file
124 // at |file_path| if the |file_path| does not exist.
125 // If a new file han not existed and is created at the |file_path|,
126 // |created| of the callback argument is set true and |error code|
127 // is set PLATFORM_FILE_OK.
128 // If the file already exists, |created| is set false and |error code|
129 // is set PLATFORM_FILE_OK.
130 // If the file hasn't existed but it couldn't be created for some other
131 // reasons, |created| is set false and |error code| indicates the error.
132 virtual PlatformFileError EnsureFileExists(
133 FileSystemOperationContext* context,
134 const FilePath& file_path, bool* created);
136 // Creates directory at given path. It's an error to create
137 // if |exclusive| is true and dir already exists.
138 virtual PlatformFileError CreateDirectory(
139 FileSystemOperationContext* context,
140 const FilePath& file_path,
141 bool exclusive,
142 bool recursive);
144 // Retrieves the information about a file. It is invalid to pass NULL for the
145 // callback.
146 virtual PlatformFileError GetFileInfo(
147 FileSystemOperationContext* context,
148 const FilePath& file_,
149 base::PlatformFileInfo* file_info,
150 FilePath* platform_path);
152 // Reads the filenames in |file_path|.
153 virtual PlatformFileError ReadDirectory(
154 FileSystemOperationContext* context,
155 const FilePath& file_path,
156 std::vector<base::FileUtilProxy::Entry>* entries);
158 // Returns a pointer to a new instance of AbstractFileEnumerator which is
159 // implemented for each FileSystemFileUtil subclass. The instance needs to be
160 // freed by the caller, and its lifetime should not extend past when the
161 // current call returns to the main FILE message loop.
163 // The supplied context must remain valid at least lifetime of the enumerator
164 // instance.
165 virtual AbstractFileEnumerator* CreateFileEnumerator(
166 FileSystemOperationContext* context,
167 const FilePath& root_path);
169 // Maps |virtual_path| given |context| into |local_path| which represents
170 // physical file location on the host OS. This may not always make sense for
171 // all subclasses.
172 virtual PlatformFileError GetLocalFilePath(
173 FileSystemOperationContext* context,
174 const FilePath& virtual_path,
175 FilePath* local_path);
177 // Touches a file. The callback can be NULL.
178 // If the file doesn't exist, this fails with PLATFORM_FILE_ERROR_NOT_FOUND.
179 virtual PlatformFileError Touch(
180 FileSystemOperationContext* context,
181 const FilePath& file_path,
182 const base::Time& last_access_time,
183 const base::Time& last_modified_time);
185 // Truncates a file to the given length. If |length| is greater than the
186 // current length of the file, the file will be extended with zeroes.
187 // The callback can be NULL.
188 virtual PlatformFileError Truncate(
189 FileSystemOperationContext* context,
190 const FilePath& path,
191 int64 length);
193 virtual bool PathExists(
194 FileSystemOperationContext* context,
195 const FilePath& file_path);
197 virtual bool DirectoryExists(
198 FileSystemOperationContext* context,
199 const FilePath& file_path);
201 virtual bool IsDirectoryEmpty(
202 FileSystemOperationContext* context,
203 const FilePath& file_path);
205 virtual PlatformFileError CopyOrMoveFile(
206 FileSystemOperationContext* context,
207 const FilePath& src_file_path,
208 const FilePath& dest_file_path,
209 bool copy);
211 // Copies in a single file from a different filesystem. The src_file_path is
212 // a true local platform path, regardless of which subclass of
213 // FileSystemFileUtil is being invoked.
214 virtual PlatformFileError CopyInForeignFile(
215 FileSystemOperationContext* context,
216 const FilePath& src_file_path,
217 const FilePath& dest_file_path);
219 // Deletes a single file.
220 // It assumes the given path points a file.
222 // This method is called from DeleteDirectoryRecursive and Delete (both are
223 // non-virtual).
224 virtual PlatformFileError DeleteFile(
225 FileSystemOperationContext* context,
226 const FilePath& file_path);
228 // Deletes a single empty directory.
229 // It assumes the given path points an empty directory.
231 // This method is called from DeleteDirectoryRecursive and Delete (both are
232 // non-virtual).
233 virtual PlatformFileError DeleteSingleDirectory(
234 FileSystemOperationContext* context,
235 const FilePath& file_path);
237 protected:
238 FileSystemFileUtil();
239 explicit FileSystemFileUtil(FileSystemFileUtil* underlying_file_util);
241 // This also removes the destination directory if it's non-empty and all
242 // other checks are passed (so that the copy/move correctly overwrites the
243 // destination).
244 PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy(
245 FileSystemOperationContext* context,
246 const FilePath& src_file_path,
247 const FilePath& dest_file_path);
249 // Performs recursive copy or move by calling CopyOrMoveFile for individual
250 // files. Operations for recursive traversal are encapsulated in this method.
251 // It assumes src_file_path and dest_file_path have passed
252 // PerformCommonCheckAndPreparationForMoveAndCopy().
253 PlatformFileError CopyOrMoveDirectory(
254 FileSystemOperationContext* context,
255 const FilePath& src_file_path,
256 const FilePath& dest_file_path,
257 bool copy);
259 // Determines whether a simple same-filesystem move or copy can be done. If
260 // so, it delegates to CopyOrMoveFile. Otherwise it looks up the true
261 // platform path of the source file, delegates to CopyInForeignFile, and [for
262 // move] calls DeleteFile on the source file.
263 PlatformFileError CopyOrMoveFileHelper(
264 FileSystemOperationContext* context,
265 const FilePath& src_file_path,
266 const FilePath& dest_file_path,
267 bool copy);
269 // Deletes a directory and all entries under the directory.
271 // This method is called from Delete. It internally calls two following
272 // virtual methods,
273 // - (virtual) DeleteFile to delete files, and
274 // - (virtual) DeleteSingleDirectory to delete empty directories after all
275 // the files are deleted.
276 PlatformFileError DeleteDirectoryRecursive(
277 FileSystemOperationContext* context,
278 const FilePath& file_path);
280 FileSystemFileUtil* underlying_file_util() const {
281 return underlying_file_util_.get();
284 private:
285 scoped_ptr<FileSystemFileUtil> underlying_file_util_;
287 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil);
290 } // namespace fileapi
292 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_