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

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

Powered by Google App Engine
This is Rietveld 408576698