Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / mounted_disk_monitor.cc
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     chromeos::disks::DiskMountManager* disk_mount_manager)
27     : power_manager_client_(power_manager_client),
28       disk_mount_manager_(disk_mount_manager),
29       is_resuming_(false),
30       resuming_time_span_(kResumingTimeSpan),
31       weak_factory_(this) {
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();
37 }
38
39 MountedDiskMonitor::~MountedDiskMonitor() {
40   disk_mount_manager_->RemoveObserver(this);
41   power_manager_client_->RemoveObserver(this);
42 }
43
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.
48   is_resuming_ = true;
49   weak_factory_.InvalidateWeakPtrs();
50 }
51
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(
57       FROM_HERE,
58       base::Bind(&MountedDiskMonitor::Reset,
59                  weak_factory_.GetWeakPtr()),
60       resuming_time_span_);
61 }
62
63 bool MountedDiskMonitor::DiskIsRemounting(
64     const DiskMountManager::Disk& disk) const {
65   return unmounted_while_resuming_.count(disk.fs_uuid()) > 0;
66 }
67
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)
73     return;
74
75   switch (event) {
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)
80         return;
81       mounted_disks_[mount_info.source_path] = disk->fs_uuid();
82       break;
83     }
84
85     case DiskMountManager::UNMOUNTING: {
86       DiskMap::iterator it = mounted_disks_.find(mount_info.source_path);
87       if (it == mounted_disks_.end())
88         return;
89       const std::string& fs_uuid = it->second;
90       if (is_resuming_)
91         unmounted_while_resuming_.insert(fs_uuid);
92       mounted_disks_.erase(it);
93       break;
94     }
95   }
96 }
97
98 void MountedDiskMonitor::OnDiskEvent(
99     chromeos::disks::DiskMountManager::DiskEvent event,
100     const chromeos::disks::DiskMountManager::Disk* disk) {
101 }
102
103 void MountedDiskMonitor::OnDeviceEvent(
104     chromeos::disks::DiskMountManager::DeviceEvent event,
105     const std::string& device_path) {
106 }
107
108 void MountedDiskMonitor::OnFormatEvent(
109     chromeos::disks::DiskMountManager::FormatEvent event,
110     chromeos::FormatError error_code,
111     const std::string& device_path) {
112 }
113
114 void MountedDiskMonitor::Reset() {
115   unmounted_while_resuming_.clear();
116   is_resuming_ = false;
117 }
118
119 }  // namespace file_manager