ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / file_manager / device_event_router.cc
blobe269073c698723d1609fa713004095435ad160ef
1 // Copyright 2014 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 "base/bind.h"
6 #include "base/thread_task_runner_handle.h"
7 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h"
8 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
9 #include "content/public/browser/browser_thread.h"
11 namespace file_manager {
12 namespace {
13 namespace file_manager_private = extensions::api::file_manager_private;
14 using content::BrowserThread;
15 } // namespace
17 DeviceEventRouter::DeviceEventRouter()
18 : resume_time_delta_(base::TimeDelta::FromSeconds(10)),
19 startup_time_delta_(base::TimeDelta::FromSeconds(10)),
20 is_starting_up_(false),
21 is_resuming_(false),
22 weak_factory_(this) {
25 DeviceEventRouter::DeviceEventRouter(base::TimeDelta overriding_time_delta)
26 : resume_time_delta_(overriding_time_delta),
27 startup_time_delta_(overriding_time_delta),
28 is_starting_up_(false),
29 is_resuming_(false),
30 weak_factory_(this) {
33 DeviceEventRouter::~DeviceEventRouter() {
36 void DeviceEventRouter::Startup() {
37 is_starting_up_ = true;
38 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
39 FROM_HERE,
40 base::Bind(&DeviceEventRouter::StartupDelayed,
41 weak_factory_.GetWeakPtr()),
42 startup_time_delta_);
45 void DeviceEventRouter::StartupDelayed() {
46 DCHECK(thread_checker_.CalledOnValidThread());
47 is_starting_up_ = false;
50 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) {
51 DCHECK(thread_checker_.CalledOnValidThread());
53 SetDeviceState(device_path, DEVICE_STATE_USUAL);
54 if (IsExternalStorageDisabled()) {
55 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_DISABLED,
56 device_path);
57 return;
61 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) {
62 DCHECK(thread_checker_.CalledOnValidThread());
63 SetDeviceState(device_path, DEVICE_STATE_USUAL);
64 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_REMOVED, device_path);
67 void DeviceEventRouter::OnDiskAdded(
68 const chromeos::disks::DiskMountManager::Disk& disk,
69 bool mounting) {
70 // Do nothing.
73 void DeviceEventRouter::OnDiskRemoved(
74 const chromeos::disks::DiskMountManager::Disk& disk) {
75 DCHECK(thread_checker_.CalledOnValidThread());
77 if (is_resuming_ || is_starting_up_)
78 return;
80 const std::string& device_path = disk.system_path_prefix();
81 if (!disk.mount_path().empty() &&
82 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) {
83 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED,
84 device_path);
85 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED);
89 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code,
90 const VolumeInfo& volume_info) {
91 DCHECK(thread_checker_.CalledOnValidThread());
93 const std::string& device_path =
94 volume_info.system_path_prefix.AsUTF8Unsafe();
95 SetDeviceState(device_path, DEVICE_STATE_USUAL);
98 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code,
99 const VolumeInfo& volume_info) {
100 // Do nothing.
103 void DeviceEventRouter::OnFormatStarted(const std::string& device_path,
104 bool success) {
105 DCHECK(thread_checker_.CalledOnValidThread());
107 if (success) {
108 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_FORMAT_START,
109 device_path);
110 } else {
111 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_FORMAT_FAIL,
112 device_path);
116 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path,
117 bool success) {
118 DCHECK(thread_checker_.CalledOnValidThread());
120 OnDeviceEvent(success ? file_manager_private::DEVICE_EVENT_TYPE_FORMAT_SUCCESS
121 : file_manager_private::DEVICE_EVENT_TYPE_FORMAT_FAIL,
122 device_path);
125 void DeviceEventRouter::SuspendImminent() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 is_resuming_ = true;
130 void DeviceEventRouter::SuspendDone(const base::TimeDelta& sleep_duration) {
131 DCHECK(thread_checker_.CalledOnValidThread());
132 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
133 FROM_HERE,
134 base::Bind(&DeviceEventRouter::SuspendDoneDelayed,
135 weak_factory_.GetWeakPtr()),
136 resume_time_delta_);
139 void DeviceEventRouter::SuspendDoneDelayed() {
140 DCHECK(thread_checker_.CalledOnValidThread());
141 is_resuming_ = false;
144 DeviceState DeviceEventRouter::GetDeviceState(
145 const std::string& device_path) const {
146 const std::map<std::string, DeviceState>::const_iterator it =
147 device_states_.find(device_path);
148 return it != device_states_.end() ? it->second : DEVICE_STATE_USUAL;
151 void DeviceEventRouter::SetDeviceState(const std::string& device_path,
152 DeviceState state) {
153 if (state != DEVICE_STATE_USUAL) {
154 device_states_[device_path] = state;
155 } else {
156 const std::map<std::string, DeviceState>::iterator it =
157 device_states_.find(device_path);
158 if (it != device_states_.end())
159 device_states_.erase(it);
163 } // namespace file_manager