OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ | |
6 #define WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util_proxy.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/platform_file.h" | |
15 #include "base/timer.h" | |
16 #include "webkit/blob/shareable_file_reference.h" | |
17 #include "webkit/fileapi/file_system_file_util.h" | |
18 #include "webkit/fileapi/file_system_types.h" | |
19 #include "webkit/fileapi/file_system_url.h" | |
20 #include "webkit/fileapi/sandbox_directory_database.h" | |
21 #include "webkit/fileapi/sandbox_origin_database.h" | |
22 #include "webkit/storage/webkit_storage_export.h" | |
23 | |
24 namespace base { | |
25 class Time; | |
26 } | |
27 | |
28 class GURL; | |
29 | |
30 namespace fileapi { | |
31 | |
32 class FileSystemOperationContext; | |
33 | |
34 // The overall implementation philosophy of this class is that partial failures | |
35 // should leave us with an intact database; we'd prefer to leak the occasional | |
36 // backing file than have a database entry whose backing file is missing. When | |
37 // doing FSCK operations, if you find a loose backing file with no reference, | |
38 // you may safely delete it. | |
39 // | |
40 // This class must be deleted on the FILE thread, because that's where | |
41 // DropDatabases needs to be called. | |
42 class WEBKIT_STORAGE_EXPORT_PRIVATE ObfuscatedFileUtil | |
43 : public FileSystemFileUtil { | |
44 public: | |
45 // Origin enumerator interface. | |
46 // An instance of this interface is assumed to be called on the file thread. | |
47 class AbstractOriginEnumerator { | |
48 public: | |
49 virtual ~AbstractOriginEnumerator() {} | |
50 | |
51 // Returns the next origin. Returns empty if there are no more origins. | |
52 virtual GURL Next() = 0; | |
53 | |
54 // Returns the current origin's information. | |
55 virtual bool HasFileSystemType(FileSystemType type) const = 0; | |
56 }; | |
57 | |
58 explicit ObfuscatedFileUtil(const base::FilePath& file_system_directory); | |
59 virtual ~ObfuscatedFileUtil(); | |
60 | |
61 // FileSystemFileUtil overrides. | |
62 virtual base::PlatformFileError CreateOrOpen( | |
63 FileSystemOperationContext* context, | |
64 const FileSystemURL& url, | |
65 int file_flags, | |
66 base::PlatformFile* file_handle, | |
67 bool* created) OVERRIDE; | |
68 virtual base::PlatformFileError Close( | |
69 FileSystemOperationContext* context, | |
70 base::PlatformFile file) OVERRIDE; | |
71 virtual base::PlatformFileError EnsureFileExists( | |
72 FileSystemOperationContext* context, | |
73 const FileSystemURL& url, bool* created) OVERRIDE; | |
74 virtual base::PlatformFileError CreateDirectory( | |
75 FileSystemOperationContext* context, | |
76 const FileSystemURL& url, | |
77 bool exclusive, | |
78 bool recursive) OVERRIDE; | |
79 virtual base::PlatformFileError GetFileInfo( | |
80 FileSystemOperationContext* context, | |
81 const FileSystemURL& url, | |
82 base::PlatformFileInfo* file_info, | |
83 base::FilePath* platform_file) OVERRIDE; | |
84 virtual scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator( | |
85 FileSystemOperationContext* context, | |
86 const FileSystemURL& root_url) OVERRIDE; | |
87 virtual base::PlatformFileError GetLocalFilePath( | |
88 FileSystemOperationContext* context, | |
89 const FileSystemURL& file_system_url, | |
90 base::FilePath* local_path) OVERRIDE; | |
91 virtual base::PlatformFileError Touch( | |
92 FileSystemOperationContext* context, | |
93 const FileSystemURL& url, | |
94 const base::Time& last_access_time, | |
95 const base::Time& last_modified_time) OVERRIDE; | |
96 virtual base::PlatformFileError Truncate( | |
97 FileSystemOperationContext* context, | |
98 const FileSystemURL& url, | |
99 int64 length) OVERRIDE; | |
100 virtual base::PlatformFileError CopyOrMoveFile( | |
101 FileSystemOperationContext* context, | |
102 const FileSystemURL& src_url, | |
103 const FileSystemURL& dest_url, | |
104 bool copy) OVERRIDE; | |
105 virtual base::PlatformFileError CopyInForeignFile( | |
106 FileSystemOperationContext* context, | |
107 const base::FilePath& src_file_path, | |
108 const FileSystemURL& dest_url) OVERRIDE; | |
109 virtual base::PlatformFileError DeleteFile( | |
110 FileSystemOperationContext* context, | |
111 const FileSystemURL& url) OVERRIDE; | |
112 virtual base::PlatformFileError DeleteDirectory( | |
113 FileSystemOperationContext* context, | |
114 const FileSystemURL& url) OVERRIDE; | |
115 virtual webkit_blob::ScopedFile CreateSnapshotFile( | |
116 FileSystemOperationContext* context, | |
117 const FileSystemURL& url, | |
118 base::PlatformFileError* error, | |
119 base::PlatformFileInfo* file_info, | |
120 base::FilePath* platform_path) OVERRIDE; | |
121 | |
122 // Same as the other CreateFileEnumerator, but with recursive support. | |
123 scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator( | |
124 FileSystemOperationContext* context, | |
125 const FileSystemURL& root_url, | |
126 bool recursive); | |
127 | |
128 // Returns true if the directory |url| is empty. | |
129 bool IsDirectoryEmpty( | |
130 FileSystemOperationContext* context, | |
131 const FileSystemURL& url); | |
132 | |
133 // Gets the topmost directory specific to this origin and type. This will | |
134 // contain both the directory database's files and all the backing file | |
135 // subdirectories. | |
136 // Returns an empty path if the directory is undefined (e.g. because |type| | |
137 // is invalid). If the directory is defined, it will be returned, even if | |
138 // there is a file system error (e.g. the directory doesn't exist on disk and | |
139 // |create| is false). Callers should always check |error_code| to make sure | |
140 // the returned path is usable. | |
141 base::FilePath GetDirectoryForOriginAndType( | |
142 const GURL& origin, | |
143 FileSystemType type, | |
144 bool create, | |
145 base::PlatformFileError* error_code); | |
146 | |
147 // Deletes the topmost directory specific to this origin and type. This will | |
148 // delete its directory database. | |
149 bool DeleteDirectoryForOriginAndType(const GURL& origin, FileSystemType type); | |
150 | |
151 // TODO(ericu): This doesn't really feel like it belongs in this class. | |
152 // The previous version lives in FileSystemPathManager, but perhaps | |
153 // SandboxMountPointProvider would be better? | |
154 static base::FilePath::StringType GetDirectoryNameForType( | |
155 FileSystemType type); | |
156 | |
157 // This method and all methods of its returned class must be called only on | |
158 // the FILE thread. The caller is responsible for deleting the returned | |
159 // object. | |
160 AbstractOriginEnumerator* CreateOriginEnumerator(); | |
161 | |
162 // Deletes a directory database from the database list in the ObfuscatedFSFU | |
163 // and destroys the database on the disk. | |
164 bool DestroyDirectoryDatabase(const GURL& origin, FileSystemType type); | |
165 | |
166 // Computes a cost for storing a given file in the obfuscated FSFU. | |
167 // As the cost of a file is independent of the cost of its parent directories, | |
168 // this ignores all but the BaseName of the supplied path. In order to | |
169 // compute the cost of adding a multi-segment directory recursively, call this | |
170 // on each path segment and add the results. | |
171 static int64 ComputeFilePathCost(const base::FilePath& path); | |
172 | |
173 private: | |
174 typedef SandboxDirectoryDatabase::FileId FileId; | |
175 typedef SandboxDirectoryDatabase::FileInfo FileInfo; | |
176 | |
177 friend class ObfuscatedFileEnumerator; | |
178 | |
179 base::PlatformFileError GetFileInfoInternal( | |
180 SandboxDirectoryDatabase* db, | |
181 FileSystemOperationContext* context, | |
182 const GURL& origin, | |
183 FileSystemType type, | |
184 FileId file_id, | |
185 FileInfo* local_info, | |
186 base::PlatformFileInfo* file_info, | |
187 base::FilePath* platform_file_path); | |
188 | |
189 // Creates a new file, both the underlying backing file and the entry in the | |
190 // database. |dest_file_info| is an in-out parameter. Supply the name and | |
191 // parent_id; data_path is ignored. On success, data_path will | |
192 // always be set to the relative path [from the root of the type-specific | |
193 // filesystem directory] of a NEW backing file, and handle, if supplied, will | |
194 // hold open PlatformFile for the backing file, which the caller is | |
195 // responsible for closing. If you supply a path in |source_path|, it will be | |
196 // used as a source from which to COPY data. | |
197 // Caveat: do not supply handle if you're also supplying a data path. It was | |
198 // easier not to support this, and no code has needed it so far, so it will | |
199 // DCHECK and handle will hold base::kInvalidPlatformFileValue. | |
200 base::PlatformFileError CreateFile( | |
201 FileSystemOperationContext* context, | |
202 const base::FilePath& source_file_path, | |
203 const GURL& dest_origin, | |
204 FileSystemType dest_type, | |
205 FileInfo* dest_file_info, | |
206 int file_flags, | |
207 base::PlatformFile* handle); | |
208 | |
209 // This converts from a relative path [as is stored in the FileInfo.data_path | |
210 // field] to an absolute platform path that can be given to the native | |
211 // filesystem. | |
212 base::FilePath DataPathToLocalPath( | |
213 const GURL& origin, | |
214 FileSystemType type, | |
215 const base::FilePath& data_file_path); | |
216 | |
217 // This returns NULL if |create| flag is false and a filesystem does not | |
218 // exist for the given |origin_url| and |type|. | |
219 // For read operations |create| should be false. | |
220 SandboxDirectoryDatabase* GetDirectoryDatabase( | |
221 const GURL& origin_url, FileSystemType type, bool create); | |
222 | |
223 // Gets the topmost directory specific to this origin. This will | |
224 // contain both the filesystem type subdirectories. | |
225 base::FilePath GetDirectoryForOrigin(const GURL& origin, | |
226 bool create, | |
227 base::PlatformFileError* error_code); | |
228 | |
229 void InvalidateUsageCache(FileSystemOperationContext* context, | |
230 const GURL& origin, | |
231 FileSystemType type); | |
232 | |
233 void MarkUsed(); | |
234 void DropDatabases(); | |
235 bool InitOriginDatabase(bool create); | |
236 | |
237 base::PlatformFileError GenerateNewLocalPath( | |
238 SandboxDirectoryDatabase* db, | |
239 FileSystemOperationContext* context, | |
240 const GURL& origin, | |
241 FileSystemType type, | |
242 base::FilePath* local_path); | |
243 | |
244 base::PlatformFileError CreateOrOpenInternal( | |
245 FileSystemOperationContext* context, | |
246 const FileSystemURL& url, | |
247 int file_flags, | |
248 base::PlatformFile* file_handle, | |
249 bool* created); | |
250 | |
251 typedef std::map<std::string, SandboxDirectoryDatabase*> DirectoryMap; | |
252 DirectoryMap directories_; | |
253 scoped_ptr<SandboxOriginDatabase> origin_database_; | |
254 base::FilePath file_system_directory_; | |
255 base::OneShotTimer<ObfuscatedFileUtil> timer_; | |
256 | |
257 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtil); | |
258 }; | |
259 | |
260 } // namespace fileapi | |
261 | |
262 #endif // WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ | |
OLD | NEW |