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 chromeos::disks::DiskMountManager* disk_mount_manager)
27 : power_manager_client_(power_manager_client),
28 disk_mount_manager_(disk_mount_manager),
30 resuming_time_span_(kResumingTimeSpan),
32 DCHECK(power_manager_client_);
33 DCHECK(disk_mount_manager_);
34 power_manager_client_->AddObserver(this);
35 disk_mount_manager_->AddObserver(this);
36 disk_mount_manager_->RequestMountInfoRefresh();
39 MountedDiskMonitor::~MountedDiskMonitor() {
40 disk_mount_manager_->RemoveObserver(this);
41 power_manager_client_->RemoveObserver(this);
44 void MountedDiskMonitor::SuspendImminent() {
45 // Flip the resuming flag while suspending, so it is possible to detect
46 // resuming as soon as possible after the lid is open. Note, that mount
47 // events may occur before the SystemResumed method is called.
49 weak_factory_.InvalidateWeakPtrs();
52 void MountedDiskMonitor::SystemResumed(
53 const base::TimeDelta& sleep_duration) {
54 // Undo any previous resets. Release the resuming flag after a fixed timeout.
55 weak_factory_.InvalidateWeakPtrs();
56 base::MessageLoopProxy::current()->PostDelayedTask(
58 base::Bind(&MountedDiskMonitor::Reset,
59 weak_factory_.GetWeakPtr()),
63 bool MountedDiskMonitor::DiskIsRemounting(
64 const DiskMountManager::Disk& disk) const {
65 return unmounted_while_resuming_.count(disk.fs_uuid()) > 0;
68 void MountedDiskMonitor::OnMountEvent(
69 chromeos::disks::DiskMountManager::MountEvent event,
70 chromeos::MountError error_code,
71 const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) {
72 if (mount_info.mount_type != chromeos::MOUNT_TYPE_DEVICE)
76 case DiskMountManager::MOUNTING: {
77 const DiskMountManager::Disk* disk =
78 disk_mount_manager_->FindDiskBySourcePath(mount_info.source_path);
79 if (!disk || error_code != chromeos::MOUNT_ERROR_NONE)
81 mounted_disks_[mount_info.source_path] = disk->fs_uuid();
85 case DiskMountManager::UNMOUNTING: {
86 DiskMap::iterator it = mounted_disks_.find(mount_info.source_path);
87 if (it == mounted_disks_.end())
89 const std::string& fs_uuid = it->second;
91 unmounted_while_resuming_.insert(fs_uuid);
92 mounted_disks_.erase(it);
98 void MountedDiskMonitor::OnDiskEvent(
99 chromeos::disks::DiskMountManager::DiskEvent event,
100 const chromeos::disks::DiskMountManager::Disk* disk) {
103 void MountedDiskMonitor::OnDeviceEvent(
104 chromeos::disks::DiskMountManager::DeviceEvent event,
105 const std::string& device_path) {
108 void MountedDiskMonitor::OnFormatEvent(
109 chromeos::disks::DiskMountManager::FormatEvent event,
110 chromeos::FormatError error_code,
111 const std::string& device_path) {
114 void MountedDiskMonitor::Reset() {
115 unmounted_while_resuming_.clear();
116 is_resuming_ = false;
119 } // namespace file_manager