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
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/memory/ref_counted.h"
11 #include "base/platform_file.h"
12 #include "webkit/fileapi/file_system_url.h"
13 #include "webkit/fileapi/fileapi_export.h"
19 namespace webkit_blob
{
20 class ShareableFileReference
;
25 using base::PlatformFile
;
26 using base::PlatformFileError
;
27 class FileSystemOperationContext
;
29 // A file utility interface that provides basic file utility methods for
32 // Layering structure of the FileSystemFileUtil was split out.
33 // See http://crbug.com/128136 if you need it.
34 class FILEAPI_EXPORT FileSystemFileUtil
{
36 // It will be implemented by each subclass such as FileSystemFileEnumerator.
37 class AbstractFileEnumerator
{
39 virtual ~AbstractFileEnumerator() {}
41 // Returns an empty string if there are no more results.
42 virtual FilePath
Next() = 0;
44 virtual int64
Size() = 0;
45 virtual base::Time
LastModifiedTime() = 0;
46 virtual bool IsDirectory() = 0;
49 // A policy flag for CreateSnapshotFile.
50 enum SnapshotFilePolicy
{
53 // The implementation just uses the local file as the snapshot file.
54 // The FileAPI backend does nothing on the returned file.
57 // The implementation returns a temporary file as the snapshot file.
58 // The FileAPI backend takes care of the lifetime of the returned file
59 // and will delete when the last reference of the file is dropped.
60 kSnapshotFileTemporary
,
63 class EmptyFileEnumerator
: public AbstractFileEnumerator
{
64 virtual FilePath
Next() OVERRIDE
;
65 virtual int64
Size() OVERRIDE
;
66 virtual base::Time
LastModifiedTime() OVERRIDE
;
67 virtual bool IsDirectory() OVERRIDE
;
70 virtual ~FileSystemFileUtil() {}
72 // Creates or opens a file with the given flags.
73 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
74 // a new file at the given |url| and calls back with
75 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |url| already exists.
76 virtual PlatformFileError
CreateOrOpen(
77 FileSystemOperationContext
* context
,
78 const FileSystemURL
& url
,
80 PlatformFile
* file_handle
,
83 // Closes the given file handle.
84 virtual PlatformFileError
Close(
85 FileSystemOperationContext
* context
,
86 PlatformFile file
) = 0;
88 // Ensures that the given |url| exist. This creates a empty new file
89 // at |url| if the |url| does not exist.
90 // If a new file han not existed and is created at the |url|,
91 // |created| is set true and |error code|
92 // is set PLATFORM_FILE_OK.
93 // If the file already exists, |created| is set false and |error code|
94 // is set PLATFORM_FILE_OK.
95 // If the file hasn't existed but it couldn't be created for some other
96 // reasons, |created| is set false and |error code| indicates the error.
97 virtual PlatformFileError
EnsureFileExists(
98 FileSystemOperationContext
* context
,
99 const FileSystemURL
& url
, bool* created
) = 0;
101 // Creates directory at given url. It's an error to create
102 // if |exclusive| is true and dir already exists.
103 virtual PlatformFileError
CreateDirectory(
104 FileSystemOperationContext
* context
,
105 const FileSystemURL
& url
,
109 // Retrieves the information about a file.
110 virtual PlatformFileError
GetFileInfo(
111 FileSystemOperationContext
* context
,
112 const FileSystemURL
& url
,
113 base::PlatformFileInfo
* file_info
,
114 FilePath
* platform_path
) = 0;
116 // Returns a pointer to a new instance of AbstractFileEnumerator which is
117 // implemented for each FileSystemFileUtil subclass. The instance needs to be
118 // freed by the caller, and its lifetime should not extend past when the
119 // current call returns to the main FILE message loop.
121 // The supplied context must remain valid at least lifetime of the enumerator
123 virtual AbstractFileEnumerator
* CreateFileEnumerator(
124 FileSystemOperationContext
* context
,
125 const FileSystemURL
& root_url
,
128 // Maps |file_system_url| given |context| into |local_file_path|
129 // which represents physical file location on the host OS.
130 // This may not always make sense for all subclasses.
131 virtual PlatformFileError
GetLocalFilePath(
132 FileSystemOperationContext
* context
,
133 const FileSystemURL
& file_system_url
,
134 FilePath
* local_file_path
) = 0;
136 // Updates the file metadata information. Unlike posix's touch, it does
137 // not create a file even if |url| does not exist, but instead fails
138 // with PLATFORM_FILE_ERROR_NOT_FOUND.
139 virtual PlatformFileError
Touch(
140 FileSystemOperationContext
* context
,
141 const FileSystemURL
& url
,
142 const base::Time
& last_access_time
,
143 const base::Time
& last_modified_time
) = 0;
145 // Truncates a file to the given length. If |length| is greater than the
146 // current length of the file, the file will be extended with zeroes.
147 virtual PlatformFileError
Truncate(
148 FileSystemOperationContext
* context
,
149 const FileSystemURL
& url
,
152 // Returns true if a given |url| is an empty directory.
153 virtual bool IsDirectoryEmpty(
154 FileSystemOperationContext
* context
,
155 const FileSystemURL
& url
) = 0;
157 // Copies or moves a single file from |src_url| to |dest_url|.
158 virtual PlatformFileError
CopyOrMoveFile(
159 FileSystemOperationContext
* context
,
160 const FileSystemURL
& src_url
,
161 const FileSystemURL
& dest_url
,
164 // Copies in a single file from a different filesystem.
165 virtual PlatformFileError
CopyInForeignFile(
166 FileSystemOperationContext
* context
,
167 const FilePath
& src_file_path
,
168 const FileSystemURL
& dest_url
) = 0;
170 // Deletes a single file.
171 // It assumes the given url points a file.
172 virtual PlatformFileError
DeleteFile(
173 FileSystemOperationContext
* context
,
174 const FileSystemURL
& url
) = 0;
176 // Deletes a single empty directory.
177 // It assumes the given url points an empty directory.
178 virtual PlatformFileError
DeleteSingleDirectory(
179 FileSystemOperationContext
* context
,
180 const FileSystemURL
& url
) = 0;
182 // Creates a local snapshot file for a given |url| and returns the
183 // metadata and platform path of the snapshot file via |callback|.
184 // In regular filesystem cases the implementation may simply return
185 // the metadata of the file itself (as well as GetMetadata does),
186 // while in non-regular filesystem case the backend may create a
187 // temporary snapshot file which holds the file data and return
188 // the metadata of the temporary file.
190 // |file_info| is the metadata of the snapshot file created.
191 // |platform_path| is the path to the snapshot file created.
192 // |policy| should indicate the policy how the fileapi backend
193 // should handle the returned file.
194 virtual base::PlatformFileError
CreateSnapshotFile(
195 FileSystemOperationContext
* context
,
196 const FileSystemURL
& url
,
197 base::PlatformFileInfo
* file_info
,
198 FilePath
* platform_path
,
199 SnapshotFilePolicy
* policy
) = 0;
202 FileSystemFileUtil() {}
205 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil
);
208 } // namespace fileapi
210 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_