| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/message_loop/message_loop_proxy.h" | |
| 10 #include "chromeos/dbus/power_manager_client.h" | |
| 11 | |
| 12 using chromeos::disks::DiskMountManager; | |
| 13 | |
| 14 namespace file_manager { | |
| 15 namespace { | |
| 16 | |
| 17 // Time span of the resuming process. All unmount events sent during this | |
| 18 // time are considered as being part of remounting process, since remounting | |
| 19 // is done just after resuming. | |
| 20 const base::TimeDelta kResumingTimeSpan = base::TimeDelta::FromSeconds(5); | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 MountedDiskMonitor::MountedDiskMonitor( | |
| 25 chromeos::PowerManagerClient* power_manager_client) | |
| 26 : power_manager_client_(power_manager_client), | |
| 27 is_resuming_(false), | |
| 28 resuming_time_span_(kResumingTimeSpan), | |
| 29 weak_factory_(this) { | |
| 30 DCHECK(power_manager_client_); | |
| 31 power_manager_client_->AddObserver(this); | |
| 32 } | |
| 33 | |
| 34 MountedDiskMonitor::~MountedDiskMonitor() { | |
| 35 power_manager_client_->RemoveObserver(this); | |
| 36 } | |
| 37 | |
| 38 void MountedDiskMonitor::SuspendImminent() { | |
| 39 // Flip the resuming flag while suspending, so it is possible to detect | |
| 40 // resuming as soon as possible after the lid is open. Note, that mount | |
| 41 // events may occur before the SuspendDone method is called. | |
| 42 is_resuming_ = true; | |
| 43 weak_factory_.InvalidateWeakPtrs(); | |
| 44 } | |
| 45 | |
| 46 void MountedDiskMonitor::SuspendDone( | |
| 47 const base::TimeDelta& sleep_duration) { | |
| 48 // Undo any previous resets. Release the resuming flag after a fixed timeout. | |
| 49 weak_factory_.InvalidateWeakPtrs(); | |
| 50 base::MessageLoopProxy::current()->PostDelayedTask( | |
| 51 FROM_HERE, | |
| 52 base::Bind(&MountedDiskMonitor::Reset, | |
| 53 weak_factory_.GetWeakPtr()), | |
| 54 resuming_time_span_); | |
| 55 } | |
| 56 | |
| 57 bool MountedDiskMonitor::DiskIsRemounting( | |
| 58 const DiskMountManager::Disk& disk) const { | |
| 59 return unmounted_while_resuming_.count(disk.fs_uuid()) > 0; | |
| 60 } | |
| 61 | |
| 62 bool MountedDiskMonitor::DeviceIsHardUnpluggedButNotReported( | |
| 63 const std::string& device_path) const { | |
| 64 const std::map<std::string, HardUnpluggedState>::const_iterator it | |
| 65 = hard_unplugged_.find(device_path); | |
| 66 return it != hard_unplugged_.end() && it->second == HARD_UNPLUGGED; | |
| 67 } | |
| 68 | |
| 69 void MountedDiskMonitor::MarkAsHardUnpluggedReported( | |
| 70 const std::string& device_path) { | |
| 71 hard_unplugged_[device_path] = HARD_UNPLUGGED_AND_REPORTED; | |
| 72 } | |
| 73 | |
| 74 void MountedDiskMonitor::OnMountEvent( | |
| 75 chromeos::disks::DiskMountManager::MountEvent event, | |
| 76 chromeos::MountError error_code, | |
| 77 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info, | |
| 78 const DiskMountManager::Disk* disk) { | |
| 79 if (mount_info.mount_type != chromeos::MOUNT_TYPE_DEVICE) | |
| 80 return; | |
| 81 | |
| 82 switch (event) { | |
| 83 case DiskMountManager::MOUNTING: { | |
| 84 if (!disk || error_code != chromeos::MOUNT_ERROR_NONE) | |
| 85 return; | |
| 86 mounted_disks_[mount_info.source_path] = disk->fs_uuid(); | |
| 87 break; | |
| 88 } | |
| 89 | |
| 90 case DiskMountManager::UNMOUNTING: { | |
| 91 DiskMap::iterator it = mounted_disks_.find(mount_info.source_path); | |
| 92 if (it == mounted_disks_.end()) | |
| 93 return; | |
| 94 const std::string& fs_uuid = it->second; | |
| 95 if (is_resuming_) | |
| 96 unmounted_while_resuming_.insert(fs_uuid); | |
| 97 mounted_disks_.erase(it); | |
| 98 break; | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void MountedDiskMonitor::OnDiskEvent( | |
| 104 chromeos::disks::DiskMountManager::DiskEvent event, | |
| 105 const chromeos::disks::DiskMountManager::Disk* disk) { | |
| 106 if (event == chromeos::disks::DiskMountManager::DISK_REMOVED) { | |
| 107 // If the mount path is not empty, the disk is hard unplugged. | |
| 108 if (!is_resuming_ && | |
| 109 !disk->mount_path().empty() && | |
| 110 hard_unplugged_.find(disk->system_path_prefix()) == | |
| 111 hard_unplugged_.end()) { | |
| 112 hard_unplugged_.insert( | |
| 113 std::make_pair(disk->system_path_prefix(), HARD_UNPLUGGED)); | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void MountedDiskMonitor::OnDeviceEvent( | |
| 119 chromeos::disks::DiskMountManager::DeviceEvent event, | |
| 120 const std::string& device_path) { | |
| 121 if (event == chromeos::disks::DiskMountManager::DEVICE_REMOVED) { | |
| 122 const std::map<std::string, HardUnpluggedState>::iterator it | |
| 123 = hard_unplugged_.find(device_path); | |
| 124 if (it != hard_unplugged_.end()) | |
| 125 hard_unplugged_.erase(it); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void MountedDiskMonitor::Reset() { | |
| 130 unmounted_while_resuming_.clear(); | |
| 131 is_resuming_ = false; | |
| 132 } | |
| 133 | |
| 134 } // namespace file_manager | |
| OLD | NEW |