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::PlatformFileInfo
& snapshot_file_info
);
90 // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
91 void CloseStorage() const;
94 // Query callback for OpenStorage() to run |callback| on the IO thread.
96 // If OpenStorage request succeeds, |error| is set to false and
97 // |device_handle| contains the handle to communicate with the MTP device.
99 // If OpenStorage request fails, |error| is set to true and |device_handle| is
100 // set to an empty string.
101 void OnDidOpenStorage(const OpenStorageCallback
& callback
,
102 const std::string
& device_handle
,
105 // Query callback for GetFileInfo().
107 // If there is no error, |file_entry| will contain the
108 // requested media device file details and |error| is set to false.
109 // |success_callback| is invoked on the IO thread to notify the caller.
111 // If there is an error, |file_entry| is invalid and |error| is
112 // set to true. |error_callback| is invoked on the IO thread to notify the
114 void OnGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
115 const ErrorCallback
& error_callback
,
116 const MtpFileEntry
& file_entry
,
119 // Query callback for ReadDirectoryByPath().
121 // If there is no error, |error| is set to false, |file_entries| has the
122 // directory file entries and |success_callback| is invoked on the IO thread
123 // to notify the caller.
125 // If there is an error, |error| is set to true, |file_entries| is empty
126 // and |error_callback| is invoked on the IO thread to notify the caller.
127 void OnDidReadDirectoryByPath(
128 const ReadDirectorySuccessCallback
& success_callback
,
129 const ErrorCallback
& error_callback
,
130 const std::vector
<MtpFileEntry
>& file_entries
,
133 // Called when the device is uninitialized.
135 // Runs |error_callback| on the IO thread to notify the caller about the
137 void HandleDeviceError(const ErrorCallback
& error_callback
,
138 base::PlatformFileError error
) const;
140 // Handle to communicate with the MTP device.
141 std::string device_handle_
;
143 // Used to handle WriteDataInfoSnapshotFile request.
144 scoped_ptr
<MTPReadFileWorker
> read_file_worker_
;
146 // For callbacks that may run after destruction.
147 base::WeakPtrFactory
<MTPDeviceTaskHelper
> weak_ptr_factory_
;
149 DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper
);
152 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_