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 <string> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" 15 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" 16 #include "webkit/browser/fileapi/async_file_util.h" 17 18 class MTPReadFileWorker; 19 struct SnapshotRequestInfo; 20 21 // MTPDeviceTaskHelper dispatches the media transfer protocol (MTP) device 22 // operation requests (such as GetFileInfo, ReadDirectory, CreateSnapshotFile, 23 // OpenStorage and CloseStorage) to the MediaTransferProtocolManager. 24 // MTPDeviceTaskHelper lives on the UI thread. MTPDeviceTaskHelperMapService 25 // owns the MTPDeviceTaskHelper objects. MTPDeviceTaskHelper is instantiated per 26 // MTP device storage. 27 class MTPDeviceTaskHelper { 28 public: 29 typedef base::Callback<void(bool succeeded)> OpenStorageCallback; 30 31 typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback 32 GetFileInfoSuccessCallback; 33 34 typedef base::Callback<void(const fileapi::AsyncFileUtil::EntryList&)> 35 ReadDirectorySuccessCallback; 36 37 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback; 38 39 MTPDeviceTaskHelper(); 40 ~MTPDeviceTaskHelper(); 41 42 // Dispatches the request to the MediaTransferProtocolManager to open the MTP 43 // storage for communication. 44 // 45 // |storage_name| specifies the name of the storage device. 46 // |callback| is called when the OpenStorage request completes. |callback| 47 // runs on the IO thread. 48 void OpenStorage(const std::string& storage_name, 49 const OpenStorageCallback& callback); 50 51 // Dispatches the GetFileInfoByPath request to the 52 // MediaTransferProtocolManager. 53 // 54 // |file_path| specifies the relative of the file whose details are requested. 55 // 56 // If the file details are fetched successfully, |success_callback| is invoked 57 // on the IO thread to notify the caller about the file details. 58 // 59 // If there is an error, |error_callback| is invoked on the IO thread to 60 // notify the caller about the file error. 61 void GetFileInfoByPath( 62 const std::string& file_path, 63 const GetFileInfoSuccessCallback& success_callback, 64 const ErrorCallback& error_callback); 65 66 // Dispatches the read directory request to the MediaTransferProtocolManager. 67 // 68 // |dir_path| specifies the directory file path. 69 // 70 // If the directory file entries are enumerated successfully, 71 // |success_callback| is invoked on the IO thread to notify the caller about 72 // the directory file entries. 73 // 74 // If there is an error, |error_callback| is invoked on the IO thread to 75 // notify the caller about the file error. 76 void ReadDirectoryByPath(const std::string& dir_path, 77 const ReadDirectorySuccessCallback& success_callback, 78 const ErrorCallback& error_callback); 79 80 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker 81 // object. 82 // 83 // |request_info| specifies the snapshot file request params. 84 // |snapshot_file_info| specifies the metadata of the snapshot file. 85 void WriteDataIntoSnapshotFile( 86 const SnapshotRequestInfo& request_info, 87 const base::File::Info& snapshot_file_info); 88 89 // Dispatches the read bytes request to the MediaTransferProtocolManager. 90 // 91 // |request| contains details about the byte request including the file path, 92 // byte range, and the callbacks. The callbacks specified within |request| are 93 // called on the IO thread to notify the caller about success or failure. 94 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest& request); 95 96 // Dispatches the CloseStorage request to the MediaTransferProtocolManager. 97 void CloseStorage() const; 98 99 private: 100 // Query callback for OpenStorage() to run |callback| on the IO thread. 101 // 102 // If OpenStorage request succeeds, |error| is set to false and 103 // |device_handle| contains the handle to communicate with the MTP device. 104 // 105 // If OpenStorage request fails, |error| is set to true and |device_handle| is 106 // set to an empty string. 107 void OnDidOpenStorage(const OpenStorageCallback& callback, 108 const std::string& device_handle, 109 bool error); 110 111 // Query callback for GetFileInfo(). 112 // 113 // If there is no error, |file_entry| will contain the 114 // requested media device file details and |error| is set to false. 115 // |success_callback| is invoked on the IO thread to notify the caller. 116 // 117 // If there is an error, |file_entry| is invalid and |error| is 118 // set to true. |error_callback| is invoked on the IO thread to notify the 119 // caller. 120 void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback, 121 const ErrorCallback& error_callback, 122 const MtpFileEntry& file_entry, 123 bool error) const; 124 125 // Query callback for ReadDirectoryByPath(). 126 // 127 // If there is no error, |error| is set to false, |file_entries| has the 128 // directory file entries and |success_callback| is invoked on the IO thread 129 // to notify the caller. 130 // 131 // If there is an error, |error| is set to true, |file_entries| is empty 132 // and |error_callback| is invoked on the IO thread to notify the caller. 133 void OnDidReadDirectoryByPath( 134 const ReadDirectorySuccessCallback& success_callback, 135 const ErrorCallback& error_callback, 136 const std::vector<MtpFileEntry>& file_entries, 137 bool error) const; 138 139 // Intermediate step to finish a ReadBytes request. 140 void OnGetFileInfoToReadBytes( 141 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, 142 const MtpFileEntry& file_entry, 143 bool error); 144 145 // Query callback for ReadBytes(); 146 // 147 // If there is no error, |error| is set to false, the buffer within |request| 148 // is written to, and the success callback within |request| is invoked on the 149 // IO thread to notify the caller. 150 // 151 // If there is an error, |error| is set to true, the buffer within |request| 152 // is untouched, and the error callback within |request| is invoked on the 153 // IO thread to notify the caller. 154 void OnDidReadBytes( 155 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, 156 const base::File::Info& file_info, 157 const std::string& data, 158 bool error) const; 159 160 // Called when the device is uninitialized. 161 // 162 // Runs |error_callback| on the IO thread to notify the caller about the 163 // device |error|. 164 void HandleDeviceError(const ErrorCallback& error_callback, 165 base::File::Error error) const; 166 167 // Handle to communicate with the MTP device. 168 std::string device_handle_; 169 170 // Used to handle WriteDataInfoSnapshotFile request. 171 scoped_ptr<MTPReadFileWorker> read_file_worker_; 172 173 // For callbacks that may run after destruction. 174 base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_; 175 176 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper); 177 }; 178 179 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_ 180