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 "base/platform_file.h"
15 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
16 #include "device/media_transfer_protocol/mtp_file_entry.pb.h"
17 #include "webkit/browser/fileapi/async_file_util.h"
19 class MTPReadFileWorker
;
20 struct SnapshotRequestInfo
;
22 // MTPDeviceTaskHelper dispatches the media transfer protocol (MTP) device
23 // operation requests (such as GetFileInfo, ReadDirectory, CreateSnapshotFile,
24 // OpenStorage and CloseStorage) to the MediaTransferProtocolManager.
25 // MTPDeviceTaskHelper lives on the UI thread. MTPDeviceTaskHelperMapService
26 // owns the MTPDeviceTaskHelper objects. MTPDeviceTaskHelper is instantiated per
27 // MTP device storage.
28 class MTPDeviceTaskHelper
{
30 typedef base::Callback
<void(bool succeeded
)> OpenStorageCallback
;
32 typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
33 GetFileInfoSuccessCallback
;
35 typedef base::Callback
<void(const fileapi::AsyncFileUtil::EntryList
&)>
36 ReadDirectorySuccessCallback
;
38 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback
;
40 MTPDeviceTaskHelper();
41 ~MTPDeviceTaskHelper();
43 // Dispatches the request to the MediaTransferProtocolManager to open the MTP
44 // storage for communication.
46 // |storage_name| specifies the name of the storage device.
47 // |callback| is called when the OpenStorage request completes. |callback|
48 // runs on the IO thread.
49 void OpenStorage(const std::string
& storage_name
,
50 const OpenStorageCallback
& callback
);
52 // Dispatches the GetFileInfoByPath request to the
53 // MediaTransferProtocolManager.
55 // |file_path| specifies the relative 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 GetFileInfoByPath(
63 const std::string
& file_path
,
64 const GetFileInfoSuccessCallback
& success_callback
,
65 const ErrorCallback
& error_callback
);
67 // Dispatches the read directory request to the MediaTransferProtocolManager.
69 // |dir_path| specifies the directory file path.
71 // If the directory file entries are enumerated successfully,
72 // |success_callback| is invoked on the IO thread to notify the caller about
73 // the directory file entries.
75 // If there is an error, |error_callback| is invoked on the IO thread to
76 // notify the caller about the file error.
77 void ReadDirectoryByPath(const std::string
& dir_path
,
78 const ReadDirectorySuccessCallback
& success_callback
,
79 const ErrorCallback
& error_callback
);
81 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
84 // |request_info| specifies the snapshot file request params.
85 // |snapshot_file_info| specifies the metadata of the snapshot file.
86 void WriteDataIntoSnapshotFile(
87 const SnapshotRequestInfo
& request_info
,
88 const base::File::Info
& snapshot_file_info
);
90 // Dispatches the read bytes request to the MediaTransferProtocolManager.
92 // |request| contains details about the byte request including the file path,
93 // byte range, and the callbacks. The callbacks specified within |request| are
94 // called on the IO thread to notify the caller about success or failure.
95 void ReadBytes(const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
);
97 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
98 void CloseStorage() const;
101 // Query callback for OpenStorage() to run |callback| on the IO thread.
103 // If OpenStorage request succeeds, |error| is set to false and
104 // |device_handle| contains the handle to communicate with the MTP device.
106 // If OpenStorage request fails, |error| is set to true and |device_handle| is
107 // set to an empty string.
108 void OnDidOpenStorage(const OpenStorageCallback
& callback
,
109 const std::string
& device_handle
,
112 // Query callback for GetFileInfo().
114 // If there is no error, |file_entry| will contain the
115 // requested media device file details and |error| is set to false.
116 // |success_callback| is invoked on the IO thread to notify the caller.
118 // If there is an error, |file_entry| is invalid and |error| is
119 // set to true. |error_callback| is invoked on the IO thread to notify the
121 void OnGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
122 const ErrorCallback
& error_callback
,
123 const MtpFileEntry
& file_entry
,
126 // Query callback for ReadDirectoryByPath().
128 // If there is no error, |error| is set to false, |file_entries| has the
129 // directory file entries and |success_callback| is invoked on the IO thread
130 // to notify the caller.
132 // If there is an error, |error| is set to true, |file_entries| is empty
133 // and |error_callback| is invoked on the IO thread to notify the caller.
134 void OnDidReadDirectoryByPath(
135 const ReadDirectorySuccessCallback
& success_callback
,
136 const ErrorCallback
& error_callback
,
137 const std::vector
<MtpFileEntry
>& file_entries
,
140 // Query callback for ReadBytes();
142 // If there is no error, |error| is set to false, the buffer within |request|
143 // is written to, and the success callback within |request| is invoked on the
144 // IO thread to notify the caller.
146 // If there is an error, |error| is set to true, the buffer within |request|
147 // is untouched, and the error callback within |request| is invoked on the
148 // IO thread to notify the caller.
150 const MTPDeviceAsyncDelegate::ReadBytesRequest
& request
,
151 const std::string
& data
,
154 // Called when the device is uninitialized.
156 // Runs |error_callback| on the IO thread to notify the caller about the
158 void HandleDeviceError(const ErrorCallback
& error_callback
,
159 base::File::Error error
) const;
161 // Handle to communicate with the MTP device.
162 std::string device_handle_
;
164 // Used to handle WriteDataInfoSnapshotFile request.
165 scoped_ptr
<MTPReadFileWorker
> read_file_worker_
;
167 // For callbacks that may run after destruction.
168 base::WeakPtrFactory
<MTPDeviceTaskHelper
> weak_ptr_factory_
;
170 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper
);
173 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_