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

Side by Side Diff: chrome/browser/media_gallery/media_device_notifications_linux.cc

Issue 10829228: [LINUX] Extract the name and id of the device and send it along the device attach message. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Created 8 years, 4 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 implementation. 5 // MediaDeviceNotificationsLinux implementation.
6 6
7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" 7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h"
8 8
9 #include <libudev.h>
9 #include <mntent.h> 10 #include <mntent.h>
10 #include <stdio.h> 11 #include <stdio.h>
11 12
12 #include <vector> 13 #include <vector>
13 14
14 #include "base/bind.h" 15 #include "base/bind.h"
15 #include "base/file_path.h" 16 #include "base/file_path.h"
17 #include "base/metrics/histogram.h"
16 #include "base/stl_util.h" 18 #include "base/stl_util.h"
17 #include "base/string_number_conversions.h" 19 #include "base/string_number_conversions.h"
18 #include "base/system_monitor/system_monitor.h" 20 #include "base/system_monitor/system_monitor.h"
19 #include "base/utf_string_conversions.h" 21 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" 22 #include "chrome/browser/media_gallery/media_device_notifications_utils.h"
21 23
22 namespace { 24 namespace {
23 25
24 // List of file systems we care about. 26 // List of file systems we care about.
25 const char* const kKnownFileSystems[] = { 27 const char* const kKnownFileSystems[] = {
26 "ext2", 28 "ext2",
27 "ext3", 29 "ext3",
28 "ext4", 30 "ext4",
29 "fat", 31 "fat",
30 "hfsplus", 32 "hfsplus",
31 "iso9660", 33 "iso9660",
32 "msdos", 34 "msdos",
33 "ntfs", 35 "ntfs",
34 "udf", 36 "udf",
35 "vfat", 37 "vfat",
36 }; 38 };
37 39
40 // Device property constants.
41 const char kDevName[] = "DEVNAME";
42 const char kFsUUID[] = "ID_FS_UUID";
43 const char kLabel[] = "ID_FS_LABEL";
44 const char kModel[] = "ID_MODEL";
45 const char kModelID[] = "ID_MODEL_ID";
46 const char kSeperator[] = "_";
47 const char kSerial[] = "ID_SERIAL";
48 const char kSerialShort[] = "ID_SERIAL_SHORT";
49 const char kVendor[] = "ID_VENDOR";
50 const char kVendorID[] = "ID_VENDOR_ID";
51
52 // Get the device information using udev library.
53 // Returns true on success, false on failure.
54 bool GetDeviceInfoHelper(const std::string& dev_path,
55 std::string* id,
56 string16* name) {
57 DCHECK(!dev_path.empty());
58
59 // libudev-related items.
60 udev* udev_ptr;
61 udev_ptr = udev_new();
Lei Zhang 2012/08/09 03:47:16 nit: combine with previous line.
kmadhusu 2012/08/09 08:10:03 Done.
62 CHECK(udev_ptr);
63
64 // Get device file status.
65 struct stat st;
66 if (stat(dev_path.c_str(), &st) == -1) {
Lei Zhang 2012/08/09 03:47:16 usually people do < 0
kmadhusu 2012/08/09 08:10:03 Done.
67 udev_unref(udev_ptr);
68 return false;
69 }
70
71 // Identify the device type.
72 char dev_type;
73 if (S_ISCHR(st.st_mode))
74 dev_type = 'c';
75 else if (S_ISBLK(st.st_mode))
76 dev_type = 'b';
77 else
78 return false; // Not a supported type.
79
80 // Create a new udev_device object from device ID.
81 udev_device* dev = udev_device_new_from_devnum(udev_ptr, dev_type,
Lei Zhang 2012/08/09 03:47:16 Can you name this "device"? It's impossible to sea
kmadhusu 2012/08/09 08:10:03 Done.
82 st.st_rdev);
83 if (!dev) {
84 udev_unref(udev_ptr);
85 return false;
86 }
87
88 // Construct a device name using label or manufacturer(vendor and model)
89 // details.
90 std::string device_name;
91 const char* dev_name = NULL;
92 if ((dev_name = udev_device_get_property_value(dev, kLabel)) ||
93 (dev_name = udev_device_get_property_value(dev, kSerial))) {
94 device_name = dev_name;
95 } else {
96 // Format: VendorInfo_ModelInfo
97 // Eg: KnCompany_Model2010
98 const char *vendor_name = NULL, *model_name = NULL;
Lei Zhang 2012/08/09 03:47:16 nit: char*
kmadhusu 2012/08/09 08:10:03 Since I am doing multiple declarations on a single
99 if ((vendor_name = udev_device_get_property_value(dev, kVendor)))
Lei Zhang 2012/08/09 03:47:16 nit: extra set of () here and below?
kmadhusu 2012/08/09 08:10:03 Since I am using the result value of the expressio
100 device_name = vendor_name;
101 if ((model_name = udev_device_get_property_value(dev, kModel))) {
102 if (!device_name.empty())
103 device_name.append(kSeperator);
Lei Zhang 2012/08/09 03:47:16 still got a bunch of appends.
kmadhusu 2012/08/09 08:10:03 Done. I didn't find enough reference to state that
104 device_name.append(model_name);
105 }
106 }
107 *name = UTF8ToUTF16(device_name);
108
109 // Construct a unique id using fs uuid or manufacturer(vendor and model)
110 // details.
111 const char* uuid = NULL;
112 if ((uuid = udev_device_get_property_value(dev, kFsUUID))) {
113 *id = uuid;
114 } else {
115 // Format: VendorInfo_ModelInfo_SerialShortInfo
116 // Eg: Kn_DataTravel_12.10_8000000000006CB02CDB
117 const char *vendor = NULL, *model = NULL, *serial_short = NULL;
118 if ((vendor = udev_device_get_property_value(dev, kVendorID)))
119 *id = vendor;
120
121 if ((model = udev_device_get_property_value(dev, kModelID))) {
122 if (!id->empty())
123 id->append(kSeperator);
124 id->append(model);
125 }
126 if ((serial_short = udev_device_get_property_value(dev, kSerialShort))) {
127 if (!id->empty())
128 id->append(kSeperator);
129 id->append(serial_short);
130 }
131 }
132
133 udev_unref(udev_ptr);
134 udev_device_unref(dev);
135 return true;
136 }
137
38 } // namespace 138 } // namespace
39 139
40 namespace chrome { 140 namespace chrome {
41 141
42 using base::SystemMonitor; 142 using base::SystemMonitor;
43 using content::BrowserThread; 143 using content::BrowserThread;
44 144
45 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( 145 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux(
46 const FilePath& path) 146 const FilePath& path)
47 : initialized_(false), 147 : initialized_(false),
48 mtab_path_(path), 148 mtab_path_(path) {
49 current_device_id_(0U) {
50 CHECK(!path.empty()); 149 CHECK(!path.empty());
51 150
52 // Put |kKnownFileSystems| in std::set to get O(log N) access time. 151 // Put |kKnownFileSystems| in std::set to get O(log N) access time.
53 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { 152 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) {
54 known_file_systems_.insert(kKnownFileSystems[i]); 153 known_file_systems_.insert(kKnownFileSystems[i]);
55 } 154 }
56 } 155 }
57 156
58 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { 157 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
(...skipping 14 matching lines...) Expand all
74 return; 173 return;
75 } 174 }
76 if (error) { 175 if (error) {
77 LOG(ERROR) << "Error watching " << mtab_path_.value(); 176 LOG(ERROR) << "Error watching " << mtab_path_.value();
78 return; 177 return;
79 } 178 }
80 179
81 UpdateMtab(); 180 UpdateMtab();
82 } 181 }
83 182
183 bool MediaDeviceNotificationsLinux::GetDeviceInfo(const std::string& dev_path,
184 std::string* id,
185 string16* name) {
186 if (dev_path.empty())
187 return false;
188
189 return GetDeviceInfoHelper(dev_path, id, name);
Lei Zhang 2012/08/09 03:47:16 I don't understand why you need to keep udev out o
kmadhusu 2012/08/09 08:10:03 Done.
190 }
191
84 void MediaDeviceNotificationsLinux::InitOnFileThread() { 192 void MediaDeviceNotificationsLinux::InitOnFileThread() {
85 DCHECK(!initialized_); 193 DCHECK(!initialized_);
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
87 initialized_ = true; 195 initialized_ = true;
88 196
89 // The callback passed to Watch() has to be unretained. Otherwise 197 // The callback passed to Watch() has to be unretained. Otherwise
90 // MediaDeviceNotificationsLinux will live longer than expected, and 198 // MediaDeviceNotificationsLinux will live longer than expected, and
91 // FilePathWatcher will get in trouble at shutdown time. 199 // FilePathWatcher will get in trouble at shutdown time.
92 bool ret = file_watcher_.Watch( 200 bool ret = file_watcher_.Watch(
93 mtab_path_, 201 mtab_path_,
94 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, 202 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged,
95 base::Unretained(this))); 203 base::Unretained(this)));
96 if (!ret) { 204 if (!ret) {
97 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; 205 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed";
98 return; 206 return;
99 } 207 }
100 208
101 UpdateMtab(); 209 UpdateMtab();
102 } 210 }
103 211
104 void MediaDeviceNotificationsLinux::UpdateMtab() { 212 void MediaDeviceNotificationsLinux::UpdateMtab() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
106 214
107 MountMap new_mtab; 215 MountPointDeviceMap new_mtab;
108 ReadMtab(&new_mtab); 216 ReadMtab(&new_mtab);
109 217
110 // Check existing mtab entries for unaccounted mount points. 218 // Check existing mtab entries for unaccounted mount points.
111 // These mount points must have been removed in the new mtab. 219 // These mount points must have been removed in the new mtab.
112 std::vector<std::string> mount_points_to_erase; 220 std::vector<std::string> mount_points_to_erase;
113 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { 221 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) {
114 const std::string& mount_point = it->first; 222 const std::string& mount_point = it->first;
115 // |mount_point| not in |new_mtab|. 223 const std::string& mount_device = it->second.first;
116 if (!ContainsKey(new_mtab, mount_point)) { 224 MountPointDeviceMap::iterator newiter = new_mtab.find(mount_point);
225 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted in
Lei Zhang 2012/08/09 03:47:16 nit: mounted in -> mounted at
kmadhusu 2012/08/09 08:10:03 Done.
226 // |mount_point|.
227 if (newiter == new_mtab.end() || newiter->second == mount_device) {
117 const std::string& device_id = it->second.second; 228 const std::string& device_id = it->second.second;
118 RemoveOldDevice(device_id); 229 RemoveOldDevice(device_id);
119 mount_points_to_erase.push_back(mount_point); 230 mount_points_to_erase.push_back(mount_point);
120 } 231 }
121 } 232 }
122 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the 233 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the
123 // iterator is slightly more efficient, but more tricky, since calling 234 // iterator is slightly more efficient, but more tricky, since calling
124 // std::map::erase() on an iterator invalidates it. 235 // std::map::erase() on an iterator invalidates it.
125 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) 236 for (size_t i = 0; i < mount_points_to_erase.size(); ++i)
126 mtab_.erase(mount_points_to_erase[i]); 237 mtab_.erase(mount_points_to_erase[i]);
127 238
128 // Check new mtab entries against existing ones. 239 // Check new mtab entries against existing ones.
129 for (MountMap::iterator newiter = new_mtab.begin(); 240 for (MountPointDeviceMap::iterator newiter = new_mtab.begin();
130 newiter != new_mtab.end(); 241 newiter != new_mtab.end();
131 ++newiter) { 242 ++newiter) {
243 std::string device_id;
132 const std::string& mount_point = newiter->first; 244 const std::string& mount_point = newiter->first;
133 const MountDeviceAndId& mount_device_and_id = newiter->second; 245 const std::string& mount_device = newiter->second;
134 const std::string& mount_device = mount_device_and_id.first;
135 std::string& id = newiter->second.second;
136 MountMap::iterator olditer = mtab_.find(mount_point); 246 MountMap::iterator olditer = mtab_.find(mount_point);
137 // Check to see if it is a new mount point. 247 // Check to see if it is a new mount point.
138 if (olditer == mtab_.end()) { 248 if (olditer == mtab_.end()) {
139 if (IsMediaDevice(mount_point)) { 249 if (IsMediaDevice(mount_point)) {
140 AddNewDevice(mount_device, mount_point, &id); 250 AddNewDevice(mount_device, mount_point, &device_id);
141 mtab_.insert(std::make_pair(mount_point, mount_device_and_id)); 251 mtab_.insert(std::make_pair(mount_point,
252 std::make_pair(mount_device, device_id)));
142 } 253 }
143 continue; 254 continue;
144 } 255 }
145 256
146 // Existing mount point. Check to see if a new device is mounted there. 257 // Existing mount point. Check to see if a new device is mounted there.
147 const MountDeviceAndId& old_mount_device_and_id = olditer->second; 258 if (mount_device == olditer->second.first)
148 if (mount_device == old_mount_device_and_id.first)
149 continue; 259 continue;
150 260
151 // New device mounted. 261 // New device mounted.
152 RemoveOldDevice(old_mount_device_and_id.second);
153 if (IsMediaDevice(mount_point)) { 262 if (IsMediaDevice(mount_point)) {
154 AddNewDevice(mount_device, mount_point, &id); 263 AddNewDevice(mount_device, mount_point, &device_id);
155 olditer->second = mount_device_and_id; 264 olditer->second = std::make_pair(mount_device, device_id);
156 } 265 }
157 } 266 }
158 } 267 }
159 268
160 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { 269 void MediaDeviceNotificationsLinux::ReadMtab(MountPointDeviceMap* mtab) {
161 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); 270 FILE* fp = setmntent(mtab_path_.value().c_str(), "r");
162 if (!fp) 271 if (!fp)
163 return; 272 return;
164 273
165 MountMap& new_mtab = *mtab; 274 // (mount point, entry position in mtab file)
275 typedef std::pair<std::string, int> MountEntryInfo;
Lei Zhang 2012/08/09 03:47:16 You can probably fold this into the next typedef,
kmadhusu 2012/08/09 08:10:03 Done.
276
277 // (mount device, MountEntryInfo)
278 typedef std::map<std::string, MountEntryInfo> DeviceMap;
279
280 // (mount point, entry position in mtab file)
281 typedef std::map<std::string, int> MountPointsInfoMap;
282
283 // Helper maps to store the device mount point details and mount point
284 // entries.
285 DeviceMap device_map;
286 MountPointsInfoMap mount_points_info_map;
Lei Zhang 2012/08/09 03:47:16 you can declare this and its typedef further below
kmadhusu 2012/08/09 08:10:03 Done.
287
288 MountPointDeviceMap& new_mtab = *mtab;
166 mntent entry; 289 mntent entry;
167 char buf[512]; 290 char buf[512];
168 int mount_position = 0; 291
169 typedef std::pair<std::string, std::string> MountPointAndId; 292 // Keep track of mount point entry positions in mtab file.
170 typedef std::map<std::string, MountPointAndId> DeviceMap; 293 int entry_pos = 0;
171 DeviceMap device_map; 294
172 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { 295 while (getmntent_r(fp, &entry, buf, sizeof(buf))) {
173 // We only care about real file systems. 296 // We only care about real file systems.
174 if (!ContainsKey(known_file_systems_, entry.mnt_type)) 297 if (!ContainsKey(known_file_systems_, entry.mnt_type))
175 continue; 298 continue;
176 // Add entries, but overwrite entries for the same mount device. Keep track 299 const std::string& mount_device = entry.mnt_fsname;
177 // of the entry positions in the device id field and use that below to 300 const std::string& mount_point = entry.mnt_dir;
178 // resolve multiple devices mounted at the same mount point. 301 DeviceMap::iterator it = device_map.find(mount_device);
179 MountPointAndId mount_point_and_id =
180 std::make_pair(entry.mnt_dir, base::IntToString(mount_position++));
181 DeviceMap::iterator it = device_map.find(entry.mnt_fsname);
182 if (it == device_map.end()) { 302 if (it == device_map.end()) {
183 device_map.insert(std::make_pair(entry.mnt_fsname, mount_point_and_id)); 303 device_map.insert(std::make_pair(mount_device,
304 std::make_pair(mount_point,
305 entry_pos++)));
184 } else { 306 } else {
185 it->second = mount_point_and_id; 307 it->second = std::make_pair(mount_point, entry_pos++);
186 } 308 }
187 } 309 }
188 endmntent(fp); 310 endmntent(fp);
189 311
190 for (DeviceMap::const_iterator device_it = device_map.begin(); 312 for (DeviceMap::const_iterator device_it = device_map.begin();
191 device_it != device_map.end(); 313 device_it != device_map.end();
192 ++device_it) { 314 ++device_it) {
193 const std::string& device = device_it->first; 315 const std::string& mount_device = device_it->first;
194 const std::string& mount_point = device_it->second.first; 316 const std::string& mount_point = device_it->second.first;
195 const std::string& position = device_it->second.second; 317 const int entry_pos = device_it->second.second;
196 318 MountPointDeviceMap::iterator new_it = new_mtab.find(mount_point);
Lei Zhang 2012/08/09 03:47:16 you may want to rename |new_it| and |it| to |new_m
kmadhusu 2012/08/09 08:10:03 Done.
197 // No device at |mount_point|, save |device| to it. 319 if (new_it == new_mtab.end()) {
198 MountMap::iterator mount_it = new_mtab.find(mount_point); 320 // No device at |mount_point|, save |device| to it.
199 if (mount_it == new_mtab.end()) { 321 new_mtab.insert(std::make_pair(mount_point, mount_device));
200 new_mtab.insert(std::make_pair(mount_point, 322 mount_points_info_map.insert(std::make_pair(mount_point, entry_pos));
201 std::make_pair(device, position))); 323 } else {
202 continue; 324 MountPointsInfoMap::iterator it = mount_points_info_map.find(mount_point);
325 DCHECK(it != mount_points_info_map.end());
326 // There is already a device mounted at |mount_point|. Check to see if
327 // the existing mount entry is newer than the current one.
328 if (it->second < entry_pos) {
329 // The current entry is newer, update the mount point entry.
330 it->second = entry_pos;
331 new_it->second = mount_device;
332 }
203 } 333 }
204
205 // There is already a device mounted at |mount_point|. Check to see if
206 // the existing mount entry is newer than the current one.
207 std::string& existing_device = mount_it->second.first;
208 std::string& existing_position = mount_it->second.second;
209 if (existing_position > position)
210 continue;
211
212 // The current entry is newer, update the mount point entry.
213 existing_device = device;
214 existing_position = position;
215 } 334 }
216 } 335 }
217 336
218 void MediaDeviceNotificationsLinux::AddNewDevice( 337 void MediaDeviceNotificationsLinux::AddNewDevice(
219 const std::string& mount_device, 338 const std::string& mount_device,
220 const std::string& mount_point, 339 const std::string& mount_point,
221 std::string* device_id) { 340 std::string* device_id) {
222 *device_id = base::IntToString(current_device_id_++); 341 string16 device_name;
342 if (!GetDeviceInfo(mount_device, device_id, &device_name))
Lei Zhang 2012/08/09 03:47:16 You may also want to keep track of how often this
kmadhusu 2012/08/09 08:10:03 Done.
343 return;
344
345 // Keep track of device uuid, to see how often we receive empty values.
346 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available",
347 !device_id->empty());
348 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available",
349 !device_name.empty());
350
351 if (device_id->empty() || device_name.empty())
352 return;
353
223 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 354 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
224 system_monitor->ProcessMediaDeviceAttached(*device_id, 355 system_monitor->ProcessMediaDeviceAttached(*device_id,
225 UTF8ToUTF16(mount_device), 356 device_name,
226 SystemMonitor::TYPE_PATH, 357 SystemMonitor::TYPE_PATH,
227 mount_point); 358 mount_point);
228 } 359 }
229 360
230 void MediaDeviceNotificationsLinux::RemoveOldDevice( 361 void MediaDeviceNotificationsLinux::RemoveOldDevice(
231 const std::string& device_id) { 362 const std::string& device_id) {
232 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 363 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
233 system_monitor->ProcessMediaDeviceDetached(device_id); 364 system_monitor->ProcessMediaDeviceDetached(device_id);
234 } 365 }
235 366
236 } // namespace chrome 367 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698