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_
13 #include "base/callback.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/files/file_path.h"
16 #include "base/location.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "webkit/browser/fileapi/async_file_util.h"
23 struct SnapshotRequestInfo;
25 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol
26 // (MTP) device to complete file system operations. These operations are
27 // performed asynchronously. Instantiate this class per MTP device storage.
28 // MTPDeviceDelegateImplLinux lives on the IO thread.
29 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
30 // to dispatch the requests to MediaTransferProtocolManager.
31 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
33 friend void CreateMTPDeviceAsyncDelegate(
35 const CreateMTPDeviceAsyncDelegateCallback&);
37 enum InitializationState {
43 // Used to represent pending task details.
44 struct PendingTaskInfo {
45 PendingTaskInfo(const base::FilePath& path,
46 content::BrowserThread::ID thread_id,
47 const tracked_objects::Location& location,
48 const base::Closure& task);
52 base::FilePath cached_path;
53 const content::BrowserThread::ID thread_id;
54 const tracked_objects::Location location;
55 const base::Closure task;
60 // Maps file ids to file nodes.
61 typedef std::map<uint32, MTPFileNode*> FileIdToMTPFileNodeMap;
63 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
64 // Defer the device initializations until the first file operation request.
65 // Do all the initializations in EnsureInitAndRunTask() function.
66 explicit MTPDeviceDelegateImplLinux(const std::string& device_location);
68 // Destructed via CancelPendingTasksAndDeleteDelegate().
69 virtual ~MTPDeviceDelegateImplLinux();
71 // MTPDeviceAsyncDelegate:
72 virtual void GetFileInfo(const base::FilePath& file_path,
73 const GetFileInfoSuccessCallback& success_callback,
74 const ErrorCallback& error_callback) OVERRIDE;
75 virtual void ReadDirectory(
76 const base::FilePath& root,
77 const ReadDirectorySuccessCallback& success_callback,
78 const ErrorCallback& error_callback) OVERRIDE;
79 virtual void CreateSnapshotFile(
80 const base::FilePath& device_file_path,
81 const base::FilePath& local_path,
82 const CreateSnapshotFileSuccessCallback& success_callback,
83 const ErrorCallback& error_callback) OVERRIDE;
84 virtual bool IsStreaming() OVERRIDE;
85 virtual void ReadBytes(
86 const base::FilePath& device_file_path,
87 net::IOBuffer* buf, int64 offset, int buf_len,
88 const ReadBytesSuccessCallback& success_callback,
89 const ErrorCallback& error_callback) OVERRIDE;
90 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
92 // The internal methods correspond to the similarly named methods above.
93 // The |root_node_| cache should be filled at this point.
94 virtual void GetFileInfoInternal(
95 const base::FilePath& file_path,
96 const GetFileInfoSuccessCallback& success_callback,
97 const ErrorCallback& error_callback);
98 virtual void ReadDirectoryInternal(
99 const base::FilePath& root,
100 const ReadDirectorySuccessCallback& success_callback,
101 const ErrorCallback& error_callback);
102 virtual void CreateSnapshotFileInternal(
103 const base::FilePath& device_file_path,
104 const base::FilePath& local_path,
105 const CreateSnapshotFileSuccessCallback& success_callback,
106 const ErrorCallback& error_callback);
107 virtual void ReadBytesInternal(
108 const base::FilePath& device_file_path,
109 net::IOBuffer* buf, int64 offset, int buf_len,
110 const ReadBytesSuccessCallback& success_callback,
111 const ErrorCallback& error_callback);
113 // Ensures the device is initialized for communication.
114 // If the device is already initialized, call RunTask().
116 // If the device is uninitialized, store the |task_info| in a pending task
117 // queue and runs the pending tasks in the queue once the device is
118 // successfully initialized.
119 void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
121 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs
122 // the task immediately.
123 // Otherwise, fills the cache first before running the task.
124 // |task_info.task| runs on the UI thread.
125 void RunTask(const PendingTaskInfo& task_info);
127 // Writes data from the device to the snapshot file path based on the
128 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
131 // |snapshot_file_info| specifies the metadata details of the snapshot file.
132 void WriteDataIntoSnapshotFile(const base::File::Info& snapshot_file_info);
134 // Marks the current request as complete and call ProcessNextPendingRequest().
135 void PendingRequestDone();
137 // Processes the next pending request.
138 void ProcessNextPendingRequest();
140 // Handles the device initialization event. |succeeded| indicates whether
141 // device initialization succeeded.
143 // If the device is successfully initialized, runs the next pending task.
144 void OnInitCompleted(bool succeeded);
146 // Called when GetFileInfo() succeeds. |file_info| specifies the
147 // requested file details. |success_callback| is invoked to notify the caller
148 // about the requested file details.
149 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
150 const base::File::Info& file_info);
152 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
153 // get the |dir_id| directory metadata details. |file_info| specifies the
154 // |dir_id| directory details.
156 // If |dir_id| is a directory, post a task on the UI thread to read the
157 // |dir_id| directory file entries.
159 // If |dir_id| is not a directory, |error_callback| is invoked to notify the
160 // caller about the file error and process the next pending request.
161 void OnDidGetFileInfoToReadDirectory(
163 const ReadDirectorySuccessCallback& success_callback,
164 const ErrorCallback& error_callback,
165 const base::File::Info& file_info);
167 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
168 // create the snapshot file of |snapshot_request_info.device_file_path|.
169 // |file_info| specifies the device file metadata details.
171 // Posts a task on the UI thread to copy the data contents of the device file
172 // to the snapshot file.
173 void OnDidGetFileInfoToCreateSnapshotFile(
174 scoped_ptr<SnapshotRequestInfo> snapshot_request_info,
175 const base::File::Info& file_info);
177 // Called when ReadDirectory() succeeds.
179 // |dir_id| is the directory read.
180 // |success_callback| is invoked to notify the caller about the directory
182 // |file_list| contains the directory file entries with their file ids.
183 // |has_more| is true if there are more file entries to read.
184 void OnDidReadDirectory(uint32 dir_id,
185 const ReadDirectorySuccessCallback& success_callback,
186 const fileapi::AsyncFileUtil::EntryList& file_list,
189 // Called when WriteDataIntoSnapshotFile() succeeds.
191 // |snapshot_file_info| specifies the snapshot file metadata details.
193 // |current_snapshot_request_info_.success_callback| is invoked to notify the
194 // caller about |snapshot_file_info|.
195 void OnDidWriteDataIntoSnapshotFile(
196 const base::File::Info& snapshot_file_info,
197 const base::FilePath& snapshot_file_path);
199 // Called when WriteDataIntoSnapshotFile() fails.
201 // |error| specifies the file error code.
203 // |current_snapshot_request_info_.error_callback| is invoked to notify the
204 // caller about |error|.
205 void OnWriteDataIntoSnapshotFileError(base::File::Error error);
207 // Called when ReadBytes() succeeds.
209 // |success_callback| is invoked to notify the caller about the read bytes.
210 // |bytes_read| is the number of bytes read.
211 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
212 const base::File::Info& file_info, int bytes_read);
214 // Called when FillFileCache() succeeds.
215 void OnDidFillFileCache(const base::FilePath& path,
216 const fileapi::AsyncFileUtil::EntryList& file_list,
219 // Called when FillFileCache() fails.
220 void OnFillFileCacheFailed(base::File::Error error);
222 // Handles the device file |error| while operating on |file_id|.
223 // |error_callback| is invoked to notify the caller about the file error.
224 void HandleDeviceFileError(const ErrorCallback& error_callback,
226 base::File::Error error);
228 // Given a full path, returns a non-empty sub-path that needs to be read into
229 // the cache if such a uncached path exists.
230 // |cached_path| is the portion of |path| that has had cache lookup attempts.
231 base::FilePath NextUncachedPathComponent(
232 const base::FilePath& path,
233 const base::FilePath& cached_path) const;
235 // Fills the file cache using the results from NextUncachedPathComponent().
236 void FillFileCache(const base::FilePath& uncached_path);
238 // Given a full path, if it exists in the cache, writes the file's id to |id|
240 bool CachedPathToId(const base::FilePath& path, uint32* id) const;
242 // MTP device initialization state.
243 InitializationState init_state_;
245 // Used to make sure only one task is in progress at any time.
246 // Otherwise the browser will try to send too many requests at once and
247 // overload the device.
248 bool task_in_progress_;
250 // Registered file system device path. This path does not
251 // correspond to a real device path (e.g. "/usb:2,2:81282").
252 const base::FilePath device_path_;
254 // MTP device storage name (e.g. "usb:2,2:81282").
255 std::string storage_name_;
257 // A list of pending tasks that needs to be run when the device is
258 // initialized or when the current task in progress is complete.
259 std::deque<PendingTaskInfo> pending_tasks_;
261 // Used to track the current snapshot file request. A snapshot file is created
262 // incrementally. CreateSnapshotFile request reads the device file and writes
263 // to the snapshot file in chunks. In order to retain the order of the
264 // snapshot file requests, make sure there is only one active snapshot file
265 // request at any time.
266 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_;
268 // A mapping for quick lookups into the |root_node_| tree structure. Since
269 // |root_node_| contains pointers to this map, it must be declared after this
270 // so destruction happens in the right order.
271 FileIdToMTPFileNodeMap file_id_to_node_map_;
273 // The root node of a tree-structure that caches the directory structure of
275 scoped_ptr<MTPFileNode> root_node_;
277 // A list of child nodes encountered while a ReadDirectory operation, which
278 // can return results over multiple callbacks, is in progress.
279 std::set<std::string> child_nodes_seen_;
281 // For callbacks that may run after destruction.
282 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
284 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
287 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_