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

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: Remove PostDelayedTask viertual function 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 "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(bool zero_time_delta)
20 : zero_time_delta_(zero_time_delta),
21 is_starting_up_(false),
22 is_resuming_(false),
23 weak_factory_(this) {
24 }
25
26 DeviceEventRouter::~DeviceEventRouter() {
27 }
28
29 void DeviceEventRouter::Startup() {
30 is_starting_up_ = true;
31 PostDelayedTask(base::Bind(&DeviceEventRouter::StartupDelayed,
mtomasz 2014/08/21 08:32:46 Is this PostDelayedTask method needed?
hirono 2014/08/21 08:48:32 This is a helper method to call MessageLoop::PostD
mtomasz 2014/08/22 04:50:10 Do we really need it? All it does is to determine
hirono 2014/08/22 05:32:17 Does not have strong idea. It looks 1 helper metho
32 weak_factory_.GetWeakPtr()),
33 kStartupTimeSpan);
34 }
35
36 void DeviceEventRouter::StartupDelayed() {
37 DCHECK_CURRENTLY_ON(BrowserThread::UI);
38 is_starting_up_ = false;
39 }
40
41 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI);
43
44 if (is_starting_up_ || is_resuming_) {
45 SetDeviceState(device_path, DEVICE_STATE_USUAL);
46 return;
47 }
48
49 if (IsExternalStorageDisabled()) {
50 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_DISABLED,
51 device_path);
52 SetDeviceState(device_path, DEVICE_STATE_USUAL);
53 return;
54 }
55
56 SetDeviceState(device_path, DEVICE_SCANNED);
57 PostDelayedTask(base::Bind(&DeviceEventRouter::OnDeviceAddedDelayed,
58 weak_factory_.GetWeakPtr(),
59 device_path),
60 kScanReportTimeSpan);
61 }
62
63 void DeviceEventRouter::OnDeviceAddedDelayed(const std::string& device_path) {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65
66 if (GetDeviceState(device_path) == DEVICE_SCANNED) {
67 // TODO(hirono): Rename DEVICE_EVENT_TYPE_ADDED with
68 // DEVICE_EVENT_TYPE_SCAN_STARTED.
69 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_ADDED, device_path);
70 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED);
71 }
72 }
73
74 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 SetDeviceState(device_path, DEVICE_STATE_USUAL);
77 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, device_path);
78 }
79
80 void DeviceEventRouter::OnDiskAdded(
81 const chromeos::disks::DiskMountManager::Disk& disk,
82 bool mounting) {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84
85 if (!mounting) {
86 // If the disk is not being mounted, mark the device scan cancelled.
87 const std::string& device_path = disk.system_path_prefix();
88 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) {
89 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED,
90 device_path);
91 }
92 SetDeviceState(device_path, DEVICE_STATE_USUAL);
93 }
94 }
95
96 void DeviceEventRouter::OnDiskRemoved(
97 const chromeos::disks::DiskMountManager::Disk& disk) {
98 DCHECK_CURRENTLY_ON(BrowserThread::UI);
99 if (is_resuming_ || is_starting_up_)
100 return;
101
102 const std::string& device_path = disk.system_path_prefix();
103 if (!disk.mount_path().empty() &&
104 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) {
105 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED,
106 device_path);
107 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED);
108 }
109 }
110
111 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code,
112 const VolumeInfo& volume_info,
113 bool is_remounting) {
114 const std::string& device_path =
115 volume_info.system_path_prefix.AsUTF8Unsafe();
116 SetDeviceState(device_path, DEVICE_STATE_USUAL);
117 }
118
119 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code,
120 const VolumeInfo& volume_info) {
121 // Do nothing.
122 }
123
124 void DeviceEventRouter::OnFormatStarted(const std::string& device_path,
125 bool success) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127
128 if (success) {
129 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_START,
130 device_path);
131 } else {
132 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL,
133 device_path);
134 }
135 }
136
137 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path,
138 bool success) {
139 DCHECK_CURRENTLY_ON(BrowserThread::UI);
140 OnDeviceEvent(success ? file_browser_private::DEVICE_EVENT_TYPE_FORMAT_SUCCESS
141 : file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL,
142 device_path);
143 }
144
145 void DeviceEventRouter::OnHardUnplugged(const std::string& device_path) {
146 }
147
148 void DeviceEventRouter::SuspendImminent() {
149 is_resuming_ = true;
150 }
151
152 void DeviceEventRouter::SuspendDone(const base::TimeDelta& sleep_duration) {
153 base::MessageLoopProxy::current()->PostDelayedTask(
mtomasz 2014/08/21 08:32:46 MessageLoopProxy is deprecated. Please use ThreadT
hirono 2014/08/21 08:48:32 Done.
154 FROM_HERE,
155 base::Bind(&DeviceEventRouter::SuspendDoneDelayed,
156 weak_factory_.GetWeakPtr()),
157 kResumingTimeSpan);
158 }
159
160 void DeviceEventRouter::SuspendDoneDelayed() {
161 is_resuming_ = false;
162 }
163
164 void DeviceEventRouter::PostDelayedTask(const base::Closure& closure,
165 base::TimeDelta time_delta) {
166 base::MessageLoopProxy::current()->PostDelayedTask(
167 FROM_HERE,
168 closure,
169 zero_time_delta_ ? base::TimeDelta::FromSeconds(0) : time_delta);
170 }
171
172 DeviceState DeviceEventRouter::GetDeviceState(const std::string& device_path) {
173 return device_states_.count(device_path) > 0 ? device_states_[device_path]
174 : DEVICE_STATE_USUAL;
175 }
176
177 void DeviceEventRouter::SetDeviceState(const std::string& device_path,
178 DeviceState state) {
179 if (state != DEVICE_STATE_USUAL) {
180 device_states_[device_path] = state;
181 } else {
182 const std::map<std::string, DeviceState>::iterator it =
183 device_states_.find(device_path);
184 if (it != device_states_.end())
185 device_states_.erase(it);
186 }
187 }
188
189 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698