Home | History | Annotate | Download | only in files
      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 BASE_FILES_FILE_UTIL_PROXY_H_
      6 #define BASE_FILES_FILE_UTIL_PROXY_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/callback_forward.h"
     10 #include "base/files/file_path.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/platform_file.h"
     13 
     14 namespace tracked_objects {
     15 class Location;
     16 };
     17 
     18 namespace base {
     19 
     20 class TaskRunner;
     21 class Time;
     22 
     23 // This class provides asynchronous access to common file routines.
     24 class BASE_EXPORT FileUtilProxy {
     25  public:
     26   // This callback is used by methods that report only an error code.  It is
     27   // valid to pass a null callback to any function that takes a StatusCallback,
     28   // in which case the operation will complete silently.
     29   typedef Callback<void(PlatformFileError)> StatusCallback;
     30 
     31   typedef Callback<void(PlatformFileError,
     32                         PassPlatformFile,
     33                         bool /* created */)> CreateOrOpenCallback;
     34   typedef Callback<void(PlatformFileError,
     35                         PassPlatformFile,
     36                         const FilePath&)> CreateTemporaryCallback;
     37   typedef Callback<void(PlatformFileError,
     38                         const PlatformFileInfo&)> GetFileInfoCallback;
     39   typedef Callback<void(PlatformFileError,
     40                         const char* /* data */,
     41                         int /* bytes read */)> ReadCallback;
     42   typedef Callback<void(PlatformFileError,
     43                         int /* bytes written */)> WriteCallback;
     44 
     45   typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask;
     46   typedef Callback<PlatformFileError(PlatformFile)> CloseTask;
     47   typedef Callback<PlatformFileError(void)> FileTask;
     48 
     49   // Creates or opens a file with the given flags. It is invalid to pass a null
     50   // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
     51   // create a new file at the given |file_path| and calls back with
     52   // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
     53   //
     54   // This returns false if task posting to |task_runner| has failed.
     55   static bool CreateOrOpen(TaskRunner* task_runner,
     56                            const FilePath& file_path,
     57                            int file_flags,
     58                            const CreateOrOpenCallback& callback);
     59 
     60   // Creates a temporary file for writing. The path and an open file handle are
     61   // returned. It is invalid to pass a null callback. The additional file flags
     62   // will be added on top of the default file flags which are:
     63   //   base::PLATFORM_FILE_CREATE_ALWAYS
     64   //   base::PLATFORM_FILE_WRITE
     65   //   base::PLATFORM_FILE_TEMPORARY.
     66   // Set |additional_file_flags| to 0 for synchronous writes and set to
     67   // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
     68   //
     69   // This returns false if task posting to |task_runner| has failed.
     70   static bool CreateTemporary(
     71       TaskRunner* task_runner,
     72       int additional_file_flags,
     73       const CreateTemporaryCallback& callback);
     74 
     75   // Close the given file handle.
     76   // This returns false if task posting to |task_runner| has failed.
     77   static bool Close(TaskRunner* task_runner,
     78                     PlatformFile,
     79                     const StatusCallback& callback);
     80 
     81   // Retrieves the information about a file. It is invalid to pass a null
     82   // callback.
     83   // This returns false if task posting to |task_runner| has failed.
     84   static bool GetFileInfo(
     85       TaskRunner* task_runner,
     86       const FilePath& file_path,
     87       const GetFileInfoCallback& callback);
     88 
     89   // Does the same as GetFileInfo but takes PlatformFile instead of FilePath.
     90   // This returns false if task posting to |task_runner| has failed.
     91   static bool GetFileInfoFromPlatformFile(
     92       TaskRunner* task_runner,
     93       PlatformFile file,
     94       const GetFileInfoCallback& callback);
     95 
     96   // Deletes a file or a directory.
     97   // It is an error to delete a non-empty directory with recursive=false.
     98   // This returns false if task posting to |task_runner| has failed.
     99   static bool DeleteFile(TaskRunner* task_runner,
    100                          const FilePath& file_path,
    101                          bool recursive,
    102                          const StatusCallback& callback);
    103 
    104   // Reads from a file. On success, the file pointer is moved to position
    105   // |offset + bytes_to_read| in the file. The callback can be null.
    106   //
    107   // This returns false if |bytes_to_read| is less than zero, or
    108   // if task posting to |task_runner| has failed.
    109   static bool Read(
    110       TaskRunner* task_runner,
    111       PlatformFile file,
    112       int64 offset,
    113       int bytes_to_read,
    114       const ReadCallback& callback);
    115 
    116   // Writes to a file. If |offset| is greater than the length of the file,
    117   // |false| is returned. On success, the file pointer is moved to position
    118   // |offset + bytes_to_write| in the file. The callback can be null.
    119   // |bytes_to_write| must be greater than zero.
    120   //
    121   // This returns false if |bytes_to_write| is less than or equal to zero,
    122   // if |buffer| is NULL, or if task posting to |task_runner| has failed.
    123   static bool Write(
    124       TaskRunner* task_runner,
    125       PlatformFile file,
    126       int64 offset,
    127       const char* buffer,
    128       int bytes_to_write,
    129       const WriteCallback& callback);
    130 
    131   // Touches a file. The callback can be null.
    132   // This returns false if task posting to |task_runner| has failed.
    133   static bool Touch(
    134       TaskRunner* task_runner,
    135       PlatformFile file,
    136       const Time& last_access_time,
    137       const Time& last_modified_time,
    138       const StatusCallback& callback);
    139 
    140   // Touches a file. The callback can be null.
    141   // This returns false if task posting to |task_runner| has failed.
    142   static bool Touch(
    143       TaskRunner* task_runner,
    144       const FilePath& file_path,
    145       const Time& last_access_time,
    146       const Time& last_modified_time,
    147       const StatusCallback& callback);
    148 
    149   // Truncates a file to the given length. If |length| is greater than the
    150   // current length of the file, the file will be extended with zeroes.
    151   // The callback can be null.
    152   // This returns false if task posting to |task_runner| has failed.
    153   static bool Truncate(
    154       TaskRunner* task_runner,
    155       PlatformFile file,
    156       int64 length,
    157       const StatusCallback& callback);
    158 
    159   // Truncates a file to the given length. If |length| is greater than the
    160   // current length of the file, the file will be extended with zeroes.
    161   // The callback can be null.
    162   // This returns false if task posting to |task_runner| has failed.
    163   static bool Truncate(
    164       TaskRunner* task_runner,
    165       const FilePath& path,
    166       int64 length,
    167       const StatusCallback& callback);
    168 
    169   // Flushes a file. The callback can be null.
    170   // This returns false if task posting to |task_runner| has failed.
    171   static bool Flush(
    172       TaskRunner* task_runner,
    173       PlatformFile file,
    174       const StatusCallback& callback);
    175 
    176   // Relay helpers.
    177   // They return false if posting a given task to |task_runner| has failed.
    178   static bool RelayCreateOrOpen(
    179       TaskRunner* task_runner,
    180       const CreateOrOpenTask& open_task,
    181       const CloseTask& close_task,
    182       const CreateOrOpenCallback& callback);
    183   static bool RelayClose(
    184       TaskRunner* task_runner,
    185       const CloseTask& close_task,
    186       PlatformFile,
    187       const StatusCallback& callback);
    188 
    189  private:
    190   DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
    191 };
    192 
    193 }  // namespace base
    194 
    195 #endif  // BASE_FILES_FILE_UTIL_PROXY_H_
    196