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.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
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 "storage/browser/fileapi/async_file_util.h"
18 class MTPReadFileWorker
;
19 struct SnapshotRequestInfo
;
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
{
29 typedef base::Callback
<void(bool succeeded
)> OpenStorageCallback
;
31 typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
32 GetFileInfoSuccessCallback
;
34 // NOTE: The file names in the entry list have their file id appended at the
35 // end. e.g. foo.jpg with file id 45 becomes foo.jpg,45.
36 typedef base::Callback
<void(const storage::AsyncFileUtil::EntryList
& entries
,
37 bool has_more
)> ReadDirectorySuccessCallback
;
39 typedef base::Closure RenameObjectSuccessCallback
;
41 typedef base::Closure CopyFileFromLocalSuccessCallback
;
43 typedef base::Closure DeleteObjectSuccessCallback
;
45 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback
;
47 MTPDeviceTaskHelper();
48 ~MTPDeviceTaskHelper();
50 // Dispatches the request to the MediaTransferProtocolManager to open the MTP
51 // storage for communication.
53 // |storage_name| specifies the name of the storage device.
54 // |callback| is called when the OpenStorage request completes. |callback|
55 // runs on the IO thread.
56 void OpenStorage(const std::string
& storage_name
,
58 const OpenStorageCallback
& callback
);
60 // Dispatches the GetFileInfo request to the MediaTransferProtocolManager.
62 // |file_id| specifies the id of the file whose details are requested.
64 // If the file details are fetched successfully, |success_callback| is invoked
65 // on the IO thread to notify the caller about the file details.
67 // If there is an error, |error_callback| is invoked on the IO thread to
68 // notify the caller about the file error.
69 void GetFileInfo(uint32 file_id
,
70 const GetFileInfoSuccessCallback
& success_callback
,
71 const ErrorCallback
& error_callback
);
73 // Dispatches the read directory request to the MediaTransferProtocolManager.
75 // |dir_id| specifies the directory id.
77 // If the directory file entries are enumerated successfully,
78 // |success_callback| is invoked on the IO thread to notify the caller about
79 // the directory file entries. Please see the note in the
80 // ReadDirectorySuccessCallback typedef regarding the special treatment of
83 // If there is an error, |error_callback| is invoked on the IO thread to
84 // notify the caller about the file error.
85 void ReadDirectory(const uint32 directory_id
,
86 const size_t max_size
,
87 const ReadDirectorySuccessCallback
& success_callback
,
88 const ErrorCallback
& error_callback
);
90 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
93 // |request_info| specifies the snapshot file request params.
94 // |snapshot_file_info| specifies the metadata of the snapshot file.
95 void WriteDataIntoSnapshotFile(
96 const SnapshotRequestInfo
& request_info
,
97 const base::File::Info
& snapshot_file_info
);
99 // Dispatches the read bytes request to the MediaTransferProtocolManager.
101 // |request| contains details about the byte request including the file path,
102 // byte range, and the callbacks. The callbacks specified within |request| are
103 // called on the IO thread to notify the caller about success or failure.
104 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
);
106 // Forwards RenameObject request to the MediaTransferProtocolManager.
107 void RenameObject(const uint32 object_id
,
108 const std::string
& new_name
,
109 const RenameObjectSuccessCallback
& success_callback
,
110 const ErrorCallback
& error_callback
);
112 // Forwards CopyFileFromLocal request to the MediaTransferProtocolManager.
113 void CopyFileFromLocal(
114 const std::string
& storage_name
,
115 const int source_file_descriptor
,
116 const uint32 parent_id
,
117 const std::string
& file_name
,
118 const CopyFileFromLocalSuccessCallback
& success_callback
,
119 const ErrorCallback
& error_callback
);
121 // Forwards DeleteObject request to the MediaTransferProtocolManager.
122 void DeleteObject(const uint32 object_id
,
123 const DeleteObjectSuccessCallback
& success_callback
,
124 const ErrorCallback
& error_callback
);
126 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
127 void CloseStorage() const;
130 // Query callback for OpenStorage() to run |callback| on the IO thread.
132 // If OpenStorage request succeeds, |error| is set to false and
133 // |device_handle| contains the handle to communicate with the MTP device.
135 // If OpenStorage request fails, |error| is set to true and |device_handle| is
136 // set to an empty string.
137 void OnDidOpenStorage(const OpenStorageCallback
& callback
,
138 const std::string
& device_handle
,
141 // Query callback for GetFileInfo().
143 // If there is no error, |file_entry| will contain the
144 // requested media device file details and |error| is set to false.
145 // |success_callback| is invoked on the IO thread to notify the caller.
147 // If there is an error, |file_entry| is invalid and |error| is
148 // set to true. |error_callback| is invoked on the IO thread to notify the
150 void OnGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
151 const ErrorCallback
& error_callback
,
152 const MtpFileEntry
& file_entry
,
155 // Query callback for ReadDirectory().
157 // If there is no error, |error| is set to false, |file_entries| has the
158 // directory file entries and |success_callback| is invoked on the IO thread
159 // to notify the caller.
161 // If there is an error, |error| is set to true, |file_entries| is empty
162 // and |error_callback| is invoked on the IO thread to notify the caller.
163 void OnDidReadDirectory(const ReadDirectorySuccessCallback
& success_callback
,
164 const ErrorCallback
& error_callback
,
165 const std::vector
<MtpFileEntry
>& file_entries
,
169 // Intermediate step to finish a ReadBytes request.
170 void OnGetFileInfoToReadBytes(
171 const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
,
172 const MtpFileEntry
& file_entry
,
175 // Query callback for ReadBytes();
177 // If there is no error, |error| is set to false, the buffer within |request|
178 // is written to, and the success callback within |request| is invoked on the
179 // IO thread to notify the caller.
181 // If there is an error, |error| is set to true, the buffer within |request|
182 // is untouched, and the error callback within |request| is invoked on the
183 // IO thread to notify the caller.
185 const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
,
186 const base::File::Info
& file_info
,
187 const std::string
& data
,
190 // Called when RenameObject completes.
191 void OnRenameObject(const RenameObjectSuccessCallback
& success_callback
,
192 const ErrorCallback
& error_callback
,
193 const bool error
) const;
195 // Called when CopyFileFromLocal completes.
196 void OnCopyFileFromLocal(
197 const CopyFileFromLocalSuccessCallback
& success_callback
,
198 const ErrorCallback
& error_callback
,
199 const bool error
) const;
201 // Called when DeleteObject completes.
202 void OnDeleteObject(const DeleteObjectSuccessCallback
& success_callback
,
203 const ErrorCallback
& error_callback
,
204 const bool error
) const;
206 // Called when the device is uninitialized.
208 // Runs |error_callback| on the IO thread to notify the caller about the
210 void HandleDeviceError(const ErrorCallback
& error_callback
,
211 base::File::Error error
) const;
213 // Handle to communicate with the MTP device.
214 std::string device_handle_
;
216 // Used to handle WriteDataInfoSnapshotFile request.
217 scoped_ptr
<MTPReadFileWorker
> read_file_worker_
;
219 // For callbacks that may run after destruction.
220 base::WeakPtrFactory
<MTPDeviceTaskHelper
> weak_ptr_factory_
;
222 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper
);
225 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_