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 "storage/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(
36 const CreateMTPDeviceAsyncDelegateCallback
&);
38 enum InitializationState
{
44 // Used to represent pending task details.
45 struct PendingTaskInfo
{
46 PendingTaskInfo(const base::FilePath
& path
,
47 content::BrowserThread::ID thread_id
,
48 const tracked_objects::Location
& location
,
49 const base::Closure
& task
);
53 base::FilePath cached_path
;
54 const content::BrowserThread::ID thread_id
;
55 const tracked_objects::Location location
;
56 const base::Closure task
;
61 // Maps file ids to file nodes.
62 typedef std::map
<uint32
, MTPFileNode
*> FileIdToMTPFileNodeMap
;
64 // Maps file paths to file info.
65 typedef std::map
<base::FilePath
, storage::DirectoryEntry
> FileInfoCache
;
67 typedef base::Closure DeleteObjectSuccessCallback
;
69 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
70 // Defer the device initializations until the first file operation request.
71 // Do all the initializations in EnsureInitAndRunTask() function.
72 MTPDeviceDelegateImplLinux(const std::string
& device_location
,
73 const bool read_only
);
75 // Destructed via CancelPendingTasksAndDeleteDelegate().
76 ~MTPDeviceDelegateImplLinux() override
;
78 // MTPDeviceAsyncDelegate:
79 void GetFileInfo(const base::FilePath
& file_path
,
80 const GetFileInfoSuccessCallback
& success_callback
,
81 const ErrorCallback
& error_callback
) override
;
82 void CreateDirectory(const base::FilePath
& directory_path
,
85 const CreateDirectorySuccessCallback
& success_callback
,
86 const ErrorCallback
& error_callback
) override
;
87 void ReadDirectory(const base::FilePath
& root
,
88 const ReadDirectorySuccessCallback
& success_callback
,
89 const ErrorCallback
& error_callback
) override
;
90 void CreateSnapshotFile(
91 const base::FilePath
& device_file_path
,
92 const base::FilePath
& local_path
,
93 const CreateSnapshotFileSuccessCallback
& success_callback
,
94 const ErrorCallback
& error_callback
) override
;
95 bool IsStreaming() override
;
96 void ReadBytes(const base::FilePath
& device_file_path
,
97 const scoped_refptr
<net::IOBuffer
>& buf
,
100 const ReadBytesSuccessCallback
& success_callback
,
101 const ErrorCallback
& error_callback
) override
;
102 bool IsReadOnly() const override
;
104 const base::FilePath
& source_file_path
,
105 const base::FilePath
& device_file_path
,
106 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
107 const CopyFileProgressCallback
& progress_callback
,
108 const CopyFileLocalSuccessCallback
& success_callback
,
109 const ErrorCallback
& error_callback
) override
;
111 const base::FilePath
& source_file_path
,
112 const base::FilePath
& device_file_path
,
113 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
114 const MoveFileLocalSuccessCallback
& success_callback
,
115 const ErrorCallback
& error_callback
) override
;
116 void CopyFileFromLocal(
117 const base::FilePath
& source_file_path
,
118 const base::FilePath
& device_file_path
,
119 const CopyFileFromLocalSuccessCallback
& success_callback
,
120 const ErrorCallback
& error_callback
) override
;
121 void DeleteFile(const base::FilePath
& file_path
,
122 const DeleteFileSuccessCallback
& success_callback
,
123 const ErrorCallback
& error_callback
) override
;
124 void DeleteDirectory(const base::FilePath
& file_path
,
125 const DeleteDirectorySuccessCallback
& success_callback
,
126 const ErrorCallback
& error_callback
) override
;
127 void CancelPendingTasksAndDeleteDelegate() override
;
129 // The internal methods correspond to the similarly named methods above.
130 // The |root_node_| cache should be filled at this point.
131 virtual void GetFileInfoInternal(
132 const base::FilePath
& file_path
,
133 const GetFileInfoSuccessCallback
& success_callback
,
134 const ErrorCallback
& error_callback
);
135 virtual void CreateDirectoryInternal(
136 const std::vector
<base::FilePath
>& components
,
137 const bool exclusive
,
138 const CreateDirectorySuccessCallback
& success_callback
,
139 const ErrorCallback
& error_callback
);
140 virtual void ReadDirectoryInternal(
141 const base::FilePath
& root
,
142 const ReadDirectorySuccessCallback
& success_callback
,
143 const ErrorCallback
& error_callback
);
144 virtual void CreateSnapshotFileInternal(
145 const base::FilePath
& device_file_path
,
146 const base::FilePath
& local_path
,
147 const CreateSnapshotFileSuccessCallback
& success_callback
,
148 const ErrorCallback
& error_callback
);
149 virtual void ReadBytesInternal(
150 const base::FilePath
& device_file_path
,
151 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
152 const ReadBytesSuccessCallback
& success_callback
,
153 const ErrorCallback
& error_callback
);
154 virtual void MoveFileLocalInternal(
155 const base::FilePath
& source_file_path
,
156 const base::FilePath
& device_file_path
,
157 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
158 const MoveFileLocalSuccessCallback
& success_callback
,
159 const ErrorCallback
& error_callback
,
160 const base::File::Info
& source_file_info
);
161 virtual void OnDidOpenFDToCopyFileFromLocal(
162 const base::FilePath
& device_file_path
,
163 const CopyFileFromLocalSuccessCallback
& success_callback
,
164 const ErrorCallback
& error_callback
,
165 const std::pair
<int, base::File::Error
>& open_fd_result
);
166 virtual void DeleteFileInternal(
167 const base::FilePath
& file_path
,
168 const DeleteFileSuccessCallback
& success_callback
,
169 const ErrorCallback
& error_callback
,
170 const base::File::Info
& file_info
);
171 virtual void DeleteDirectoryInternal(
172 const base::FilePath
& file_path
,
173 const DeleteDirectorySuccessCallback
& success_callback
,
174 const ErrorCallback
& error_callback
,
175 const base::File::Info
& file_info
);
177 // Creates a single directory to |directory_path|. The caller must ensure that
178 // parent directory |directory_path.DirName()| already exists.
179 virtual void CreateSingleDirectory(
180 const base::FilePath
& directory_path
,
181 const bool exclusive
,
182 const CreateDirectorySuccessCallback
& success_callback
,
183 const ErrorCallback
& error_callback
);
185 // Called when ReadDirectoryInternal() completes for filling cache as part of
186 // creating directories.
187 virtual void OnDidReadDirectoryToCreateDirectory(
188 const std::vector
<base::FilePath
>& components
,
189 const bool exclusive
,
190 const CreateDirectorySuccessCallback
& success_callback
,
191 const ErrorCallback
& error_callback
,
192 const storage::AsyncFileUtil::EntryList
& /* file_list */,
193 const bool has_more
);
195 // Called when ReadDirectory succeeds.
196 virtual void OnDidReadDirectoryToDeleteDirectory(
197 const uint32 directory_id
,
198 const DeleteDirectorySuccessCallback
& success_callback
,
199 const ErrorCallback
& error_callback
,
200 const storage::AsyncFileUtil::EntryList
& entries
,
201 const bool has_more
);
203 // Calls DeleteObjectOnUIThread on UI thread.
204 virtual void RunDeleteObjectOnUIThread(
205 const uint32 object_id
,
206 const DeleteObjectSuccessCallback
& success_callback
,
207 const ErrorCallback
& error_callback
);
209 // Ensures the device is initialized for communication.
210 // If the device is already initialized, call RunTask().
212 // If the device is uninitialized, store the |task_info| in a pending task
213 // queue and runs the pending tasks in the queue once the device is
214 // successfully initialized.
215 void EnsureInitAndRunTask(const PendingTaskInfo
& task_info
);
217 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs
218 // the task immediately.
219 // Otherwise, fills the cache first before running the task.
220 // |task_info.task| runs on the UI thread.
221 void RunTask(const PendingTaskInfo
& task_info
);
223 // Writes data from the device to the snapshot file path based on the
224 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
227 // |snapshot_file_info| specifies the metadata details of the snapshot file.
228 void WriteDataIntoSnapshotFile(const base::File::Info
& snapshot_file_info
);
230 // Marks the current request as complete and call ProcessNextPendingRequest().
231 void PendingRequestDone();
233 // Processes the next pending request.
234 void ProcessNextPendingRequest();
236 // Handles the device initialization event. |succeeded| indicates whether
237 // device initialization succeeded.
239 // If the device is successfully initialized, runs the next pending task.
240 void OnInitCompleted(bool succeeded
);
242 // Called when GetFileInfo() succeeds. |file_info| specifies the
243 // requested file details. |success_callback| is invoked to notify the caller
244 // about the requested file details.
245 void OnDidGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
246 const base::File::Info
& file_info
);
248 // Called when GetFileInfo() of |directory_path| succeeded at checking the
249 // path already exists.
250 void OnPathAlreadyExistsForCreateSingleDirectory(
251 const bool exclusive
,
252 const CreateDirectorySuccessCallback
& success_callback
,
253 const ErrorCallback
& error_callback
,
254 const base::File::Info
& file_info
);
256 // Called when GetFileInfo() of |directory_path| failed to check the path
258 void OnPathDoesNotExistForCreateSingleDirectory(
259 const base::FilePath
& directory_path
,
260 const CreateDirectorySuccessCallback
& success_callback
,
261 const ErrorCallback
& error_callback
,
262 const base::File::Error error
);
264 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
265 // get the |dir_id| directory metadata details. |file_info| specifies the
266 // |dir_id| directory details.
268 // If |dir_id| is a directory, post a task on the UI thread to read the
269 // |dir_id| directory file entries.
271 // If |dir_id| is not a directory, |error_callback| is invoked to notify the
272 // caller about the file error and process the next pending request.
273 void OnDidGetFileInfoToReadDirectory(
275 const ReadDirectorySuccessCallback
& success_callback
,
276 const ErrorCallback
& error_callback
,
277 const base::File::Info
& file_info
);
279 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
280 // create the snapshot file of |snapshot_request_info.device_file_path|.
281 // |file_info| specifies the device file metadata details.
283 // Posts a task on the UI thread to copy the data contents of the device file
284 // to the snapshot file.
285 void OnDidGetFileInfoToCreateSnapshotFile(
286 scoped_ptr
<SnapshotRequestInfo
> snapshot_request_info
,
287 const base::File::Info
& file_info
);
289 // Called when GetFileInfo() for destination path succeeded for a
290 // CopyFileFromLocal operation.
291 void OnDidGetDestFileInfoToCopyFileFromLocal(
292 const ErrorCallback
& error_callback
,
293 const base::File::Info
& file_info
);
295 // Called when GetFileInfo() for destination path failed to copy file from
297 void OnGetDestFileInfoErrorToCopyFileFromLocal(
298 const base::FilePath
& source_file_path
,
299 const base::FilePath
& device_file_path
,
300 const CopyFileFromLocalSuccessCallback
& success_callback
,
301 const ErrorCallback
& error_callback
,
302 const base::File::Error error
);
304 // Called when CreateSignleDirectory() succeeds.
305 void OnDidCreateSingleDirectory(
306 const CreateDirectorySuccessCallback
& success_callback
);
308 // Called when parent directory |created_directory| is created as part of
310 void OnDidCreateParentDirectoryToCreateDirectory(
311 const base::FilePath
& created_directory
,
312 const std::vector
<base::FilePath
>& components
,
313 const bool exclusive
,
314 const CreateDirectorySuccessCallback
& success_callback
,
315 const ErrorCallback
& error_callback
);
317 // Called when it failed to create a parent directory. For creating parent
318 // directories, all errors should be reported as FILE_ERROR_FAILED. This
319 // method wraps error callbacks of creating parent directories.
320 void OnCreateParentDirectoryErrorToCreateDirectory(
321 const ErrorCallback
& callback
,
322 const base::File::Error error
);
324 // Called when ReadDirectory() succeeds.
326 // |dir_id| is the directory read.
327 // |success_callback| is invoked to notify the caller about the directory
329 // |file_list| contains the directory file entries with their file ids.
330 // |has_more| is true if there are more file entries to read.
331 void OnDidReadDirectory(uint32 dir_id
,
332 const ReadDirectorySuccessCallback
& success_callback
,
333 const storage::AsyncFileUtil::EntryList
& file_list
,
336 // Called when WriteDataIntoSnapshotFile() succeeds.
338 // |snapshot_file_info| specifies the snapshot file metadata details.
340 // |current_snapshot_request_info_.success_callback| is invoked to notify the
341 // caller about |snapshot_file_info|.
342 void OnDidWriteDataIntoSnapshotFile(
343 const base::File::Info
& snapshot_file_info
,
344 const base::FilePath
& snapshot_file_path
);
346 // Called when WriteDataIntoSnapshotFile() fails.
348 // |error| specifies the file error code.
350 // |current_snapshot_request_info_.error_callback| is invoked to notify the
351 // caller about |error|.
352 void OnWriteDataIntoSnapshotFileError(base::File::Error error
);
354 // Called when ReadBytes() succeeds.
356 // |success_callback| is invoked to notify the caller about the read bytes.
357 // |bytes_read| is the number of bytes read.
358 void OnDidReadBytes(const ReadBytesSuccessCallback
& success_callback
,
359 const base::File::Info
& file_info
, int bytes_read
);
361 // Called when FillFileCache() succeeds.
362 void OnDidFillFileCache(const base::FilePath
& path
,
363 const storage::AsyncFileUtil::EntryList
& file_list
,
366 // Called when FillFileCache() fails.
367 void OnFillFileCacheFailed(base::File::Error error
);
369 // Called when CreateTemporaryFile() completes for CopyFileLocal.
370 void OnDidCreateTemporaryFileToCopyFileLocal(
371 const base::FilePath
& source_file_path
,
372 const base::FilePath
& device_file_path
,
373 const CopyFileProgressCallback
& progress_callback
,
374 const CopyFileLocalSuccessCallback
& success_callback
,
375 const ErrorCallback
& error_callback
,
376 const base::FilePath
& temporary_file_path
);
378 // Called when CreateSnapshotFile() succeeds for CopyFileLocal.
379 void OnDidCreateSnapshotFileOfCopyFileLocal(
380 const base::FilePath
& device_file_path
,
381 const CopyFileProgressCallback
& progress_callback
,
382 const CopyFileLocalSuccessCallback
& success_callback
,
383 const ErrorCallback
& error_callback
,
384 const base::File::Info
& file_info
,
385 const base::FilePath
& temporary_file_path
);
387 // Called when CopyFileFromLocal() succeeds for CopyFileLocal.
388 void OnDidCopyFileFromLocalOfCopyFileLocal(
389 const CopyFileFromLocalSuccessCallback success_callback
,
390 const base::FilePath
& temporary_file_path
);
392 // Called when MoveFileLocal() succeeds with rename operation.
393 void OnDidMoveFileLocalWithRename(
394 const MoveFileLocalSuccessCallback
& success_callback
,
395 const uint32 file_id
);
397 // Called when CopyFileFromLocal() succeeds.
398 void OnDidCopyFileFromLocal(
399 const CopyFileFromLocalSuccessCallback
& success_callback
,
400 const int source_file_descriptor
);
402 // Called when CopyFileLocal() fails.
403 void HandleCopyFileLocalError(const ErrorCallback
& error_callback
,
404 const base::FilePath
& temporary_file_path
,
405 const base::File::Error error
);
407 // Called when CopyFileFromLocal() fails.
408 void HandleCopyFileFromLocalError(const ErrorCallback
& error_callback
,
409 const int source_file_descriptor
,
410 base::File::Error error
);
412 // Called when DeleteObject() succeeds.
413 void OnDidDeleteObject(const uint32 object_id
,
414 const DeleteObjectSuccessCallback success_callback
);
416 // Called when DeleteFileOrDirectory() fails.
417 void HandleDeleteFileOrDirectoryError(const ErrorCallback
& error_callback
,
418 base::File::Error error
);
420 // Handles the device file |error| while operating on |file_id|.
421 // |error_callback| is invoked to notify the caller about the file error.
422 void HandleDeviceFileError(const ErrorCallback
& error_callback
,
424 base::File::Error error
);
426 // Given a full path, returns a non-empty sub-path that needs to be read into
427 // the cache if such a uncached path exists.
428 // |cached_path| is the portion of |path| that has had cache lookup attempts.
429 base::FilePath
NextUncachedPathComponent(
430 const base::FilePath
& path
,
431 const base::FilePath
& cached_path
) const;
433 // Fills the file cache using the results from NextUncachedPathComponent().
434 void FillFileCache(const base::FilePath
& uncached_path
);
436 // Given a full path, if it exists in the cache, writes the file's id to |id|
438 bool CachedPathToId(const base::FilePath
& path
, uint32
* id
) const;
440 // Evict the cache of |id|.
441 void EvictCachedPathToId(const uint32 id
);
443 // MTP device initialization state.
444 InitializationState init_state_
;
446 // Used to make sure only one task is in progress at any time.
447 // Otherwise the browser will try to send too many requests at once and
448 // overload the device.
449 bool task_in_progress_
;
451 // Registered file system device path. This path does not
452 // correspond to a real device path (e.g. "/usb:2,2:81282").
453 const base::FilePath device_path_
;
455 // MTP device storage name (e.g. "usb:2,2:81282").
456 std::string storage_name_
;
458 // Mode for opening storage.
459 const bool read_only_
;
461 // A list of pending tasks that needs to be run when the device is
462 // initialized or when the current task in progress is complete.
463 std::deque
<PendingTaskInfo
> pending_tasks_
;
465 // Used to track the current snapshot file request. A snapshot file is created
466 // incrementally. CreateSnapshotFile request reads the device file and writes
467 // to the snapshot file in chunks. In order to retain the order of the
468 // snapshot file requests, make sure there is only one active snapshot file
469 // request at any time.
470 scoped_ptr
<SnapshotRequestInfo
> current_snapshot_request_info_
;
472 // A mapping for quick lookups into the |root_node_| tree structure. Since
473 // |root_node_| contains pointers to this map, it must be declared after this
474 // so destruction happens in the right order.
475 FileIdToMTPFileNodeMap file_id_to_node_map_
;
477 // The root node of a tree-structure that caches the directory structure of
479 scoped_ptr
<MTPFileNode
> root_node_
;
481 // A list of child nodes encountered while a ReadDirectory operation, which
482 // can return results over multiple callbacks, is in progress.
483 std::set
<std::string
> child_nodes_seen_
;
485 // A cache to store file metadata for file entries read during a ReadDirectory
486 // operation. Used to service incoming GetFileInfo calls for the duration of
487 // the ReadDirectory operation.
488 FileInfoCache file_info_cache_
;
490 // For callbacks that may run after destruction.
491 base::WeakPtrFactory
<MTPDeviceDelegateImplLinux
> weak_ptr_factory_
;
493 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux
);
496 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_