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 AddWatcher(const GURL
& origin
,
128 const base::FilePath
& file_path
,
129 const bool recursive
,
130 const storage::WatcherManager::StatusCallback
& callback
,
131 const storage::WatcherManager::NotificationCallback
&
132 notification_callback
) override
;
135 const base::FilePath
& file_path
,
136 const bool recursive
,
137 const storage::WatcherManager::StatusCallback
& callback
) override
;
138 void CancelPendingTasksAndDeleteDelegate() override
;
140 // The internal methods correspond to the similarly named methods above.
141 // The |root_node_| cache should be filled at this point.
142 virtual void GetFileInfoInternal(
143 const base::FilePath
& file_path
,
144 const GetFileInfoSuccessCallback
& success_callback
,
145 const ErrorCallback
& error_callback
);
146 virtual void CreateDirectoryInternal(
147 const std::vector
<base::FilePath
>& components
,
148 const bool exclusive
,
149 const CreateDirectorySuccessCallback
& success_callback
,
150 const ErrorCallback
& error_callback
);
151 virtual void ReadDirectoryInternal(
152 const base::FilePath
& root
,
153 const ReadDirectorySuccessCallback
& success_callback
,
154 const ErrorCallback
& error_callback
);
155 virtual void CreateSnapshotFileInternal(
156 const base::FilePath
& device_file_path
,
157 const base::FilePath
& local_path
,
158 const CreateSnapshotFileSuccessCallback
& success_callback
,
159 const ErrorCallback
& error_callback
);
160 virtual void ReadBytesInternal(
161 const base::FilePath
& device_file_path
,
162 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
163 const ReadBytesSuccessCallback
& success_callback
,
164 const ErrorCallback
& error_callback
);
165 virtual void MoveFileLocalInternal(
166 const base::FilePath
& source_file_path
,
167 const base::FilePath
& device_file_path
,
168 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
169 const MoveFileLocalSuccessCallback
& success_callback
,
170 const ErrorCallback
& error_callback
,
171 const base::File::Info
& source_file_info
);
172 virtual void OnDidOpenFDToCopyFileFromLocal(
173 const base::FilePath
& device_file_path
,
174 const CopyFileFromLocalSuccessCallback
& success_callback
,
175 const ErrorCallback
& error_callback
,
176 const std::pair
<int, base::File::Error
>& open_fd_result
);
177 virtual void DeleteFileInternal(
178 const base::FilePath
& file_path
,
179 const DeleteFileSuccessCallback
& success_callback
,
180 const ErrorCallback
& error_callback
,
181 const base::File::Info
& file_info
);
182 virtual void DeleteDirectoryInternal(
183 const base::FilePath
& file_path
,
184 const DeleteDirectorySuccessCallback
& success_callback
,
185 const ErrorCallback
& error_callback
,
186 const base::File::Info
& file_info
);
188 // Creates a single directory to |directory_path|. The caller must ensure that
189 // parent directory |directory_path.DirName()| already exists.
190 virtual void CreateSingleDirectory(
191 const base::FilePath
& directory_path
,
192 const bool exclusive
,
193 const CreateDirectorySuccessCallback
& success_callback
,
194 const ErrorCallback
& error_callback
);
196 // Called when ReadDirectoryInternal() completes for filling cache as part of
197 // creating directories.
198 virtual void OnDidReadDirectoryToCreateDirectory(
199 const std::vector
<base::FilePath
>& components
,
200 const bool exclusive
,
201 const CreateDirectorySuccessCallback
& success_callback
,
202 const ErrorCallback
& error_callback
,
203 const storage::AsyncFileUtil::EntryList
& /* file_list */,
204 const bool has_more
);
206 // Called when ReadDirectory succeeds.
207 virtual void OnDidReadDirectoryToDeleteDirectory(
208 const base::FilePath
& directory_path
,
209 const uint32 directory_id
,
210 const DeleteDirectorySuccessCallback
& success_callback
,
211 const ErrorCallback
& error_callback
,
212 const storage::AsyncFileUtil::EntryList
& entries
,
213 const bool has_more
);
215 // Calls DeleteObjectOnUIThread on UI thread.
216 virtual void RunDeleteObjectOnUIThread(
217 const base::FilePath
& object_path
,
218 const uint32 object_id
,
219 const DeleteObjectSuccessCallback
& success_callback
,
220 const ErrorCallback
& error_callback
);
222 // Notifies |chage_type| of |file_path| to watchers.
223 void NotifyFileChange(const base::FilePath
& file_path
,
224 const storage::WatcherManager::ChangeType change_type
);
226 // Ensures the device is initialized for communication.
227 // If the device is already initialized, call RunTask().
229 // If the device is uninitialized, store the |task_info| in a pending task
230 // queue and runs the pending tasks in the queue once the device is
231 // successfully initialized.
232 void EnsureInitAndRunTask(const PendingTaskInfo
& task_info
);
234 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs
235 // the task immediately.
236 // Otherwise, fills the cache first before running the task.
237 // |task_info.task| runs on the UI thread.
238 void RunTask(const PendingTaskInfo
& task_info
);
240 // Writes data from the device to the snapshot file path based on the
241 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
244 // |snapshot_file_info| specifies the metadata details of the snapshot file.
245 void WriteDataIntoSnapshotFile(const base::File::Info
& snapshot_file_info
);
247 // Marks the current request as complete and call ProcessNextPendingRequest().
248 void PendingRequestDone();
250 // Processes the next pending request.
251 void ProcessNextPendingRequest();
253 // Handles the device initialization event. |succeeded| indicates whether
254 // device initialization succeeded.
256 // If the device is successfully initialized, runs the next pending task.
257 void OnInitCompleted(bool succeeded
);
259 // Called when GetFileInfo() succeeds. |file_info| specifies the
260 // requested file details. |success_callback| is invoked to notify the caller
261 // about the requested file details.
262 void OnDidGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
263 const base::File::Info
& file_info
);
265 // Called when GetFileInfo() of |directory_path| succeeded at checking the
266 // path already exists.
267 void OnPathAlreadyExistsForCreateSingleDirectory(
268 const bool exclusive
,
269 const CreateDirectorySuccessCallback
& success_callback
,
270 const ErrorCallback
& error_callback
,
271 const base::File::Info
& file_info
);
273 // Called when GetFileInfo() of |directory_path| failed to check the path
275 void OnPathDoesNotExistForCreateSingleDirectory(
276 const base::FilePath
& directory_path
,
277 const CreateDirectorySuccessCallback
& success_callback
,
278 const ErrorCallback
& error_callback
,
279 const base::File::Error error
);
281 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
282 // get the |dir_id| directory metadata details. |file_info| specifies the
283 // |dir_id| directory details.
285 // If |dir_id| is a directory, post a task on the UI thread to read the
286 // |dir_id| directory file entries.
288 // If |dir_id| is not a directory, |error_callback| is invoked to notify the
289 // caller about the file error and process the next pending request.
290 void OnDidGetFileInfoToReadDirectory(
292 const ReadDirectorySuccessCallback
& success_callback
,
293 const ErrorCallback
& error_callback
,
294 const base::File::Info
& file_info
);
296 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
297 // create the snapshot file of |snapshot_request_info.device_file_path|.
298 // |file_info| specifies the device file metadata details.
300 // Posts a task on the UI thread to copy the data contents of the device file
301 // to the snapshot file.
302 void OnDidGetFileInfoToCreateSnapshotFile(
303 scoped_ptr
<SnapshotRequestInfo
> snapshot_request_info
,
304 const base::File::Info
& file_info
);
306 // Called when GetFileInfo() for destination path succeeded for a
307 // CopyFileFromLocal operation.
308 void OnDidGetDestFileInfoToCopyFileFromLocal(
309 const ErrorCallback
& error_callback
,
310 const base::File::Info
& file_info
);
312 // Called when GetFileInfo() for destination path failed to copy file from
314 void OnGetDestFileInfoErrorToCopyFileFromLocal(
315 const base::FilePath
& source_file_path
,
316 const base::FilePath
& device_file_path
,
317 const CopyFileFromLocalSuccessCallback
& success_callback
,
318 const ErrorCallback
& error_callback
,
319 const base::File::Error error
);
321 // Called when CreateSignleDirectory() succeeds.
322 void OnDidCreateSingleDirectory(
323 const base::FilePath
& directory_path
,
324 const CreateDirectorySuccessCallback
& success_callback
);
326 // Called when parent directory |created_directory| is created as part of
328 void OnDidCreateParentDirectoryToCreateDirectory(
329 const base::FilePath
& created_directory
,
330 const std::vector
<base::FilePath
>& components
,
331 const bool exclusive
,
332 const CreateDirectorySuccessCallback
& success_callback
,
333 const ErrorCallback
& error_callback
);
335 // Called when it failed to create a parent directory. For creating parent
336 // directories, all errors should be reported as FILE_ERROR_FAILED. This
337 // method wraps error callbacks of creating parent directories.
338 void OnCreateParentDirectoryErrorToCreateDirectory(
339 const ErrorCallback
& callback
,
340 const base::File::Error error
);
342 // Called when ReadDirectory() succeeds.
344 // |dir_id| is the directory read.
345 // |success_callback| is invoked to notify the caller about the directory
347 // |file_list| contains the directory file entries with their file ids.
348 // |has_more| is true if there are more file entries to read.
349 void OnDidReadDirectory(uint32 dir_id
,
350 const ReadDirectorySuccessCallback
& success_callback
,
351 const storage::AsyncFileUtil::EntryList
& file_list
,
354 // Called when WriteDataIntoSnapshotFile() succeeds.
356 // |snapshot_file_info| specifies the snapshot file metadata details.
358 // |current_snapshot_request_info_.success_callback| is invoked to notify the
359 // caller about |snapshot_file_info|.
360 void OnDidWriteDataIntoSnapshotFile(
361 const base::File::Info
& snapshot_file_info
,
362 const base::FilePath
& snapshot_file_path
);
364 // Called when WriteDataIntoSnapshotFile() fails.
366 // |error| specifies the file error code.
368 // |current_snapshot_request_info_.error_callback| is invoked to notify the
369 // caller about |error|.
370 void OnWriteDataIntoSnapshotFileError(base::File::Error error
);
372 // Called when ReadBytes() succeeds.
374 // |success_callback| is invoked to notify the caller about the read bytes.
375 // |bytes_read| is the number of bytes read.
376 void OnDidReadBytes(const ReadBytesSuccessCallback
& success_callback
,
377 const base::File::Info
& file_info
, int bytes_read
);
379 // Called when FillFileCache() succeeds.
380 void OnDidFillFileCache(const base::FilePath
& path
,
381 const storage::AsyncFileUtil::EntryList
& file_list
,
384 // Called when FillFileCache() fails.
385 void OnFillFileCacheFailed(base::File::Error error
);
387 // Called when CreateTemporaryFile() completes for CopyFileLocal.
388 void OnDidCreateTemporaryFileToCopyFileLocal(
389 const base::FilePath
& source_file_path
,
390 const base::FilePath
& device_file_path
,
391 const CopyFileProgressCallback
& progress_callback
,
392 const CopyFileLocalSuccessCallback
& success_callback
,
393 const ErrorCallback
& error_callback
,
394 const base::FilePath
& temporary_file_path
);
396 // Called when CreateSnapshotFile() succeeds for CopyFileLocal.
397 void OnDidCreateSnapshotFileOfCopyFileLocal(
398 const base::FilePath
& device_file_path
,
399 const CopyFileProgressCallback
& progress_callback
,
400 const CopyFileLocalSuccessCallback
& success_callback
,
401 const ErrorCallback
& error_callback
,
402 const base::File::Info
& file_info
,
403 const base::FilePath
& temporary_file_path
);
405 // Called when CopyFileFromLocal() succeeds for CopyFileLocal.
406 void OnDidCopyFileFromLocalOfCopyFileLocal(
407 const CopyFileFromLocalSuccessCallback success_callback
,
408 const base::FilePath
& temporary_file_path
);
410 // Called when MoveFileLocal() succeeds with rename operation.
411 void OnDidMoveFileLocalWithRename(
412 const MoveFileLocalSuccessCallback
& success_callback
,
413 const base::FilePath
& source_file_path
,
414 const uint32 file_id
);
416 // Called when CopyFileFromLocal() succeeds.
417 void OnDidCopyFileFromLocal(
418 const CopyFileFromLocalSuccessCallback
& success_callback
,
419 const base::FilePath
& file_path
,
420 const int source_file_descriptor
);
422 // Called when CopyFileLocal() fails.
423 void HandleCopyFileLocalError(const ErrorCallback
& error_callback
,
424 const base::FilePath
& temporary_file_path
,
425 const base::File::Error error
);
427 // Called when CopyFileFromLocal() fails.
428 void HandleCopyFileFromLocalError(const ErrorCallback
& error_callback
,
429 const int source_file_descriptor
,
430 base::File::Error error
);
432 // Called when DeleteObject() succeeds.
433 void OnDidDeleteObject(const base::FilePath
& object_path
,
434 const uint32 object_id
,
435 const DeleteObjectSuccessCallback success_callback
);
437 // Called when DeleteFileOrDirectory() fails.
438 void HandleDeleteFileOrDirectoryError(const ErrorCallback
& error_callback
,
439 base::File::Error error
);
441 // Handles the device file |error| while operating on |file_id|.
442 // |error_callback| is invoked to notify the caller about the file error.
443 void HandleDeviceFileError(const ErrorCallback
& error_callback
,
445 base::File::Error error
);
447 // Given a full path, returns a non-empty sub-path that needs to be read into
448 // the cache if such a uncached path exists.
449 // |cached_path| is the portion of |path| that has had cache lookup attempts.
450 base::FilePath
NextUncachedPathComponent(
451 const base::FilePath
& path
,
452 const base::FilePath
& cached_path
) const;
454 // Fills the file cache using the results from NextUncachedPathComponent().
455 void FillFileCache(const base::FilePath
& uncached_path
);
457 // Given a full path, if it exists in the cache, writes the file's id to |id|
459 bool CachedPathToId(const base::FilePath
& path
, uint32
* id
) const;
461 // Evict the cache of |id|.
462 void EvictCachedPathToId(const uint32 id
);
464 // MTP device initialization state.
465 InitializationState init_state_
;
467 // Used to make sure only one task is in progress at any time.
468 // Otherwise the browser will try to send too many requests at once and
469 // overload the device.
470 bool task_in_progress_
;
472 // Registered file system device path. This path does not
473 // correspond to a real device path (e.g. "/usb:2,2:81282").
474 const base::FilePath device_path_
;
476 // MTP device storage name (e.g. "usb:2,2:81282").
477 std::string storage_name_
;
479 // Mode for opening storage.
480 const bool read_only_
;
482 // Maps for holding notification callbacks.
483 typedef std::map
<GURL
, storage::WatcherManager::NotificationCallback
>
484 OriginNotificationCallbackMap
;
485 typedef std::map
<base::FilePath
, OriginNotificationCallbackMap
> Subscribers
;
486 Subscribers subscribers_
;
488 // A list of pending tasks that needs to be run when the device is
489 // initialized or when the current task in progress is complete.
490 std::deque
<PendingTaskInfo
> pending_tasks_
;
492 // Used to track the current snapshot file request. A snapshot file is created
493 // incrementally. CreateSnapshotFile request reads the device file and writes
494 // to the snapshot file in chunks. In order to retain the order of the
495 // snapshot file requests, make sure there is only one active snapshot file
496 // request at any time.
497 scoped_ptr
<SnapshotRequestInfo
> current_snapshot_request_info_
;
499 // A mapping for quick lookups into the |root_node_| tree structure. Since
500 // |root_node_| contains pointers to this map, it must be declared after this
501 // so destruction happens in the right order.
502 FileIdToMTPFileNodeMap file_id_to_node_map_
;
504 // The root node of a tree-structure that caches the directory structure of
506 scoped_ptr
<MTPFileNode
> root_node_
;
508 // A list of child nodes encountered while a ReadDirectory operation, which
509 // can return results over multiple callbacks, is in progress.
510 std::set
<std::string
> child_nodes_seen_
;
512 // A cache to store file metadata for file entries read during a ReadDirectory
513 // operation. Used to service incoming GetFileInfo calls for the duration of
514 // the ReadDirectory operation.
515 FileInfoCache file_info_cache_
;
517 // For callbacks that may run after destruction.
518 base::WeakPtrFactory
<MTPDeviceDelegateImplLinux
> weak_ptr_factory_
;
520 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux
);
523 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_