1 // Copyright (c) 2012 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_DELEGATE_IMPL_LINUX_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
12 #include "base/callback.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/files/file_path.h"
15 #include "base/location.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "webkit/browser/fileapi/async_file_util.h"
22 struct SnapshotRequestInfo
;
24 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol
25 // (MTP) device to complete file system operations. These operations are
26 // performed asynchronously. Instantiate this class per MTP device storage.
27 // MTPDeviceDelegateImplLinux lives on the IO thread.
28 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
29 // to dispatch the requests to MediaTransferProtocolManager.
30 class MTPDeviceDelegateImplLinux
: public MTPDeviceAsyncDelegate
{
32 friend void CreateMTPDeviceAsyncDelegate(
34 const CreateMTPDeviceAsyncDelegateCallback
&);
36 enum InitializationState
{
42 // Used to represent pending task details.
43 struct PendingTaskInfo
{
44 PendingTaskInfo(const base::FilePath
& path
,
45 content::BrowserThread::ID thread_id
,
46 const tracked_objects::Location
& location
,
47 const base::Closure
& task
);
51 base::FilePath cached_path
;
52 const content::BrowserThread::ID thread_id
;
53 const tracked_objects::Location location
;
54 const base::Closure task
;
59 // Maps file ids to file nodes.
60 typedef std::map
<uint32
, MTPFileNode
*> FileIdToMTPFileNodeMap
;
62 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
63 // Defer the device initializations until the first file operation request.
64 // Do all the initializations in EnsureInitAndRunTask() function.
65 explicit MTPDeviceDelegateImplLinux(const std::string
& device_location
);
67 // Destructed via CancelPendingTasksAndDeleteDelegate().
68 virtual ~MTPDeviceDelegateImplLinux();
70 // MTPDeviceAsyncDelegate:
71 virtual void GetFileInfo(const base::FilePath
& file_path
,
72 const GetFileInfoSuccessCallback
& success_callback
,
73 const ErrorCallback
& error_callback
) OVERRIDE
;
74 virtual void ReadDirectory(
75 const base::FilePath
& root
,
76 const ReadDirectorySuccessCallback
& success_callback
,
77 const ErrorCallback
& error_callback
) OVERRIDE
;
78 virtual void CreateSnapshotFile(
79 const base::FilePath
& device_file_path
,
80 const base::FilePath
& local_path
,
81 const CreateSnapshotFileSuccessCallback
& success_callback
,
82 const ErrorCallback
& error_callback
) OVERRIDE
;
83 virtual bool IsStreaming() OVERRIDE
;
84 virtual void ReadBytes(
85 const base::FilePath
& device_file_path
,
86 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
87 const ReadBytesSuccessCallback
& success_callback
,
88 const ErrorCallback
& error_callback
) OVERRIDE
;
89 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE
;
91 // The internal methods correspond to the similarly named methods above.
92 // The |root_node_| cache should be filled at this point.
93 virtual void GetFileInfoInternal(
94 const base::FilePath
& file_path
,
95 const GetFileInfoSuccessCallback
& success_callback
,
96 const ErrorCallback
& error_callback
);
97 virtual void ReadDirectoryInternal(
98 const base::FilePath
& root
,
99 const ReadDirectorySuccessCallback
& success_callback
,
100 const ErrorCallback
& error_callback
);
101 virtual void CreateSnapshotFileInternal(
102 const base::FilePath
& device_file_path
,
103 const base::FilePath
& local_path
,
104 const CreateSnapshotFileSuccessCallback
& success_callback
,
105 const ErrorCallback
& error_callback
);
106 virtual void ReadBytesInternal(
107 const base::FilePath
& device_file_path
,
108 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
109 const ReadBytesSuccessCallback
& success_callback
,
110 const ErrorCallback
& error_callback
);
112 // Ensures the device is initialized for communication.
113 // If the device is already initialized, call RunTask().
115 // If the device is uninitialized, store the |task_info| in a pending task
116 // queue and runs the pending tasks in the queue once the device is
117 // successfully initialized.
118 void EnsureInitAndRunTask(const PendingTaskInfo
& task_info
);
120 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs
121 // the task immediately.
122 // Otherwise, fills the cache first before running the task.
123 // |task_info.task| runs on the UI thread.
124 void RunTask(const PendingTaskInfo
& task_info
);
126 // Writes data from the device to the snapshot file path based on the
127 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
130 // |snapshot_file_info| specifies the metadata details of the snapshot file.
131 void WriteDataIntoSnapshotFile(const base::File::Info
& snapshot_file_info
);
133 // Marks the current request as complete and call ProcessNextPendingRequest().
134 void PendingRequestDone();
136 // Processes the next pending request.
137 void ProcessNextPendingRequest();
139 // Handles the device initialization event. |succeeded| indicates whether
140 // device initialization succeeded.
142 // If the device is successfully initialized, runs the next pending task.
143 void OnInitCompleted(bool succeeded
);
145 // Called when GetFileInfo() succeeds. |file_info| specifies the
146 // requested file details. |success_callback| is invoked to notify the caller
147 // about the requested file details.
148 void OnDidGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
149 const base::File::Info
& file_info
);
151 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
152 // get the |dir_id| directory metadata details. |file_info| specifies the
153 // |dir_id| directory details.
155 // If |dir_id| is a directory, post a task on the UI thread to read the
156 // |dir_id| directory file entries.
158 // If |dir_id| is not a directory, |error_callback| is invoked to notify the
159 // caller about the file error and process the next pending request.
160 void OnDidGetFileInfoToReadDirectory(
162 const ReadDirectorySuccessCallback
& success_callback
,
163 const ErrorCallback
& error_callback
,
164 const base::File::Info
& file_info
);
166 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
167 // create the snapshot file of |snapshot_request_info.device_file_path|.
168 // |file_info| specifies the device file metadata details.
170 // Posts a task on the UI thread to copy the data contents of the device file
171 // to the snapshot file.
172 void OnDidGetFileInfoToCreateSnapshotFile(
173 scoped_ptr
<SnapshotRequestInfo
> snapshot_request_info
,
174 const base::File::Info
& file_info
);
176 // Called when ReadDirectory() succeeds.
178 // |dir_id| is the directory read.
179 // |file_list| contains the directory file entries with their file ids.
180 // |success_callback| is invoked to notify the caller about the directory
182 void OnDidReadDirectory(uint32 dir_id
,
183 const ReadDirectorySuccessCallback
& success_callback
,
184 const fileapi::AsyncFileUtil::EntryList
& file_list
);
186 // Called when WriteDataIntoSnapshotFile() succeeds.
188 // |snapshot_file_info| specifies the snapshot file metadata details.
190 // |current_snapshot_request_info_.success_callback| is invoked to notify the
191 // caller about |snapshot_file_info|.
192 void OnDidWriteDataIntoSnapshotFile(
193 const base::File::Info
& snapshot_file_info
,
194 const base::FilePath
& snapshot_file_path
);
196 // Called when WriteDataIntoSnapshotFile() fails.
198 // |error| specifies the file error code.
200 // |current_snapshot_request_info_.error_callback| is invoked to notify the
201 // caller about |error|.
202 void OnWriteDataIntoSnapshotFileError(base::File::Error error
);
204 // Called when ReadBytes() succeeds.
206 // |success_callback| is invoked to notify the caller about the read bytes.
207 // |bytes_read| is the number of bytes read.
208 void OnDidReadBytes(const ReadBytesSuccessCallback
& success_callback
,
209 const base::File::Info
& file_info
, int bytes_read
);
211 // Called when FillFileCache() succeeds.
212 void OnDidFillFileCache(const base::FilePath
& path
,
213 const fileapi::AsyncFileUtil::EntryList
& file_list
,
216 // Called when FillFileCache() fails.
217 void OnFillFileCacheFailed(base::File::Error error
);
219 // Handles the device file |error| while operating on |file_id|.
220 // |error_callback| is invoked to notify the caller about the file error.
221 void HandleDeviceFileError(const ErrorCallback
& error_callback
,
223 base::File::Error error
);
225 // Given a full path, returns a non-empty sub-path that needs to be read into
226 // the cache if such a uncached path exists.
227 // |cached_path| is the portion of |path| that has had cache lookup attempts.
228 base::FilePath
NextUncachedPathComponent(
229 const base::FilePath
& path
,
230 const base::FilePath
& cached_path
) const;
232 // Fills the file cache using the results from NextUncachedPathComponent().
233 void FillFileCache(const base::FilePath
& uncached_path
);
235 // Given a full path, if it exists in the cache, writes the file's id to |id|
237 bool CachedPathToId(const base::FilePath
& path
, uint32
* id
) const;
239 // MTP device initialization state.
240 InitializationState init_state_
;
242 // Used to make sure only one task is in progress at any time.
243 // Otherwise the browser will try to send too many requests at once and
244 // overload the device.
245 bool task_in_progress_
;
247 // Registered file system device path. This path does not
248 // correspond to a real device path (e.g. "/usb:2,2:81282").
249 const base::FilePath device_path_
;
251 // MTP device storage name (e.g. "usb:2,2:81282").
252 std::string storage_name_
;
254 // A list of pending tasks that needs to be run when the device is
255 // initialized or when the current task in progress is complete.
256 std::deque
<PendingTaskInfo
> pending_tasks_
;
258 // Used to track the current snapshot file request. A snapshot file is created
259 // incrementally. CreateSnapshotFile request reads the device file and writes
260 // to the snapshot file in chunks. In order to retain the order of the
261 // snapshot file requests, make sure there is only one active snapshot file
262 // request at any time.
263 scoped_ptr
<SnapshotRequestInfo
> current_snapshot_request_info_
;
265 // A mapping for quick lookups into the |root_node_| tree structure. Since
266 // |root_node_| contains pointers to this map, it must be declared after this
267 // so destruction happens in the right order.
268 FileIdToMTPFileNodeMap file_id_to_node_map_
;
270 // The root node of a tree-structure that caches the directory structure of
272 scoped_ptr
<MTPFileNode
> root_node_
;
274 // For callbacks that may run after destruction.
275 base::WeakPtrFactory
<MTPDeviceDelegateImplLinux
> weak_ptr_factory_
;
277 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux
);
280 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_