Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / linux / mtp_device_task_helper.h
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.
4
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_
7
8 #include <string>
9 #include <vector>
10
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"
17
18 class MTPReadFileWorker;
19 struct SnapshotRequestInfo;
20
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;
30
31   typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
32       GetFileInfoSuccessCallback;
33
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;
38
39   typedef MTPDeviceAsyncDelegate::ErrorCallback ErrorCallback;
40
41   MTPDeviceTaskHelper();
42   ~MTPDeviceTaskHelper();
43
44   // Dispatches the request to the MediaTransferProtocolManager to open the MTP
45   // storage for communication.
46   //
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);
52
53   // Dispatches the GetFileInfo request to the MediaTransferProtocolManager.
54   //
55   // |file_id| specifies the id of the file whose details are requested.
56   //
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.
59   //
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);
65
66   // Dispatches the read directory request to the MediaTransferProtocolManager.
67   //
68   // |dir_id| specifies the directory id.
69   //
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
74   // file names.
75   //
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);
81
82   // Forwards the WriteDataIntoSnapshotFile request to the MTPReadFileWorker
83   // object.
84   //
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);
90
91   // Dispatches the read bytes request to the MediaTransferProtocolManager.
92   //
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);
97
98   // Dispatches the CloseStorage request to the MediaTransferProtocolManager.
99   void CloseStorage() const;
100
101  private:
102   // Query callback for OpenStorage() to run |callback| on the IO thread.
103   //
104   // If OpenStorage request succeeds, |error| is set to false and
105   // |device_handle| contains the handle to communicate with the MTP device.
106   //
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,
111                         bool error);
112
113   // Query callback for GetFileInfo().
114   //
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.
118   //
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
121   // caller.
122   void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
123                      const ErrorCallback& error_callback,
124                      const MtpFileEntry& file_entry,
125                      bool error) const;
126
127   // Query callback for ReadDirectory().
128   //
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.
132   //
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,
138                           bool has_more,
139                           bool error) const;
140
141   // Intermediate step to finish a ReadBytes request.
142   void OnGetFileInfoToReadBytes(
143       const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
144       const MtpFileEntry& file_entry,
145       bool error);
146
147   // Query callback for ReadBytes();
148   //
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.
152   //
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.
156   void OnDidReadBytes(
157       const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
158       const base::File::Info& file_info,
159       const std::string& data,
160       bool error) const;
161
162   // Called when the device is uninitialized.
163   //
164   // Runs |error_callback| on the IO thread to notify the caller about the
165   // device |error|.
166   void HandleDeviceError(const ErrorCallback& error_callback,
167                          base::File::Error error) const;
168
169   // Handle to communicate with the MTP device.
170   std::string device_handle_;
171
172   // Used to handle WriteDataInfoSnapshotFile request.
173   scoped_ptr<MTPReadFileWorker> read_file_worker_;
174
175   // For callbacks that may run after destruction.
176   base::WeakPtrFactory<MTPDeviceTaskHelper> weak_ptr_factory_;
177
178   DISALLOW_COPY_AND_ASSIGN(MTPDeviceTaskHelper);
179 };
180
181 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_TASK_HELPER_H_