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_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ |
7 | 7 |
8 #include <map> | |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
14 #include "base/sequenced_task_runner_helpers.h" | 15 #include "webkit/fileapi/media/mtp_device_async_delegate.h" |
15 #include "base/synchronization/lock.h" | |
16 #include "base/synchronization/waitable_event.h" | |
17 #include "webkit/fileapi/file_system_file_util.h" | |
18 #include "webkit/fileapi/media/mtp_device_delegate.h" | |
19 | |
20 namespace base { | |
21 class SequencedTaskRunner; | |
22 } | |
23 | 16 |
24 namespace chrome { | 17 namespace chrome { |
25 | 18 |
26 // Delegate for presenting an Image Capture device through the filesystem | 19 // Delegate for presenting an Image Capture device through the filesystem |
27 // API. The synthetic filesystem will be rooted at the constructed location, | 20 // API. The synthetic filesystem will be rooted at the constructed location, |
28 // and names of all files notified through the ItemAdded call will be | 21 // and names of all files notified through the ItemAdded call will be |
29 // all appear as children of that directory. (ItemAdded calls with directories | 22 // all appear as children of that directory. (ItemAdded calls with directories |
30 // will be ignored.) | 23 // will be ignored.) |
31 class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceDelegate { | 24 // Note on thread management: This class is thread-compatible: it can be created |
25 // on any thread, but then mutates all state on the UI thread. The async | |
26 // delegate interface can be invoked on any thread, as it simply forwards calls | |
27 // to the UI thread. | |
28 class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceAsyncDelegate { | |
32 public: | 29 public: |
33 MTPDeviceDelegateImplMac(const std::string& device_id, | 30 MTPDeviceDelegateImplMac(const std::string& device_id, |
34 const base::FilePath::StringType& synthetic_path, | 31 const base::FilePath::StringType& synthetic_path); |
35 base::SequencedTaskRunner* media_task_runner); | |
36 | 32 |
37 // MTPDeviceDelegate: | 33 // MTPDeviceAsyncDelegate implementation. These functions are called on the |
38 virtual base::PlatformFileError GetFileInfo( | 34 // IO thread by the async filesystem file util. They forward to |
35 // similarly-named methods on the UI thread. | |
36 virtual void GetFileInfo( | |
39 const base::FilePath& file_path, | 37 const base::FilePath& file_path, |
40 base::PlatformFileInfo* file_info) OVERRIDE; | 38 const GetFileInfoSuccessCallback& success_callback, |
41 virtual scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | 39 const ErrorCallback& error_callback) OVERRIDE; |
42 CreateFileEnumerator(const base::FilePath& root, bool recursive) OVERRIDE; | 40 virtual void ReadDirectory( |
43 virtual base::PlatformFileError CreateSnapshotFile( | 41 const base::FilePath& root, |
42 const ReadDirectorySuccessCallback& success_callback, | |
43 const ErrorCallback& error_callback) OVERRIDE; | |
44 virtual void CreateSnapshotFile( | |
44 const base::FilePath& device_file_path, | 45 const base::FilePath& device_file_path, |
45 const base::FilePath& local_path, | 46 const base::FilePath& local_path, |
46 base::PlatformFileInfo* file_info) OVERRIDE; | 47 const CreateSnapshotFileSuccessCallback& success_callback, |
48 const ErrorCallback& error_callback) OVERRIDE; | |
47 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE; | 49 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE; |
48 | 50 |
49 // Forward delegates for ImageCaptureDeviceListener | 51 // Forward delegates for ImageCaptureDeviceListener. These are |
52 // invoked in callbacks of the ImageCapture library on the UI thread. | |
50 virtual void ItemAdded(const std::string& name, | 53 virtual void ItemAdded(const std::string& name, |
51 const base::PlatformFileInfo& info); | 54 const base::PlatformFileInfo& info); |
52 virtual void NoMoreItems(); | 55 virtual void NoMoreItems(); |
53 virtual void DownloadedFile(const std::string& name, | 56 virtual void DownloadedFile(const std::string& name, |
54 base::PlatformFileError error); | 57 base::PlatformFileError error); |
55 | 58 |
56 // Implementation returned by |CreateFileEnumerator()|. Must be deleted | 59 // Public for closures; should not be called except by |
57 // before CancelPendingTasksAndDeleteDelegate is called. | 60 // CancelTasksAndDeleteDelegate. |
58 class Enumerator : | 61 void CancelAndDelete(); |
Lei Zhang
2013/03/18 09:25:55
I think this can be private?
Greg Billock
2013/03/20 23:43:55
Done.
| |
59 public fileapi::FileSystemFileUtil::AbstractFileEnumerator { | |
60 public: | |
61 explicit Enumerator(MTPDeviceDelegateImplMac* delegate); | |
62 virtual ~Enumerator(); | |
63 | 62 |
64 virtual base::FilePath Next() OVERRIDE; | 63 // Scheduled when early directory reads are requested. The |
65 virtual int64 Size() OVERRIDE; | 64 // timeout will signal an ABORT error to the caller if the |
66 virtual base::Time LastModifiedTime() OVERRIDE; | 65 // device metadata cannot be read. |
67 virtual bool IsDirectory() OVERRIDE; | 66 void ReadDirectoryTimeout(const base::FilePath& root); |
68 | |
69 // Called by the delegate to signal any waiters that the received items | |
70 // list has changed state. | |
71 void ItemsChanged(); | |
72 | |
73 private: | |
74 MTPDeviceDelegateImplMac* delegate_; | |
75 size_t position_; | |
76 base::WaitableEvent wait_for_items_; | |
77 }; | |
78 | |
79 // GetFile and HasAllFiles called by enumerators. | |
80 base::FilePath GetFile(size_t index); | |
81 bool ReceivedAllFiles(); | |
82 void RemoveEnumerator(Enumerator* enumerator); | |
83 | 67 |
84 private: | 68 private: |
85 friend class base::DeleteHelper<MTPDeviceDelegateImplMac>; | |
86 | |
87 class DeviceListener; | 69 class DeviceListener; |
88 | 70 |
89 virtual ~MTPDeviceDelegateImplMac(); | 71 virtual ~MTPDeviceDelegateImplMac(); |
90 | 72 |
73 // Delegate for GetFileInfo, called on the UI thread. | |
74 void GetFileInfoImpl(const base::FilePath& file_path, | |
75 base::PlatformFileInfo* file_info, | |
76 base::PlatformFileError* error); | |
77 | |
78 // Delegate for ReadDirectory, called on the UI thread. | |
79 void ReadDirectoryImpl( | |
80 const base::FilePath& root, | |
81 const ReadDirectorySuccessCallback& success_callback, | |
82 const ErrorCallback& error_callback); | |
83 | |
84 // Delegate for CreateSnapshotFile, called on the UI thread. | |
85 void DownloadFile( | |
86 const base::FilePath& device_file_path, | |
87 const base::FilePath& local_path, | |
88 const CreateSnapshotFileSuccessCallback& success_callback, | |
89 const ErrorCallback& error_callback); | |
90 | |
91 // Cancels any outstanding downloads. | |
92 void CancelDownloads(); | |
93 | |
94 // If necessary, notifies the ReadDirectory callback that all data | |
95 // has been read. | |
96 void NotifyReadDir(); | |
97 | |
91 std::string device_id_; | 98 std::string device_id_; |
92 base::FilePath root_path_; | 99 base::FilePath root_path_; |
93 | 100 |
94 // Stores a reference to worker pool thread. All requests and response of file | |
95 // operations are posted on |media_task_runner_|. All callbacks from the | |
96 // camera will come through this task runner as well. | |
97 scoped_refptr<base::SequencedTaskRunner> media_task_runner_; | |
98 | |
99 // Interface object for the camera underlying this MTP session. | 101 // Interface object for the camera underlying this MTP session. |
100 scoped_ptr<DeviceListener> camera_interface_; | 102 scoped_ptr<DeviceListener> camera_interface_; |
101 | 103 |
102 // This lock protects all subsequent state. While calling into the delegate | |
103 // from the FileSystem API happens in sequence, these calls may be | |
104 // interleaved with calls on other threads in the sequenced task runner | |
105 // forwarded from the device. | |
106 base::Lock mutex_; | |
107 | |
108 // Weak pointer to the enumerator which may be listening for files to come in. | |
109 Enumerator* enumerator_; | |
110 | |
111 // Stores a map from filename to file metadata received from the camera. | 104 // Stores a map from filename to file metadata received from the camera. |
112 base::hash_map<base::FilePath::StringType, base::PlatformFileInfo> file_info_; | 105 base::hash_map<base::FilePath::StringType, |
113 | 106 base::PlatformFileInfo> file_info_; |
114 // Notification for pending download. | |
115 base::WaitableEvent* file_download_event_; | |
116 | |
117 // Resulting error code for pending download. | |
118 base::PlatformFileError file_download_error_; | |
119 | 107 |
120 // List of files received from the camera. | 108 // List of files received from the camera. |
121 std::vector<base::FilePath> file_paths_; | 109 std::vector<base::FilePath> file_paths_; |
Lei Zhang
2013/03/18 09:25:55
Do we still need this? Does it help keep the files
Greg Billock
2013/03/20 23:43:55
Hmm. I think we could work without it, but when I
Lei Zhang
2013/03/21 22:34:58
Sure.
| |
122 | 110 |
123 // Set to true when all file metadata has been received from the camera. | 111 // Set to true when all file metadata has been received from the camera. |
124 bool received_all_files_; | 112 bool received_all_files_; |
125 | 113 |
114 typedef std::map<std::string, | |
115 std::pair<CreateSnapshotFileSuccessCallback, | |
116 ErrorCallback> > ReadFileTransactionMap; | |
117 | |
118 typedef std::map<std::string, | |
119 std::pair<ReadDirectorySuccessCallback, | |
120 ErrorCallback> > ReadDirTransactionMap; | |
121 | |
122 ReadFileTransactionMap read_file_transactions_; | |
123 ReadDirTransactionMap read_dir_transactions_; | |
124 | |
125 base::WeakPtrFactory<MTPDeviceDelegateImplMac> weak_factory_; | |
126 | |
126 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac); | 127 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac); |
127 }; | 128 }; |
128 | 129 |
129 } // namespace chrome | 130 } // namespace chrome |
130 | 131 |
131 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ | 132 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ |
OLD | NEW |