| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 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_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol | 25 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol |
| 26 // (MTP) device to complete file system operations. These operations are | 26 // (MTP) device to complete file system operations. These operations are |
| 27 // performed asynchronously. Instantiate this class per MTP device storage. | 27 // performed asynchronously. Instantiate this class per MTP device storage. |
| 28 // MTPDeviceDelegateImplLinux lives on the IO thread. | 28 // MTPDeviceDelegateImplLinux lives on the IO thread. |
| 29 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread | 29 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread |
| 30 // to dispatch the requests to MediaTransferProtocolManager. | 30 // to dispatch the requests to MediaTransferProtocolManager. |
| 31 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate { | 31 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate { |
| 32 private: | 32 private: |
| 33 friend void CreateMTPDeviceAsyncDelegate( | 33 friend void CreateMTPDeviceAsyncDelegate( |
| 34 const std::string&, | 34 const std::string&, |
| 35 const bool read_only, |
| 35 const CreateMTPDeviceAsyncDelegateCallback&); | 36 const CreateMTPDeviceAsyncDelegateCallback&); |
| 36 | 37 |
| 37 enum InitializationState { | 38 enum InitializationState { |
| 38 UNINITIALIZED = 0, | 39 UNINITIALIZED = 0, |
| 39 PENDING_INIT, | 40 PENDING_INIT, |
| 40 INITIALIZED | 41 INITIALIZED |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 // Used to represent pending task details. | 44 // Used to represent pending task details. |
| 44 struct PendingTaskInfo { | 45 struct PendingTaskInfo { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 59 | 60 |
| 60 // Maps file ids to file nodes. | 61 // Maps file ids to file nodes. |
| 61 typedef std::map<uint32, MTPFileNode*> FileIdToMTPFileNodeMap; | 62 typedef std::map<uint32, MTPFileNode*> FileIdToMTPFileNodeMap; |
| 62 | 63 |
| 63 // Maps file paths to file info. | 64 // Maps file paths to file info. |
| 64 typedef std::map<base::FilePath, storage::DirectoryEntry> FileInfoCache; | 65 typedef std::map<base::FilePath, storage::DirectoryEntry> FileInfoCache; |
| 65 | 66 |
| 66 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call. | 67 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call. |
| 67 // Defer the device initializations until the first file operation request. | 68 // Defer the device initializations until the first file operation request. |
| 68 // Do all the initializations in EnsureInitAndRunTask() function. | 69 // Do all the initializations in EnsureInitAndRunTask() function. |
| 69 explicit MTPDeviceDelegateImplLinux(const std::string& device_location); | 70 MTPDeviceDelegateImplLinux(const std::string& device_location, |
| 71 const bool read_only); |
| 70 | 72 |
| 71 // Destructed via CancelPendingTasksAndDeleteDelegate(). | 73 // Destructed via CancelPendingTasksAndDeleteDelegate(). |
| 72 ~MTPDeviceDelegateImplLinux() override; | 74 ~MTPDeviceDelegateImplLinux() override; |
| 73 | 75 |
| 74 // MTPDeviceAsyncDelegate: | 76 // MTPDeviceAsyncDelegate: |
| 75 void GetFileInfo(const base::FilePath& file_path, | 77 void GetFileInfo(const base::FilePath& file_path, |
| 76 const GetFileInfoSuccessCallback& success_callback, | 78 const GetFileInfoSuccessCallback& success_callback, |
| 77 const ErrorCallback& error_callback) override; | 79 const ErrorCallback& error_callback) override; |
| 78 void ReadDirectory(const base::FilePath& root, | 80 void ReadDirectory(const base::FilePath& root, |
| 79 const ReadDirectorySuccessCallback& success_callback, | 81 const ReadDirectorySuccessCallback& success_callback, |
| 80 const ErrorCallback& error_callback) override; | 82 const ErrorCallback& error_callback) override; |
| 81 void CreateSnapshotFile( | 83 void CreateSnapshotFile( |
| 82 const base::FilePath& device_file_path, | 84 const base::FilePath& device_file_path, |
| 83 const base::FilePath& local_path, | 85 const base::FilePath& local_path, |
| 84 const CreateSnapshotFileSuccessCallback& success_callback, | 86 const CreateSnapshotFileSuccessCallback& success_callback, |
| 85 const ErrorCallback& error_callback) override; | 87 const ErrorCallback& error_callback) override; |
| 86 bool IsStreaming() override; | 88 bool IsStreaming() override; |
| 87 void ReadBytes(const base::FilePath& device_file_path, | 89 void ReadBytes(const base::FilePath& device_file_path, |
| 88 const scoped_refptr<net::IOBuffer>& buf, | 90 const scoped_refptr<net::IOBuffer>& buf, |
| 89 int64 offset, | 91 int64 offset, |
| 90 int buf_len, | 92 int buf_len, |
| 91 const ReadBytesSuccessCallback& success_callback, | 93 const ReadBytesSuccessCallback& success_callback, |
| 92 const ErrorCallback& error_callback) override; | 94 const ErrorCallback& error_callback) override; |
| 95 bool IsReadOnly() override; |
| 96 void CopyFileFromLocal( |
| 97 const base::FilePath& source_file_path, |
| 98 const base::FilePath& device_file_path, |
| 99 const CopyFileFromLocalSuccessCallback& success_callback, |
| 100 const ErrorCallback& error_callback) override; |
| 93 void CancelPendingTasksAndDeleteDelegate() override; | 101 void CancelPendingTasksAndDeleteDelegate() override; |
| 94 | 102 |
| 95 // The internal methods correspond to the similarly named methods above. | 103 // The internal methods correspond to the similarly named methods above. |
| 96 // The |root_node_| cache should be filled at this point. | 104 // The |root_node_| cache should be filled at this point. |
| 97 virtual void GetFileInfoInternal( | 105 virtual void GetFileInfoInternal( |
| 98 const base::FilePath& file_path, | 106 const base::FilePath& file_path, |
| 99 const GetFileInfoSuccessCallback& success_callback, | 107 const GetFileInfoSuccessCallback& success_callback, |
| 100 const ErrorCallback& error_callback); | 108 const ErrorCallback& error_callback); |
| 101 virtual void ReadDirectoryInternal( | 109 virtual void ReadDirectoryInternal( |
| 102 const base::FilePath& root, | 110 const base::FilePath& root, |
| 103 const ReadDirectorySuccessCallback& success_callback, | 111 const ReadDirectorySuccessCallback& success_callback, |
| 104 const ErrorCallback& error_callback); | 112 const ErrorCallback& error_callback); |
| 105 virtual void CreateSnapshotFileInternal( | 113 virtual void CreateSnapshotFileInternal( |
| 106 const base::FilePath& device_file_path, | 114 const base::FilePath& device_file_path, |
| 107 const base::FilePath& local_path, | 115 const base::FilePath& local_path, |
| 108 const CreateSnapshotFileSuccessCallback& success_callback, | 116 const CreateSnapshotFileSuccessCallback& success_callback, |
| 109 const ErrorCallback& error_callback); | 117 const ErrorCallback& error_callback); |
| 110 virtual void ReadBytesInternal( | 118 virtual void ReadBytesInternal( |
| 111 const base::FilePath& device_file_path, | 119 const base::FilePath& device_file_path, |
| 112 net::IOBuffer* buf, int64 offset, int buf_len, | 120 net::IOBuffer* buf, int64 offset, int buf_len, |
| 113 const ReadBytesSuccessCallback& success_callback, | 121 const ReadBytesSuccessCallback& success_callback, |
| 114 const ErrorCallback& error_callback); | 122 const ErrorCallback& error_callback); |
| 123 virtual void CopyFileFromLocalInternal( |
| 124 const base::FilePath& device_file_path, |
| 125 const CopyFileFromLocalSuccessCallback& success_callback, |
| 126 const ErrorCallback& error_callback, |
| 127 const int source_file_descriptor); |
| 115 | 128 |
| 116 // Ensures the device is initialized for communication. | 129 // Ensures the device is initialized for communication. |
| 117 // If the device is already initialized, call RunTask(). | 130 // If the device is already initialized, call RunTask(). |
| 118 // | 131 // |
| 119 // If the device is uninitialized, store the |task_info| in a pending task | 132 // If the device is uninitialized, store the |task_info| in a pending task |
| 120 // queue and runs the pending tasks in the queue once the device is | 133 // queue and runs the pending tasks in the queue once the device is |
| 121 // successfully initialized. | 134 // successfully initialized. |
| 122 void EnsureInitAndRunTask(const PendingTaskInfo& task_info); | 135 void EnsureInitAndRunTask(const PendingTaskInfo& task_info); |
| 123 | 136 |
| 124 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs | 137 // Runs a task. If |task_info.path| is empty, or if the path is cached, runs |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 const base::File::Info& file_info, int bytes_read); | 228 const base::File::Info& file_info, int bytes_read); |
| 216 | 229 |
| 217 // Called when FillFileCache() succeeds. | 230 // Called when FillFileCache() succeeds. |
| 218 void OnDidFillFileCache(const base::FilePath& path, | 231 void OnDidFillFileCache(const base::FilePath& path, |
| 219 const storage::AsyncFileUtil::EntryList& file_list, | 232 const storage::AsyncFileUtil::EntryList& file_list, |
| 220 bool has_more); | 233 bool has_more); |
| 221 | 234 |
| 222 // Called when FillFileCache() fails. | 235 // Called when FillFileCache() fails. |
| 223 void OnFillFileCacheFailed(base::File::Error error); | 236 void OnFillFileCacheFailed(base::File::Error error); |
| 224 | 237 |
| 238 // Called when CopyFileFromLocal() succeeds. |
| 239 void OnDidCopyFileFromLocal( |
| 240 const CopyFileFromLocalSuccessCallback& success_callback, |
| 241 const int source_file_descriptor); |
| 242 |
| 243 // Called when CopyFileFromLocal() fails. |
| 244 void HandleCopyFileFromLocalError(const ErrorCallback& error_callback, |
| 245 const int source_file_descriptor, |
| 246 base::File::Error error); |
| 247 |
| 225 // Handles the device file |error| while operating on |file_id|. | 248 // Handles the device file |error| while operating on |file_id|. |
| 226 // |error_callback| is invoked to notify the caller about the file error. | 249 // |error_callback| is invoked to notify the caller about the file error. |
| 227 void HandleDeviceFileError(const ErrorCallback& error_callback, | 250 void HandleDeviceFileError(const ErrorCallback& error_callback, |
| 228 uint32 file_id, | 251 uint32 file_id, |
| 229 base::File::Error error); | 252 base::File::Error error); |
| 230 | 253 |
| 231 // Given a full path, returns a non-empty sub-path that needs to be read into | 254 // Given a full path, returns a non-empty sub-path that needs to be read into |
| 232 // the cache if such a uncached path exists. | 255 // the cache if such a uncached path exists. |
| 233 // |cached_path| is the portion of |path| that has had cache lookup attempts. | 256 // |cached_path| is the portion of |path| that has had cache lookup attempts. |
| 234 base::FilePath NextUncachedPathComponent( | 257 base::FilePath NextUncachedPathComponent( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 250 // overload the device. | 273 // overload the device. |
| 251 bool task_in_progress_; | 274 bool task_in_progress_; |
| 252 | 275 |
| 253 // Registered file system device path. This path does not | 276 // Registered file system device path. This path does not |
| 254 // correspond to a real device path (e.g. "/usb:2,2:81282"). | 277 // correspond to a real device path (e.g. "/usb:2,2:81282"). |
| 255 const base::FilePath device_path_; | 278 const base::FilePath device_path_; |
| 256 | 279 |
| 257 // MTP device storage name (e.g. "usb:2,2:81282"). | 280 // MTP device storage name (e.g. "usb:2,2:81282"). |
| 258 std::string storage_name_; | 281 std::string storage_name_; |
| 259 | 282 |
| 283 // Mode for opening storage. |
| 284 const bool read_only_; |
| 285 |
| 260 // A list of pending tasks that needs to be run when the device is | 286 // A list of pending tasks that needs to be run when the device is |
| 261 // initialized or when the current task in progress is complete. | 287 // initialized or when the current task in progress is complete. |
| 262 std::deque<PendingTaskInfo> pending_tasks_; | 288 std::deque<PendingTaskInfo> pending_tasks_; |
| 263 | 289 |
| 264 // Used to track the current snapshot file request. A snapshot file is created | 290 // Used to track the current snapshot file request. A snapshot file is created |
| 265 // incrementally. CreateSnapshotFile request reads the device file and writes | 291 // incrementally. CreateSnapshotFile request reads the device file and writes |
| 266 // to the snapshot file in chunks. In order to retain the order of the | 292 // to the snapshot file in chunks. In order to retain the order of the |
| 267 // snapshot file requests, make sure there is only one active snapshot file | 293 // snapshot file requests, make sure there is only one active snapshot file |
| 268 // request at any time. | 294 // request at any time. |
| 269 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_; | 295 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 286 // the ReadDirectory operation. | 312 // the ReadDirectory operation. |
| 287 FileInfoCache file_info_cache_; | 313 FileInfoCache file_info_cache_; |
| 288 | 314 |
| 289 // For callbacks that may run after destruction. | 315 // For callbacks that may run after destruction. |
| 290 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_; | 316 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_; |
| 291 | 317 |
| 292 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); | 318 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); |
| 293 }; | 319 }; |
| 294 | 320 |
| 295 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H
_ | 321 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H
_ |
| OLD | NEW |