Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / linux / mtp_device_task_helper.h
blobc21c8527067fd17344469650b0f599733e9a71d8
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_
8 #include <string>
9 #include <vector>
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 {
28 public:
29 typedef base::Callback<void(bool succeeded)> OpenStorageCallback;
31 typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
32 GetFileInfoSuccessCallback;
34 typedef base::Closure CreateDirectorySuccessCallback;
36 // NOTE: The file names in the entry list have their file id appended at the
37 // end. e.g. foo.jpg with file id 45 becomes foo.jpg,45.
38 typedef base::Callback<void(const storage::AsyncFileUtil::EntryList& entries,
39 bool has_more)> ReadDirectorySuccessCallback;
41 typedef base::Closure RenameObjectSuccessCallback;
43 typedef base::Closure CopyFileFromLocalSuccessCallback;
45 typedef base::Closure DeleteObjectSuccessCallback;
47 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback;
49 MTPDeviceTaskHelper();
50 ~MTPDeviceTaskHelper();
52 // Dispatches the request to the MediaTransferProtocolManager to open the MTP
53 // storage for communication.
55 // |storage_name| specifies the name of the storage device.
56 // |callback| is called when the OpenStorage request completes. |callback|
57 // runs on the IO thread.
58 void OpenStorage(const std::string& storage_name,
59 const bool read_only,
60 const OpenStorageCallback& callback);
62 // Dispatches the GetFileInfo request to the MediaTransferProtocolManager.
64 // |file_id| specifies the id of the file whose details are requested.
66 // If the file details are fetched successfully, |success_callback| is invoked
67 // on the IO thread to notify the caller about the file details.
69 // If there is an error, |error_callback| is invoked on the IO thread to
70 // notify the caller about the file error.
71 void GetFileInfo(uint32 file_id,
72 const GetFileInfoSuccessCallback& success_callback,
73 const ErrorCallback& error_callback);
75 // Forwards CreateDirectory request to the MediaTransferProtocolManager.
76 void CreateDirectory(const uint32 parent_id,
77 const std::string& directory_name,
78 const CreateDirectorySuccessCallback& success_callback,
79 const ErrorCallback& error_callback);
81 // Dispatches the read directory request to the MediaTransferProtocolManager.
83 // |dir_id| specifies the directory id.
85 // If the directory file entries are enumerated successfully,
86 // |success_callback| is invoked on the IO thread to notify the caller about
87 // the directory file entries. Please see the note in the
88 // ReadDirectorySuccessCallback typedef regarding the special treatment of
89 // file names.
91 // If there is an error, |error_callback| is invoked on the IO thread to
92 // notify the caller about the file error.
93 void ReadDirectory(const uint32 directory_id,
94 const size_t max_size,
95 const ReadDirectorySuccessCallback& success_callback,
96 const ErrorCallback& error_callback);
98 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
99 // object.
101 // |request_info| specifies the snapshot file request params.
102 // |snapshot_file_info| specifies the metadata of the snapshot file.
103 void WriteDataIntoSnapshotFile(
104 const SnapshotRequestInfo& request_info,
105 const base::File::Info& snapshot_file_info);
107 // Dispatches the read bytes request to the MediaTransferProtocolManager.
109 // |request| contains details about the byte request including the file path,
110 // byte range, and the callbacks. The callbacks specified within |request| are
111 // called on the IO thread to notify the caller about success or failure.
112 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest& request);
114 // Forwards RenameObject request to the MediaTransferProtocolManager.
115 void RenameObject(const uint32 object_id,
116 const std::string& new_name,
117 const RenameObjectSuccessCallback& success_callback,
118 const ErrorCallback& error_callback);
120 // Forwards CopyFileFromLocal request to the MediaTransferProtocolManager.
121 void CopyFileFromLocal(
122 const std::string& storage_name,
123 const int source_file_descriptor,
124 const uint32 parent_id,
125 const std::string& file_name,
126 const CopyFileFromLocalSuccessCallback& success_callback,
127 const ErrorCallback& error_callback);
129 // Forwards DeleteObject request to the MediaTransferProtocolManager.
130 void DeleteObject(const uint32 object_id,
131 const DeleteObjectSuccessCallback& success_callback,
132 const ErrorCallback& error_callback);
134 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
135 void CloseStorage() const;
137 private:
138 // Query callback for OpenStorage() to run |callback| on the IO thread.
140 // If OpenStorage request succeeds, |error| is set to false and
141 // |device_handle| contains the handle to communicate with the MTP device.
143 // If OpenStorage request fails, |error| is set to true and |device_handle| is
144 // set to an empty string.
145 void OnDidOpenStorage(const OpenStorageCallback& callback,
146 const std::string& device_handle,
147 bool error);
149 // Query callback for GetFileInfo().
151 // If there is no error, |file_entry| will contain the
152 // requested media device file details and |error| is set to false.
153 // |success_callback| is invoked on the IO thread to notify the caller.
155 // If there is an error, |file_entry| is invalid and |error| is
156 // set to true. |error_callback| is invoked on the IO thread to notify the
157 // caller.
158 void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
159 const ErrorCallback& error_callback,
160 const MtpFileEntry& file_entry,
161 bool error) const;
163 // Called when CreateDirectory completes.
164 void OnCreateDirectory(const CreateDirectorySuccessCallback& success_callback,
165 const ErrorCallback& error_callback,
166 const bool error) const;
168 // Query callback for ReadDirectory().
170 // If there is no error, |error| is set to false, |file_entries| has the
171 // directory file entries and |success_callback| is invoked on the IO thread
172 // to notify the caller.
174 // If there is an error, |error| is set to true, |file_entries| is empty
175 // and |error_callback| is invoked on the IO thread to notify the caller.
176 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
177 const ErrorCallback& error_callback,
178 const std::vector<MtpFileEntry>& file_entries,
179 bool has_more,
180 bool error) const;
182 // Intermediate step to finish a ReadBytes request.
183 void OnGetFileInfoToReadBytes(
184 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
185 const MtpFileEntry& file_entry,
186 bool error);
188 // Query callback for ReadBytes();
190 // If there is no error, |error| is set to false, the buffer within |request|
191 // is written to, and the success callback within |request| is invoked on the
192 // IO thread to notify the caller.
194 // If there is an error, |error| is set to true, the buffer within |request|
195 // is untouched, and the error callback within |request| is invoked on the
196 // IO thread to notify the caller.
197 void OnDidReadBytes(
198 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
199 const base::File::Info& file_info,
200 const std::string& data,
201 bool error) const;
203 // Called when RenameObject completes.
204 void OnRenameObject(const RenameObjectSuccessCallback& success_callback,
205 const ErrorCallback& error_callback,
206 const bool error) const;
208 // Called when CopyFileFromLocal completes.
209 void OnCopyFileFromLocal(
210 const CopyFileFromLocalSuccessCallback& success_callback,
211 const ErrorCallback& error_callback,
212 const bool error) const;
214 // Called when DeleteObject completes.
215 void OnDeleteObject(const DeleteObjectSuccessCallback& success_callback,
216 const ErrorCallback& error_callback,
217 const bool error) const;
219 // Called when the device is uninitialized.
221 // Runs |error_callback| on the IO thread to notify the caller about the
222 // device |error|.
223 void HandleDeviceError(const ErrorCallback& error_callback,
224 base::File::Error error) const;
226 // Handle to communicate with the MTP device.
227 std::string device_handle_;
229 // Used to handle WriteDataInfoSnapshotFile request.
230 scoped_ptr<MTPReadFileWorker> read_file_worker_;
232 // For callbacks that may run after destruction.
233 base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_;
235 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper);
238 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_