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.
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
{
13 namespace file_manager_private
= extensions::api::file_manager_private
;
14 using content::BrowserThread
;
17 DeviceEventRouter::DeviceEventRouter()
18 : resume_time_delta_(base::TimeDelta::FromSeconds(10)),
19 startup_time_delta_(base::TimeDelta::FromSeconds(10)),
20 is_starting_up_(false),
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),
33 DeviceEventRouter::~DeviceEventRouter() {
36 void DeviceEventRouter::Startup() {
37 is_starting_up_
= true;
38 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
40 base::Bind(&DeviceEventRouter::StartupDelayed
,
41 weak_factory_
.GetWeakPtr()),
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
,
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
,
73 void DeviceEventRouter::OnDiskRemoved(
74 const chromeos::disks::DiskMountManager::Disk
& disk
) {
75 DCHECK(thread_checker_
.CalledOnValidThread());
77 if (is_resuming_
|| is_starting_up_
)
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
,
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
) {
103 void DeviceEventRouter::OnFormatStarted(const std::string
& device_path
,
105 DCHECK(thread_checker_
.CalledOnValidThread());
108 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_FORMAT_START
,
111 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_FORMAT_FAIL
,
116 void DeviceEventRouter::OnFormatCompleted(const std::string
& device_path
,
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
,
125 void DeviceEventRouter::SuspendImminent() {
126 DCHECK(thread_checker_
.CalledOnValidThread());
130 void DeviceEventRouter::SuspendDone(const base::TimeDelta
& sleep_duration
) {
131 DCHECK(thread_checker_
.CalledOnValidThread());
132 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
134 base::Bind(&DeviceEventRouter::SuspendDoneDelayed
,
135 weak_factory_
.GetWeakPtr()),
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
,
153 if (state
!= DEVICE_STATE_USUAL
) {
154 device_states_
[device_path
] = state
;
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