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" |
18 #include "base/system_monitor/system_monitor.h" | |
19 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
20 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" | 22 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
26 using base::SystemMonitor; | |
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 // Device property constants. | |
Lei Zhang
2012/08/10 10:26:33
nit: you may want to separate the udev constants f
kmadhusu
2012/08/10 19:41:01
Done.
| |
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 kSpaceDelim[] = " "; | |
51 const char kUnderscoreDelim[] = "_"; | |
52 const char kVendor[] = "ID_VENDOR"; | |
53 const char kVendorID[] = "ID_VENDOR_ID"; | |
54 | |
55 // Unique id prefix constants. | |
56 const char kMtpDeviceUniqueIdPrefix[] = "mtp: "; | |
vandebo (ex-Chrome)
2012/08/10 00:32:58
nit: I wouldn't put a space in these
kmadhusu
2012/08/10 19:41:01
Done.
| |
57 const char kNativeDeviceUniqueIdPrefix[] = "uuid: "; | |
58 | |
59 // Device mount point details. | |
60 struct MountPointEntryInfo { | |
61 MountPointEntryInfo() : entry_pos(-1) {} | |
Lei Zhang
2012/08/10 10:26:33
you don't use this ctor anywhere?
kmadhusu
2012/08/10 19:41:01
Removed.
| |
62 | |
63 MountPointEntryInfo(const std::string& mount_point, int pos) | |
64 : mount_point(mount_point), | |
65 entry_pos(pos) { | |
66 } | |
67 | |
68 // Device mount point. | |
vandebo (ex-Chrome)
2012/08/10 00:32:58
nit: comments here are a bit redundant
kmadhusu
2012/08/10 19:41:01
Done.
| |
69 std::string mount_point; | |
70 | |
71 // Entry position in mtab file. | |
72 int entry_pos; | |
73 }; | |
74 | |
75 // ScopedGenericObj functor for UdevObjectRelease(). | |
76 class ScopedReleaseUdevObject { | |
77 public: | |
78 void operator()(struct udev* udev) const { | |
79 udev_unref(udev); | |
80 } | |
81 }; | |
82 | |
83 // ScopedGenericObj functor for UdevDeviceObjectRelease(). | |
84 class ScopedReleaseUdevDeviceObject { | |
85 public: | |
86 void operator()(struct udev_device* device) const { | |
87 udev_device_unref(device); | |
88 } | |
89 }; | |
90 | |
91 // Get the device information using udev library. | |
92 // Returns true on success, false on failure. | |
Lei Zhang
2012/08/10 10:26:33
On success, returns true and fill in |id| and |nam
kmadhusu
2012/08/10 19:41:01
Done.
| |
93 bool GetDeviceInfo(const std::string& device_path, | |
94 SystemMonitor::MediaDeviceType media_device_type, | |
95 std::string* id, | |
96 string16* name) { | |
97 DCHECK(!device_path.empty()); | |
98 | |
99 ScopedGenericObj<struct udev*, ScopedReleaseUdevObject> udev_ptr(udev_new()); | |
100 CHECK(udev_ptr.get()); | |
101 | |
102 struct stat st; | |
103 if (stat(device_path.c_str(), &st) < 0) | |
104 return false; | |
105 | |
106 char device_type; | |
107 if (S_ISCHR(st.st_mode)) | |
108 device_type = 'c'; | |
109 else if (S_ISBLK(st.st_mode)) | |
110 device_type = 'b'; | |
111 else | |
112 return false; // Not a supported type. | |
113 | |
114 ScopedGenericObj<struct udev_device*, ScopedReleaseUdevDeviceObject> | |
115 device(udev_device_new_from_devnum(udev_ptr, device_type, st.st_rdev)); | |
116 if (!device.get()) | |
117 return false; | |
118 | |
119 // Construct a device name using label or manufacturer(vendor and model) | |
120 // details. | |
121 std::string device_label; | |
122 const char* device_name = NULL; | |
123 if ((device_name = udev_device_get_property_value(device, kLabel)) || | |
124 (device_name = udev_device_get_property_value(device, kSerial))) { | |
125 device_label = device_name; | |
126 } else { | |
127 // Format: VendorInfo ModelInfo | |
128 // Eg: KnCompany Model2010 | |
Lei Zhang
2012/08/10 10:26:33
nit: E.g.
kmadhusu
2012/08/10 19:41:01
Done.
| |
129 const char *vendor_name = NULL, *model_name = NULL; | |
Lei Zhang
2012/08/10 10:26:33
If you put these on two lines, you can avoid viola
kmadhusu
2012/08/10 19:41:01
Done.
| |
130 if ((vendor_name = udev_device_get_property_value(device, kVendor))) | |
131 device_label = vendor_name; | |
132 if ((model_name = udev_device_get_property_value(device, kModel))) { | |
133 if (!device_label.empty()) | |
134 device_label += kSpaceDelim; | |
135 device_label += model_name; | |
136 } | |
137 } | |
138 *name = UTF8ToUTF16(device_label); | |
Lei Zhang
2012/08/10 10:26:33
Shall we CHECK(IsStringUTF8(device_label)) and see
kmadhusu
2012/08/10 19:41:01
Done.
| |
139 | |
140 // Construct a unique id using fs uuid or manufacturer(vendor and model) | |
141 // details. Add a schema as a prefix to the unique id. | |
142 switch (media_device_type) { | |
vandebo (ex-Chrome)
2012/08/10 00:32:58
It's not the device type that defines the schema,
Lei Zhang
2012/08/10 10:26:33
This implies we will be adding mtp device handling
kmadhusu
2012/08/10 19:41:01
What vandebo@ meant is to add "UUID, VID, MID, SSI
| |
143 case SystemMonitor::TYPE_PATH: | |
144 *id = kNativeDeviceUniqueIdPrefix; | |
145 break; | |
146 case SystemMonitor::TYPE_MTP: | |
147 *id = kMtpDeviceUniqueIdPrefix; | |
148 break; | |
149 } | |
150 | |
151 const char* uuid = NULL; | |
152 if ((uuid = udev_device_get_property_value(device, kFsUUID))) { | |
153 *id += uuid; | |
154 } else { | |
155 // Format: VendorInfo_ModelInfo_SerialShortInfo | |
156 // Eg: Kn DataTravel_12.10 8000000000006CB02CDB | |
157 const char *vendor = NULL, *model = NULL, *serial_short = NULL; | |
158 if ((vendor = udev_device_get_property_value(device, kVendorID))) | |
159 *id += vendor; | |
160 | |
161 if ((model = udev_device_get_property_value(device, kModelID))) { | |
162 if (!id->empty()) | |
163 *id += kUnderscoreDelim; | |
164 *id += model; | |
165 } | |
166 if ((serial_short = udev_device_get_property_value(device, kSerialShort))) { | |
167 if (!id->empty()) | |
168 *id += kUnderscoreDelim; | |
169 *id += serial_short; | |
170 } | |
171 } | |
172 | |
173 return true; | |
174 } | |
175 | |
38 } // namespace | 176 } // namespace |
39 | 177 |
40 namespace chrome { | 178 namespace chrome { |
41 | 179 |
42 using base::SystemMonitor; | 180 using base::SystemMonitor; |
43 using content::BrowserThread; | 181 using content::BrowserThread; |
44 | 182 |
45 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | 183 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( |
46 const FilePath& path) | 184 const FilePath& path) |
47 : initialized_(false), | 185 : initialized_(false), |
48 mtab_path_(path), | 186 mtab_path_(path), |
49 current_device_id_(0U) { | 187 get_device_info_func_(&GetDeviceInfo) { |
50 CHECK(!path.empty()); | 188 CHECK(!path.empty()); |
189 PopulateKnownFileSystemsInfo(); | |
190 } | |
51 | 191 |
52 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | 192 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( |
53 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { | 193 const FilePath& path, |
54 known_file_systems_.insert(kKnownFileSystems[i]); | 194 GetDeviceInfoFunc get_device_info_func) |
55 } | 195 : initialized_(false), |
196 mtab_path_(path), | |
197 get_device_info_func_(get_device_info_func) { | |
198 CHECK(!path.empty()); | |
Lei Zhang
2012/08/10 10:26:33
Move this out of both ctors and put it in an Init(
kmadhusu
2012/08/10 19:41:01
Done.
| |
199 PopulateKnownFileSystemsInfo(); | |
56 } | 200 } |
57 | 201 |
58 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | 202 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
60 } | 204 } |
61 | 205 |
62 void MediaDeviceNotificationsLinux::Init() { | 206 void MediaDeviceNotificationsLinux::Init() { |
63 BrowserThread::PostTask( | 207 BrowserThread::PostTask( |
64 BrowserThread::FILE, FROM_HERE, | 208 BrowserThread::FILE, FROM_HERE, |
65 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); | 209 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); |
66 } | 210 } |
67 | 211 |
68 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path, | 212 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path, |
69 bool error) { | 213 bool error) { |
70 if (path != mtab_path_) { | 214 if (path != mtab_path_) { |
71 // This cannot happen unless FilePathWatcher is buggy. Just ignore this | 215 // This cannot happen unless FilePathWatcher is buggy. Just ignore this |
72 // notification and do nothing. | 216 // notification and do nothing. |
73 NOTREACHED(); | 217 NOTREACHED(); |
74 return; | 218 return; |
75 } | 219 } |
76 if (error) { | 220 if (error) { |
77 LOG(ERROR) << "Error watching " << mtab_path_.value(); | 221 LOG(ERROR) << "Error watching " << mtab_path_.value(); |
78 return; | 222 return; |
79 } | 223 } |
80 | 224 |
81 UpdateMtab(); | 225 UpdateMtab(); |
82 } | 226 } |
83 | 227 |
228 void MediaDeviceNotificationsLinux::PopulateKnownFileSystemsInfo() { | |
229 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | |
230 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { | |
231 known_file_systems_.insert(kKnownFileSystems[i]); | |
232 } | |
233 } | |
234 | |
84 void MediaDeviceNotificationsLinux::InitOnFileThread() { | 235 void MediaDeviceNotificationsLinux::InitOnFileThread() { |
85 DCHECK(!initialized_); | 236 DCHECK(!initialized_); |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
87 initialized_ = true; | 238 initialized_ = true; |
88 | 239 |
89 // The callback passed to Watch() has to be unretained. Otherwise | 240 // The callback passed to Watch() has to be unretained. Otherwise |
90 // MediaDeviceNotificationsLinux will live longer than expected, and | 241 // MediaDeviceNotificationsLinux will live longer than expected, and |
91 // FilePathWatcher will get in trouble at shutdown time. | 242 // FilePathWatcher will get in trouble at shutdown time. |
92 bool ret = file_watcher_.Watch( | 243 bool ret = file_watcher_.Watch( |
93 mtab_path_, | 244 mtab_path_, |
94 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, | 245 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, |
95 base::Unretained(this))); | 246 base::Unretained(this))); |
96 if (!ret) { | 247 if (!ret) { |
97 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | 248 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; |
98 return; | 249 return; |
99 } | 250 } |
100 | 251 |
101 UpdateMtab(); | 252 UpdateMtab(); |
102 } | 253 } |
103 | 254 |
104 void MediaDeviceNotificationsLinux::UpdateMtab() { | 255 void MediaDeviceNotificationsLinux::UpdateMtab() { |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
106 | 257 |
107 MountMap new_mtab; | 258 MountPointDeviceMap new_mtab; |
108 ReadMtab(&new_mtab); | 259 ReadMtab(&new_mtab); |
109 | 260 |
110 // Check existing mtab entries for unaccounted mount points. | 261 // Check existing mtab entries for unaccounted mount points. |
111 // These mount points must have been removed in the new mtab. | 262 // These mount points must have been removed in the new mtab. |
112 std::vector<std::string> mount_points_to_erase; | 263 std::vector<std::string> mount_points_to_erase; |
113 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { | 264 for (MountMap::const_iterator it = mount_info_map_.begin(); |
265 it != mount_info_map_.end(); ++it) { | |
114 const std::string& mount_point = it->first; | 266 const std::string& mount_point = it->first; |
115 // |mount_point| not in |new_mtab|. | 267 const std::string& mount_device = it->second.mount_device; |
116 if (!ContainsKey(new_mtab, mount_point)) { | 268 MountPointDeviceMap::iterator newiter = new_mtab.find(mount_point); |
117 const std::string& device_id = it->second.second; | 269 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted at |
118 RemoveOldDevice(device_id); | 270 // |mount_point|. |
271 if (newiter == new_mtab.end() || (newiter->second != mount_device)) { | |
272 RemoveOldDevice(it->second.device_unique_id); | |
119 mount_points_to_erase.push_back(mount_point); | 273 mount_points_to_erase.push_back(mount_point); |
120 } | 274 } |
121 } | 275 } |
122 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the | 276 // Erase the |mount_info_map_| entries afterwards. Erasing in the loop above |
123 // iterator is slightly more efficient, but more tricky, since calling | 277 // using the iterator is slightly more efficient, but more tricky, since |
124 // std::map::erase() on an iterator invalidates it. | 278 // calling std::map::erase() on an iterator invalidates it. |
125 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) | 279 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) |
126 mtab_.erase(mount_points_to_erase[i]); | 280 mount_info_map_.erase(mount_points_to_erase[i]); |
127 | 281 |
128 // Check new mtab entries against existing ones. | 282 // Check new mtab entries against existing ones. |
129 for (MountMap::iterator newiter = new_mtab.begin(); | 283 for (MountPointDeviceMap::iterator newiter = new_mtab.begin(); |
130 newiter != new_mtab.end(); | 284 newiter != new_mtab.end(); |
131 ++newiter) { | 285 ++newiter) { |
132 const std::string& mount_point = newiter->first; | 286 const std::string& mount_point = newiter->first; |
133 const MountDeviceAndId& mount_device_and_id = newiter->second; | 287 const std::string& mount_device = newiter->second; |
134 const std::string& mount_device = mount_device_and_id.first; | 288 MountMap::iterator olditer = mount_info_map_.find(mount_point); |
135 std::string& id = newiter->second.second; | |
136 MountMap::iterator olditer = mtab_.find(mount_point); | |
137 // Check to see if it is a new mount point. | 289 // Check to see if it is a new mount point. |
138 if (olditer == mtab_.end()) { | 290 if (olditer == mount_info_map_.end()) { |
139 if (IsMediaDevice(mount_point)) { | 291 CheckAndAddMediaDevice(mount_device, mount_point); |
vandebo (ex-Chrome)
2012/08/10 00:32:58
Looks like this can be cleaned up a bit now...
if
kmadhusu
2012/08/10 19:41:01
Done.
| |
140 AddNewDevice(mount_device, mount_point, &id); | |
141 mtab_.insert(std::make_pair(mount_point, mount_device_and_id)); | |
142 } | |
143 continue; | 292 continue; |
144 } | 293 } |
145 | 294 |
146 // Existing mount point. Check to see if a new device is mounted there. | 295 // Existing mount point. Check to see if a new device is mounted there. |
147 const MountDeviceAndId& old_mount_device_and_id = olditer->second; | 296 if (mount_device == olditer->second.mount_device) |
148 if (mount_device == old_mount_device_and_id.first) | |
149 continue; | 297 continue; |
150 | 298 |
151 // New device mounted. | 299 // New device mounted on existing mount. |
152 RemoveOldDevice(old_mount_device_and_id.second); | 300 CheckAndAddMediaDevice(mount_device, mount_point); |
153 if (IsMediaDevice(mount_point)) { | |
154 AddNewDevice(mount_device, mount_point, &id); | |
155 olditer->second = mount_device_and_id; | |
156 } | |
157 } | 301 } |
158 } | 302 } |
159 | 303 |
160 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { | 304 void MediaDeviceNotificationsLinux::ReadMtab(MountPointDeviceMap* mtab) { |
161 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); | 305 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); |
162 if (!fp) | 306 if (!fp) |
163 return; | 307 return; |
164 | 308 |
165 MountMap& new_mtab = *mtab; | |
166 mntent entry; | 309 mntent entry; |
167 char buf[512]; | 310 char buf[512]; |
168 int mount_position = 0; | 311 |
169 typedef std::pair<std::string, std::string> MountPointAndId; | 312 // Keep track of mount point entry positions in mtab file. |
170 typedef std::map<std::string, MountPointAndId> DeviceMap; | 313 int entry_pos = 0; |
314 | |
315 // Helper map to store the device mount point details. | |
316 // (mount device, MountPointEntryInfo) | |
317 typedef std::map<std::string, MountPointEntryInfo> DeviceMap; | |
171 DeviceMap device_map; | 318 DeviceMap device_map; |
172 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | 319 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { |
173 // We only care about real file systems. | 320 // We only care about real file systems. |
174 if (!ContainsKey(known_file_systems_, entry.mnt_type)) | 321 if (!ContainsKey(known_file_systems_, entry.mnt_type)) |
175 continue; | 322 continue; |
323 | |
176 // Add entries, but overwrite entries for the same mount device. Keep track | 324 // 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 | 325 // of the entry positions in |entry_info| and use that below to resolve |
178 // resolve multiple devices mounted at the same mount point. | 326 // multiple devices mounted at the same mount point. |
179 MountPointAndId mount_point_and_id = | 327 const std::string& mount_device = entry.mnt_fsname; |
180 std::make_pair(entry.mnt_dir, base::IntToString(mount_position++)); | 328 const std::string& mount_point = entry.mnt_dir; |
181 DeviceMap::iterator it = device_map.find(entry.mnt_fsname); | 329 MountPointEntryInfo entry_info(mount_point, entry_pos++); |
182 if (it == device_map.end()) { | 330 DeviceMap::iterator it = device_map.find(mount_device); |
vandebo (ex-Chrome)
2012/08/10 00:32:58
In either case below, you set the same value... se
Lei Zhang
2012/08/10 10:26:33
google3 has a preference to not use map's [] opera
vandebo (ex-Chrome)
2012/08/10 17:25:10
I audited base and found only six uses of insert o
kmadhusu
2012/08/10 19:41:01
Done.
| |
183 device_map.insert(std::make_pair(entry.mnt_fsname, mount_point_and_id)); | 331 if (it == device_map.end()) |
184 } else { | 332 device_map.insert(std::make_pair(mount_device, entry_info)); |
185 it->second = mount_point_and_id; | 333 else |
186 } | 334 it->second = entry_info; |
187 } | 335 } |
188 endmntent(fp); | 336 endmntent(fp); |
189 | 337 |
338 // Helper map to store mount point entries. | |
339 // (mount point, entry position in mtab file) | |
340 typedef std::map<std::string, int> MountPointsInfoMap; | |
341 MountPointsInfoMap mount_points_info_map; | |
342 // Handle mount point collisions. | |
190 for (DeviceMap::const_iterator device_it = device_map.begin(); | 343 for (DeviceMap::const_iterator device_it = device_map.begin(); |
191 device_it != device_map.end(); | 344 device_it != device_map.end(); |
192 ++device_it) { | 345 ++device_it) { |
193 const std::string& device = device_it->first; | 346 const std::string& mount_point = device_it->second.mount_point; |
194 const std::string& mount_point = device_it->second.first; | 347 const int entry_pos = device_it->second.entry_pos; |
195 const std::string& position = device_it->second.second; | 348 // Add entries, but overwrite entries for the same mount point. Keep track |
349 // of the entry positions and use that information to resolve multiple | |
350 // devices mounted at the same mount point. | |
351 MountPointsInfoMap::iterator mount_position_it = | |
352 mount_points_info_map.find(mount_point); | |
353 if (mount_position_it == mount_points_info_map.end()) { | |
354 // The current entry is newer, update the mount point entry. | |
355 mount_points_info_map.insert(std::make_pair(mount_point, entry_pos)); | |
356 } else { | |
357 // There is already a device mounted at |mount_point|. Check to see if | |
358 // the existing mount entry is newer than the current one. | |
359 if (mount_position_it->second < entry_pos) | |
360 mount_position_it->second = entry_pos; | |
361 } | |
362 } | |
363 | |
364 MountPointDeviceMap& new_mtab = *mtab; | |
365 for (DeviceMap::const_iterator device_it = device_map.begin(); | |
366 device_it != device_map.end(); | |
367 ++device_it) { | |
368 const std::string& mount_point = device_it->second.mount_point; | |
369 MountPointsInfoMap::iterator mount_position_it = | |
370 mount_points_info_map.find(mount_point); | |
371 DCHECK(mount_position_it != mount_points_info_map.end()); | |
372 | |
373 // Check to see if the mount point entry is the latest one. | |
374 if (mount_position_it->second != device_it->second.entry_pos) | |
375 continue; | |
196 | 376 |
197 // No device at |mount_point|, save |device| to it. | 377 // No device at |mount_point|, save |device| to it. |
198 MountMap::iterator mount_it = new_mtab.find(mount_point); | 378 CHECK(new_mtab.find(mount_point) == new_mtab.end()); |
199 if (mount_it == new_mtab.end()) { | 379 new_mtab.insert(std::make_pair(mount_point, device_it->first)); |
200 new_mtab.insert(std::make_pair(mount_point, | |
201 std::make_pair(device, position))); | |
202 continue; | |
203 } | |
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 } | 380 } |
216 } | 381 } |
217 | 382 |
218 void MediaDeviceNotificationsLinux::AddNewDevice( | 383 void MediaDeviceNotificationsLinux::CheckAndAddMediaDevice( |
219 const std::string& mount_device, | 384 const std::string& mount_device, |
220 const std::string& mount_point, | 385 const std::string& mount_point) { |
221 std::string* device_id) { | 386 if (!IsMediaDevice(mount_point)) |
222 *device_id = base::IntToString(current_device_id_++); | 387 return; |
388 | |
389 // TODO(kmadhusu): Handle other device types. | |
390 SystemMonitor::MediaDeviceType device_type = SystemMonitor::TYPE_PATH; | |
391 std::string device_id; | |
392 string16 device_name; | |
393 bool result = (*get_device_info_func_)(mount_device, device_type, &device_id, | |
394 &device_name); | |
395 | |
396 // Keep track of GetDeviceInfo result, to see how often we fail to get device | |
397 // details. | |
398 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_info_available", | |
399 result); | |
400 if (!result) | |
401 return; | |
402 | |
403 // Keep track of device uuid, to see how often we receive empty values. | |
404 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available", | |
405 !device_id.empty()); | |
406 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available", | |
407 !device_name.empty()); | |
408 | |
409 if (device_id.empty() || device_name.empty()) | |
410 return; | |
411 | |
412 MountMap::iterator mount_info_it = mount_info_map_.find(mount_point); | |
413 MountDeviceAndId mount_device_and_id(mount_device, device_id); | |
vandebo (ex-Chrome)
2012/08/10 00:32:58
why not mount_info_map_[mount_point] = mount_devic
kmadhusu
2012/08/10 19:41:01
Done.
| |
414 if (mount_info_it != mount_info_map_.end()) { | |
415 mount_info_it->second = mount_device_and_id; | |
416 } else { | |
417 mount_info_map_.insert(std::make_pair(mount_point, mount_device_and_id)); | |
418 } | |
419 | |
223 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 420 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
224 system_monitor->ProcessMediaDeviceAttached(*device_id, | 421 system_monitor->ProcessMediaDeviceAttached(device_id, |
225 UTF8ToUTF16(mount_device), | 422 device_name, |
226 SystemMonitor::TYPE_PATH, | 423 SystemMonitor::TYPE_PATH, |
227 mount_point); | 424 mount_point); |
228 } | 425 } |
229 | 426 |
230 void MediaDeviceNotificationsLinux::RemoveOldDevice( | 427 void MediaDeviceNotificationsLinux::RemoveOldDevice( |
231 const std::string& device_id) { | 428 const std::string& device_id) { |
232 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 429 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
233 system_monitor->ProcessMediaDeviceDetached(device_id); | 430 system_monitor->ProcessMediaDeviceDetached(device_id); |
234 } | 431 } |
235 | 432 |
236 } // namespace chrome | 433 } // namespace chrome |
OLD | NEW |