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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_operation_registry.cc

Issue 10837338: Remove "GData" prefix from non-GData specific classes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase. Created 8 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 (c) 2012 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 "chrome/browser/chromeos/gdata/gdata_operation_registry.h"
6
7 #include "base/string_number_conversions.h"
8 #include "content/public/browser/browser_thread.h"
9
10 using content::BrowserThread;
11
12 namespace {
13
14 const int64 kNotificationFrequencyInMilliseconds = 1000;
15
16 } // namespace
17
18 namespace gdata {
19
20 // static
21 std::string GDataOperationRegistry::OperationTypeToString(OperationType type) {
22 switch (type) {
23 case OPERATION_UPLOAD: return "upload";
24 case OPERATION_DOWNLOAD: return "download";
25 case OPERATION_OTHER: return "other";
26 }
27 NOTREACHED();
28 return "unknown_transfer_state";
29 }
30
31 // static
32 std::string GDataOperationRegistry::OperationTransferStateToString(
33 OperationTransferState state) {
34 switch (state) {
35 case OPERATION_NOT_STARTED: return "not_started";
36 case OPERATION_STARTED: return "started";
37 case OPERATION_IN_PROGRESS: return "in_progress";
38 case OPERATION_COMPLETED: return "completed";
39 case OPERATION_FAILED: return "failed";
40 // Suspended state is opaque to users and looks as same as "in_progress".
41 case OPERATION_SUSPENDED: return "in_progress";
42 }
43 NOTREACHED();
44 return "unknown_transfer_state";
45 }
46
47 GDataOperationRegistry::ProgressStatus::ProgressStatus(OperationType type,
48 const FilePath& path)
49 : operation_id(-1),
50 operation_type(type),
51 file_path(path),
52 transfer_state(OPERATION_NOT_STARTED),
53 progress_current(0),
54 progress_total(-1) {
55 }
56
57 std::string GDataOperationRegistry::ProgressStatus::DebugString() const {
58 std::string str;
59 str += "id=";
60 str += base::IntToString(operation_id);
61 str += " type=";
62 str += OperationTypeToString(operation_type);
63 str += " path=";
64 str += file_path.AsUTF8Unsafe();
65 str += " state=";
66 str += OperationTransferStateToString(transfer_state);
67 str += " progress=";
68 str += base::Int64ToString(progress_current);
69 str += "/";
70 str += base::Int64ToString(progress_total);
71 return str;
72 }
73
74 GDataOperationRegistry::Operation::Operation(GDataOperationRegistry* registry)
75 : registry_(registry),
76 progress_status_(GDataOperationRegistry::OPERATION_OTHER, FilePath()) {
77 }
78
79 GDataOperationRegistry::Operation::Operation(GDataOperationRegistry* registry,
80 OperationType type,
81 const FilePath& path)
82 : registry_(registry),
83 progress_status_(type, path) {
84 }
85
86 GDataOperationRegistry::Operation::~Operation() {
87 DCHECK(progress_status_.transfer_state == OPERATION_COMPLETED ||
88 progress_status_.transfer_state == OPERATION_SUSPENDED ||
89 progress_status_.transfer_state == OPERATION_FAILED);
90 }
91
92 void GDataOperationRegistry::Operation::Cancel() {
93 DoCancel();
94 NotifyFinish(OPERATION_FAILED);
95 }
96
97 void GDataOperationRegistry::Operation::NotifyStart() {
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 // Some operations may be restarted. Report only the first "start".
100 if (progress_status_.transfer_state == OPERATION_NOT_STARTED) {
101 progress_status_.transfer_state = OPERATION_STARTED;
102 progress_status_.start_time = base::Time::Now();
103 registry_->OnOperationStart(this, &progress_status_.operation_id);
104 }
105 }
106
107 void GDataOperationRegistry::Operation::NotifyProgress(
108 int64 current, int64 total) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110 DCHECK(progress_status_.transfer_state >= OPERATION_STARTED);
111 progress_status_.transfer_state = OPERATION_IN_PROGRESS;
112 progress_status_.progress_current = current;
113 progress_status_.progress_total = total;
114 registry_->OnOperationProgress(progress_status().operation_id);
115 }
116
117 void GDataOperationRegistry::Operation::NotifyFinish(
118 OperationTransferState status) {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
120 DCHECK(progress_status_.transfer_state >= OPERATION_STARTED);
121 DCHECK(status == OPERATION_COMPLETED || status == OPERATION_FAILED);
122 progress_status_.transfer_state = status;
123 registry_->OnOperationFinish(progress_status().operation_id);
124 }
125
126 void GDataOperationRegistry::Operation::NotifySuspend() {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 DCHECK(progress_status_.transfer_state >= OPERATION_STARTED);
129 progress_status_.transfer_state = OPERATION_SUSPENDED;
130 registry_->OnOperationSuspend(progress_status().operation_id);
131 }
132
133 void GDataOperationRegistry::Operation::NotifyResume() {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
135 if (progress_status_.transfer_state == OPERATION_NOT_STARTED) {
136 progress_status_.transfer_state = OPERATION_IN_PROGRESS;
137 registry_->OnOperationResume(this, &progress_status_);
138 }
139 }
140
141 void GDataOperationRegistry::Operation::NotifyAuthFailed() {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143 registry_->OnOperationAuthFailed();
144 }
145
146 GDataOperationRegistry::GDataOperationRegistry()
147 : do_notification_frequency_control_(true) {
148 in_flight_operations_.set_check_on_null_data(true);
149 }
150
151 GDataOperationRegistry::~GDataOperationRegistry() {
152 DCHECK(in_flight_operations_.IsEmpty());
153 }
154
155 void GDataOperationRegistry::AddObserver(Observer* observer) {
156 observer_list_.AddObserver(observer);
157 }
158
159 void GDataOperationRegistry::RemoveObserver(Observer* observer) {
160 observer_list_.RemoveObserver(observer);
161 }
162
163 void GDataOperationRegistry::DisableNotificationFrequencyControlForTest() {
164 do_notification_frequency_control_ = false;
165 }
166
167 void GDataOperationRegistry::CancelAll() {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
169
170 for (OperationIDMap::iterator iter(&in_flight_operations_);
171 !iter.IsAtEnd();
172 iter.Advance()) {
173 Operation* operation = iter.GetCurrentValue();
174 operation->Cancel();
175 // Cancel() may immediately trigger OnOperationFinish and remove the
176 // operation from the map, but IDMap is designed to be safe on such remove
177 // while iteration.
178 }
179 }
180
181 bool GDataOperationRegistry::CancelForFilePath(const FilePath& file_path) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
183
184 for (OperationIDMap::iterator iter(&in_flight_operations_);
185 !iter.IsAtEnd();
186 iter.Advance()) {
187 Operation* operation = iter.GetCurrentValue();
188 if (operation->progress_status().file_path == file_path) {
189 operation->Cancel();
190 return true;
191 }
192 }
193 return false;
194 }
195
196 void GDataOperationRegistry::OnOperationStart(
197 GDataOperationRegistry::Operation* operation,
198 OperationID* id) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
200
201 *id = in_flight_operations_.Add(operation);
202 DVLOG(1) << "GDataOperation[" << *id << "] started.";
203 if (IsFileTransferOperation(operation))
204 NotifyStatusToObservers();
205 }
206
207 void GDataOperationRegistry::OnOperationProgress(OperationID id) {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
209
210 Operation* operation = in_flight_operations_.Lookup(id);
211 DCHECK(operation);
212
213 DVLOG(1) << "GDataOperation[" << id << "] "
214 << operation->progress_status().DebugString();
215 if (IsFileTransferOperation(operation))
216 NotifyStatusToObservers();
217 }
218
219 void GDataOperationRegistry::OnOperationFinish(OperationID id) {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
221
222 Operation* operation = in_flight_operations_.Lookup(id);
223 DCHECK(operation);
224
225 DVLOG(1) << "GDataOperation[" << id << "] finished.";
226 if (IsFileTransferOperation(operation))
227 NotifyStatusToObservers();
228 in_flight_operations_.Remove(id);
229 }
230
231 void GDataOperationRegistry::OnOperationResume(
232 GDataOperationRegistry::Operation* operation,
233 GDataOperationRegistry::ProgressStatus* new_status) {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235
236 // Find the corresponding suspended task.
237 Operation* suspended = NULL;
238 for (OperationIDMap::iterator iter(&in_flight_operations_);
239 !iter.IsAtEnd();
240 iter.Advance()) {
241 Operation* in_flight_operation = iter.GetCurrentValue();
242 const ProgressStatus& status = in_flight_operation->progress_status();
243 if (status.transfer_state == OPERATION_SUSPENDED &&
244 status.file_path == operation->progress_status().file_path) {
245 suspended = in_flight_operation;
246 break;
247 }
248 }
249 DCHECK(suspended);
250
251 // Copy the progress status.
252 const ProgressStatus& old_status = suspended->progress_status();
253 OperationID old_id = old_status.operation_id;
254
255 new_status->progress_current = old_status.progress_current;
256 new_status->progress_total = old_status.progress_total;
257 new_status->start_time = old_status.start_time;
258
259 // Remove the old one and initiate the new operation.
260 in_flight_operations_.Remove(old_id);
261 new_status->operation_id = in_flight_operations_.Add(operation);
262 DVLOG(1) << "GDataOperation[" << old_id << " -> " <<
263 new_status->operation_id << "] resumed.";
264 if (IsFileTransferOperation(operation))
265 NotifyStatusToObservers();
266 }
267
268 void GDataOperationRegistry::OnOperationSuspend(OperationID id) {
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
270
271 Operation* operation = in_flight_operations_.Lookup(id);
272 DCHECK(operation);
273
274 DVLOG(1) << "GDataOperation[" << id << "] suspended.";
275 if (IsFileTransferOperation(operation))
276 NotifyStatusToObservers();
277 }
278
279 void GDataOperationRegistry::OnOperationAuthFailed() {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
281
282 DVLOG(1) << "GDataOperation authentication failed.";
283 FOR_EACH_OBSERVER(Observer, observer_list_, OnAuthenticationFailed());
284 }
285
286 bool GDataOperationRegistry::IsFileTransferOperation(
287 const Operation* operation) const {
288 OperationType type = operation->progress_status().operation_type;
289 return type == OPERATION_UPLOAD || type == OPERATION_DOWNLOAD;
290 }
291
292 GDataOperationRegistry::ProgressStatusList
293 GDataOperationRegistry::GetProgressStatusList() {
294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
295
296 ProgressStatusList status_list;
297 for (OperationIDMap::const_iterator iter(&in_flight_operations_);
298 !iter.IsAtEnd();
299 iter.Advance()) {
300 const Operation* operation = iter.GetCurrentValue();
301 if (IsFileTransferOperation(operation))
302 status_list.push_back(operation->progress_status());
303 }
304 return status_list;
305 }
306
307 bool GDataOperationRegistry::ShouldNotifyStatusNow(
308 const ProgressStatusList& list) {
309 if (!do_notification_frequency_control_)
310 return true;
311
312 base::Time now = base::Time::Now();
313
314 // If it is a first event, or some time abnormality is detected, we should
315 // not skip this notification.
316 if (last_notification_.is_null() || now < last_notification_) {
317 last_notification_ = now;
318 return true;
319 }
320
321 // If sufficiently long time has elapsed since the previous event, we should
322 // not skip this notification.
323 if ((now - last_notification_).InMilliseconds() >=
324 kNotificationFrequencyInMilliseconds) {
325 last_notification_ = now;
326 return true;
327 }
328
329 // If important events (OPERATION_STARTED, COMPLETED, or FAILED) are there,
330 // we should not skip this notification.
331 for (size_t i = 0; i < list.size(); ++i) {
332 if (list[i].transfer_state != OPERATION_IN_PROGRESS) {
333 last_notification_ = now;
334 return true;
335 }
336 }
337
338 // Otherwise we can skip it.
339 return false;
340 }
341
342 void GDataOperationRegistry::NotifyStatusToObservers() {
343 ProgressStatusList list(GetProgressStatusList());
344 if (ShouldNotifyStatusNow(list))
345 FOR_EACH_OBSERVER(Observer, observer_list_, OnProgressUpdate(list));
346 }
347
348 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698