Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/device_event_router.cc

Issue 472603004: Add DeviceEventRouter class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 const base::TimeDelta kStartupTimeSpan = base::TimeDelta::FromSeconds(10);
mtomasz 2014/08/22 04:50:10 Objects of class types are not allowed as global/s
hirono 2014/08/22 05:32:17 Done.
16 const base::TimeDelta kScanReportTimeSpan = base::TimeDelta::FromSeconds(5);
17 const base::TimeDelta kResumingTimeSpan = base::TimeDelta::FromSeconds(5);
18 } // namespace
19
20 DeviceEventRouter::DeviceEventRouter(bool zero_time_delta)
21 : zero_time_delta_(zero_time_delta),
22 is_starting_up_(false),
23 is_resuming_(false),
24 weak_factory_(this) {
25 }
26
27 DeviceEventRouter::~DeviceEventRouter() {
28 }
29
30 void DeviceEventRouter::Startup() {
31 is_starting_up_ = true;
32 PostDelayedTask(base::Bind(&DeviceEventRouter::StartupDelayed,
33 weak_factory_.GetWeakPtr()),
34 kStartupTimeSpan);
35 }
36
37 void DeviceEventRouter::StartupDelayed() {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 is_starting_up_ = false;
40 }
41
42 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44
45 if (is_starting_up_ || is_resuming_) {
46 SetDeviceState(device_path, DEVICE_STATE_USUAL);
47 return;
48 }
49
50 if (IsExternalStorageDisabled()) {
51 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_DISABLED,
52 device_path);
53 SetDeviceState(device_path, DEVICE_STATE_USUAL);
54 return;
55 }
56
57 SetDeviceState(device_path, DEVICE_SCANNED);
58 PostDelayedTask(base::Bind(&DeviceEventRouter::OnDeviceAddedDelayed,
59 weak_factory_.GetWeakPtr(),
60 device_path),
61 kScanReportTimeSpan);
62 }
63
64 void DeviceEventRouter::OnDeviceAddedDelayed(const std::string& device_path) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mtomasz 2014/08/22 05:48:40 Why are the thread checks gone? If they don't work
hirono 2014/08/22 06:29:25 Done.
66
67 if (GetDeviceState(device_path) == DEVICE_SCANNED) {
68 // TODO(hirono): Rename DEVICE_EVENT_TYPE_ADDED with
69 // DEVICE_EVENT_TYPE_SCAN_STARTED.
70 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_ADDED, device_path);
71 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED);
72 }
73 }
74
75 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) {
76 DCHECK_CURRENTLY_ON(BrowserThread::UI);
77 SetDeviceState(device_path, DEVICE_STATE_USUAL);
78 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, device_path);
79 }
80
81 void DeviceEventRouter::OnDiskAdded(
82 const chromeos::disks::DiskMountManager::Disk& disk,
83 bool mounting) {
84 DCHECK_CURRENTLY_ON(BrowserThread::UI);
85
86 if (!mounting) {
87 // If the disk is not being mounted, mark the device scan cancelled.
88 const std::string& device_path = disk.system_path_prefix();
89 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) {
90 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED,
91 device_path);
92 }
93 SetDeviceState(device_path, DEVICE_STATE_USUAL);
94 }
95 }
96
97 void DeviceEventRouter::OnDiskRemoved(
98 const chromeos::disks::DiskMountManager::Disk& disk) {
99 DCHECK_CURRENTLY_ON(BrowserThread::UI);
100 if (is_resuming_ || is_starting_up_)
101 return;
102
103 const std::string& device_path = disk.system_path_prefix();
104 if (!disk.mount_path().empty() &&
105 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) {
106 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED,
107 device_path);
108 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED);
109 }
110 }
111
112 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code,
113 const VolumeInfo& volume_info,
114 bool is_remounting) {
115 const std::string& device_path =
116 volume_info.system_path_prefix.AsUTF8Unsafe();
117 SetDeviceState(device_path, DEVICE_STATE_USUAL);
118 }
119
120 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code,
121 const VolumeInfo& volume_info) {
122 // Do nothing.
123 }
124
125 void DeviceEventRouter::OnFormatStarted(const std::string& device_path,
126 bool success) {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI);
128
129 if (success) {
130 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_START,
131 device_path);
132 } else {
133 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL,
134 device_path);
135 }
136 }
137
138 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path,
139 bool success) {
140 DCHECK_CURRENTLY_ON(BrowserThread::UI);
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(
mtomasz 2014/08/22 04:50:10 ditto
hirono 2014/08/22 05:32:17 Done.
155 FROM_HERE,
156 base::Bind(&DeviceEventRouter::SuspendDoneDelayed,
157 weak_factory_.GetWeakPtr()),
158 kResumingTimeSpan);
159 }
160
161 void DeviceEventRouter::SuspendDoneDelayed() {
162 is_resuming_ = false;
163 }
164
165 void DeviceEventRouter::PostDelayedTask(const base::Closure& closure,
166 base::TimeDelta time_delta) {
167 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
168 FROM_HERE,
169 closure,
170 zero_time_delta_ ? base::TimeDelta::FromSeconds(0) : time_delta);
171 }
172
173 DeviceState DeviceEventRouter::GetDeviceState(
174 const std::string& device_path) const {
175 const std::map<std::string, DeviceState>::const_iterator it =
176 device_states_.find(device_path);
177 return it != device_states_.end() ? it->second : DEVICE_STATE_USUAL;
178 }
179
180 void DeviceEventRouter::SetDeviceState(const std::string& device_path,
181 DeviceState state) {
182 if (state != DEVICE_STATE_USUAL) {
183 device_states_[device_path] = state;
184 } else {
185 const std::map<std::string, DeviceState>::iterator it =
186 device_states_.find(device_path);
187 if (it != device_states_.end())
188 device_states_.erase(it);
189 }
190 }
191
192 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698