Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h

Issue 2358493002: Remove MTP support on Linux. (Closed)
Patch Set: move files Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
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_
7
8 #include <stdint.h>
9
10 #include <deque>
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <string>
15
16 #include "base/callback.h"
17 #include "base/containers/scoped_ptr_hash_map.h"
18 #include "base/files/file_path.h"
19 #include "base/location.h"
20 #include "base/macros.h"
21 #include "base/memory/weak_ptr.h"
22 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
23 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "storage/browser/fileapi/async_file_util.h"
26
27 struct SnapshotRequestInfo;
28
29 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol
30 // (MTP) device to complete file system operations. These operations are
31 // performed asynchronously. Instantiate this class per MTP device storage.
32 // MTPDeviceDelegateImplLinux lives on the IO thread.
33 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
34 // to dispatch the requests to MediaTransferProtocolManager.
35 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
36 private:
37 friend void CreateMTPDeviceAsyncDelegate(
38 const std::string&,
39 const bool read_only,
40 const CreateMTPDeviceAsyncDelegateCallback&);
41
42 enum InitializationState {
43 UNINITIALIZED = 0,
44 PENDING_INIT,
45 INITIALIZED
46 };
47
48 // Used to represent pending task details.
49 struct PendingTaskInfo {
50 PendingTaskInfo(const base::FilePath& path,
51 content::BrowserThread::ID thread_id,
52 const tracked_objects::Location& location,
53 const base::Closure& task);
54 PendingTaskInfo(const PendingTaskInfo& other);
55 ~PendingTaskInfo();
56
57 base::FilePath path;
58 base::FilePath cached_path;
59 const content::BrowserThread::ID thread_id;
60 const tracked_objects::Location location;
61 const base::Closure task;
62 };
63
64 class MTPFileNode;
65
66 // Maps file ids to file nodes.
67 typedef std::map<uint32_t, MTPFileNode*> FileIdToMTPFileNodeMap;
68
69 // Maps file paths to file info.
70 typedef std::map<base::FilePath, MTPDeviceTaskHelper::MTPEntry> FileInfoCache;
71
72 typedef base::Closure DeleteObjectSuccessCallback;
73
74 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
75 // Defer the device initializations until the first file operation request.
76 // Do all the initializations in EnsureInitAndRunTask() function.
77 MTPDeviceDelegateImplLinux(const std::string& device_location,
78 const bool read_only);
79
80 // Destructed via CancelPendingTasksAndDeleteDelegate().
81 ~MTPDeviceDelegateImplLinux() override;
82
83 // MTPDeviceAsyncDelegate:
84 void GetFileInfo(const base::FilePath& file_path,
85 const GetFileInfoSuccessCallback& success_callback,
86 const ErrorCallback& error_callback) override;
87 void CreateDirectory(const base::FilePath& directory_path,
88 const bool exclusive,
89 const bool recursive,
90 const CreateDirectorySuccessCallback& success_callback,
91 const ErrorCallback& error_callback) override;
92 void ReadDirectory(const base::FilePath& root,
93 const ReadDirectorySuccessCallback& success_callback,
94 const ErrorCallback& error_callback) override;
95 void CreateSnapshotFile(
96 const base::FilePath& device_file_path,
97 const base::FilePath& local_path,
98 const CreateSnapshotFileSuccessCallback& success_callback,
99 const ErrorCallback& error_callback) override;
100 bool IsStreaming() override;
101 void ReadBytes(const base::FilePath& device_file_path,
102 const scoped_refptr<net::IOBuffer>& buf,
103 int64_t offset,
104 int buf_len,
105 const ReadBytesSuccessCallback& success_callback,
106 const ErrorCallback& error_callback) override;
107 bool IsReadOnly() const override;
108 void CopyFileLocal(
109 const base::FilePath& source_file_path,
110 const base::FilePath& device_file_path,
111 const CreateTemporaryFileCallback& create_temporary_file_callback,
112 const CopyFileProgressCallback& progress_callback,
113 const CopyFileLocalSuccessCallback& success_callback,
114 const ErrorCallback& error_callback) override;
115 void MoveFileLocal(
116 const base::FilePath& source_file_path,
117 const base::FilePath& device_file_path,
118 const CreateTemporaryFileCallback& create_temporary_file_callback,
119 const MoveFileLocalSuccessCallback& success_callback,
120 const ErrorCallback& error_callback) override;
121 void CopyFileFromLocal(
122 const base::FilePath& source_file_path,
123 const base::FilePath& device_file_path,
124 const CopyFileFromLocalSuccessCallback& success_callback,
125 const ErrorCallback& error_callback) override;
126 void DeleteFile(const base::FilePath& file_path,
127 const DeleteFileSuccessCallback& success_callback,
128 const ErrorCallback& error_callback) override;
129 void DeleteDirectory(const base::FilePath& file_path,
130 const DeleteDirectorySuccessCallback& success_callback,
131 const ErrorCallback& error_callback) override;
132 void AddWatcher(const GURL& origin,
133 const base::FilePath& file_path,
134 const bool recursive,
135 const storage::WatcherManager::StatusCallback& callback,
136 const storage::WatcherManager::NotificationCallback&
137 notification_callback) override;
138 void RemoveWatcher(
139 const GURL& origin,
140 const base::FilePath& file_path,
141 const bool recursive,
142 const storage::WatcherManager::StatusCallback& callback) override;
143 void CancelPendingTasksAndDeleteDelegate() override;
144
145 // The internal methods correspond to the similarly named methods above.
146 // The |root_node_| cache should be filled at this point.
147 virtual void GetFileInfoInternal(
148 const base::FilePath& file_path,
149 const GetFileInfoSuccessCallback& success_callback,
150 const ErrorCallback& error_callback);
151 virtual void CreateDirectoryInternal(
152 const std::vector<base::FilePath>& components,
153 const bool exclusive,
154 const CreateDirectorySuccessCallback& success_callback,
155 const ErrorCallback& error_callback);
156 virtual void ReadDirectoryInternal(
157 const base::FilePath& root,
158 const ReadDirectorySuccessCallback& success_callback,
159 const ErrorCallback& error_callback);
160 virtual void CreateSnapshotFileInternal(
161 const base::FilePath& device_file_path,
162 const base::FilePath& local_path,
163 const CreateSnapshotFileSuccessCallback& success_callback,
164 const ErrorCallback& error_callback);
165 virtual void ReadBytesInternal(
166 const base::FilePath& device_file_path,
167 net::IOBuffer* buf,
168 int64_t offset,
169 int buf_len,
170 const ReadBytesSuccessCallback& success_callback,
171 const ErrorCallback& error_callback);
172 virtual void MoveFileLocalInternal(
173 const base::FilePath& source_file_path,
174 const base::FilePath& device_file_path,
175 const CreateTemporaryFileCallback& create_temporary_file_callback,
176 const MoveFileLocalSuccessCallback& success_callback,
177 const ErrorCallback& error_callback,
178 const base::File::Info& source_file_info);
179 virtual void OnDidOpenFDToCopyFileFromLocal(
180 const base::FilePath& device_file_path,
181 const CopyFileFromLocalSuccessCallback& success_callback,
182 const ErrorCallback& error_callback,
183 const std::pair<int, base::File::Error>& open_fd_result);
184 virtual void DeleteFileInternal(
185 const base::FilePath& file_path,
186 const DeleteFileSuccessCallback& success_callback,
187 const ErrorCallback& error_callback,
188 const base::File::Info& file_info);
189 virtual void DeleteDirectoryInternal(
190 const base::FilePath& file_path,
191 const DeleteDirectorySuccessCallback& success_callback,
192 const ErrorCallback& error_callback,
193 const base::File::Info& file_info);
194
195 // Creates a single directory to |directory_path|. The caller must ensure that
196 // parent directory |directory_path.DirName()| already exists.
197 virtual void CreateSingleDirectory(
198 const base::FilePath& directory_path,
199 const bool exclusive,
200 const CreateDirectorySuccessCallback& success_callback,
201 const ErrorCallback& error_callback);
202
203 // Called when ReadDirectoryInternal() completes for filling cache as part of
204 // creating directories.
205 virtual void OnDidReadDirectoryToCreateDirectory(
206 const std::vector<base::FilePath>& components,
207 const bool exclusive,
208 const CreateDirectorySuccessCallback& success_callback,
209 const ErrorCallback& error_callback,
210 const storage::AsyncFileUtil::EntryList& entries,
211 const bool has_more);
212
213 // Called when ReadDirectory succeeds.
214 virtual void OnDidReadDirectoryToDeleteDirectory(
215 const base::FilePath& directory_path,
216 const uint32_t directory_id,
217 const DeleteDirectorySuccessCallback& success_callback,
218 const ErrorCallback& error_callback,
219 const MTPDeviceTaskHelper::MTPEntries& entries,
220 const bool has_more);
221
222 // Calls DeleteObjectOnUIThread on UI thread.
223 virtual void RunDeleteObjectOnUIThread(
224 const base::FilePath& object_path,
225 const uint32_t object_id,
226 const DeleteObjectSuccessCallback& success_callback,
227 const ErrorCallback& error_callback);
228
229 // Notifies |chage_type| of |file_path| to watchers.
230 void NotifyFileChange(const base::FilePath& file_path,
231 const storage::WatcherManager::ChangeType change_type);
232
233 // Ensures the device is initialized for communication.
234 // If the device is already initialized, call RunTask().
235 //
236 // If the device is uninitialized, store the |task_info| in a pending task
237 // queue and runs the pending tasks in the queue once the device is
238 // successfully initialized.
239 void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
240
241 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs
242 // the task immediately.
243 // Otherwise, fills the cache first before running the task.
244 // |task_info.task| runs on the UI thread.
245 void RunTask(const PendingTaskInfo& task_info);
246
247 // Writes data from the device to the snapshot file path based on the
248 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
249 // the UI thread.
250 //
251 // |snapshot_file_info| specifies the metadata details of the snapshot file.
252 void WriteDataIntoSnapshotFile(const base::File::Info& snapshot_file_info);
253
254 // Marks the current request as complete and call ProcessNextPendingRequest().
255 void PendingRequestDone();
256
257 // Processes the next pending request.
258 void ProcessNextPendingRequest();
259
260 // Handles the device initialization event. |succeeded| indicates whether
261 // device initialization succeeded.
262 //
263 // If the device is successfully initialized, runs the next pending task.
264 void OnInitCompleted(bool succeeded);
265
266 // Called when GetFileInfo() succeeds. |file_info| specifies the
267 // requested file details. |success_callback| is invoked to notify the caller
268 // about the requested file details.
269 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
270 const base::File::Info& file_info);
271
272 // Called when GetFileInfo() of |directory_path| succeeded at checking the
273 // path already exists.
274 void OnPathAlreadyExistsForCreateSingleDirectory(
275 const bool exclusive,
276 const CreateDirectorySuccessCallback& success_callback,
277 const ErrorCallback& error_callback,
278 const base::File::Info& file_info);
279
280 // Called when GetFileInfo() of |directory_path| failed to check the path
281 // already exists.
282 void OnPathDoesNotExistForCreateSingleDirectory(
283 const base::FilePath& directory_path,
284 const CreateDirectorySuccessCallback& success_callback,
285 const ErrorCallback& error_callback,
286 const base::File::Error error);
287
288 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
289 // get the |dir_id| directory metadata details. |file_info| specifies the
290 // |dir_id| directory details.
291 //
292 // If |dir_id| is a directory, post a task on the UI thread to read the
293 // |dir_id| directory file entries.
294 //
295 // If |dir_id| is not a directory, |error_callback| is invoked to notify the
296 // caller about the file error and process the next pending request.
297 void OnDidGetFileInfoToReadDirectory(
298 uint32_t dir_id,
299 const ReadDirectorySuccessCallback& success_callback,
300 const ErrorCallback& error_callback,
301 const base::File::Info& file_info);
302
303 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
304 // create the snapshot file of |snapshot_request_info.device_file_path|.
305 // |file_info| specifies the device file metadata details.
306 //
307 // Posts a task on the UI thread to copy the data contents of the device file
308 // to the snapshot file.
309 void OnDidGetFileInfoToCreateSnapshotFile(
310 std::unique_ptr<SnapshotRequestInfo> snapshot_request_info,
311 const base::File::Info& file_info);
312
313 // Called when GetFileInfo() for destination path succeeded for a
314 // CopyFileFromLocal operation.
315 void OnDidGetDestFileInfoToCopyFileFromLocal(
316 const ErrorCallback& error_callback,
317 const base::File::Info& file_info);
318
319 // Called when GetFileInfo() for destination path failed to copy file from
320 // local.
321 void OnGetDestFileInfoErrorToCopyFileFromLocal(
322 const base::FilePath& source_file_path,
323 const base::FilePath& device_file_path,
324 const CopyFileFromLocalSuccessCallback& success_callback,
325 const ErrorCallback& error_callback,
326 const base::File::Error error);
327
328 // Called when CreateSignleDirectory() succeeds.
329 void OnDidCreateSingleDirectory(
330 const base::FilePath& directory_path,
331 const CreateDirectorySuccessCallback& success_callback);
332
333 // Called when parent directory |created_directory| is created as part of
334 // CreateDirectory.
335 void OnDidCreateParentDirectoryToCreateDirectory(
336 const base::FilePath& created_directory,
337 const std::vector<base::FilePath>& components,
338 const bool exclusive,
339 const CreateDirectorySuccessCallback& success_callback,
340 const ErrorCallback& error_callback);
341
342 // Called when it failed to create a parent directory. For creating parent
343 // directories, all errors should be reported as FILE_ERROR_FAILED. This
344 // method wraps error callbacks of creating parent directories.
345 void OnCreateParentDirectoryErrorToCreateDirectory(
346 const ErrorCallback& callback,
347 const base::File::Error error);
348
349 // Called when ReadDirectory() succeeds.
350 //
351 // |dir_id| is the directory read.
352 // |success_callback| is invoked to notify the caller about the directory
353 // file entries.
354 // |file_list| contains the directory file entries with their file ids.
355 // |has_more| is true if there are more file entries to read.
356 void OnDidReadDirectory(uint32_t dir_id,
357 const ReadDirectorySuccessCallback& success_callback,
358 const MTPDeviceTaskHelper::MTPEntries& mtp_entries,
359 bool has_more);
360
361 // Called when WriteDataIntoSnapshotFile() succeeds.
362 //
363 // |snapshot_file_info| specifies the snapshot file metadata details.
364 //
365 // |current_snapshot_request_info_.success_callback| is invoked to notify the
366 // caller about |snapshot_file_info|.
367 void OnDidWriteDataIntoSnapshotFile(
368 const base::File::Info& snapshot_file_info,
369 const base::FilePath& snapshot_file_path);
370
371 // Called when WriteDataIntoSnapshotFile() fails.
372 //
373 // |error| specifies the file error code.
374 //
375 // |current_snapshot_request_info_.error_callback| is invoked to notify the
376 // caller about |error|.
377 void OnWriteDataIntoSnapshotFileError(base::File::Error error);
378
379 // Called when ReadBytes() succeeds.
380 //
381 // |success_callback| is invoked to notify the caller about the read bytes.
382 // |bytes_read| is the number of bytes read.
383 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
384 const base::File::Info& file_info, int bytes_read);
385
386 // Called when FillFileCache() succeeds.
387 void OnDidFillFileCache(
388 const base::FilePath& path,
389 const storage::AsyncFileUtil::EntryList& /* entries */,
390 bool has_more);
391
392 // Called when FillFileCache() fails.
393 void OnFillFileCacheFailed(base::File::Error error);
394
395 // Called when CreateTemporaryFile() completes for CopyFileLocal.
396 void OnDidCreateTemporaryFileToCopyFileLocal(
397 const base::FilePath& source_file_path,
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::FilePath& temporary_file_path);
403
404 // Called when CreateSnapshotFile() succeeds for CopyFileLocal.
405 void OnDidCreateSnapshotFileOfCopyFileLocal(
406 const base::FilePath& device_file_path,
407 const CopyFileProgressCallback& progress_callback,
408 const CopyFileLocalSuccessCallback& success_callback,
409 const ErrorCallback& error_callback,
410 const base::File::Info& file_info,
411 const base::FilePath& temporary_file_path);
412
413 // Called when CopyFileFromLocal() succeeds for CopyFileLocal.
414 void OnDidCopyFileFromLocalOfCopyFileLocal(
415 const CopyFileFromLocalSuccessCallback success_callback,
416 const base::FilePath& temporary_file_path);
417
418 // Called when MoveFileLocal() succeeds with rename operation.
419 void OnDidMoveFileLocalWithRename(
420 const MoveFileLocalSuccessCallback& success_callback,
421 const base::FilePath& source_file_path,
422 const uint32_t file_id);
423
424 // Called when CopyFileFromLocal() succeeds.
425 void OnDidCopyFileFromLocal(
426 const CopyFileFromLocalSuccessCallback& success_callback,
427 const base::FilePath& file_path,
428 const int source_file_descriptor);
429
430 // Called when CopyFileLocal() fails.
431 void HandleCopyFileLocalError(const ErrorCallback& error_callback,
432 const base::FilePath& temporary_file_path,
433 const base::File::Error error);
434
435 // Called when CopyFileFromLocal() fails.
436 void HandleCopyFileFromLocalError(const ErrorCallback& error_callback,
437 const int source_file_descriptor,
438 base::File::Error error);
439
440 // Called when DeleteObject() succeeds.
441 void OnDidDeleteObject(const base::FilePath& object_path,
442 const uint32_t object_id,
443 const DeleteObjectSuccessCallback success_callback);
444
445 // Called when DeleteFileOrDirectory() fails.
446 void HandleDeleteFileOrDirectoryError(const ErrorCallback& error_callback,
447 base::File::Error error);
448
449 // Handles the device file |error| while operating on |file_id|.
450 // |error_callback| is invoked to notify the caller about the file error.
451 void HandleDeviceFileError(const ErrorCallback& error_callback,
452 uint32_t file_id,
453 base::File::Error error);
454
455 // Given a full path, returns a non-empty sub-path that needs to be read into
456 // the cache if such a uncached path exists.
457 // |cached_path| is the portion of |path| that has had cache lookup attempts.
458 base::FilePath NextUncachedPathComponent(
459 const base::FilePath& path,
460 const base::FilePath& cached_path) const;
461
462 // Fills the file cache using the results from NextUncachedPathComponent().
463 void FillFileCache(const base::FilePath& uncached_path);
464
465 // Given a full path, if it exists in the cache, writes the file's id to |id|
466 // and return true.
467 bool CachedPathToId(const base::FilePath& path, uint32_t* id) const;
468
469 // Evict the cache of |id|.
470 void EvictCachedPathToId(const uint32_t id);
471
472 // MTP device initialization state.
473 InitializationState init_state_;
474
475 // Used to make sure only one task is in progress at any time.
476 // Otherwise the browser will try to send too many requests at once and
477 // overload the device.
478 bool task_in_progress_;
479
480 // Registered file system device path. This path does not
481 // correspond to a real device path (e.g. "/usb:2,2:81282").
482 const base::FilePath device_path_;
483
484 // MTP device storage name (e.g. "usb:2,2:81282").
485 std::string storage_name_;
486
487 // Mode for opening storage.
488 const bool read_only_;
489
490 // Maps for holding notification callbacks.
491 typedef std::map<GURL, storage::WatcherManager::NotificationCallback>
492 OriginNotificationCallbackMap;
493 typedef std::map<base::FilePath, OriginNotificationCallbackMap> Subscribers;
494 Subscribers subscribers_;
495
496 // A list of pending tasks that needs to be run when the device is
497 // initialized or when the current task in progress is complete.
498 std::deque<PendingTaskInfo> pending_tasks_;
499
500 // Used to track the current snapshot file request. A snapshot file is created
501 // incrementally. CreateSnapshotFile request reads the device file and writes
502 // to the snapshot file in chunks. In order to retain the order of the
503 // snapshot file requests, make sure there is only one active snapshot file
504 // request at any time.
505 std::unique_ptr<SnapshotRequestInfo> current_snapshot_request_info_;
506
507 // A mapping for quick lookups into the |root_node_| tree structure. Since
508 // |root_node_| contains pointers to this map, it must be declared after this
509 // so destruction happens in the right order.
510 FileIdToMTPFileNodeMap file_id_to_node_map_;
511
512 // The root node of a tree-structure that caches the directory structure of
513 // the MTP device.
514 std::unique_ptr<MTPFileNode> root_node_;
515
516 // A list of child nodes encountered while a ReadDirectory operation, which
517 // can return results over multiple callbacks, is in progress.
518 std::set<std::string> child_nodes_seen_;
519
520 // A cache to store file metadata for file entries read during a ReadDirectory
521 // operation. Used to service incoming GetFileInfo calls for the duration of
522 // the ReadDirectory operation.
523 FileInfoCache file_info_cache_;
524
525 // For callbacks that may run after destruction.
526 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
527
528 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
529 };
530
531 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H _
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698