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.
5 #include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "chromeos/dbus/power_manager_client.h"
12 using chromeos::disks::DiskMountManager;
14 namespace file_manager {
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);
24 MountedDiskMonitor::MountedDiskMonitor(
25 chromeos::PowerManagerClient* power_manager_client)
26 : power_manager_client_(power_manager_client),
28 resuming_time_span_(kResumingTimeSpan),
30 DCHECK(power_manager_client_);
31 power_manager_client_->AddObserver(this);
34 MountedDiskMonitor::~MountedDiskMonitor() {
35 power_manager_client_->RemoveObserver(this);
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.
43 weak_factory_.InvalidateWeakPtrs();
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(
52 base::Bind(&MountedDiskMonitor::Reset,
53 weak_factory_.GetWeakPtr()),
57 bool MountedDiskMonitor::DiskIsRemounting(
58 const DiskMountManager::Disk& disk) const {
59 return unmounted_while_resuming_.count(disk.fs_uuid()) > 0;
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;
69 void MountedDiskMonitor::MarkAsHardUnpluggedReported(
70 const std::string& device_path) {
71 hard_unplugged_[device_path] = HARD_UNPLUGGED_AND_REPORTED;
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)
83 case DiskMountManager::MOUNTING: {
84 if (!disk || error_code != chromeos::MOUNT_ERROR_NONE)
86 mounted_disks_[mount_info.source_path] = disk->fs_uuid();
90 case DiskMountManager::UNMOUNTING: {
91 DiskMap::iterator it = mounted_disks_.find(mount_info.source_path);
92 if (it == mounted_disks_.end())
94 const std::string& fs_uuid = it->second;
96 unmounted_while_resuming_.insert(fs_uuid);
97 mounted_disks_.erase(it);
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.
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));
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);
129 void MountedDiskMonitor::Reset() {
130 unmounted_while_resuming_.clear();
131 is_resuming_ = false;
134 } // namespace file_manager