OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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" | |
10 | |
11 namespace file_manager { | |
12 namespace { | |
13 namespace file_browser_private = extensions::api::file_browser_private; | |
14 using content::BrowserThread; | |
15 } // namespace | |
16 | |
17 DeviceEventRouter::DeviceEventRouter() | |
18 : resume_time_delta_(base::TimeDelta::FromSeconds(5)), | |
mtomasz
2014/08/22 05:48:40
nit: These should be constants (int).
hirono
2014/08/22 06:29:26
After offline discussion, it won't fix.
* resume_
| |
19 startup_time_delta_(base::TimeDelta::FromSeconds(10)), | |
20 scan_time_delta_(base::TimeDelta::FromSeconds(5)), | |
21 is_starting_up_(false), | |
22 is_resuming_(false), | |
23 weak_factory_(this) { | |
24 } | |
25 | |
26 DeviceEventRouter::DeviceEventRouter(base::TimeDelta overriding_time_delta) | |
27 : resume_time_delta_(overriding_time_delta), | |
28 startup_time_delta_(overriding_time_delta), | |
29 scan_time_delta_(overriding_time_delta), | |
30 is_starting_up_(false), | |
31 is_resuming_(false), | |
32 weak_factory_(this) { | |
33 } | |
34 | |
35 DeviceEventRouter::~DeviceEventRouter() { | |
36 } | |
37 | |
38 void DeviceEventRouter::Startup() { | |
39 is_starting_up_ = true; | |
40 base::MessageLoopProxy::current()->PostDelayedTask( | |
mtomasz
2014/08/22 05:48:40
MessageLoopProxy is deprecated. Please use base::T
hirono
2014/08/22 06:29:26
Oops, the lines were mixed from old lines.
| |
41 FROM_HERE, | |
42 base::Bind(&DeviceEventRouter::StartupDelayed, | |
43 weak_factory_.GetWeakPtr()), | |
44 startup_time_delta_); | |
45 } | |
46 | |
47 void DeviceEventRouter::StartupDelayed() { | |
48 is_starting_up_ = false; | |
49 } | |
50 | |
51 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) { | |
52 if (is_starting_up_ || is_resuming_) { | |
53 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
54 return; | |
55 } | |
56 | |
57 if (IsExternalStorageDisabled()) { | |
58 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_DISABLED, | |
59 device_path); | |
60 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
61 return; | |
62 } | |
63 | |
64 SetDeviceState(device_path, DEVICE_SCANNED); | |
65 base::MessageLoopProxy::current()->PostDelayedTask( | |
66 FROM_HERE, | |
67 base::Bind(&DeviceEventRouter::OnDeviceAddedDelayed, | |
68 weak_factory_.GetWeakPtr(), | |
69 device_path), | |
70 scan_time_delta_); | |
71 } | |
72 | |
73 void DeviceEventRouter::OnDeviceAddedDelayed(const std::string& device_path) { | |
74 if (GetDeviceState(device_path) == DEVICE_SCANNED) { | |
75 // TODO(hirono): Rename DEVICE_EVENT_TYPE_ADDED with | |
76 // DEVICE_EVENT_TYPE_SCAN_STARTED. | |
77 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_ADDED, device_path); | |
78 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED); | |
79 } | |
80 } | |
81 | |
82 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) { | |
83 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
84 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, device_path); | |
85 } | |
86 | |
87 void DeviceEventRouter::OnDiskAdded( | |
88 const chromeos::disks::DiskMountManager::Disk& disk, | |
89 bool mounting) { | |
90 if (!mounting) { | |
91 // If the disk is not being mounted, mark the device scan cancelled. | |
92 const std::string& device_path = disk.system_path_prefix(); | |
93 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) { | |
94 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED, | |
95 device_path); | |
96 } | |
97 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
98 } | |
99 } | |
100 | |
101 void DeviceEventRouter::OnDiskRemoved( | |
102 const chromeos::disks::DiskMountManager::Disk& disk) { | |
103 if (is_resuming_ || is_starting_up_) | |
104 return; | |
105 | |
106 const std::string& device_path = disk.system_path_prefix(); | |
107 if (!disk.mount_path().empty() && | |
108 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) { | |
109 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, | |
110 device_path); | |
111 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED); | |
112 } | |
113 } | |
114 | |
115 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code, | |
116 const VolumeInfo& volume_info, | |
117 bool is_remounting) { | |
118 const std::string& device_path = | |
119 volume_info.system_path_prefix.AsUTF8Unsafe(); | |
120 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
121 } | |
122 | |
123 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code, | |
124 const VolumeInfo& volume_info) { | |
125 // Do nothing. | |
126 } | |
127 | |
128 void DeviceEventRouter::OnFormatStarted(const std::string& device_path, | |
129 bool success) { | |
130 if (success) { | |
131 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_START, | |
132 device_path); | |
133 } else { | |
134 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, | |
135 device_path); | |
136 } | |
137 } | |
138 | |
139 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path, | |
140 bool success) { | |
141 OnDeviceEvent(success ? file_browser_private::DEVICE_EVENT_TYPE_FORMAT_SUCCESS | |
142 : file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, | |
143 device_path); | |
144 } | |
145 | |
146 void DeviceEventRouter::OnHardUnplugged(const std::string& device_path) { | |
147 } | |
148 | |
149 void DeviceEventRouter::SuspendImminent() { | |
150 is_resuming_ = true; | |
151 } | |
152 | |
153 void DeviceEventRouter::SuspendDone(const base::TimeDelta& sleep_duration) { | |
154 base::MessageLoopProxy::current()->PostDelayedTask( | |
155 FROM_HERE, | |
156 base::Bind(&DeviceEventRouter::SuspendDoneDelayed, | |
157 weak_factory_.GetWeakPtr()), | |
158 resume_time_delta_); | |
159 } | |
160 | |
161 void DeviceEventRouter::SuspendDoneDelayed() { | |
162 is_resuming_ = false; | |
163 } | |
164 | |
165 DeviceState DeviceEventRouter::GetDeviceState( | |
166 const std::string& device_path) const { | |
167 const std::map<std::string, DeviceState>::const_iterator it = | |
168 device_states_.find(device_path); | |
169 return it != device_states_.end() ? it->second : DEVICE_STATE_USUAL; | |
170 } | |
171 | |
172 void DeviceEventRouter::SetDeviceState(const std::string& device_path, | |
173 DeviceState state) { | |
174 if (state != DEVICE_STATE_USUAL) { | |
175 device_states_[device_path] = state; | |
176 } else { | |
177 const std::map<std::string, DeviceState>::iterator it = | |
178 device_states_.find(device_path); | |
179 if (it != device_states_.end()) | |
180 device_states_.erase(it); | |
181 } | |
182 } | |
183 | |
184 } // namespace file_manager | |
OLD | NEW |