Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 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 | #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_ |
| 6 | #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_ |
| 7 | |
| 8 | #include "base/files/file_path.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "base/platform_file.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 12 | #include "webkit/browser/fileapi/async_file_util.h" |
| 13 | #include "webkit/common/blob/shareable_file_reference.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | class SequencedTaskRunner; |
| 17 | class Time; |
| 18 | } |
| 19 | |
| 20 | namespace fileapi { |
| 21 | class FileSystemOperationContext; |
| 22 | class FileSystemURL; |
| 23 | } |
| 24 | |
| 25 | namespace chrome { |
| 26 | |
| 27 | class DeviceMediaAsyncFileUtil : public fileapi::AsyncFileUtil { |
| 28 | public: |
| 29 | virtual ~DeviceMediaAsyncFileUtil(); |
| 30 | |
| 31 | // Returns an instance of DeviceMediaAsyncFileUtil. Returns NULL if |
| 32 | // asynchronous operation is not supported. Callers own the returned |
| 33 | // object. |
| 34 | static DeviceMediaAsyncFileUtil* Create(const base::FilePath& profile_path); |
| 35 | |
| 36 | // AsyncFileUtil overrides. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 37 | virtual void CreateOrOpen( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 38 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 39 | const fileapi::FileSystemURL& url, |
| 40 | int file_flags, |
| 41 | const CreateOrOpenCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 42 | virtual void EnsureFileExists( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 43 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 44 | const fileapi::FileSystemURL& url, |
| 45 | const EnsureFileExistsCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 46 | virtual void CreateDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 47 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 48 | const fileapi::FileSystemURL& url, |
| 49 | bool exclusive, |
| 50 | bool recursive, |
| 51 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 52 | virtual void GetFileInfo( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 53 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 54 | const fileapi::FileSystemURL& url, |
| 55 | const GetFileInfoCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 56 | virtual void ReadDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 57 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 58 | const fileapi::FileSystemURL& url, |
| 59 | const ReadDirectoryCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 60 | virtual void Touch( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 61 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 62 | const fileapi::FileSystemURL& url, |
| 63 | const base::Time& last_access_time, |
| 64 | const base::Time& last_modified_time, |
| 65 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 66 | virtual void Truncate( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 67 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 68 | const fileapi::FileSystemURL& url, |
| 69 | int64 length, |
| 70 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 71 | virtual void CopyFileLocal( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 72 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 73 | const fileapi::FileSystemURL& src_url, |
| 74 | const fileapi::FileSystemURL& dest_url, |
| 75 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 76 | virtual void MoveFileLocal( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 77 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 78 | const fileapi::FileSystemURL& src_url, |
| 79 | const fileapi::FileSystemURL& dest_url, |
| 80 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 81 | virtual void CopyInForeignFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 82 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 83 | const base::FilePath& src_file_path, |
| 84 | const fileapi::FileSystemURL& dest_url, |
| 85 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 86 | virtual void DeleteFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 87 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 88 | const fileapi::FileSystemURL& url, |
| 89 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 90 | virtual void DeleteDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 91 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 92 | const fileapi::FileSystemURL& url, |
| 93 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 94 | virtual void DeleteRecursively( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 95 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
| 96 | const fileapi::FileSystemURL& url, |
| 97 | const StatusCallback& callback) OVERRIDE; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 98 | virtual void CreateSnapshotFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 99 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 100 | const fileapi::FileSystemURL& url, |
| 101 | const CreateSnapshotFileCallback& callback) OVERRIDE; |
| 102 | |
| 103 | private: |
| 104 | // Use Create() to get an instance of DeviceMediaAsyncFileUtil. |
| 105 | explicit DeviceMediaAsyncFileUtil(const base::FilePath& profile_path); |
| 106 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 107 | // Called when GetFileInfo method call succeeds. |file_info| contains the |
| 108 | // file details of the requested url. |callback| is invoked to complete the |
| 109 | // GetFileInfo request. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 110 | void OnDidGetFileInfo( |
| 111 | const AsyncFileUtil::GetFileInfoCallback& callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 112 | const base::PlatformFileInfo& file_info); |
| 113 | |
| 114 | // Called when GetFileInfo method call failed to get the details of file |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 115 | // specified by the requested url. |callback| is invoked to notify the |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 116 | // caller about the platform file |error|. |
| 117 | void OnGetFileInfoError( |
| 118 | const AsyncFileUtil::GetFileInfoCallback& callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 119 | base::PlatformFileError error); |
| 120 | |
| 121 | // Called when ReadDirectory method call succeeds. |callback| is invoked to |
| 122 | // complete the ReadDirectory request. |
| 123 | // |
| 124 | // If the contents of the given directory are reported in one batch, then |
| 125 | // |file_list| will have the list of all files/directories in the given |
| 126 | // directory and |has_more| will be false. |
| 127 | // |
| 128 | // If the contents of the given directory are reported in multiple chunks, |
| 129 | // |file_list| will have only a subset of all contents (the subsets reported |
| 130 | // in any two calls are disjoint), and |has_more| will be true, except for |
| 131 | // the last chunk. |
| 132 | void OnDidReadDirectory( |
| 133 | const AsyncFileUtil::ReadDirectoryCallback& callback, |
| 134 | const AsyncFileUtil::EntryList& file_list, |
| 135 | bool has_more); |
| 136 | |
| 137 | // Called when ReadDirectory method call failed to enumerate the directory |
| 138 | // objects. |callback| is invoked to notify the caller about the |error| |
| 139 | // that occured while reading the directory objects. |
| 140 | void OnReadDirectoryError( |
| 141 | const AsyncFileUtil::ReadDirectoryCallback& callback, |
| 142 | base::PlatformFileError error); |
| 143 | |
| 144 | // Called when the snapshot file specified by the |platform_path| is |
| 145 | // successfully created. |file_info| contains the device media file details |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 146 | // for which the snapshot file is created. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 147 | void OnDidCreateSnapshotFile( |
| 148 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
| 149 | base::SequencedTaskRunner* media_task_runner, |
| 150 | const base::PlatformFileInfo& file_info, |
| 151 | const base::FilePath& platform_path); |
| 152 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 153 | // Called after OnDidCreateSnapshotFile finishes media check. |
| 154 | // |callback| is invoked to complete the CreateSnapshotFile request. |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 155 | void OnDidCheckMedia( |
| 156 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 157 | const base::PlatformFileInfo& file_info, |
| 158 | scoped_refptr<webkit_blob::ShareableFileReference> platform_file, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 159 | base::PlatformFileError error); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 160 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 161 | // Called when CreateSnapshotFile method call fails. |callback| is invoked to |
| 162 | // notify the caller about the |error|. |
| 163 | void OnCreateSnapshotFileError( |
| 164 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
| 165 | base::PlatformFileError error); |
| 166 | |
| 167 | // Called when the snapshot file specified by the |snapshot_file_path| is |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 168 | // created to hold the contents of the url.path(). If the snapshot |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 169 | // file is successfully created, |snapshot_file_path| will be an non-empty |
| 170 | // file path. In case of failure, |snapshot_file_path| will be an empty file |
| 171 | // path. Forwards the CreateSnapshot request to the delegate to copy the |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 172 | // contents of url.path() to |snapshot_file_path|. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 173 | void OnSnapshotFileCreatedRunTask( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 174 | scoped_ptr<fileapi::FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 175 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 176 | const fileapi::FileSystemURL& url, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 177 | base::FilePath* snapshot_file_path); |
| 178 | |
| 179 | // Profile path. |
| 180 | const base::FilePath profile_path_; |
| 181 | |
| 182 | // For callbacks that may run after destruction. |
| 183 | base::WeakPtrFactory<DeviceMediaAsyncFileUtil> weak_ptr_factory_; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(DeviceMediaAsyncFileUtil); |
| 186 | }; |
| 187 | |
| 188 | } // namespace chrome |
| 189 | |
| 190 | #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_DEVICE_MEDIA_ASYNC_FILE_UTIL_H_ |