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