Home | History | Annotate | Download | only in drive
      1 // Copyright 2013 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 // This file provides the core implementation of fileapi methods.
      6 // The functions should be called on UI thread.
      7 // Note that most method invocation of fileapi is done on IO thread. The gap is
      8 // filled by FileSystemProxy.
      9 // Also, the order of arguments for the functions which take FileSystemInterface
     10 // at the last is intentional. The instance of FileSystemInterface should be
     11 // accessible only on UI thread, but arguments are passed on IO thread.
     12 // So, here is an intended use case:
     13 //   1) Bind arguments on IO thread. Then a callback instance whose type is
     14 //      Callback<void(FileSysstemInterface*)> is created.
     15 //   2) Post the task to the UI thread.
     16 //   3) On UI thread, check if the instance of FileSystemInterface is alive or
     17 //      not. If yes, Run the callback with it.
     18 
     19 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILEAPI_WORKER_H_
     20 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILEAPI_WORKER_H_
     21 
     22 #include <vector>
     23 
     24 #include "base/basictypes.h"
     25 #include "base/callback_forward.h"
     26 #include "base/memory/weak_ptr.h"
     27 #include "base/platform_file.h"
     28 #include "chrome/browser/chromeos/drive/file_errors.h"
     29 #include "webkit/common/blob/scoped_file.h"
     30 
     31 namespace base {
     32 class FilePath;
     33 }  // namespace base
     34 
     35 namespace fileapi {
     36 struct DirectoryEntry;
     37 }  // namespace fileapi
     38 
     39 namespace drive {
     40 
     41 class FileSystemInterface;
     42 
     43 namespace fileapi_internal {
     44 
     45 typedef base::Callback<FileSystemInterface*()> FileSystemGetter;
     46 
     47 typedef base::Callback<
     48     void(base::PlatformFileError result)> StatusCallback;
     49 typedef base::Callback<
     50     void(base::PlatformFileError result,
     51          const base::PlatformFileInfo& file_info)> GetFileInfoCallback;
     52 typedef base::Callback<
     53     void(base::PlatformFileError result,
     54          const std::vector<fileapi::DirectoryEntry>& file_list,
     55          bool has_more)> ReadDirectoryCallback;
     56 typedef base::Callback<
     57     void(base::PlatformFileError result,
     58          const base::PlatformFileInfo& file_info,
     59          const base::FilePath& snapshot_file_path,
     60          webkit_blob::ScopedFile::ScopeOutPolicy scope_out_policy)>
     61     CreateSnapshotFileCallback;
     62 typedef base::Callback<
     63     void(base::PlatformFileError result,
     64          const base::FilePath& snapshot_file_path,
     65          const base::Closure& close_callback)>
     66     CreateWritableSnapshotFileCallback;
     67 typedef base::Callback<
     68     void(base::PlatformFileError result,
     69          base::PlatformFile platform_file,
     70          const base::Closure& close_callback)> OpenFileCallback;
     71 
     72 // Runs |file_system_getter| to obtain the instance of FileSystemInstance,
     73 // and then runs |callback| with it.
     74 // If |file_system_getter| returns NULL, runs |error_callback| instead.
     75 // This function must be called on UI thread.
     76 // |file_system_getter| and |callback| must not be null, but
     77 // |error_callback| can be null (if no operation is necessary for error
     78 // case).
     79 void RunFileSystemCallback(
     80     const FileSystemGetter& file_system_getter,
     81     const base::Callback<void(FileSystemInterface*)>& callback,
     82     const base::Closure& error_callback);
     83 
     84 // Returns the metadata info of the file at |file_path|.
     85 // Called from FileSystemProxy::GetFileInfo().
     86 void GetFileInfo(const base::FilePath& file_path,
     87                  const GetFileInfoCallback& callback,
     88                  FileSystemInterface* file_system);
     89 
     90 // Copies a file from |src_file_path| to |dest_file_path|.
     91 // Called from FileSystemProxy::Copy().
     92 void Copy(const base::FilePath& src_file_path,
     93           const base::FilePath& dest_file_path,
     94           const StatusCallback& callback,
     95           FileSystemInterface* file_system);
     96 
     97 // Moves a file from |src_file_path| to |dest_file_path|.
     98 // Called from FileSystemProxy::Move().
     99 void Move(const base::FilePath& src_file_path,
    100           const base::FilePath& dest_file_path,
    101           const StatusCallback& callback,
    102           FileSystemInterface* file_system);
    103 
    104 
    105 // Copies a file at |src_foreign_file_path|, which is not managed by Drive File
    106 // System, to |dest_file_path|.
    107 void CopyInForeignFile(const base::FilePath& src_foreign_file_path,
    108                        const base::FilePath& dest_file_path,
    109                        const StatusCallback& callback,
    110                        FileSystemInterface* file_system);
    111 
    112 // Reads the contents of the directory at |file_path|.
    113 // Called from FileSystemProxy::ReadDirectory().
    114 void ReadDirectory(const base::FilePath& file_path,
    115                    const ReadDirectoryCallback& callback,
    116                    FileSystemInterface* file_system);
    117 
    118 // Removes a file at |file_path|. Called from FileSystemProxy::Remove().
    119 void Remove(const base::FilePath& file_path,
    120             bool is_recursive,
    121             const StatusCallback& callback,
    122             FileSystemInterface* file_system);
    123 
    124 // Creates a new directory at |file_path|.
    125 // Called from FileSystemProxy::CreateDirectory().
    126 void CreateDirectory(const base::FilePath& file_path,
    127                      bool is_exclusive,
    128                      bool is_recursive,
    129                      const StatusCallback& callback,
    130                      FileSystemInterface* file_system);
    131 
    132 // Creates a new file at |file_path|.
    133 // Called from FileSystemProxy::CreateFile().
    134 void CreateFile(const base::FilePath& file_path,
    135                 bool is_exclusive,
    136                 const StatusCallback& callback,
    137                 FileSystemInterface* file_system);
    138 
    139 // Truncates the file at |file_path| to |length| bytes.
    140 // Called from FileSystemProxy::Truncate().
    141 void Truncate(const base::FilePath& file_path,
    142               int64 length,
    143               const StatusCallback& callback,
    144               FileSystemInterface* file_system);
    145 
    146 // Creates a snapshot for the file at |file_path|.
    147 // Called from FileSystemProxy::CreateSnapshotFile().
    148 void CreateSnapshotFile(const base::FilePath& file_path,
    149                         const CreateSnapshotFileCallback& callback,
    150                         FileSystemInterface* file_system);
    151 
    152 // Creates a writable snapshot for the file at |file_path|.
    153 // After writing operation is done, |close_callback| must be called.
    154 void CreateWritableSnapshotFile(
    155     const base::FilePath& file_path,
    156     const CreateWritableSnapshotFileCallback& callback,
    157     FileSystemInterface* file_system);
    158 
    159 // Opens the file at |file_path| with options |file_flags|.
    160 // Called from FileSystemProxy::OpenFile.
    161 void OpenFile(const base::FilePath& file_path,
    162               int file_flags,
    163               const OpenFileCallback& callback,
    164               FileSystemInterface* file_system);
    165 
    166 // Changes timestamp of the file at |file_path| to |last_access_time| and
    167 // |last_modified_time|. Called from FileSystemProxy::TouchFile().
    168 void TouchFile(const base::FilePath& file_path,
    169                const base::Time& last_access_time,
    170                const base::Time& last_modified_time,
    171                const StatusCallback& callback,
    172                FileSystemInterface* file_system);
    173 
    174 }  // namespace fileapi_internal
    175 }  // namespace drive
    176 
    177 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILEAPI_WORKER_H_
    178