1 // Copyright 2014 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_FILEAPI_WORKER_H_ 20 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILEAPI_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 "chrome/browser/chromeos/drive/file_errors.h" 28 #include "storage/common/blob/scoped_file.h" 29 30 namespace base { 31 class FilePath; 32 } // namespace base 33 34 namespace storage { 35 struct DirectoryEntry; 36 class FileSystemURL; 37 } // namespace storage 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::File::Error result)> StatusCallback; 49 typedef base::Callback< 50 void(base::File::Error result, 51 const base::File::Info& file_info)> GetFileInfoCallback; 52 typedef base::Callback< 53 void(base::File::Error result, 54 const std::vector<storage::DirectoryEntry>& file_list, 55 bool has_more)> ReadDirectoryCallback; 56 typedef base::Callback<void(base::File::Error result, 57 const base::File::Info& file_info, 58 const base::FilePath& snapshot_file_path, 59 storage::ScopedFile::ScopeOutPolicy 60 scope_out_policy)> CreateSnapshotFileCallback; 61 typedef base::Callback< 62 void(base::File::Error result, 63 const base::FilePath& snapshot_file_path, 64 const base::Closure& close_callback)> 65 CreateWritableSnapshotFileCallback; 66 typedef base::Callback< 67 void(base::File file, 68 const base::Closure& close_callback)> OpenFileCallback; 69 70 // Gets the profile of the Drive entry pointed by |url|. Used as 71 // FileSystemGetter callback by binding an URL on the IO thread and passing to 72 // the UI thread. 73 FileSystemInterface* GetFileSystemFromUrl(const storage::FileSystemURL& url); 74 75 // Runs |file_system_getter| to obtain the instance of FileSystemInstance, 76 // and then runs |callback| with it. 77 // If |file_system_getter| returns NULL, runs |error_callback| instead. 78 // This function must be called on UI thread. 79 // |file_system_getter| and |callback| must not be null, but 80 // |error_callback| can be null (if no operation is necessary for error 81 // case). 82 void RunFileSystemCallback( 83 const FileSystemGetter& file_system_getter, 84 const base::Callback<void(FileSystemInterface*)>& callback, 85 const base::Closure& error_callback); 86 87 // Returns the metadata info of the file at |file_path|. 88 // Called from FileSystemProxy::GetFileInfo(). 89 void GetFileInfo(const base::FilePath& file_path, 90 const GetFileInfoCallback& callback, 91 FileSystemInterface* file_system); 92 93 // Copies a file from |src_file_path| to |dest_file_path|. 94 // Called from FileSystemProxy::Copy(). 95 void Copy(const base::FilePath& src_file_path, 96 const base::FilePath& dest_file_path, 97 bool preserve_last_modified, 98 const StatusCallback& callback, 99 FileSystemInterface* file_system); 100 101 // Moves a file from |src_file_path| to |dest_file_path|. 102 // Called from FileSystemProxy::Move(). 103 void Move(const base::FilePath& src_file_path, 104 const base::FilePath& dest_file_path, 105 const StatusCallback& callback, 106 FileSystemInterface* file_system); 107 108 109 // Copies a file at |src_foreign_file_path|, which is not managed by Drive File 110 // System, to |dest_file_path|. 111 void CopyInForeignFile(const base::FilePath& src_foreign_file_path, 112 const base::FilePath& dest_file_path, 113 const StatusCallback& callback, 114 FileSystemInterface* file_system); 115 116 // Reads the contents of the directory at |file_path|. 117 // Called from FileSystemProxy::ReadDirectory(). 118 void ReadDirectory(const base::FilePath& file_path, 119 const ReadDirectoryCallback& callback, 120 FileSystemInterface* file_system); 121 122 // Removes a file at |file_path|. Called from FileSystemProxy::Remove(). 123 void Remove(const base::FilePath& file_path, 124 bool is_recursive, 125 const StatusCallback& callback, 126 FileSystemInterface* file_system); 127 128 // Creates a new directory at |file_path|. 129 // Called from FileSystemProxy::CreateDirectory(). 130 void CreateDirectory(const base::FilePath& file_path, 131 bool is_exclusive, 132 bool is_recursive, 133 const StatusCallback& callback, 134 FileSystemInterface* file_system); 135 136 // Creates a new file at |file_path|. 137 // Called from FileSystemProxy::CreateFile(). 138 void CreateFile(const base::FilePath& file_path, 139 bool is_exclusive, 140 const StatusCallback& callback, 141 FileSystemInterface* file_system); 142 143 // Truncates the file at |file_path| to |length| bytes. 144 // Called from FileSystemProxy::Truncate(). 145 void Truncate(const base::FilePath& file_path, 146 int64 length, 147 const StatusCallback& callback, 148 FileSystemInterface* file_system); 149 150 // Creates a snapshot for the file at |file_path|. 151 // Called from FileSystemProxy::CreateSnapshotFile(). 152 void CreateSnapshotFile(const base::FilePath& file_path, 153 const CreateSnapshotFileCallback& callback, 154 FileSystemInterface* file_system); 155 156 // Creates a writable snapshot for the file at |file_path|. 157 // After writing operation is done, |close_callback| must be called. 158 void CreateWritableSnapshotFile( 159 const base::FilePath& file_path, 160 const CreateWritableSnapshotFileCallback& callback, 161 FileSystemInterface* file_system); 162 163 // Opens the file at |file_path| with options |file_flags|. 164 // Called from FileSystemProxy::OpenFile. 165 void OpenFile(const base::FilePath& file_path, 166 int file_flags, 167 const OpenFileCallback& callback, 168 FileSystemInterface* file_system); 169 170 // Changes timestamp of the file at |file_path| to |last_access_time| and 171 // |last_modified_time|. Called from FileSystemProxy::TouchFile(). 172 void TouchFile(const base::FilePath& file_path, 173 const base::Time& last_access_time, 174 const base::Time& last_modified_time, 175 const StatusCallback& callback, 176 FileSystemInterface* file_system); 177 178 } // namespace fileapi_internal 179 } // namespace drive 180 181 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILEAPI_FILEAPI_WORKER_H_ 182