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_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" |
10 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
12 #include "content/public/browser/notification_observer.h" | |
13 #include "content/public/browser/notification_registrar.h" | |
14 #include "webkit/fileapi/file_system_file_util.h" | 13 #include "webkit/fileapi/file_system_file_util.h" |
15 #include "webkit/fileapi/media/mtp_device_delegate.h" | 14 #include "webkit/fileapi/media/mtp_device_delegate.h" |
16 | 15 |
17 class FilePath; | 16 class FilePath; |
18 | 17 |
19 namespace base { | 18 namespace base { |
20 class SequencedTaskRunner; | 19 class SequencedTaskRunner; |
21 } | 20 } |
22 | 21 |
23 namespace chrome { | 22 namespace chrome { |
24 | 23 |
25 // Helper class to communicate with MTP storage to complete isolated file system | 24 // This class communicates with the MTP storage to complete the isolated file |
26 // operations. This class contains platform specific code to communicate with | 25 // system operations. This class contains platform specific code to communicate |
27 // the attached MTP storage. Instantiate this class per MTP storage. | 26 // with the attached MTP storage. Instantiate this class per MTP storage. This |
28 // This class is ref-counted, because MTPDeviceDelegate is ref-counted. | 27 // object is constructed on the UI thread. This object is operated and |
29 class MTPDeviceDelegateImplLinux : public fileapi::MTPDeviceDelegate, | 28 // destructed on the sequenced task runner thread. ScopedMTPDeviceMapEntry class |
30 public content::NotificationObserver { | 29 // manages the lifetime of this object via MTPDeviceMapService class. This class |
| 30 // supports weak pointers because the base class supports weak pointers. |
| 31 class MTPDeviceDelegateImplLinux : public fileapi::MTPDeviceDelegate { |
31 public: | 32 public: |
32 // Constructed on UI thread. Defer the device initializations until the first | 33 // Should only be called by ScopedMTPDeviceMapEntry. Use |
33 // file operation request. Do all the initializations in LazyInit() function. | 34 // GetAsWeakPtrOnIOThread() to get a weak pointer instance of this class. |
| 35 // Defer the device initializations until the first file operation request. |
| 36 // Do all the initializations in LazyInit() function. |
34 explicit MTPDeviceDelegateImplLinux(const std::string& device_location); | 37 explicit MTPDeviceDelegateImplLinux(const std::string& device_location); |
35 | 38 |
36 // Overridden from MTPDeviceDelegate. All the functions are called on | 39 // MTPDeviceDelegate: |
37 // |media_task_runner_|. | |
38 virtual base::PlatformFileError GetFileInfo( | 40 virtual base::PlatformFileError GetFileInfo( |
39 const FilePath& file_path, | 41 const FilePath& file_path, |
40 base::PlatformFileInfo* file_info) OVERRIDE; | 42 base::PlatformFileInfo* file_info) OVERRIDE; |
41 virtual scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | 43 virtual scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> |
42 CreateFileEnumerator(const FilePath& root, | 44 CreateFileEnumerator(const FilePath& root, |
43 bool recursive) OVERRIDE; | 45 bool recursive) OVERRIDE; |
44 virtual base::PlatformFileError CreateSnapshotFile( | 46 virtual base::PlatformFileError CreateSnapshotFile( |
45 const FilePath& device_file_path, | 47 const FilePath& device_file_path, |
46 const FilePath& local_path, | 48 const FilePath& local_path, |
47 base::PlatformFileInfo* file_info) OVERRIDE; | 49 base::PlatformFileInfo* file_info) OVERRIDE; |
48 virtual base::SequencedTaskRunner* GetMediaTaskRunner() OVERRIDE; | 50 virtual base::SequencedTaskRunner* GetMediaTaskRunner() OVERRIDE; |
49 virtual void DeleteOnCorrectThread() const OVERRIDE; | 51 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE; |
| 52 virtual base::WeakPtr<fileapi::MTPDeviceDelegate> GetAsWeakPtrOnIOThread() |
| 53 OVERRIDE; |
50 | 54 |
51 private: | 55 private: |
52 friend struct fileapi::MTPDeviceDelegateDeleter; | 56 // Destructed via DeleteDelegateOnTaskRunner(). Do all the clean up in |
53 friend class base::DeleteHelper<MTPDeviceDelegateImplLinux>; | 57 // DeleteDelegateOnTaskRunner(). |
54 | |
55 // Private because this class is ref-counted. | |
56 virtual ~MTPDeviceDelegateImplLinux(); | 58 virtual ~MTPDeviceDelegateImplLinux(); |
57 | 59 |
58 // content::NotificationObserver implementation. | |
59 virtual void Observe(int type, | |
60 const content::NotificationSource& source, | |
61 const content::NotificationDetails& details) OVERRIDE; | |
62 | |
63 // Opens the device for communication. This function is called on | 60 // Opens the device for communication. This function is called on |
64 // |media_task_runner_|. Returns true if the device is ready for | 61 // |media_task_runner_|. Returns true if the device is ready for |
65 // communication, else false. | 62 // communication, else false. |
66 bool LazyInit(); | 63 bool LazyInit(); |
67 | 64 |
| 65 // Deletes the delegate on the task runner thread. Called by |
| 66 // CancelPendingTasksAndDeleteDelegate(). Performs clean up that needs to |
| 67 // happen on |media_task_runner_|. |
| 68 void DeleteDelegateOnTaskRunner(); |
| 69 |
68 // Stores the registered file system device path value. This path does not | 70 // Stores the registered file system device path value. This path does not |
69 // correspond to a real device path (E.g.: "/usb:2,2:81282"). | 71 // correspond to a real device path (E.g.: "/usb:2,2:81282"). |
70 const std::string device_path_; | 72 const std::string device_path_; |
71 | 73 |
72 // Stores the device handle returned by | 74 // Stores the device handle returned by |
73 // MediaTransferProtocolManager::OpenStorage(). | 75 // MediaTransferProtocolManager::OpenStorage(). |
74 std::string device_handle_; | 76 std::string device_handle_; |
75 | 77 |
76 // Stores a reference to worker pool thread. All requests and response of file | 78 // Stores a reference to worker pool thread. All requests and response of file |
77 // operations are posted on |media_task_runner_|. | 79 // operations are posted on |media_task_runner_|. |
78 scoped_refptr<base::SequencedTaskRunner> media_task_runner_; | 80 scoped_refptr<base::SequencedTaskRunner> media_task_runner_; |
79 | 81 |
80 // |media_task_runner_| can wait on this event until the requested task is | 82 // |media_task_runner_| can wait on this event until the requested task is |
81 // complete. | 83 // complete. |
82 base::WaitableEvent on_task_completed_event_; | 84 base::WaitableEvent on_task_completed_event_; |
83 | 85 |
84 // Used to notify |media_task_runner_| pending tasks about the shutdown | 86 // Used to notify |media_task_runner_| pending tasks about the shutdown |
85 // sequence. | 87 // sequence. Signaled on the IO thread. |
86 base::WaitableEvent on_shutdown_event_; | 88 base::WaitableEvent on_shutdown_event_; |
87 | 89 |
88 // Handles registering notifications with the NotificationService. | |
89 // Used to listen for application termination message. | |
90 content::NotificationRegistrar registrar_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); | 90 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux); |
93 }; | 91 }; |
94 | 92 |
95 } // namespace chrome | 93 } // namespace chrome |
96 | 94 |
97 #endif // CHROME_BROWSER_MEDIA_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ | 95 #endif // CHROME_BROWSER_MEDIA_GALLERY_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_ |
OLD | NEW |