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 // MediaDeviceNotificationsLinux listens for mount point changes and notifies | |
6 // the SystemMonitor about the addition and deletion of media devices. | |
7 | |
5 #ifndef CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 8 #ifndef CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
6 #define CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 9 #define CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
7 #pragma once | 10 #pragma once |
8 | 11 |
9 #include <map> | 12 #include <map> |
10 #include <set> | 13 #include <set> |
11 #include <string> | 14 #include <string> |
12 #include <utility> | 15 #include <utility> |
13 | 16 |
14 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
15 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
16 #include "base/file_path.h" | |
17 #include "base/files/file_path_watcher.h" | 19 #include "base/files/file_path_watcher.h" |
18 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
19 #include "base/system_monitor/system_monitor.h" | 21 #include "base/system_monitor/system_monitor.h" |
20 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
21 | 23 |
24 class FilePath; | |
25 | |
22 namespace content { | 26 namespace content { |
23 | 27 |
24 class CONTENT_EXPORT MediaDeviceNotificationsLinux | 28 class CONTENT_EXPORT MediaDeviceNotificationsLinux |
25 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux> { | 29 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux> { |
26 public: | 30 public: |
27 explicit MediaDeviceNotificationsLinux(const FilePath& path); | 31 explicit MediaDeviceNotificationsLinux(const FilePath& path); |
28 | 32 |
29 // Must be called for MediaDeviceNotificationsLinux to work. | 33 // Must be called for MediaDeviceNotificationsLinux to work. |
30 void Init(); | 34 void Init(); |
31 | 35 |
32 protected: | 36 protected: |
37 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; | |
38 | |
39 // Avoids code deleting the object while there are references to it. | |
ajl
2012/04/03 23:35:57
I think it would be better in this to explicitly m
Lei Zhang
2012/04/04 05:31:29
Done.
| |
33 virtual ~MediaDeviceNotificationsLinux(); | 40 virtual ~MediaDeviceNotificationsLinux(); |
34 | 41 |
35 virtual void OnFilePathChanged(const FilePath& path); | 42 virtual void OnFilePathChanged(const FilePath& path); |
36 | 43 |
37 private: | 44 private: |
38 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; | 45 class WatcherDelegate; |
39 | 46 |
40 typedef std::string MountDevice; | 47 // (mount device, device id) |
41 typedef std::string MountPoint; | 48 typedef std::pair<std::string, |
42 typedef std::pair<MountDevice, | |
43 base::SystemMonitor::DeviceIdType> MountDeviceAndId; | 49 base::SystemMonitor::DeviceIdType> MountDeviceAndId; |
44 typedef std::map<MountPoint, MountDeviceAndId> MountMap; | 50 // Mapping of mount points to MountDeviceAndId. |
45 | 51 typedef std::map<std::string, MountDeviceAndId> MountMap; |
46 // A simple pass-through class. MediaDeviceNotificationsLinux cannot directly | |
47 // inherit from FilePathWatcher::Delegate due to multiple inheritance. | |
48 class WatcherDelegate : public base::files::FilePathWatcher::Delegate { | |
49 public: | |
50 explicit WatcherDelegate(MediaDeviceNotificationsLinux* notifier); | |
51 | |
52 // base::files::FilePathWatcher::Delegate implementation. | |
53 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; | |
54 | |
55 private: | |
56 friend class base::RefCountedThreadSafe<WatcherDelegate>; | |
57 | |
58 virtual ~WatcherDelegate(); | |
59 MediaDeviceNotificationsLinux* notifier_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(WatcherDelegate); | |
62 }; | |
63 | 52 |
64 void InitOnFileThread(); | 53 void InitOnFileThread(); |
65 | 54 |
66 // Parse the mtab file and find all changes. | 55 // Parse the mtab file and find all changes. |
67 void UpdateMtab(); | 56 void UpdateMtab(); |
68 | 57 |
69 // Read the mtab file entries into |mtab|. | 58 // Read the mtab file entries into |mtab|. |
70 void ReadMtab(MountMap* mtab); | 59 void ReadMtab(MountMap* mtab); |
71 | 60 |
72 // For a new device mounted at |mount_point|, see if it is a media device by | 61 // For a new device mounted at |mount_point|, see if it is a media device by |
73 // checking for the existence of a DCIM directory. | 62 // checking for the existence of a DCIM directory. |
74 // If it is a media device, return true, otherwise return false. | 63 // If it is a media device, return true, otherwise return false. |
75 // Mac OS X behaves similarly, but this is not the only heuristic it uses. | 64 // Mac OS X behaves similarly, but this is not the only heuristic it uses. |
76 // TODO(vandebo) Try to figure out how Mac OS X decides this. | 65 // TODO(vandebo) Try to figure out how Mac OS X decides this. |
77 bool IsMediaDevice(const MountPoint& mount_point); | 66 bool IsMediaDevice(const std::string& mount_point); |
78 | 67 |
79 // Add a media device with a given device and mount device. Assign it a device | 68 // Add a media device with a given device and mount device. Assign it a device |
80 // id as well. | 69 // id as well. |
81 void AddNewDevice(const MountDevice& mount_device, | 70 void AddNewDevice(const std::string& mount_device, |
82 const MountPoint& mount_point, | 71 const std::string& mount_point, |
83 base::SystemMonitor::DeviceIdType* device_id); | 72 base::SystemMonitor::DeviceIdType* device_id); |
84 | 73 |
85 // Remove a media device with a given device id. | 74 // Remove a media device with a given device id. |
86 void RemoveOldDevice(const base::SystemMonitor::DeviceIdType& device_id); | 75 void RemoveOldDevice(const base::SystemMonitor::DeviceIdType& device_id); |
87 | 76 |
88 // Whether Init() has been called or not. | 77 // Whether Init() has been called or not. |
89 bool initialized_; | 78 bool initialized_; |
90 | 79 |
91 // Mtab file that lists the mount points. | 80 // Mtab file that lists the mount points. |
92 const FilePath mtab_path_; | 81 const FilePath mtab_path_; |
(...skipping 12 matching lines...) Expand all Loading... | |
105 | 94 |
106 // Set of known file systems that we care about. | 95 // Set of known file systems that we care about. |
107 std::set<std::string> known_file_systems_; | 96 std::set<std::string> known_file_systems_; |
108 | 97 |
109 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); | 98 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); |
110 }; | 99 }; |
111 | 100 |
112 } // namespace content | 101 } // namespace content |
113 | 102 |
114 #endif // CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 103 #endif // CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
OLD | NEW |