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_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ 6 #define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ 7 8 #include "base/files/file_path.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/platform_file.h" 11 #include "webkit/browser/webkit_storage_browser_export.h" 12 #include "webkit/common/blob/scoped_file.h" 13 14 namespace base { 15 class Time; 16 } 17 18 namespace fileapi { 19 20 class FileSystemOperationContext; 21 class FileSystemURL; 22 23 // A file utility interface that provides basic file utility methods for 24 // FileSystem API. 25 // 26 // Layering structure of the FileSystemFileUtil was split out. 27 // See http://crbug.com/128136 if you need it. 28 class WEBKIT_STORAGE_BROWSER_EXPORT FileSystemFileUtil { 29 public: 30 // It will be implemented by each subclass such as FileSystemFileEnumerator. 31 class WEBKIT_STORAGE_BROWSER_EXPORT AbstractFileEnumerator { 32 public: 33 virtual ~AbstractFileEnumerator() {} 34 35 // Returns an empty string if there are no more results. 36 virtual base::FilePath Next() = 0; 37 38 // These methods return metadata for the file most recently returned by 39 // Next(). If Next() has never been called, or if Next() most recently 40 // returned an empty string, then return the default values of 0, 41 // "null time", and false, respectively. 42 virtual int64 Size() = 0; 43 virtual base::Time LastModifiedTime() = 0; 44 virtual bool IsDirectory() = 0; 45 }; 46 47 class WEBKIT_STORAGE_BROWSER_EXPORT EmptyFileEnumerator 48 : public AbstractFileEnumerator { 49 virtual base::FilePath Next() OVERRIDE; 50 virtual int64 Size() OVERRIDE; 51 virtual base::Time LastModifiedTime() OVERRIDE; 52 virtual bool IsDirectory() OVERRIDE; 53 }; 54 55 virtual ~FileSystemFileUtil() {} 56 57 // Creates or opens a file with the given flags. 58 // See header comments for AsyncFileUtil::CreateOrOpen() for more details. 59 // This is used only by Pepper/NaCL File API. 60 virtual base::PlatformFileError CreateOrOpen( 61 FileSystemOperationContext* context, 62 const FileSystemURL& url, 63 int file_flags, 64 base::PlatformFile* file_handle, 65 bool* created) = 0; 66 67 // Closes the given file handle. 68 // This is used only for Pepper/NaCL File API. 69 virtual base::PlatformFileError Close( 70 FileSystemOperationContext* context, 71 base::PlatformFile file) = 0; 72 73 // Ensures that the given |url| exist. This creates a empty new file 74 // at |url| if the |url| does not exist. 75 // See header comments for AsyncFileUtil::EnsureFileExists() for more details. 76 virtual base::PlatformFileError EnsureFileExists( 77 FileSystemOperationContext* context, 78 const FileSystemURL& url, bool* created) = 0; 79 80 // Creates directory at given url. 81 // See header comments for AsyncFileUtil::CreateDirectory() for more details. 82 virtual base::PlatformFileError CreateDirectory( 83 FileSystemOperationContext* context, 84 const FileSystemURL& url, 85 bool exclusive, 86 bool recursive) = 0; 87 88 // Retrieves the information about a file. 89 // See header comments for AsyncFileUtil::GetFileInfo() for more details. 90 virtual base::PlatformFileError GetFileInfo( 91 FileSystemOperationContext* context, 92 const FileSystemURL& url, 93 base::PlatformFileInfo* file_info, 94 base::FilePath* platform_path) = 0; 95 96 // Returns a pointer to a new instance of AbstractFileEnumerator which is 97 // implemented for each FileSystemFileUtil subclass. The instance needs to be 98 // freed by the caller, and its lifetime should not extend past when the 99 // current call returns to the main FILE message loop. 100 // 101 // The supplied context must remain valid at least lifetime of the enumerator 102 // instance. 103 virtual scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator( 104 FileSystemOperationContext* context, 105 const FileSystemURL& root_url) = 0; 106 107 // Maps |file_system_url| given |context| into |local_file_path| 108 // which represents physical file location on the host OS. 109 // This may not always make sense for all subclasses. 110 virtual base::PlatformFileError GetLocalFilePath( 111 FileSystemOperationContext* context, 112 const FileSystemURL& file_system_url, 113 base::FilePath* local_file_path) = 0; 114 115 // Updates the file metadata information. 116 // See header comments for AsyncFileUtil::Touch() for more details. 117 virtual base::PlatformFileError Touch( 118 FileSystemOperationContext* context, 119 const FileSystemURL& url, 120 const base::Time& last_access_time, 121 const base::Time& last_modified_time) = 0; 122 123 // Truncates a file to the given length. 124 // See header comments for AsyncFileUtil::Truncate() for more details. 125 virtual base::PlatformFileError Truncate( 126 FileSystemOperationContext* context, 127 const FileSystemURL& url, 128 int64 length) = 0; 129 130 // Copies or moves a single file from |src_url| to |dest_url|. 131 // The filesystem type of |src_url| and |dest_url| MUST be same. 132 // 133 // This returns: 134 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url| 135 // or the parent directory of |dest_url| does not exist. 136 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file. 137 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and 138 // is not a file. 139 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and 140 // its parent path is a file. 141 // 142 virtual base::PlatformFileError CopyOrMoveFile( 143 FileSystemOperationContext* context, 144 const FileSystemURL& src_url, 145 const FileSystemURL& dest_url, 146 bool copy) = 0; 147 148 // Copies in a single file from a different filesystem. 149 // See header comments for AsyncFileUtil::CopyInForeignFile() for 150 // more details. 151 virtual base::PlatformFileError CopyInForeignFile( 152 FileSystemOperationContext* context, 153 const base::FilePath& src_file_path, 154 const FileSystemURL& dest_url) = 0; 155 156 // Deletes a single file. 157 // See header comments for AsyncFileUtil::DeleteFile() for more details. 158 virtual base::PlatformFileError DeleteFile( 159 FileSystemOperationContext* context, 160 const FileSystemURL& url) = 0; 161 162 // Deletes a single empty directory. 163 // See header comments for AsyncFileUtil::DeleteDirectory() for more details. 164 virtual base::PlatformFileError DeleteDirectory( 165 FileSystemOperationContext* context, 166 const FileSystemURL& url) = 0; 167 168 // Creates a local snapshot file for a given |url| and returns the 169 // metadata and platform path of the snapshot file via |callback|. 170 // 171 // See header comments for AsyncFileUtil::CreateSnapshotFile() for 172 // more details. 173 virtual webkit_blob::ScopedFile CreateSnapshotFile( 174 FileSystemOperationContext* context, 175 const FileSystemURL& url, 176 base::PlatformFileError* error, 177 base::PlatformFileInfo* file_info, 178 base::FilePath* platform_path) = 0; 179 180 protected: 181 FileSystemFileUtil() {} 182 183 private: 184 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil); 185 }; 186 187 } // namespace fileapi 188 189 #endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ 190