Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 | 11 |
| 12 #include <libudev.h> | |
|
Lei Zhang
2012/08/09 00:32:41
not: C header should be grouped with the other C h
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 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" |
| 19 #include "base/string_util.h" | |
|
Lei Zhang
2012/08/09 00:32:41
nit: alphabetical order.
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 17 #include "base/string_number_conversions.h" | 20 #include "base/string_number_conversions.h" |
| 18 #include "base/system_monitor/system_monitor.h" | 21 #include "base/system_monitor/system_monitor.h" |
| 19 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" | 23 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 // List of file systems we care about. | 27 // List of file systems we care about. |
| 25 const char* const kKnownFileSystems[] = { | 28 const char* const kKnownFileSystems[] = { |
| 26 "ext2", | 29 "ext2", |
| 27 "ext3", | 30 "ext3", |
| 28 "ext4", | 31 "ext4", |
| 29 "fat", | 32 "fat", |
| 30 "hfsplus", | 33 "hfsplus", |
| 31 "iso9660", | 34 "iso9660", |
| 32 "msdos", | 35 "msdos", |
| 33 "ntfs", | 36 "ntfs", |
| 34 "udf", | 37 "udf", |
| 35 "vfat", | 38 "vfat", |
| 36 }; | 39 }; |
| 37 | 40 |
| 41 // Device property constants. | |
| 42 const char kDevName[] = "DEVNAME"; | |
| 43 const char kFsUUID[] = "ID_FS_UUID"; | |
| 44 const char kLabel[] = "ID_FS_LABEL"; | |
| 45 const char kModel[] = "ID_MODEL"; | |
| 46 const char kModelID[] = "ID_MODEL_ID"; | |
| 47 const char kSeperator[] = "_"; | |
| 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 // Return syspath from devpath or an empty string on failure. | |
| 54 std::string GetSysPathFromDevPath(struct udev* udev, | |
| 55 const std::string& dev_path) { | |
| 56 DCHECK(!dev_path.empty()); | |
| 57 | |
| 58 udev_enumerate* enumerate = udev_enumerate_new(udev); | |
| 59 if (!enumerate) | |
|
Lei Zhang
2012/08/09 00:32:41
Just CHECK(enumerate). It should never fail, and i
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 60 return std::string(); | |
| 61 | |
| 62 if (udev_enumerate_add_match_property(enumerate, kDevName, | |
| 63 dev_path.c_str()) < 0 || | |
| 64 udev_enumerate_scan_devices(enumerate) < 0) { | |
| 65 udev_enumerate_unref(enumerate); | |
| 66 return std::string(); | |
| 67 } | |
| 68 | |
| 69 // Enumerate the devices that matches the specified device name filter. | |
| 70 udev_list_entry* devs = udev_enumerate_get_list_entry(enumerate); | |
| 71 if (!devs) { | |
| 72 udev_enumerate_unref(enumerate); | |
| 73 return std::string(); | |
| 74 } | |
| 75 | |
| 76 udev_list_entry *cur = NULL; | |
|
Lei Zhang
2012/08/09 00:32:41
nit: foo_struct* varname; Same elsewhere.
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 77 std::string sys_path; | |
| 78 udev_list_entry_foreach(cur, devs) { | |
|
Lei Zhang
2012/08/09 00:32:41
Why not just call udev_list_entry_get_next()? Can
kmadhusu
2012/08/09 02:48:40
I don't expect more than one entry. Just to be on
| |
| 79 // Get the first entry and return. | |
| 80 sys_path.assign(udev_list_entry_get_name(cur)); | |
|
Lei Zhang
2012/08/09 00:32:41
Is there a reason you use std_string_var.assign()
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 81 break; | |
| 82 } | |
| 83 | |
| 84 udev_enumerate_unref(enumerate); | |
| 85 return sys_path; | |
| 86 } | |
| 87 | |
| 88 // Get the device information using udev library. | |
| 89 // Returns true on success, false on failure. | |
| 90 bool GetDeviceInfoHelper(const std::string& dev_path, | |
| 91 std::string* id, | |
| 92 string16* name) { | |
| 93 DCHECK(!dev_path.empty()); | |
| 94 | |
| 95 // libudev-related items. | |
| 96 udev* udev_ptr; | |
| 97 udev_ptr = udev_new(); | |
| 98 if (!udev_ptr) | |
|
Lei Zhang
2012/08/09 00:32:41
Just CHECK() instead.
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 99 return false; | |
| 100 | |
| 101 // Get sys path from dev path. | |
| 102 std::string sys_path = GetSysPathFromDevPath(udev_ptr, dev_path); | |
| 103 if (sys_path.empty()) { | |
| 104 udev_unref(udev_ptr); | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 // Create a new udev_device object from sys path. | |
| 109 udev_device* dev = udev_device_new_from_syspath(udev_ptr, sys_path.c_str()); | |
|
Lei Zhang
2012/08/09 00:32:41
I think you may be able to just stat(dev_path) and
kmadhusu
2012/08/09 02:48:40
Thanks for the pointer. That worked.
| |
| 110 if (!dev) { | |
| 111 udev_unref(udev_ptr); | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 // Construct a device name using label or vendor and model information. | |
|
Lei Zhang
2012/08/09 00:32:41
From the comment, I can't tell if this means
(lab
kmadhusu
2012/08/09 02:48:40
Rephrased the comment to "Construct a device name
| |
| 116 std::string device_name; | |
| 117 const char* dev_name = NULL; | |
| 118 if ((dev_name = udev_device_get_property_value(dev, kLabel)) || | |
| 119 (dev_name = udev_device_get_property_value(dev, kSerial))) { | |
| 120 device_name.assign(dev_name); | |
| 121 } else { | |
| 122 // Format: VendorInfo_ModelInfo | |
| 123 // Eg: KnCompany_Model2010 | |
| 124 const char *vendor_name = NULL, *model_name = NULL; | |
| 125 if ((vendor_name = udev_device_get_property_value(dev, kVendor))) | |
| 126 device_name.assign(vendor_name); | |
| 127 if ((model_name = udev_device_get_property_value(dev, kModel))) { | |
| 128 if (!device_name.empty()) | |
| 129 device_name.append(kSeperator); | |
| 130 device_name.append(model_name); | |
| 131 } | |
| 132 } | |
| 133 *name = UTF8ToUTF16(device_name); | |
| 134 | |
| 135 // Construct a unique id using fs uuid or vendor and model information. | |
| 136 const char* uuid = NULL; | |
| 137 if ((uuid = udev_device_get_property_value(dev, kFsUUID))) { | |
| 138 id->assign(uuid); | |
| 139 } else { | |
| 140 // Format: VendorInfo_ModelInfo_SerialShortInfo | |
| 141 // Eg: Kn_DataTravel_12.10_8000000000006CB02CDB | |
| 142 const char *vendor = NULL, *model = NULL, *serial_short = NULL; | |
| 143 if ((vendor = udev_device_get_property_value(dev, kVendorID))) | |
| 144 id->assign(vendor); | |
| 145 | |
| 146 if ((model = udev_device_get_property_value(dev, kModelID))) { | |
| 147 if (!id->empty()) | |
| 148 id->append(kSeperator); | |
| 149 id->append(model); | |
| 150 } | |
| 151 if ((serial_short = udev_device_get_property_value(dev, kSerialShort))) { | |
| 152 if (!id->empty()) | |
| 153 id->append(kSeperator); | |
| 154 id->append(serial_short); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 udev_unref(udev_ptr); | |
| 159 udev_device_unref(dev); | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 38 } // namespace | 163 } // namespace |
| 39 | 164 |
| 40 namespace chrome { | 165 namespace chrome { |
| 41 | 166 |
| 42 using base::SystemMonitor; | 167 using base::SystemMonitor; |
| 43 using content::BrowserThread; | 168 using content::BrowserThread; |
| 44 | 169 |
| 45 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | 170 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( |
| 46 const FilePath& path) | 171 const FilePath& path) |
| 47 : initialized_(false), | 172 : initialized_(false), |
| 48 mtab_path_(path), | 173 mtab_path_(path) { |
| 49 current_device_id_(0U) { | |
| 50 CHECK(!path.empty()); | 174 CHECK(!path.empty()); |
| 51 | 175 |
| 52 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | 176 // Put |kKnownFileSystems| in std::set to get O(log N) access time. |
| 53 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { | 177 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { |
| 54 known_file_systems_.insert(kKnownFileSystems[i]); | 178 known_file_systems_.insert(kKnownFileSystems[i]); |
| 55 } | 179 } |
| 56 } | 180 } |
| 57 | 181 |
| 58 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | 182 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 74 return; | 198 return; |
| 75 } | 199 } |
| 76 if (error) { | 200 if (error) { |
| 77 LOG(ERROR) << "Error watching " << mtab_path_.value(); | 201 LOG(ERROR) << "Error watching " << mtab_path_.value(); |
| 78 return; | 202 return; |
| 79 } | 203 } |
| 80 | 204 |
| 81 UpdateMtab(); | 205 UpdateMtab(); |
| 82 } | 206 } |
| 83 | 207 |
| 208 bool MediaDeviceNotificationsLinux::GetDeviceInfo(const std::string& dev_path, | |
|
Lei Zhang
2012/08/09 00:32:41
Is it possible to make this static, get rid of Get
kmadhusu
2012/08/09 02:48:40
I don't want to add udev function calls in this cl
| |
| 209 std::string* id, | |
| 210 string16* name) { | |
| 211 if (dev_path.empty()) | |
| 212 return false; | |
| 213 | |
| 214 return GetDeviceInfoHelper(dev_path, id, name); | |
| 215 } | |
| 216 | |
| 84 void MediaDeviceNotificationsLinux::InitOnFileThread() { | 217 void MediaDeviceNotificationsLinux::InitOnFileThread() { |
| 85 DCHECK(!initialized_); | 218 DCHECK(!initialized_); |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 87 initialized_ = true; | 220 initialized_ = true; |
| 88 | 221 |
| 89 // The callback passed to Watch() has to be unretained. Otherwise | 222 // The callback passed to Watch() has to be unretained. Otherwise |
| 90 // MediaDeviceNotificationsLinux will live longer than expected, and | 223 // MediaDeviceNotificationsLinux will live longer than expected, and |
| 91 // FilePathWatcher will get in trouble at shutdown time. | 224 // FilePathWatcher will get in trouble at shutdown time. |
| 92 bool ret = file_watcher_.Watch( | 225 bool ret = file_watcher_.Watch( |
| 93 mtab_path_, | 226 mtab_path_, |
| 94 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, | 227 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, |
| 95 base::Unretained(this))); | 228 base::Unretained(this))); |
| 96 if (!ret) { | 229 if (!ret) { |
| 97 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | 230 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; |
| 98 return; | 231 return; |
| 99 } | 232 } |
| 100 | 233 |
| 101 UpdateMtab(); | 234 UpdateMtab(); |
| 102 } | 235 } |
| 103 | 236 |
| 104 void MediaDeviceNotificationsLinux::UpdateMtab() { | 237 void MediaDeviceNotificationsLinux::UpdateMtab() { |
| 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 106 | 239 |
| 107 MountMap new_mtab; | 240 MountPointDeviceMap new_mtab; |
| 108 ReadMtab(&new_mtab); | 241 ReadMtab(&new_mtab); |
| 109 | 242 |
| 110 // Check existing mtab entries for unaccounted mount points. | 243 // Check existing mtab entries for unaccounted mount points. |
| 111 // These mount points must have been removed in the new mtab. | 244 // These mount points must have been removed in the new mtab. |
| 112 std::vector<std::string> mount_points_to_erase; | 245 std::vector<std::string> mount_points_to_erase; |
| 113 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { | 246 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { |
| 114 const std::string& mount_point = it->first; | 247 const std::string& mount_point = it->first; |
| 115 // |mount_point| not in |new_mtab|. | 248 const std::string& mount_device = it->second.first; |
| 116 if (!ContainsKey(new_mtab, mount_point)) { | 249 MountPointDeviceMap::iterator newiter = new_mtab.find(mount_point); |
| 250 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted in | |
| 251 // |mount_point|. | |
| 252 if ((!ContainsKey(new_mtab, mount_point)) || | |
| 253 (base::strcasecmp(newiter->second.c_str(), | |
|
Lei Zhang
2012/08/09 00:32:41
Can't you just compare |newiter->second| to |mount
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 254 mount_device.c_str()) != 0)) { | |
| 117 const std::string& device_id = it->second.second; | 255 const std::string& device_id = it->second.second; |
| 118 RemoveOldDevice(device_id); | 256 RemoveOldDevice(device_id); |
| 119 mount_points_to_erase.push_back(mount_point); | 257 mount_points_to_erase.push_back(mount_point); |
| 120 } | 258 } |
| 121 } | 259 } |
| 122 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the | 260 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the |
| 123 // iterator is slightly more efficient, but more tricky, since calling | 261 // iterator is slightly more efficient, but more tricky, since calling |
| 124 // std::map::erase() on an iterator invalidates it. | 262 // std::map::erase() on an iterator invalidates it. |
| 125 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) | 263 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) |
| 126 mtab_.erase(mount_points_to_erase[i]); | 264 mtab_.erase(mount_points_to_erase[i]); |
| 127 | 265 |
| 128 // Check new mtab entries against existing ones. | 266 // Check new mtab entries against existing ones. |
| 129 for (MountMap::iterator newiter = new_mtab.begin(); | 267 for (MountPointDeviceMap::iterator newiter = new_mtab.begin(); |
| 130 newiter != new_mtab.end(); | 268 newiter != new_mtab.end(); |
| 131 ++newiter) { | 269 ++newiter) { |
| 270 std::string device_id; | |
| 132 const std::string& mount_point = newiter->first; | 271 const std::string& mount_point = newiter->first; |
| 133 const MountDeviceAndId& mount_device_and_id = newiter->second; | 272 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); | 273 MountMap::iterator olditer = mtab_.find(mount_point); |
| 137 // Check to see if it is a new mount point. | 274 // Check to see if it is a new mount point. |
| 138 if (olditer == mtab_.end()) { | 275 if (olditer == mtab_.end()) { |
| 139 if (IsMediaDevice(mount_point)) { | 276 if (IsMediaDevice(mount_point)) { |
| 140 AddNewDevice(mount_device, mount_point, &id); | 277 AddNewDevice(mount_device, mount_point, &device_id); |
| 141 mtab_.insert(std::make_pair(mount_point, mount_device_and_id)); | 278 mtab_.insert(std::make_pair(mount_point, |
| 279 std::make_pair(mount_device, device_id))); | |
| 142 } | 280 } |
| 143 continue; | 281 continue; |
| 144 } | 282 } |
| 145 | 283 |
| 146 // Existing mount point. Check to see if a new device is mounted there. | 284 // Existing mount point. Check to see if a new device is mounted there. |
| 147 const MountDeviceAndId& old_mount_device_and_id = olditer->second; | 285 if (mount_device == olditer->second.first) |
| 148 if (mount_device == old_mount_device_and_id.first) | |
| 149 continue; | 286 continue; |
| 150 | 287 |
| 151 // New device mounted. | |
| 152 RemoveOldDevice(old_mount_device_and_id.second); | |
| 153 if (IsMediaDevice(mount_point)) { | 288 if (IsMediaDevice(mount_point)) { |
| 154 AddNewDevice(mount_device, mount_point, &id); | 289 AddNewDevice(mount_device, mount_point, &device_id); |
| 155 olditer->second = mount_device_and_id; | 290 mtab_[mount_point] = std::make_pair(mount_device, device_id); |
| 156 } | 291 } |
| 157 } | 292 } |
| 158 } | 293 } |
| 159 | 294 |
| 160 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { | 295 void MediaDeviceNotificationsLinux::ReadMtab(MountPointDeviceMap* mtab) { |
| 161 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); | 296 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); |
| 162 if (!fp) | 297 if (!fp) |
| 163 return; | 298 return; |
| 164 | 299 |
| 165 MountMap& new_mtab = *mtab; | 300 // Mount point entry position. |
| 301 typedef int EntryPos; | |
|
Lei Zhang
2012/08/09 00:32:41
just use int.
kmadhusu
2012/08/09 02:48:40
Done.
| |
| 302 | |
| 303 // (mount point, entry position in mtab file) | |
| 304 typedef std::pair<std::string, EntryPos> MountEntryInfo; | |
| 305 | |
| 306 // (mount device, MountEntryInfo) | |
| 307 typedef std::map<std::string, MountEntryInfo> DeviceMap; | |
| 308 | |
| 309 // (mount point, entry position in mtab file) | |
| 310 typedef std::map<std::string, EntryPos> MountPointsInfoMap; | |
| 311 | |
| 312 // Helper maps to store the device mount point details and mount point | |
| 313 // entries. | |
| 314 DeviceMap device_map; | |
| 315 MountPointsInfoMap mount_points_info_map; | |
| 316 | |
| 317 MountPointDeviceMap& new_mtab = *mtab; | |
| 166 mntent entry; | 318 mntent entry; |
| 167 char buf[512]; | 319 char buf[512]; |
| 168 int mount_position = 0; | 320 |
| 169 typedef std::pair<std::string, std::string> MountPointAndId; | 321 // Keep track of mount point entry positions in mtab file. |
| 170 typedef std::map<std::string, MountPointAndId> DeviceMap; | 322 EntryPos entry_pos = 0; |
| 171 DeviceMap device_map; | 323 |
| 172 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | 324 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { |
| 173 // We only care about real file systems. | 325 // We only care about real file systems. |
| 174 if (!ContainsKey(known_file_systems_, entry.mnt_type)) | 326 if (!ContainsKey(known_file_systems_, entry.mnt_type)) |
| 175 continue; | 327 continue; |
| 176 // Add entries, but overwrite entries for the same mount device. Keep track | 328 const std::string mount_device = entry.mnt_fsname; |
| 177 // of the entry positions in the device id field and use that below to | 329 const std::string mount_point = entry.mnt_dir; |
| 178 // resolve multiple devices mounted at the same mount point. | 330 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()) { | 331 if (it == device_map.end()) { |
| 183 device_map.insert(std::make_pair(entry.mnt_fsname, mount_point_and_id)); | 332 device_map.insert(std::make_pair(mount_device, |
| 333 std::make_pair(mount_point, | |
| 334 entry_pos++))); | |
| 184 } else { | 335 } else { |
| 185 it->second = mount_point_and_id; | 336 device_map[mount_device] = std::make_pair(mount_point, entry_pos++); |
| 186 } | 337 } |
| 187 } | 338 } |
| 188 endmntent(fp); | 339 endmntent(fp); |
| 189 | 340 |
| 190 for (DeviceMap::const_iterator device_it = device_map.begin(); | 341 for (DeviceMap::const_iterator device_it = device_map.begin(); |
| 191 device_it != device_map.end(); | 342 device_it != device_map.end(); |
| 192 ++device_it) { | 343 ++device_it) { |
| 193 const std::string& device = device_it->first; | 344 const std::string mount_device = device_it->first; |
| 194 const std::string& mount_point = device_it->second.first; | 345 const std::string mount_point = device_it->second.first; |
| 195 const std::string& position = device_it->second.second; | 346 const EntryPos entry_pos = device_it->second.second; |
| 196 | 347 MountPointDeviceMap::iterator new_it = new_mtab.find(mount_point); |
| 197 // No device at |mount_point|, save |device| to it. | 348 if (new_it == new_mtab.end()) { |
| 198 MountMap::iterator mount_it = new_mtab.find(mount_point); | 349 new_mtab.insert(std::make_pair(mount_point, mount_device)); |
| 199 if (mount_it == new_mtab.end()) { | 350 mount_points_info_map.insert(std::make_pair(mount_point, entry_pos)); |
| 200 new_mtab.insert(std::make_pair(mount_point, | 351 } else { |
| 201 std::make_pair(device, position))); | 352 MountPointsInfoMap::iterator it = mount_points_info_map.find(mount_point); |
| 202 continue; | 353 DCHECK(it != mount_points_info_map.end()); |
| 354 // There is already a device mounted at |mount_point|. Check to see if | |
| 355 // the existing mount entry is newer than the current one. | |
| 356 if (it->second < entry_pos) { | |
| 357 // The current entry is newer, update the mount point entry. | |
| 358 it->second = entry_pos; | |
| 359 new_mtab[mount_point] = mount_device; | |
| 360 } | |
| 203 } | 361 } |
| 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 } | 362 } |
| 216 } | 363 } |
| 217 | 364 |
| 218 void MediaDeviceNotificationsLinux::AddNewDevice( | 365 void MediaDeviceNotificationsLinux::AddNewDevice( |
| 219 const std::string& mount_device, | 366 const std::string& mount_device, |
| 220 const std::string& mount_point, | 367 const std::string& mount_point, |
| 221 std::string* device_id) { | 368 std::string* device_id) { |
| 222 *device_id = base::IntToString(current_device_id_++); | 369 string16 device_name; |
| 370 if (!GetDeviceInfo(mount_device, device_id, &device_name)) | |
| 371 return; | |
| 372 | |
| 373 // Keep track of device uuid, to see how often we receive empty values. | |
| 374 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available", | |
| 375 !device_id->empty()); | |
| 376 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available", | |
| 377 !device_name.empty()); | |
| 378 | |
| 379 if (device_id->empty() || device_name.empty()) | |
| 380 return; | |
| 381 | |
| 223 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 382 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
| 224 system_monitor->ProcessMediaDeviceAttached(*device_id, | 383 system_monitor->ProcessMediaDeviceAttached(*device_id, |
| 225 UTF8ToUTF16(mount_device), | 384 device_name, |
| 226 SystemMonitor::TYPE_PATH, | 385 SystemMonitor::TYPE_PATH, |
| 227 mount_point); | 386 mount_point); |
| 228 } | 387 } |
| 229 | 388 |
| 230 void MediaDeviceNotificationsLinux::RemoveOldDevice( | 389 void MediaDeviceNotificationsLinux::RemoveOldDevice( |
| 231 const std::string& device_id) { | 390 const std::string& device_id) { |
| 232 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 391 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
| 233 system_monitor->ProcessMediaDeviceDetached(device_id); | 392 system_monitor->ProcessMediaDeviceDetached(device_id); |
| 234 } | 393 } |
| 235 | 394 |
| 236 } // namespace chrome | 395 } // namespace chrome |
| OLD | NEW |