OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" | |
7 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 | |
10 namespace file_manager { | |
11 namespace { | |
12 namespace file_browser_private = extensions::api::file_browser_private; | |
13 using content::BrowserThread; | |
14 const base::TimeDelta kStartupTimeSpan = base::TimeDelta::FromSeconds(10); | |
15 const base::TimeDelta kScanReportTimeSpan = base::TimeDelta::FromSeconds(5); | |
16 const base::TimeDelta kResumingTimeSpan = base::TimeDelta::FromSeconds(5); | |
17 } // namespace | |
18 | |
19 DeviceEventRouter::DeviceEventRouter() | |
20 : is_startup_(false), is_suspended_(false), weak_factory_(this) { | |
21 } | |
22 | |
23 DeviceEventRouter::~DeviceEventRouter() { | |
24 } | |
25 | |
26 void DeviceEventRouter::Startup() { | |
27 is_startup_ = true; | |
28 PostDelayedTask(base::Bind(&DeviceEventRouter::StartupDelayed, | |
mtomasz
2014/08/20 06:31:01
PostDelayedTask seems to be not implemented.
hirono
2014/08/20 08:07:16
Yes, the method is implemented in sub-classes.
mtomasz
2014/08/20 08:20:17
The Impl seems not included in this CL, is it OK?
hirono
2014/08/21 07:09:32
Yes, currently the class is used only from the uni
| |
29 weak_factory_.GetWeakPtr()), | |
30 kStartupTimeSpan); | |
31 } | |
32 | |
33 void DeviceEventRouter::StartupDelayed() { | |
34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
35 is_startup_ = false; | |
36 } | |
37 | |
38 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) { | |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
40 | |
41 if (is_startup_ || is_suspended_) { | |
mtomasz
2014/08/20 06:31:01
is_suspended_ is true while we're are not suspende
hirono
2014/08/20 08:07:16
Done.
| |
42 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
43 return; | |
44 } | |
45 | |
46 if (IsExternalStorageDisabled()) { | |
47 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_DISABLED, | |
48 device_path); | |
49 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
50 return; | |
51 } | |
52 | |
53 SetDeviceState(device_path, DEVICE_SCANNED); | |
54 PostDelayedTask(base::Bind(&DeviceEventRouter::OnDeviceAddedDelayed, | |
55 weak_factory_.GetWeakPtr(), | |
56 device_path), | |
57 kScanReportTimeSpan); | |
58 } | |
59 | |
60 void DeviceEventRouter::OnDeviceAddedDelayed(const std::string& device_path) { | |
61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
62 | |
63 if (GetDeviceState(device_path) == DEVICE_SCANNED) { | |
64 // TODO(hirono): Rename DEVICE_EVENT_TYPE_ADDED with | |
65 // DEVICE_EVENT_TYPE_SCAN_STARTED. | |
66 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_ADDED, device_path); | |
67 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED); | |
68 } | |
69 } | |
70 | |
71 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) { | |
72 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
73 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
74 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, device_path); | |
75 } | |
76 | |
77 void DeviceEventRouter::OnDiskAdded( | |
78 const chromeos::disks::DiskMountManager::Disk& disk, | |
79 bool mounting) { | |
80 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
81 | |
82 if (!mounting) { | |
83 // If the disk is not being mounted, mark the device scan cancelled. | |
84 const std::string& device_path = disk.system_path_prefix(); | |
85 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) { | |
86 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED, | |
87 device_path); | |
88 } | |
89 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
90 } | |
91 } | |
92 | |
93 void DeviceEventRouter::OnDiskRemoved( | |
94 const chromeos::disks::DiskMountManager::Disk& disk) { | |
95 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
96 if (is_suspended_ || is_startup_) | |
97 return; | |
98 | |
99 const std::string& device_path = disk.system_path_prefix(); | |
100 if (!disk.mount_path().empty() && | |
101 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) { | |
102 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, | |
103 device_path); | |
104 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED); | |
105 } | |
106 } | |
107 | |
108 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code, | |
109 const VolumeInfo& volume_info, | |
110 bool is_remounting) { | |
111 const std::string& device_path = | |
112 volume_info.system_path_prefix.AsUTF8Unsafe(); | |
113 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
114 } | |
115 | |
116 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code, | |
117 const VolumeInfo& volume_info) { | |
118 // Do nothing. | |
119 } | |
120 | |
121 void DeviceEventRouter::OnFormatStarted(const std::string& device_path, | |
122 bool success) { | |
123 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
124 | |
125 if (success) { | |
126 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_START, | |
127 device_path); | |
128 } else { | |
129 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, | |
130 device_path); | |
131 } | |
132 } | |
133 | |
134 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path, | |
135 bool success) { | |
136 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
137 OnDeviceEvent(success ? file_browser_private::DEVICE_EVENT_TYPE_FORMAT_SUCCESS | |
138 : file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, | |
139 device_path); | |
140 } | |
141 | |
142 void DeviceEventRouter::SuspendImminent() { | |
143 is_suspended_ = true; | |
144 } | |
145 | |
146 void DeviceEventRouter::SuspendDone(const base::TimeDelta& sleep_duration) { | |
147 PostDelayedTask(base::Bind(&DeviceEventRouter::SuspendDoneDelayed, | |
148 weak_factory_.GetWeakPtr()), | |
149 kResumingTimeSpan); | |
150 } | |
151 | |
152 void DeviceEventRouter::SuspendDoneDelayed() { | |
153 is_suspended_ = false; | |
154 } | |
155 | |
156 DeviceState DeviceEventRouter::GetDeviceState(const std::string& device_path) { | |
157 return device_states_.count(device_path) > 0 ? device_states_[device_path] | |
158 : DEVICE_STATE_USUAL; | |
159 } | |
160 | |
161 void DeviceEventRouter::SetDeviceState(const std::string& device_path, | |
162 DeviceState state) { | |
163 if (state != DEVICE_STATE_USUAL) { | |
164 device_states_[device_path] = state; | |
165 } else { | |
166 const std::map<std::string, DeviceState>::iterator it = | |
167 device_states_.find(device_path); | |
168 if (it != device_states_.end()) | |
169 device_states_.erase(it); | |
170 } | |
171 } | |
172 | |
173 } // namespace file_manager | |
OLD | NEW |