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

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

Powered by Google App Engine
This is Rietveld 408576698