| 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 // RemovableDeviceNotificationsLinux implementation. | 5 // RemovableDeviceNotificationsLinux implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" | 7 #include "chrome/browser/system_monitor/removable_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 <list> | 12 #include <list> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/file_path.h" | 16 #include "base/file_path.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
| 20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "base/system_monitor/system_monitor.h" | |
| 22 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 23 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 22 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 24 #include "chrome/browser/system_monitor/media_storage_util.h" | 23 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 25 #include "chrome/browser/system_monitor/removable_device_constants.h" | 24 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 26 #include "chrome/browser/system_monitor/udev_util_linux.h" | 25 #include "chrome/browser/system_monitor/udev_util_linux.h" |
| 27 | 26 |
| 28 namespace chrome { | 27 namespace chrome { |
| 29 | 28 |
| 30 using content::BrowserThread; | 29 using content::BrowserThread; |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 static RemovableDeviceNotificationsLinux* | |
| 35 g_removable_device_notifications_linux = NULL; | |
| 36 | |
| 37 // List of file systems we care about. | 33 // List of file systems we care about. |
| 38 const char* const kKnownFileSystems[] = { | 34 const char* const kKnownFileSystems[] = { |
| 39 "ext2", | 35 "ext2", |
| 40 "ext3", | 36 "ext3", |
| 41 "ext4", | 37 "ext4", |
| 42 "fat", | 38 "fat", |
| 43 "hfsplus", | 39 "hfsplus", |
| 44 "iso9660", | 40 "iso9660", |
| 45 "msdos", | 41 "msdos", |
| 46 "ntfs", | 42 "ntfs", |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 RecordGetDeviceInfoResult(true); | 222 RecordGetDeviceInfoResult(true); |
| 227 } | 223 } |
| 228 | 224 |
| 229 } // namespace | 225 } // namespace |
| 230 | 226 |
| 231 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( | 227 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( |
| 232 const FilePath& path) | 228 const FilePath& path) |
| 233 : initialized_(false), | 229 : initialized_(false), |
| 234 mtab_path_(path), | 230 mtab_path_(path), |
| 235 get_device_info_func_(&GetDeviceInfo) { | 231 get_device_info_func_(&GetDeviceInfo) { |
| 236 DCHECK(!g_removable_device_notifications_linux); | |
| 237 g_removable_device_notifications_linux = this; | |
| 238 } | 232 } |
| 239 | 233 |
| 240 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( | 234 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( |
| 241 const FilePath& path, | 235 const FilePath& path, |
| 242 GetDeviceInfoFunc get_device_info_func) | 236 GetDeviceInfoFunc get_device_info_func) |
| 243 : initialized_(false), | 237 : initialized_(false), |
| 244 mtab_path_(path), | 238 mtab_path_(path), |
| 245 get_device_info_func_(get_device_info_func) { | 239 get_device_info_func_(get_device_info_func) { |
| 246 DCHECK(!g_removable_device_notifications_linux); | |
| 247 g_removable_device_notifications_linux = this; | |
| 248 } | 240 } |
| 249 | 241 |
| 250 RemovableDeviceNotificationsLinux::~RemovableDeviceNotificationsLinux() { | 242 RemovableDeviceNotificationsLinux::~RemovableDeviceNotificationsLinux() { |
| 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 252 DCHECK_EQ(this, g_removable_device_notifications_linux); | |
| 253 g_removable_device_notifications_linux = NULL; | |
| 254 } | 244 } |
| 255 | 245 |
| 256 void RemovableDeviceNotificationsLinux::Init() { | 246 void RemovableDeviceNotificationsLinux::Init() { |
| 257 DCHECK(!mtab_path_.empty()); | 247 DCHECK(!mtab_path_.empty()); |
| 258 | 248 |
| 259 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | 249 // Put |kKnownFileSystems| in std::set to get O(log N) access time. |
| 260 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) | 250 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) |
| 261 known_file_systems_.insert(kKnownFileSystems[i]); | 251 known_file_systems_.insert(kKnownFileSystems[i]); |
| 262 | 252 |
| 263 BrowserThread::PostTask( | 253 BrowserThread::PostTask( |
| 264 BrowserThread::FILE, FROM_HERE, | 254 BrowserThread::FILE, FROM_HERE, |
| 265 base::Bind(&RemovableDeviceNotificationsLinux::InitOnFileThread, this)); | 255 base::Bind(&RemovableDeviceNotificationsLinux::InitOnFileThread, this)); |
| 266 } | 256 } |
| 267 | 257 |
| 268 bool RemovableDeviceNotificationsLinux::GetDeviceInfoForPath( | 258 bool RemovableDeviceNotificationsLinux::GetDeviceInfoForPath( |
| 269 const FilePath& path, | 259 const FilePath& path, |
| 270 base::SystemMonitor::RemovableStorageInfo* device_info) const { | 260 StorageInfo* device_info) const { |
| 271 if (!path.IsAbsolute()) | 261 if (!path.IsAbsolute()) |
| 272 return false; | 262 return false; |
| 273 | 263 |
| 274 FilePath current = path; | 264 FilePath current = path; |
| 275 while (!ContainsKey(mount_info_map_, current) && current != current.DirName()) | 265 while (!ContainsKey(mount_info_map_, current) && current != current.DirName()) |
| 276 current = current.DirName(); | 266 current = current.DirName(); |
| 277 | 267 |
| 278 MountMap::const_iterator mount_info = mount_info_map_.find(current); | 268 MountMap::const_iterator mount_info = mount_info_map_.find(current); |
| 279 if (mount_info == mount_info_map_.end()) | 269 if (mount_info == mount_info_map_.end()) |
| 280 return false; | 270 return false; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // |mount_point|. | 344 // |mount_point|. |
| 355 if (new_iter == new_mtab.end() || (new_iter->second != mount_device)) { | 345 if (new_iter == new_mtab.end() || (new_iter->second != mount_device)) { |
| 356 MountPriorityMap::iterator priority = | 346 MountPriorityMap::iterator priority = |
| 357 mount_priority_map_.find(mount_device); | 347 mount_priority_map_.find(mount_device); |
| 358 DCHECK(priority != mount_priority_map_.end()); | 348 DCHECK(priority != mount_priority_map_.end()); |
| 359 ReferencedMountPoint::const_iterator has_priority = | 349 ReferencedMountPoint::const_iterator has_priority = |
| 360 priority->second.find(mount_point); | 350 priority->second.find(mount_point); |
| 361 if (MediaStorageUtil::IsRemovableDevice(old_iter->second.device_id)) { | 351 if (MediaStorageUtil::IsRemovableDevice(old_iter->second.device_id)) { |
| 362 DCHECK(has_priority != priority->second.end()); | 352 DCHECK(has_priority != priority->second.end()); |
| 363 if (has_priority->second) { | 353 if (has_priority->second) { |
| 364 base::SystemMonitor::Get()->ProcessRemovableStorageDetached( | 354 ProcessDetach(old_iter->second.device_id); |
| 365 old_iter->second.device_id); | |
| 366 } | 355 } |
| 367 if (priority->second.size() > 1) | 356 if (priority->second.size() > 1) |
| 368 multiple_mounted_devices_needing_reattachment.push_back(mount_device); | 357 multiple_mounted_devices_needing_reattachment.push_back(mount_device); |
| 369 } | 358 } |
| 370 priority->second.erase(mount_point); | 359 priority->second.erase(mount_point); |
| 371 if (priority->second.empty()) | 360 if (priority->second.empty()) |
| 372 mount_priority_map_.erase(mount_device); | 361 mount_priority_map_.erase(mount_device); |
| 373 mount_points_to_erase.push_back(mount_point); | 362 mount_points_to_erase.push_back(mount_point); |
| 374 } | 363 } |
| 375 } | 364 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 391 it != multiple_mounted_devices_needing_reattachment.end(); | 380 it != multiple_mounted_devices_needing_reattachment.end(); |
| 392 ++it) { | 381 ++it) { |
| 393 ReferencedMountPoint::iterator first_mount_point_info = | 382 ReferencedMountPoint::iterator first_mount_point_info = |
| 394 mount_priority_map_.find(*it)->second.begin(); | 383 mount_priority_map_.find(*it)->second.begin(); |
| 395 const FilePath& mount_point = first_mount_point_info->first; | 384 const FilePath& mount_point = first_mount_point_info->first; |
| 396 first_mount_point_info->second = true; | 385 first_mount_point_info->second = true; |
| 397 | 386 |
| 398 const MountPointInfo& mount_info = | 387 const MountPointInfo& mount_info = |
| 399 mount_info_map_.find(mount_point)->second; | 388 mount_info_map_.find(mount_point)->second; |
| 400 DCHECK(MediaStorageUtil::IsRemovableDevice(mount_info.device_id)); | 389 DCHECK(MediaStorageUtil::IsRemovableDevice(mount_info.device_id)); |
| 401 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | 390 ProcessAttach( |
| 402 mount_info.device_id, mount_info.device_name, mount_point.value()); | 391 mount_info.device_id, mount_info.device_name, mount_point.value()); |
| 403 } | 392 } |
| 404 | 393 |
| 405 // Check new mtab entries against existing ones. | 394 // Check new mtab entries against existing ones. |
| 406 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); | 395 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); |
| 407 new_iter != new_mtab.end(); ++new_iter) { | 396 new_iter != new_mtab.end(); ++new_iter) { |
| 408 const FilePath& mount_point = new_iter->first; | 397 const FilePath& mount_point = new_iter->first; |
| 409 const FilePath& mount_device = new_iter->second; | 398 const FilePath& mount_device = new_iter->second; |
| 410 MountMap::iterator old_iter = mount_info_map_.find(mount_point); | 399 MountMap::iterator old_iter = mount_info_map_.find(mount_point); |
| 411 if (old_iter == mount_info_map_.end() || | 400 if (old_iter == mount_info_map_.end() || |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 MountPointInfo mount_point_info; | 446 MountPointInfo mount_point_info; |
| 458 mount_point_info.mount_device = mount_device; | 447 mount_point_info.mount_device = mount_device; |
| 459 mount_point_info.device_id = device_id; | 448 mount_point_info.device_id = device_id; |
| 460 mount_point_info.device_name = name; | 449 mount_point_info.device_name = name; |
| 461 mount_point_info.partition_size_in_bytes = partition_size_in_bytes; | 450 mount_point_info.partition_size_in_bytes = partition_size_in_bytes; |
| 462 | 451 |
| 463 mount_info_map_[mount_point] = mount_point_info; | 452 mount_info_map_[mount_point] = mount_point_info; |
| 464 mount_priority_map_[mount_device][mount_point] = removable; | 453 mount_priority_map_[mount_device][mount_point] = removable; |
| 465 | 454 |
| 466 if (removable) { | 455 if (removable) { |
| 467 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | 456 ProcessAttach( |
| 468 device_id, GetDisplayNameForDevice(partition_size_in_bytes, name), | 457 device_id, GetDisplayNameForDevice(partition_size_in_bytes, name), |
| 469 mount_point.value()); | 458 mount_point.value()); |
| 470 } | 459 } |
| 471 } | 460 } |
| 472 | 461 |
| 473 // static | |
| 474 RemovableStorageNotifications* RemovableStorageNotifications::GetInstance() { | |
| 475 DCHECK(g_removable_device_notifications_linux != NULL); | |
| 476 return g_removable_device_notifications_linux; | |
| 477 } | |
| 478 | |
| 479 } // namespace chrome | 462 } // namespace chrome |
| OLD | NEW |