1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_MOUNTED_DISK_MONITOR_H_ 6 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_MOUNTED_DISK_MONITOR_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "chromeos/dbus/power_manager_client.h" 15 #include "chromeos/disks/disk_mount_manager.h" 16 17 namespace file_manager { 18 19 // Observes PowerManager and updates its state when the system suspends and 20 // resumes. After the system resumes it will stay in "is_resuming" state for 21 // couple of seconds. This is to give DiskManager time to process device 22 // removed/added events (events for the devices that were present before suspend 23 // should not trigger any new notifications or file manager windows). 24 class MountedDiskMonitor 25 : public chromeos::PowerManagerClient::Observer, 26 public chromeos::disks::DiskMountManager::Observer { 27 public: 28 MountedDiskMonitor( 29 chromeos::PowerManagerClient* power_manager_client, 30 chromeos::disks::DiskMountManager* disk_mount_manager); 31 virtual ~MountedDiskMonitor(); 32 33 // PowerManagerClient::Observer overrides: 34 virtual void SuspendImminent() OVERRIDE; 35 virtual void SuspendDone(const base::TimeDelta& sleep_duration) OVERRIDE; 36 37 // DiskMountManager::Observer overrides. 38 virtual void OnDiskEvent( 39 chromeos::disks::DiskMountManager::DiskEvent event, 40 const chromeos::disks::DiskMountManager::Disk* disk) OVERRIDE; 41 virtual void OnDeviceEvent( 42 chromeos::disks::DiskMountManager::DeviceEvent event, 43 const std::string& device_path) OVERRIDE; 44 virtual void OnMountEvent( 45 chromeos::disks::DiskMountManager::MountEvent event, 46 chromeos::MountError error_code, 47 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) 48 OVERRIDE; 49 virtual void OnFormatEvent( 50 chromeos::disks::DiskMountManager::FormatEvent event, 51 chromeos::FormatError error_code, 52 const std::string& device_path) OVERRIDE; 53 54 // Checks if the disk is being remounted. The disk is remounting if it has 55 // been unmounted during the resuming time span. 56 bool DiskIsRemounting( 57 const chromeos::disks::DiskMountManager::Disk& disk) const; 58 bool DeviceIsHardUnplugged(const std::string& device_path) const; 59 void ClearHardUnpluggedFlag(const std::string& device_path); 60 61 // In order to avoid consuming time a lot for testing, this allows to set the 62 // resuming time span. 63 void set_resuming_time_span_for_testing( 64 const base::TimeDelta& resuming_time_span) { 65 resuming_time_span_ = resuming_time_span; 66 } 67 68 private: 69 // Maps source paths with corresponding uuids. 70 typedef std::map<std::string, std::string> DiskMap; 71 72 // Set of uuids. 73 typedef std::set<std::string> DiskSet; 74 75 void Reset(); 76 77 chromeos::PowerManagerClient* power_manager_client_; 78 chromeos::disks::DiskMountManager* disk_mount_manager_; 79 80 bool is_resuming_; 81 DiskMap mounted_disks_; 82 DiskSet unmounted_while_resuming_; 83 // Map of mount paths and device paths that are mount but are not unmount 84 // requested. 85 std::map<std::string, std::string> not_unmount_requested_; 86 // Set of device path that is hard unplugged. 87 std::set<std::string> hard_unplugged_; 88 base::TimeDelta resuming_time_span_; 89 base::WeakPtrFactory<MountedDiskMonitor> weak_factory_; 90 91 DISALLOW_COPY_AND_ASSIGN(MountedDiskMonitor); 92 }; 93 94 } // namespace file_manager 95 96 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_MOUNTED_DISK_MONITOR_H_ 97