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 MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback
;
41 MTPDeviceTaskHelper();
42 ~MTPDeviceTaskHelper();
44 // Dispatches the request to the MediaTransferProtocolManager to open the MTP
45 // storage for communication.
47 // |storage_name| specifies the name of the storage device.
48 // |callback| is called when the OpenStorage request completes. |callback|
49 // runs on the IO thread.
50 void OpenStorage(const std::string
& storage_name
,
51 const OpenStorageCallback
& callback
);
53 // Dispatches the GetFileInfo request to the MediaTransferProtocolManager.
55 // |file_id| specifies the id of the file whose details are requested.
57 // If the file details are fetched successfully, |success_callback| is invoked
58 // on the IO thread to notify the caller about the file details.
60 // If there is an error, |error_callback| is invoked on the IO thread to
61 // notify the caller about the file error.
62 void GetFileInfo(uint32 file_id
,
63 const GetFileInfoSuccessCallback
& success_callback
,
64 const ErrorCallback
& error_callback
);
66 // Dispatches the read directory request to the MediaTransferProtocolManager.
68 // |dir_id| specifies the directory id.
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. Please see the note in the
73 // ReadDirectorySuccessCallback typedef regarding the special treatment of
76 // If there is an error, |error_callback| is invoked on the IO thread to
77 // notify the caller about the file error.
78 void ReadDirectory(uint32 dir_id
,
79 const ReadDirectorySuccessCallback
& success_callback
,
80 const ErrorCallback
& error_callback
);
82 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
85 // |request_info| specifies the snapshot file request params.
86 // |snapshot_file_info| specifies the metadata of the snapshot file.
87 void WriteDataIntoSnapshotFile(
88 const SnapshotRequestInfo
& request_info
,
89 const base::File::Info
& snapshot_file_info
);
91 // Dispatches the read bytes request to the MediaTransferProtocolManager.
93 // |request| contains details about the byte request including the file path,
94 // byte range, and the callbacks. The callbacks specified within |request| are
95 // called on the IO thread to notify the caller about success or failure.
96 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
);
98 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
99 void CloseStorage() const;
102 // Query callback for OpenStorage() to run |callback| on the IO thread.
104 // If OpenStorage request succeeds, |error| is set to false and
105 // |device_handle| contains the handle to communicate with the MTP device.
107 // If OpenStorage request fails, |error| is set to true and |device_handle| is
108 // set to an empty string.
109 void OnDidOpenStorage(const OpenStorageCallback
& callback
,
110 const std::string
& device_handle
,
113 // Query callback for GetFileInfo().
115 // If there is no error, |file_entry| will contain the
116 // requested media device file details and |error| is set to false.
117 // |success_callback| is invoked on the IO thread to notify the caller.
119 // If there is an error, |file_entry| is invalid and |error| is
120 // set to true. |error_callback| is invoked on the IO thread to notify the
122 void OnGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
123 const ErrorCallback
& error_callback
,
124 const MtpFileEntry
& file_entry
,
127 // Query callback for ReadDirectory().
129 // If there is no error, |error| is set to false, |file_entries| has the
130 // directory file entries and |success_callback| is invoked on the IO thread
131 // to notify the caller.
133 // If there is an error, |error| is set to true, |file_entries| is empty
134 // and |error_callback| is invoked on the IO thread to notify the caller.
135 void OnDidReadDirectory(const ReadDirectorySuccessCallback
& success_callback
,
136 const ErrorCallback
& error_callback
,
137 const std::vector
<MtpFileEntry
>& file_entries
,
141 // Intermediate step to finish a ReadBytes request.
142 void OnGetFileInfoToReadBytes(
143 const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
,
144 const MtpFileEntry
& file_entry
,
147 // Query callback for ReadBytes();
149 // If there is no error, |error| is set to false, the buffer within |request|
150 // is written to, and the success callback within |request| is invoked on the
151 // IO thread to notify the caller.
153 // If there is an error, |error| is set to true, the buffer within |request|
154 // is untouched, and the error callback within |request| is invoked on the
155 // IO thread to notify the caller.
157 const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
,
158 const base::File::Info
& file_info
,
159 const std::string
& data
,
162 // Called when the device is uninitialized.
164 // Runs |error_callback| on the IO thread to notify the caller about the
166 void HandleDeviceError(const ErrorCallback
& error_callback
,
167 base::File::Error error
) const;
169 // Handle to communicate with the MTP device.
170 std::string device_handle_
;
172 // Used to handle WriteDataInfoSnapshotFile request.
173 scoped_ptr
<MTPReadFileWorker
> read_file_worker_
;
175 // For callbacks that may run after destruction.
176 base::WeakPtrFactory
<MTPDeviceTaskHelper
> weak_ptr_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper
);
181 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_