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

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: '' 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/memory/scoped_generic_obj.h"
18 #include "base/metrics/histogram.h"
16 #include "base/stl_util.h" 19 #include "base/stl_util.h"
17 #include "base/string_number_conversions.h" 20 #include "base/string_number_conversions.h"
21 #include "base/string_util.h"
18 #include "base/system_monitor/system_monitor.h" 22 #include "base/system_monitor/system_monitor.h"
19 #include "base/utf_string_conversions.h" 23 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" 24 #include "chrome/browser/media_gallery/media_device_notifications_utils.h"
21 25
22 namespace { 26 namespace {
23 27
24 // List of file systems we care about. 28 // List of file systems we care about.
25 const char* const kKnownFileSystems[] = { 29 const char* const kKnownFileSystems[] = {
26 "ext2", 30 "ext2",
27 "ext3", 31 "ext3",
28 "ext4", 32 "ext4",
29 "fat", 33 "fat",
30 "hfsplus", 34 "hfsplus",
31 "iso9660", 35 "iso9660",
32 "msdos", 36 "msdos",
33 "ntfs", 37 "ntfs",
34 "udf", 38 "udf",
35 "vfat", 39 "vfat",
36 }; 40 };
37 41
42 // udev device property constants.
43 const char kDevName[] = "DEVNAME";
44 const char kFsUUID[] = "ID_FS_UUID";
45 const char kLabel[] = "ID_FS_LABEL";
46 const char kModel[] = "ID_MODEL";
47 const char kModelID[] = "ID_MODEL_ID";
48 const char kSerial[] = "ID_SERIAL";
49 const char kSerialShort[] = "ID_SERIAL_SHORT";
50 const char kVendor[] = "ID_VENDOR";
51 const char kVendorID[] = "ID_VENDOR_ID";
52
53 // Delimiter constants.
54 const char kNonSpaceDelim[] = ":";
55 const char kSpaceDelim[] = " ";
56
57 // Unique id prefix constants.
58 const char kFSUniqueIdPrefix[] = "UUID:";
59 const char kVendorModelSerialPrefix[] = "VendorModelSerial:";
60
61 // Device mount point details.
62 struct MountPointEntryInfo {
63 std::string mount_point;
64 int entry_pos;
65 };
66
67 // ScopedGenericObj functor for UdevObjectRelease().
68 class ScopedReleaseUdevObject {
69 public:
70 void operator()(struct udev* udev) const {
71 udev_unref(udev);
72 }
73 };
74
75 // ScopedGenericObj functor for UdevDeviceObjectRelease().
76 class ScopedReleaseUdevDeviceObject {
77 public:
78 void operator()(struct udev_device* device) const {
79 udev_device_unref(device);
80 }
81 };
82
83 // Get the device information using udev library.
84 // On success, returns true and fill in |id| and |name|.
85 bool GetDeviceInfo(const std::string& device_path,
86 std::string* id,
87 string16* name) {
88 DCHECK(!device_path.empty());
89
90 ScopedGenericObj<struct udev*, ScopedReleaseUdevObject> udev_obj(udev_new());
91 if (!udev_obj.get())
92 return false;
93
94 struct stat device_stat;
95 if (stat(device_path.c_str(), &device_stat) < 0)
96 return false;
97
98 char device_type;
99 if (S_ISCHR(device_stat.st_mode))
100 device_type = 'c';
101 else if (S_ISBLK(device_stat.st_mode))
102 device_type = 'b';
103 else
104 return false; // Not a supported type.
105
106 ScopedGenericObj<struct udev_device*, ScopedReleaseUdevDeviceObject>
107 device(udev_device_new_from_devnum(udev_obj, device_type,
108 device_stat.st_rdev));
109 if (!device.get())
110 return false;
111
112 // Construct a device name using label or manufacturer(vendor and model)
113 // details.
114 std::string device_label;
115 const char* device_name = NULL;
116 if ((device_name = udev_device_get_property_value(device, kLabel)) ||
117 (device_name = udev_device_get_property_value(device, kSerial))) {
118 device_label = device_name;
119 } else {
120 // Format: VendorInfo ModelInfo
121 // E.g.: KnCompany Model2010
122 const char* vendor_name = NULL;
123 if ((vendor_name = udev_device_get_property_value(device, kVendor)))
124 device_label = vendor_name;
125
126 const char* model_name = NULL;
127 if ((model_name = udev_device_get_property_value(device, kModel))) {
128 if (!device_label.empty())
129 device_label += kSpaceDelim;
130 device_label += model_name;
131 }
132 }
133
134 if (IsStringUTF8(device_label))
James Hawkins 2012/08/14 16:16:29 When is |device_label| not UTF8?
kmadhusu 2012/08/14 17:11:11 With the devices we tested, |device_label| is in U
135 *name = UTF8ToUTF16(device_label);
136
137 const char* uuid = NULL;
138 if ((uuid = udev_device_get_property_value(device, kFsUUID))) {
139 *id = kFSUniqueIdPrefix;
140 *id += uuid;
141 } else {
142 // If one of the vendor, model, serial information is missing, its value in
143 // the string is empty.
144 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialShortInfo
145 // E.g.: VendorModelSerial:Kn:DataTravel_12.10:8000000000006CB02CDB
146 const char* vendor = udev_device_get_property_value(device, kVendorID);
147 const char* model = udev_device_get_property_value(device, kModelID);
148 const char* serial_short = udev_device_get_property_value(device,
149 kSerialShort);
150 if (!vendor && !model && !serial_short)
151 return false;
152
153 *id = kVendorModelSerialPrefix;
154 if (vendor)
155 *id += vendor;
156 *id += kNonSpaceDelim;
157 if (model)
158 *id += model;
159 *id += kNonSpaceDelim;
160 if (serial_short)
161 *id += serial_short;
162 }
163
164 return true;
165 }
166
38 } // namespace 167 } // namespace
39 168
40 namespace chrome { 169 namespace chrome {
41 170
42 using base::SystemMonitor; 171 using base::SystemMonitor;
43 using content::BrowserThread; 172 using content::BrowserThread;
44 173
45 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( 174 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux(
46 const FilePath& path) 175 const FilePath& path)
47 : initialized_(false), 176 : initialized_(false),
48 mtab_path_(path), 177 mtab_path_(path),
49 current_device_id_(0U) { 178 get_device_info_func_(&GetDeviceInfo) {
50 CHECK(!path.empty()); 179 }
51 180
52 // Put |kKnownFileSystems| in std::set to get O(log N) access time. 181 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux(
53 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { 182 const FilePath& path,
54 known_file_systems_.insert(kKnownFileSystems[i]); 183 GetDeviceInfoFunc get_device_info_func)
55 } 184 : initialized_(false),
185 mtab_path_(path),
186 get_device_info_func_(get_device_info_func) {
56 } 187 }
57 188
58 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { 189 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
60 } 191 }
61 192
62 void MediaDeviceNotificationsLinux::Init() { 193 void MediaDeviceNotificationsLinux::Init() {
194 CHECK(!mtab_path_.empty());
James Hawkins 2012/08/14 16:16:29 DCHECK
kmadhusu 2012/08/14 17:11:11 Done.
195
196 // Put |kKnownFileSystems| in std::set to get O(log N) access time.
197 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i)
198 known_file_systems_.insert(kKnownFileSystems[i]);
199
63 BrowserThread::PostTask( 200 BrowserThread::PostTask(
64 BrowserThread::FILE, FROM_HERE, 201 BrowserThread::FILE, FROM_HERE,
65 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); 202 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this));
66 } 203 }
67 204
68 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path, 205 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path,
69 bool error) { 206 bool error) {
70 if (path != mtab_path_) { 207 if (path != mtab_path_) {
71 // This cannot happen unless FilePathWatcher is buggy. Just ignore this 208 // This cannot happen unless FilePathWatcher is buggy. Just ignore this
72 // notification and do nothing. 209 // notification and do nothing.
(...skipping 24 matching lines...) Expand all
97 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; 234 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed";
98 return; 235 return;
99 } 236 }
100 237
101 UpdateMtab(); 238 UpdateMtab();
102 } 239 }
103 240
104 void MediaDeviceNotificationsLinux::UpdateMtab() { 241 void MediaDeviceNotificationsLinux::UpdateMtab() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
106 243
107 MountMap new_mtab; 244 MountPointDeviceMap new_mtab;
108 ReadMtab(&new_mtab); 245 ReadMtab(&new_mtab);
109 246
110 // Check existing mtab entries for unaccounted mount points. 247 // Check existing mtab entries for unaccounted mount points.
111 // These mount points must have been removed in the new mtab. 248 // These mount points must have been removed in the new mtab.
112 std::vector<std::string> mount_points_to_erase; 249 std::vector<std::string> mount_points_to_erase;
113 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { 250 for (MountMap::const_iterator old_iter = mount_info_map_.begin();
114 const std::string& mount_point = it->first; 251 old_iter != mount_info_map_.end(); ++old_iter) {
115 // |mount_point| not in |new_mtab|. 252 const std::string& mount_point = old_iter->first;
116 if (!ContainsKey(new_mtab, mount_point)) { 253 const std::string& mount_device = old_iter->second.mount_device;
117 const std::string& device_id = it->second.second; 254 MountPointDeviceMap::iterator new_iter = new_mtab.find(mount_point);
118 RemoveOldDevice(device_id); 255 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted at
256 // |mount_point|.
257 if (new_iter == new_mtab.end() || (new_iter->second != mount_device)) {
258 RemoveOldDevice(old_iter->second.device_id);
119 mount_points_to_erase.push_back(mount_point); 259 mount_points_to_erase.push_back(mount_point);
120 } 260 }
121 } 261 }
122 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the 262 // Erase the |mount_info_map_| entries afterwards. Erasing in the loop above
123 // iterator is slightly more efficient, but more tricky, since calling 263 // using the iterator is slightly more efficient, but more tricky, since
124 // std::map::erase() on an iterator invalidates it. 264 // calling std::map::erase() on an iterator invalidates it.
125 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) 265 for (size_t i = 0; i < mount_points_to_erase.size(); ++i)
126 mtab_.erase(mount_points_to_erase[i]); 266 mount_info_map_.erase(mount_points_to_erase[i]);
127 267
128 // Check new mtab entries against existing ones. 268 // Check new mtab entries against existing ones.
129 for (MountMap::iterator newiter = new_mtab.begin(); 269 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin();
130 newiter != new_mtab.end(); 270 new_iter != new_mtab.end();
131 ++newiter) { 271 ++new_iter) {
James Hawkins 2012/08/14 16:16:29 Optional nit: This can fit on the line above.
kmadhusu 2012/08/14 17:11:11 Done.
132 const std::string& mount_point = newiter->first; 272 const std::string& mount_point = new_iter->first;
133 const MountDeviceAndId& mount_device_and_id = newiter->second; 273 const std::string& mount_device = new_iter->second;
134 const std::string& mount_device = mount_device_and_id.first; 274 MountMap::iterator old_iter = mount_info_map_.find(mount_point);
135 std::string& id = newiter->second.second; 275 if (old_iter == mount_info_map_.end() ||
136 MountMap::iterator olditer = mtab_.find(mount_point); 276 old_iter->second.mount_device != mount_device) {
137 // Check to see if it is a new mount point. 277 // New mount point found or an existing mount point found with a new
138 if (olditer == mtab_.end()) { 278 // device.
139 if (IsMediaDevice(mount_point)) { 279 CheckAndAddMediaDevice(mount_device, mount_point);
140 AddNewDevice(mount_device, mount_point, &id);
141 mtab_.insert(std::make_pair(mount_point, mount_device_and_id));
142 }
143 continue;
144 }
145
146 // Existing mount point. Check to see if a new device is mounted there.
147 const MountDeviceAndId& old_mount_device_and_id = olditer->second;
148 if (mount_device == old_mount_device_and_id.first)
149 continue;
150
151 // New device mounted.
152 RemoveOldDevice(old_mount_device_and_id.second);
153 if (IsMediaDevice(mount_point)) {
154 AddNewDevice(mount_device, mount_point, &id);
155 olditer->second = mount_device_and_id;
156 } 280 }
157 } 281 }
158 } 282 }
159 283
160 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { 284 void MediaDeviceNotificationsLinux::ReadMtab(MountPointDeviceMap* mtab) {
161 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); 285 FILE* fp = setmntent(mtab_path_.value().c_str(), "r");
162 if (!fp) 286 if (!fp)
163 return; 287 return;
164 288
165 MountMap& 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;
294
295 // Helper map to store the device mount point details.
296 // (mount device, MountPointEntryInfo)
297 typedef std::map<std::string, MountPointEntryInfo> DeviceMap;
171 DeviceMap device_map; 298 DeviceMap device_map;
172 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { 299 while (getmntent_r(fp, &entry, buf, sizeof(buf))) {
173 // We only care about real file systems. 300 // We only care about real file systems.
174 if (!ContainsKey(known_file_systems_, entry.mnt_type)) 301 if (!ContainsKey(known_file_systems_, entry.mnt_type))
175 continue; 302 continue;
303
176 // Add entries, but overwrite entries for the same mount device. Keep track 304 // Add entries, but overwrite entries for the same mount device. Keep track
177 // of the entry positions in the device id field and use that below to 305 // of the entry positions in |entry_info| and use that below to resolve
178 // resolve multiple devices mounted at the same mount point. 306 // multiple devices mounted at the same mount point.
179 MountPointAndId mount_point_and_id = 307 MountPointEntryInfo entry_info;
180 std::make_pair(entry.mnt_dir, base::IntToString(mount_position++)); 308 entry_info.mount_point = entry.mnt_dir;
181 DeviceMap::iterator it = device_map.find(entry.mnt_fsname); 309 entry_info.entry_pos = entry_pos++;
182 if (it == device_map.end()) { 310 device_map[entry.mnt_fsname] = entry_info;
183 device_map.insert(std::make_pair(entry.mnt_fsname, mount_point_and_id));
184 } else {
185 it->second = mount_point_and_id;
186 }
187 } 311 }
188 endmntent(fp); 312 endmntent(fp);
189 313
314 // Helper map to store mount point entries.
315 // (mount point, entry position in mtab file)
316 typedef std::map<std::string, int> MountPointsInfoMap;
317 MountPointsInfoMap mount_points_info_map;
318 MountPointDeviceMap& new_mtab = *mtab;
190 for (DeviceMap::const_iterator device_it = device_map.begin(); 319 for (DeviceMap::const_iterator device_it = device_map.begin();
191 device_it != device_map.end(); 320 device_it != device_map.end();
192 ++device_it) { 321 ++device_it) {
193 const std::string& device = device_it->first; 322 // Add entries, but overwrite entries for the same mount point. Keep track
194 const std::string& mount_point = device_it->second.first; 323 // of the entry positions and use that information to resolve multiple
195 const std::string& position = device_it->second.second; 324 // devices mounted at the same mount point.
325 const std::string& mount_device = device_it->first;
326 const std::string& mount_point = device_it->second.mount_point;
327 const int entry_pos = device_it->second.entry_pos;
328 MountPointDeviceMap::iterator new_it = new_mtab.find(mount_point);
329 MountPointsInfoMap::iterator mount_point_info_map_it =
330 mount_points_info_map.find(mount_point);
196 331
197 // No device at |mount_point|, save |device| to it. 332 // New mount point entry found or there is already a device mounted at
198 MountMap::iterator mount_it = new_mtab.find(mount_point); 333 // |mount_point| and the existing mount entry is older than the current one.
199 if (mount_it == new_mtab.end()) { 334 if (new_it == new_mtab.end() ||
200 new_mtab.insert(std::make_pair(mount_point, 335 (mount_point_info_map_it != mount_points_info_map.end() &&
201 std::make_pair(device, position))); 336 mount_point_info_map_it->second < entry_pos)) {
202 continue; 337 new_mtab[mount_point] = mount_device;
338 mount_points_info_map[mount_point] = entry_pos;
203 } 339 }
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 } 340 }
216 } 341 }
217 342
218 void MediaDeviceNotificationsLinux::AddNewDevice( 343 void MediaDeviceNotificationsLinux::CheckAndAddMediaDevice(
219 const std::string& mount_device, 344 const std::string& mount_device,
220 const std::string& mount_point, 345 const std::string& mount_point) {
221 std::string* device_id) { 346 if (!IsMediaDevice(mount_point))
222 *device_id = base::IntToString(current_device_id_++); 347 return;
348
349 std::string device_id;
350 string16 device_name;
351 bool result = (*get_device_info_func_)(mount_device, &device_id,
352 &device_name);
353
354 // Keep track of GetDeviceInfo result, to see how often we fail to get device
355 // details.
356 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_info_available",
357 result);
358 if (!result)
359 return;
360
361 // Keep track of device uuid, to see how often we receive empty values.
362 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available",
363 !device_id.empty());
364 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available",
365 !device_name.empty());
366
367 if (device_id.empty() || device_name.empty())
368 return;
369
370 MountDeviceAndId mount_device_and_id;
371 mount_device_and_id.mount_device = mount_device;
372 mount_device_and_id.device_id = device_id;
373 mount_info_map_[mount_point] = mount_device_and_id;
374
223 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 375 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
224 system_monitor->ProcessMediaDeviceAttached(*device_id, 376 system_monitor->ProcessMediaDeviceAttached(device_id,
225 UTF8ToUTF16(mount_device), 377 device_name,
226 SystemMonitor::TYPE_PATH, 378 SystemMonitor::TYPE_PATH,
227 mount_point); 379 mount_point);
228 } 380 }
229 381
230 void MediaDeviceNotificationsLinux::RemoveOldDevice( 382 void MediaDeviceNotificationsLinux::RemoveOldDevice(
231 const std::string& device_id) { 383 const std::string& device_id) {
232 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 384 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
233 system_monitor->ProcessMediaDeviceDetached(device_id); 385 system_monitor->ProcessMediaDeviceDetached(device_id);
234 } 386 }
235 387
236 } // namespace chrome 388 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698