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