1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/extensions/file_manager/device_event_router.h"
#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chromeos/ash/components/disks/disk.h"
#include "content/public/browser/browser_thread.h"
namespace file_manager {
namespace file_manager_private = extensions::api::file_manager_private;
using content::BrowserThread;
DeviceEventRouter::DeviceEventRouter(
SystemNotificationManager* notification_manager)
: notification_manager_(notification_manager),
resume_time_delta_(base::Seconds(10)),
startup_time_delta_(base::Seconds(10)),
is_starting_up_(false),
is_resuming_(false) {}
DeviceEventRouter::DeviceEventRouter(
SystemNotificationManager* notification_manager,
base::TimeDelta overriding_time_delta)
: notification_manager_(notification_manager),
resume_time_delta_(overriding_time_delta),
startup_time_delta_(overriding_time_delta),
is_starting_up_(false),
is_resuming_(false) {}
DeviceEventRouter::~DeviceEventRouter() = default;
void DeviceEventRouter::Startup() {
is_starting_up_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DeviceEventRouter::StartupDelayed,
weak_factory_.GetWeakPtr()),
startup_time_delta_);
}
void DeviceEventRouter::StartupDelayed() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
is_starting_up_ = false;
}
void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
SetDeviceState(device_path, DEVICE_STATE_USUAL);
}
void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
SetDeviceState(device_path, DEVICE_STATE_USUAL);
OnDeviceEvent(file_manager_private::DeviceEventType::kRemoved, device_path,
"");
}
void DeviceEventRouter::OnDiskAdded(const ash::disks::Disk& disk,
bool mounting) {
// Do nothing.
}
void DeviceEventRouter::OnDiskAddBlockedByPolicy(
const std::string& device_path) {
OnDeviceEvent(file_manager_private::DeviceEventType::kDisabled, device_path,
"");
}
void DeviceEventRouter::OnDiskRemoved(const ash::disks::Disk& disk) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (is_resuming_ || is_starting_up_) {
return;
}
const std::string& device_path = disk.storage_device_path();
if (!disk.is_read_only() && disk.is_mounted() &&
GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) {
OnDeviceEvent(file_manager_private::DeviceEventType::kHardUnplugged,
device_path, disk.device_label());
SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED);
}
}
void DeviceEventRouter::OnVolumeMounted(ash::MountError error_code,
const Volume& volume) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
const std::string& device_path = volume.storage_device_path().AsUTF8Unsafe();
SetDeviceState(device_path, DEVICE_STATE_USUAL);
}
void DeviceEventRouter::OnVolumeUnmounted(ash::MountError error_code,
const Volume& volume) {
// Do nothing.
}
void DeviceEventRouter::OnFormatStarted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (success) {
OnDeviceEvent(file_manager_private::DeviceEventType::kFormatStart,
device_path, device_label);
} else {
OnDeviceEvent(file_manager_private::DeviceEventType::kFormatFail,
device_path, device_label);
}
}
void DeviceEventRouter::OnFormatCompleted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
OnDeviceEvent(success ? file_manager_private::DeviceEventType::kFormatSuccess
: file_manager_private::DeviceEventType::kFormatFail,
device_path, device_label);
}
void DeviceEventRouter::OnPartitionStarted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (success) {
OnDeviceEvent(file_manager_private::DeviceEventType::kPartitionStart,
device_path, device_label);
} else {
OnDeviceEvent(file_manager_private::DeviceEventType::kPartitionFail,
device_path, device_label);
}
}
void DeviceEventRouter::OnPartitionCompleted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
OnDeviceEvent(success
? file_manager_private::DeviceEventType::kPartitionSuccess
: file_manager_private::DeviceEventType::kPartitionFail,
device_path, device_label);
}
void DeviceEventRouter::OnRenameStarted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
OnDeviceEvent(success ? file_manager_private::DeviceEventType::kRenameStart
: file_manager_private::DeviceEventType::kRenameFail,
device_path, device_label);
}
void DeviceEventRouter::OnRenameCompleted(const std::string& device_path,
const std::string& device_label,
bool success) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
OnDeviceEvent(success ? file_manager_private::DeviceEventType::kRenameSuccess
: file_manager_private::DeviceEventType::kRenameFail,
device_path, device_label);
}
void DeviceEventRouter::SuspendImminent(
power_manager::SuspendImminent::Reason reason) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
is_resuming_ = true;
}
void DeviceEventRouter::SuspendDone(base::TimeDelta sleep_duration) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DeviceEventRouter::SuspendDoneDelayed,
weak_factory_.GetWeakPtr()),
resume_time_delta_);
}
void DeviceEventRouter::SuspendDoneDelayed() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
is_resuming_ = false;
}
DeviceState DeviceEventRouter::GetDeviceState(
const std::string& device_path) const {
const std::map<std::string, DeviceState>::const_iterator it =
device_states_.find(device_path);
return it != device_states_.end() ? it->second : DEVICE_STATE_USUAL;
}
void DeviceEventRouter::SetDeviceState(const std::string& device_path,
DeviceState state) {
if (state != DEVICE_STATE_USUAL) {
device_states_[device_path] = state;
} else {
const std::map<std::string, DeviceState>::iterator it =
device_states_.find(device_path);
if (it != device_states_.end()) {
device_states_.erase(it);
}
}
}
} // namespace file_manager
|