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

Side by Side Diff: chrome/browser/media_gallery/removable_device_notifications_linux.h

Issue 10882039: Make the Linux System Monitor implementation track all devices (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix move status Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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 5 // RemovableDeviceNotificationsLinux listens for mount point changes, notifies
6 // the SystemMonitor about the addition and deletion of media devices. 6 // the SystemMonitor about the addition and deletion of media devices, and
7 // answers queries about mounted devices.
7 8
8 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ 9 #ifndef CHROME_BROWSER_MEDIA_GALLERY_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_
9 #define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ 10 #define CHROME_BROWSER_MEDIA_GALLERY_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_
10 11
11 #if defined(OS_CHROMEOS) 12 #if defined(OS_CHROMEOS)
12 #error "Use the ChromeOS-specific implementation instead." 13 #error "Use the ChromeOS-specific implementation instead."
13 #endif 14 #endif
14 15
15 #include <map> 16 #include <map>
16 #include <set> 17 #include <set>
17 #include <string> 18 #include <string>
18 #include <utility> 19 #include <utility>
19 20
20 #include "base/basictypes.h" 21 #include "base/basictypes.h"
21 #include "base/compiler_specific.h" 22 #include "base/compiler_specific.h"
22 #include "base/files/file_path_watcher.h" 23 #include "base/files/file_path_watcher.h"
23 #include "base/memory/ref_counted.h" 24 #include "base/memory/ref_counted.h"
24 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
25 26
26 class FilePath; 27 class FilePath;
27 28
28 // Gets the media device information given a |device_path|. On success, 29 // Gets the media device information given a |device_path|. On success,
29 // returns true and fills in |name| and |id|. 30 // returns true and fills in |device_name| and |unique_id|.
Lei Zhang 2012/08/28 00:05:04 nit: variable name in comment is not consistent wi
vandebo (ex-Chrome) 2012/08/28 00:33:32 Done.
30 typedef bool (*GetDeviceInfoFunc)(const std::string& device_path, 31 typedef bool (*GetDeviceInfoFunc)(const FilePath& device_path,
31 std::string* id, 32 std::string* unique_id, string16* name,
32 string16* name); 33 bool* removable);
33 34
34 namespace chrome { 35 namespace chrome {
35 36
36 class MediaDeviceNotificationsLinux 37 class RemovableDeviceNotificationsLinux
37 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux, 38 : public base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux,
38 content::BrowserThread::DeleteOnFileThread> { 39 content::BrowserThread::DeleteOnFileThread> {
39 public: 40 public:
40 explicit MediaDeviceNotificationsLinux(const FilePath& path); 41 // Should only be called by browser start up code. Use GetInstance() instead.
42 explicit RemovableDeviceNotificationsLinux(const FilePath& path);
41 43
42 // Must be called for MediaDeviceNotificationsLinux to work. 44 static RemovableDeviceNotificationsLinux* GetInstance();
45
46 // Must be called for RemovableDeviceNotificationsLinux to work.
43 void Init(); 47 void Init();
44 48
49 // Use |unique_id| to find and return where the device is mounted.
Lei Zhang 2012/08/28 00:05:04 nit: variable name in comment is not consistent wi
vandebo (ex-Chrome) 2012/08/28 00:33:32 Done.
50 FilePath GetDeviceMountPoint(const std::string& device_id) const;
51
52 // Determines which device |path| is located on and returns the unique id
53 // for that device. If |mount_point| is not NULL, set it to the mount point
54 // of the device.
55 std::string GetDeviceIdForPath(const FilePath& path,
56 FilePath* mount_point) const;
57
45 protected: 58 protected:
46 // Only for use in unit tests. 59 // Only for use in unit tests.
47 MediaDeviceNotificationsLinux(const FilePath& path, 60 RemovableDeviceNotificationsLinux(const FilePath& path,
48 GetDeviceInfoFunc getDeviceInfo); 61 GetDeviceInfoFunc getDeviceInfo);
49 62
50 // Avoids code deleting the object while there are references to it. 63 // Avoids code deleting the object while there are references to it.
51 // Aside from the base::RefCountedThreadSafe friend class, and derived 64 // Aside from the base::RefCountedThreadSafe friend class, and derived
52 // classes, any attempts to call this dtor will result in a compile-time 65 // classes, any attempts to call this dtor will result in a compile-time
53 // error. 66 // error.
54 virtual ~MediaDeviceNotificationsLinux(); 67 virtual ~RemovableDeviceNotificationsLinux();
55 68
56 virtual void OnFilePathChanged(const FilePath& path, bool error); 69 virtual void OnFilePathChanged(const FilePath& path, bool error);
57 70
58 private: 71 private:
59 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; 72 friend class base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux>;
60 friend class base::DeleteHelper<MediaDeviceNotificationsLinux>; 73 friend class base::DeleteHelper<RemovableDeviceNotificationsLinux>;
61 friend struct content::BrowserThread::DeleteOnThread< 74 friend struct content::BrowserThread::DeleteOnThread<
62 content::BrowserThread::FILE>; 75 content::BrowserThread::FILE>;
63 76
64 // Structure to save mounted device information such as device path and unique 77 // Structure to save mounted device information such as device path and unique
65 // identifier. 78 // identifier.
66 struct MountDeviceAndId { 79 struct MountPointInfo {
67 std::string mount_device; 80 FilePath mount_device;
68 std::string device_id; 81 std::string device_id;
82 string16 device_name;
83 bool has_dcim;
69 }; 84 };
70 85
71 // Mapping of mount points to MountDeviceAndId. 86 // Mapping of mount points to MountPointInfo.
72 typedef std::map<std::string, MountDeviceAndId> MountMap; 87 typedef std::map<FilePath, MountPointInfo> MountMap;
73 88
74 // (mount point, mount device) 89 // (mount point, priority)
75 // Helper Map to get new entries from mtab file. 90 // For devices that are mounted to multiple mount points, this helps us track
76 typedef std::map<std::string, std::string> MountPointDeviceMap; 91 // which one we've notified system monitor about.
92 typedef std::map<FilePath, bool> ReferencedMountPoint;
93
94 // (mount device, map of known mount points)
95 // For each mount device, track the places it is mounted and which one (if
96 // any) we have notified system monitor about.
97 typedef std::map<FilePath, ReferencedMountPoint> MountPriorityMap;
77 98
78 // Do initialization on the File Thread. 99 // Do initialization on the File Thread.
79 void InitOnFileThread(); 100 void InitOnFileThread();
80 101
81 // Parses mtab file and find all changes. 102 // Parses mtab file and find all changes.
82 void UpdateMtab(); 103 void UpdateMtab();
83 104
84 // Reads mtab file entries into |mtab|. 105 // Adds |mount_device| as mounted on |mount_point|. If the deice is a new
Lei Zhang 2012/08/28 00:05:04 typo
vandebo (ex-Chrome) 2012/08/28 00:33:32 Done.
85 void ReadMtab(MountPointDeviceMap* mtab); 106 // media // device, SystemMonitor is notified.
86 107 void AddNewMount(const FilePath& mount_device, const FilePath& mount_point);
87 // Checks and adds |mount_device| as media device given the |mount_point|.
88 void CheckAndAddMediaDevice(const std::string& mount_device,
89 const std::string& mount_point);
90 108
91 // Removes media device with a given device id. 109 // Removes media device with a given device id.
92 void RemoveOldDevice(const std::string& device_id); 110 void RemoveMediaMount(const std::string& device_id);
93 111
94 // Whether Init() has been called or not. 112 // Whether Init() has been called or not.
95 bool initialized_; 113 bool initialized_;
96 114
97 // Mtab file that lists the mount points. 115 // Mtab file that lists the mount points.
98 const FilePath mtab_path_; 116 const FilePath mtab_path_;
99 117
100 // Watcher for |mtab_path_|. 118 // Watcher for |mtab_path_|.
101 base::files::FilePathWatcher file_watcher_; 119 base::files::FilePathWatcher file_watcher_;
102 120
103 // Mapping of relevant mount points and their corresponding mount devices.
104 // Keep in mind on Linux, a device can be mounted at multiple mount points,
105 // and multiple devices can be mounted at a mount point.
106 MountMap mount_info_map_;
107
108 // Set of known file systems that we care about. 121 // Set of known file systems that we care about.
109 std::set<std::string> known_file_systems_; 122 std::set<std::string> known_file_systems_;
110 123
111 // Function handler to get device information. This is useful to set a mock 124 // Function handler to get device information. This is useful to set a mock
112 // handler for unit testing. 125 // handler for unit testing.
113 GetDeviceInfoFunc get_device_info_func_; 126 GetDeviceInfoFunc get_device_info_func_;
114 127
115 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); 128 // Mapping of relevant mount points and their corresponding mount devices.
129 // Keep in mind on Linux, a device can be mounted at multiple mount points,
130 // and multiple devices can be mounted at a mount point.
131 MountMap mount_info_map_;
132
133 // Because a device can be mounted to multiple places, we only want to
134 // notify about one of them. If (and only if) that one is unmounted, we need
135 // to notify about it's departure and notify about another one of it's mount
136 // points.
137 MountPriorityMap mount_priority_map_;
138
139 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinux);
116 }; 140 };
117 141
118 } // namespace chrome 142 } // namespace chrome
119 143
120 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ 144 #endif // CHROME_BROWSER_MEDIA_GALLERY_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698