| OLD | NEW |
| (Empty) |
| 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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/callback.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" | |
| 19 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" | |
| 20 #include "storage/browser/fileapi/async_file_util.h" | |
| 21 | |
| 22 class MTPReadFileWorker; | |
| 23 struct SnapshotRequestInfo; | |
| 24 | |
| 25 // MTPDeviceTaskHelper dispatches the media transfer protocol (MTP) device | |
| 26 // operation requests (such as GetFileInfo, ReadDirectory, CreateSnapshotFile, | |
| 27 // OpenStorage and CloseStorage) to the MediaTransferProtocolManager. | |
| 28 // MTPDeviceTaskHelper lives on the UI thread. MTPDeviceTaskHelperMapService | |
| 29 // owns the MTPDeviceTaskHelper objects. MTPDeviceTaskHelper is instantiated per | |
| 30 // MTP device storage. | |
| 31 class MTPDeviceTaskHelper { | |
| 32 public: | |
| 33 struct MTPEntry { | |
| 34 MTPEntry(); | |
| 35 | |
| 36 uint32_t file_id; | |
| 37 std::string name; | |
| 38 base::File::Info file_info; | |
| 39 }; | |
| 40 | |
| 41 typedef std::vector<MTPEntry> MTPEntries; | |
| 42 | |
| 43 typedef base::Callback<void(bool succeeded)> OpenStorageCallback; | |
| 44 | |
| 45 typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback | |
| 46 GetFileInfoSuccessCallback; | |
| 47 | |
| 48 typedef base::Closure CreateDirectorySuccessCallback; | |
| 49 | |
| 50 typedef base::Callback<void(const MTPEntries& entries, bool has_more)> | |
| 51 ReadDirectorySuccessCallback; | |
| 52 | |
| 53 typedef base::Closure RenameObjectSuccessCallback; | |
| 54 | |
| 55 typedef base::Closure CopyFileFromLocalSuccessCallback; | |
| 56 | |
| 57 typedef base::Closure DeleteObjectSuccessCallback; | |
| 58 | |
| 59 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback; | |
| 60 | |
| 61 MTPDeviceTaskHelper(); | |
| 62 ~MTPDeviceTaskHelper(); | |
| 63 | |
| 64 // Dispatches the request to the MediaTransferProtocolManager to open the MTP | |
| 65 // storage for communication. | |
| 66 // | |
| 67 // |storage_name| specifies the name of the storage device. | |
| 68 // |callback| is called when the OpenStorage request completes. |callback| | |
| 69 // runs on the IO thread. | |
| 70 void OpenStorage(const std::string& storage_name, | |
| 71 const bool read_only, | |
| 72 const OpenStorageCallback& callback); | |
| 73 | |
| 74 // Dispatches the GetFileInfo request to the MediaTransferProtocolManager. | |
| 75 // | |
| 76 // |file_id| specifies the id of the file whose details are requested. | |
| 77 // | |
| 78 // If the file details are fetched successfully, |success_callback| is invoked | |
| 79 // on the IO thread to notify the caller about the file details. | |
| 80 // | |
| 81 // If there is an error, |error_callback| is invoked on the IO thread to | |
| 82 // notify the caller about the file error. | |
| 83 void GetFileInfo(uint32_t file_id, | |
| 84 const GetFileInfoSuccessCallback& success_callback, | |
| 85 const ErrorCallback& error_callback); | |
| 86 | |
| 87 // Forwards CreateDirectory request to the MediaTransferProtocolManager. | |
| 88 void CreateDirectory(const uint32_t parent_id, | |
| 89 const std::string& directory_name, | |
| 90 const CreateDirectorySuccessCallback& success_callback, | |
| 91 const ErrorCallback& error_callback); | |
| 92 | |
| 93 // Dispatches the read directory request to the MediaTransferProtocolManager. | |
| 94 // | |
| 95 // |dir_id| specifies the directory id. | |
| 96 // | |
| 97 // If the directory file entries are enumerated successfully, | |
| 98 // |success_callback| is invoked on the IO thread to notify the caller about | |
| 99 // the directory file entries. Please see the note in the | |
| 100 // ReadDirectorySuccessCallback typedef regarding the special treatment of | |
| 101 // file names. | |
| 102 // | |
| 103 // If there is an error, |error_callback| is invoked on the IO thread to | |
| 104 // notify the caller about the file error. | |
| 105 void ReadDirectory(const uint32_t directory_id, | |
| 106 const size_t max_size, | |
| 107 const ReadDirectorySuccessCallback& success_callback, | |
| 108 const ErrorCallback& error_callback); | |
| 109 | |
| 110 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker | |
| 111 // object. | |
| 112 // | |
| 113 // |request_info| specifies the snapshot file request params. | |
| 114 // |snapshot_file_info| specifies the metadata of the snapshot file. | |
| 115 void WriteDataIntoSnapshotFile( | |
| 116 const SnapshotRequestInfo& request_info, | |
| 117 const base::File::Info& snapshot_file_info); | |
| 118 | |
| 119 // Dispatches the read bytes request to the MediaTransferProtocolManager. | |
| 120 // | |
| 121 // |request| contains details about the byte request including the file path, | |
| 122 // byte range, and the callbacks. The callbacks specified within |request| are | |
| 123 // called on the IO thread to notify the caller about success or failure. | |
| 124 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest& request); | |
| 125 | |
| 126 // Forwards RenameObject request to the MediaTransferProtocolManager. | |
| 127 void RenameObject(const uint32_t object_id, | |
| 128 const std::string& new_name, | |
| 129 const RenameObjectSuccessCallback& success_callback, | |
| 130 const ErrorCallback& error_callback); | |
| 131 | |
| 132 // Forwards CopyFileFromLocal request to the MediaTransferProtocolManager. | |
| 133 void CopyFileFromLocal( | |
| 134 const std::string& storage_name, | |
| 135 const int source_file_descriptor, | |
| 136 const uint32_t parent_id, | |
| 137 const std::string& file_name, | |
| 138 const CopyFileFromLocalSuccessCallback& success_callback, | |
| 139 const ErrorCallback& error_callback); | |
| 140 | |
| 141 // Forwards DeleteObject request to the MediaTransferProtocolManager. | |
| 142 void DeleteObject(const uint32_t object_id, | |
| 143 const DeleteObjectSuccessCallback& success_callback, | |
| 144 const ErrorCallback& error_callback); | |
| 145 | |
| 146 // Dispatches the CloseStorage request to the MediaTransferProtocolManager. | |
| 147 void CloseStorage() const; | |
| 148 | |
| 149 private: | |
| 150 // Query callback for OpenStorage() to run |callback| on the IO thread. | |
| 151 // | |
| 152 // If OpenStorage request succeeds, |error| is set to false and | |
| 153 // |device_handle| contains the handle to communicate with the MTP device. | |
| 154 // | |
| 155 // If OpenStorage request fails, |error| is set to true and |device_handle| is | |
| 156 // set to an empty string. | |
| 157 void OnDidOpenStorage(const OpenStorageCallback& callback, | |
| 158 const std::string& device_handle, | |
| 159 bool error); | |
| 160 | |
| 161 // Query callback for GetFileInfo(). | |
| 162 // | |
| 163 // If there is no error, |file_entry| will contain the | |
| 164 // requested media device file details and |error| is set to false. | |
| 165 // |success_callback| is invoked on the IO thread to notify the caller. | |
| 166 // | |
| 167 // If there is an error, |file_entry| is invalid and |error| is | |
| 168 // set to true. |error_callback| is invoked on the IO thread to notify the | |
| 169 // caller. | |
| 170 void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback, | |
| 171 const ErrorCallback& error_callback, | |
| 172 const MtpFileEntry& file_entry, | |
| 173 bool error) const; | |
| 174 | |
| 175 // Called when CreateDirectory completes. | |
| 176 void OnCreateDirectory(const CreateDirectorySuccessCallback& success_callback, | |
| 177 const ErrorCallback& error_callback, | |
| 178 const bool error) const; | |
| 179 | |
| 180 // Query callback for ReadDirectory(). | |
| 181 // | |
| 182 // If there is no error, |error| is set to false, |file_entries| has the | |
| 183 // directory file entries and |success_callback| is invoked on the IO thread | |
| 184 // to notify the caller. | |
| 185 // | |
| 186 // If there is an error, |error| is set to true, |file_entries| is empty | |
| 187 // and |error_callback| is invoked on the IO thread to notify the caller. | |
| 188 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback, | |
| 189 const ErrorCallback& error_callback, | |
| 190 const std::vector<MtpFileEntry>& file_entries, | |
| 191 bool has_more, | |
| 192 bool error) const; | |
| 193 | |
| 194 // Intermediate step to finish a ReadBytes request. | |
| 195 void OnGetFileInfoToReadBytes( | |
| 196 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | |
| 197 const MtpFileEntry& file_entry, | |
| 198 bool error); | |
| 199 | |
| 200 // Query callback for ReadBytes(); | |
| 201 // | |
| 202 // If there is no error, |error| is set to false, the buffer within |request| | |
| 203 // is written to, and the success callback within |request| is invoked on the | |
| 204 // IO thread to notify the caller. | |
| 205 // | |
| 206 // If there is an error, |error| is set to true, the buffer within |request| | |
| 207 // is untouched, and the error callback within |request| is invoked on the | |
| 208 // IO thread to notify the caller. | |
| 209 void OnDidReadBytes( | |
| 210 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | |
| 211 const base::File::Info& file_info, | |
| 212 const std::string& data, | |
| 213 bool error) const; | |
| 214 | |
| 215 // Called when RenameObject completes. | |
| 216 void OnRenameObject(const RenameObjectSuccessCallback& success_callback, | |
| 217 const ErrorCallback& error_callback, | |
| 218 const bool error) const; | |
| 219 | |
| 220 // Called when CopyFileFromLocal completes. | |
| 221 void OnCopyFileFromLocal( | |
| 222 const CopyFileFromLocalSuccessCallback& success_callback, | |
| 223 const ErrorCallback& error_callback, | |
| 224 const bool error) const; | |
| 225 | |
| 226 // Called when DeleteObject completes. | |
| 227 void OnDeleteObject(const DeleteObjectSuccessCallback& success_callback, | |
| 228 const ErrorCallback& error_callback, | |
| 229 const bool error) const; | |
| 230 | |
| 231 // Called when the device is uninitialized. | |
| 232 // | |
| 233 // Runs |error_callback| on the IO thread to notify the caller about the | |
| 234 // device |error|. | |
| 235 void HandleDeviceError(const ErrorCallback& error_callback, | |
| 236 base::File::Error error) const; | |
| 237 | |
| 238 // Handle to communicate with the MTP device. | |
| 239 std::string device_handle_; | |
| 240 | |
| 241 // Used to handle WriteDataInfoSnapshotFile request. | |
| 242 std::unique_ptr<MTPReadFileWorker> read_file_worker_; | |
| 243 | |
| 244 // For callbacks that may run after destruction. | |
| 245 base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper); | |
| 248 }; | |
| 249 | |
| 250 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_ | |
| OLD | NEW |