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 "webkit/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 typedef base::Callback<void(const fileapi::AsyncFileUtil::EntryList&)>
35 ReadDirectorySuccessCallback;
37 typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback;
39 MTPDeviceTaskHelper();
40 ~MTPDeviceTaskHelper();
42 // Dispatches the request to the MediaTransferProtocolManager to open the MTP
43 // storage for communication.
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);
51 // Dispatches the GetFileInfoByPath request to the
52 // MediaTransferProtocolManager.
54 // |file_path| specifies the relative of the file whose details are requested.
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.
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);
66 // Dispatches the read directory request to the MediaTransferProtocolManager.
68 // |dir_path| specifies the directory file path.
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.
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);
80 // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
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);
89 // Dispatches the read bytes request to the MediaTransferProtocolManager.
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);
96 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
97 void CloseStorage() const;
100 // Query callback for OpenStorage() to run |callback| on the IO thread.
102 // If OpenStorage request succeeds, |error| is set to false and
103 // |device_handle| contains the handle to communicate with the MTP device.
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,
111 // Query callback for GetFileInfo().
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.
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
120 void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
121 const ErrorCallback& error_callback,
122 const MtpFileEntry& file_entry,
125 // Query callback for ReadDirectoryByPath().
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.
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,
139 // Intermediate step to finish a ReadBytes request.
140 void OnGetFileInfoToReadBytes(
141 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
142 const MtpFileEntry& file_entry,
145 // Query callback for ReadBytes();
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.
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.
155 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
156 const base::File::Info& file_info,
157 const std::string& data,
160 // Called when the device is uninitialized.
162 // Runs |error_callback| on the IO thread to notify the caller about the
164 void HandleDeviceError(const ErrorCallback& error_callback,
165 base::File::Error error) const;
167 // Handle to communicate with the MTP device.
168 std::string device_handle_;
170 // Used to handle WriteDataInfoSnapshotFile request.
171 scoped_ptr<MTPReadFileWorker> read_file_worker_;
173 // For callbacks that may run after destruction.
174 base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_;
176 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper);
179 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_