| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/google_apis/operation_registry.h" | 5 #include "chrome/browser/google_apis/operation_registry.h" |
| 6 | 6 |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 | 9 |
| 10 using content::BrowserThread; | 10 using content::BrowserThread; |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const int64 kNotificationFrequencyInMilliseconds = 1000; | 14 const int64 kNotificationFrequencyInMilliseconds = 1000; |
| 15 | 15 |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 namespace gdata { | 18 namespace gdata { |
| 19 | 19 |
| 20 // static | 20 std::string OperationTypeToString(OperationType type) { |
| 21 std::string OperationRegistry::OperationTypeToString(OperationType type) { | |
| 22 switch (type) { | 21 switch (type) { |
| 23 case OPERATION_UPLOAD: return "upload"; | 22 case OPERATION_UPLOAD: return "upload"; |
| 24 case OPERATION_DOWNLOAD: return "download"; | 23 case OPERATION_DOWNLOAD: return "download"; |
| 25 case OPERATION_OTHER: return "other"; | 24 case OPERATION_OTHER: return "other"; |
| 26 } | 25 } |
| 27 NOTREACHED(); | 26 NOTREACHED(); |
| 28 return "unknown_transfer_state"; | 27 return "unknown_transfer_state"; |
| 29 } | 28 } |
| 30 | 29 |
| 31 // static | 30 std::string OperationTransferStateToString(OperationTransferState state) { |
| 32 std::string OperationRegistry::OperationTransferStateToString( | |
| 33 OperationTransferState state) { | |
| 34 switch (state) { | 31 switch (state) { |
| 35 case OPERATION_NOT_STARTED: return "not_started"; | 32 case OPERATION_NOT_STARTED: return "not_started"; |
| 36 case OPERATION_STARTED: return "started"; | 33 case OPERATION_STARTED: return "started"; |
| 37 case OPERATION_IN_PROGRESS: return "in_progress"; | 34 case OPERATION_IN_PROGRESS: return "in_progress"; |
| 38 case OPERATION_COMPLETED: return "completed"; | 35 case OPERATION_COMPLETED: return "completed"; |
| 39 case OPERATION_FAILED: return "failed"; | 36 case OPERATION_FAILED: return "failed"; |
| 40 // Suspended state is opaque to users and looks as same as "in_progress". | 37 // Suspended state is opaque to users and looks as same as "in_progress". |
| 41 case OPERATION_SUSPENDED: return "in_progress"; | 38 case OPERATION_SUSPENDED: return "in_progress"; |
| 42 } | 39 } |
| 43 NOTREACHED(); | 40 NOTREACHED(); |
| 44 return "unknown_transfer_state"; | 41 return "unknown_transfer_state"; |
| 45 } | 42 } |
| 46 | 43 |
| 47 OperationRegistry::ProgressStatus::ProgressStatus(OperationType type, | 44 OperationProgressStatus::OperationProgressStatus(OperationType type, |
| 48 const FilePath& path) | 45 const FilePath& path) |
| 49 : operation_id(-1), | 46 : operation_id(-1), |
| 50 operation_type(type), | 47 operation_type(type), |
| 51 file_path(path), | 48 file_path(path), |
| 52 transfer_state(OPERATION_NOT_STARTED), | 49 transfer_state(OPERATION_NOT_STARTED), |
| 53 progress_current(0), | 50 progress_current(0), |
| 54 progress_total(-1) { | 51 progress_total(-1) { |
| 55 } | 52 } |
| 56 | 53 |
| 57 std::string OperationRegistry::ProgressStatus::DebugString() const { | 54 std::string OperationProgressStatus::DebugString() const { |
| 58 std::string str; | 55 std::string str; |
| 59 str += "id="; | 56 str += "id="; |
| 60 str += base::IntToString(operation_id); | 57 str += base::IntToString(operation_id); |
| 61 str += " type="; | 58 str += " type="; |
| 62 str += OperationTypeToString(operation_type); | 59 str += OperationTypeToString(operation_type); |
| 63 str += " path="; | 60 str += " path="; |
| 64 str += file_path.AsUTF8Unsafe(); | 61 str += file_path.AsUTF8Unsafe(); |
| 65 str += " state="; | 62 str += " state="; |
| 66 str += OperationTransferStateToString(transfer_state); | 63 str += OperationTransferStateToString(transfer_state); |
| 67 str += " progress="; | 64 str += " progress="; |
| 68 str += base::Int64ToString(progress_current); | 65 str += base::Int64ToString(progress_current); |
| 69 str += "/"; | 66 str += "/"; |
| 70 str += base::Int64ToString(progress_total); | 67 str += base::Int64ToString(progress_total); |
| 71 return str; | 68 return str; |
| 72 } | 69 } |
| 73 | 70 |
| 74 OperationRegistry::Operation::Operation(OperationRegistry* registry) | 71 OperationRegistry::Operation::Operation(OperationRegistry* registry) |
| 75 : registry_(registry), | 72 : registry_(registry), |
| 76 progress_status_(OperationRegistry::OPERATION_OTHER, FilePath()) { | 73 progress_status_(OPERATION_OTHER, FilePath()) { |
| 77 } | 74 } |
| 78 | 75 |
| 79 OperationRegistry::Operation::Operation(OperationRegistry* registry, | 76 OperationRegistry::Operation::Operation(OperationRegistry* registry, |
| 80 OperationType type, | 77 OperationType type, |
| 81 const FilePath& path) | 78 const FilePath& path) |
| 82 : registry_(registry), | 79 : registry_(registry), |
| 83 progress_status_(type, path) { | 80 progress_status_(type, path) { |
| 84 } | 81 } |
| 85 | 82 |
| 86 OperationRegistry::Operation::~Operation() { | 83 OperationRegistry::Operation::~Operation() { |
| 87 DCHECK(progress_status_.transfer_state == OPERATION_COMPLETED || | 84 DCHECK(progress_status_.transfer_state == OPERATION_COMPLETED || |
| 88 progress_status_.transfer_state == OPERATION_SUSPENDED || | 85 progress_status_.transfer_state == OPERATION_SUSPENDED || |
| 89 progress_status_.transfer_state == OPERATION_FAILED); | 86 progress_status_.transfer_state == OPERATION_FAILED); |
| 90 } | 87 } |
| 91 | 88 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 DCHECK(operation); | 220 DCHECK(operation); |
| 224 | 221 |
| 225 DVLOG(1) << "GDataOperation[" << id << "] finished."; | 222 DVLOG(1) << "GDataOperation[" << id << "] finished."; |
| 226 if (IsFileTransferOperation(operation)) | 223 if (IsFileTransferOperation(operation)) |
| 227 NotifyStatusToObservers(); | 224 NotifyStatusToObservers(); |
| 228 in_flight_operations_.Remove(id); | 225 in_flight_operations_.Remove(id); |
| 229 } | 226 } |
| 230 | 227 |
| 231 void OperationRegistry::OnOperationResume( | 228 void OperationRegistry::OnOperationResume( |
| 232 OperationRegistry::Operation* operation, | 229 OperationRegistry::Operation* operation, |
| 233 OperationRegistry::ProgressStatus* new_status) { | 230 OperationProgressStatus* new_status) { |
| 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 235 | 232 |
| 236 // Find the corresponding suspended task. | 233 // Find the corresponding suspended task. |
| 237 Operation* suspended = NULL; | 234 Operation* suspended = NULL; |
| 238 for (OperationIDMap::iterator iter(&in_flight_operations_); | 235 for (OperationIDMap::iterator iter(&in_flight_operations_); |
| 239 !iter.IsAtEnd(); | 236 !iter.IsAtEnd(); |
| 240 iter.Advance()) { | 237 iter.Advance()) { |
| 241 Operation* in_flight_operation = iter.GetCurrentValue(); | 238 Operation* in_flight_operation = iter.GetCurrentValue(); |
| 242 const ProgressStatus& status = in_flight_operation->progress_status(); | 239 const OperationProgressStatus& status = |
| 240 in_flight_operation->progress_status(); |
| 243 if (status.transfer_state == OPERATION_SUSPENDED && | 241 if (status.transfer_state == OPERATION_SUSPENDED && |
| 244 status.file_path == operation->progress_status().file_path) { | 242 status.file_path == operation->progress_status().file_path) { |
| 245 suspended = in_flight_operation; | 243 suspended = in_flight_operation; |
| 246 break; | 244 break; |
| 247 } | 245 } |
| 248 } | 246 } |
| 249 DCHECK(suspended); | 247 DCHECK(suspended); |
| 250 | 248 |
| 251 // Copy the progress status. | 249 // Copy the progress status. |
| 252 const ProgressStatus& old_status = suspended->progress_status(); | 250 const OperationProgressStatus& old_status = suspended->progress_status(); |
| 253 OperationID old_id = old_status.operation_id; | 251 OperationID old_id = old_status.operation_id; |
| 254 | 252 |
| 255 new_status->progress_current = old_status.progress_current; | 253 new_status->progress_current = old_status.progress_current; |
| 256 new_status->progress_total = old_status.progress_total; | 254 new_status->progress_total = old_status.progress_total; |
| 257 new_status->start_time = old_status.start_time; | 255 new_status->start_time = old_status.start_time; |
| 258 | 256 |
| 259 // Remove the old one and initiate the new operation. | 257 // Remove the old one and initiate the new operation. |
| 260 in_flight_operations_.Remove(old_id); | 258 in_flight_operations_.Remove(old_id); |
| 261 new_status->operation_id = in_flight_operations_.Add(operation); | 259 new_status->operation_id = in_flight_operations_.Add(operation); |
| 262 DVLOG(1) << "GDataOperation[" << old_id << " -> " << | 260 DVLOG(1) << "GDataOperation[" << old_id << " -> " << |
| (...skipping 19 matching lines...) Expand all Loading... |
| 282 DVLOG(1) << "GDataOperation authentication failed."; | 280 DVLOG(1) << "GDataOperation authentication failed."; |
| 283 FOR_EACH_OBSERVER(Observer, observer_list_, OnAuthenticationFailed()); | 281 FOR_EACH_OBSERVER(Observer, observer_list_, OnAuthenticationFailed()); |
| 284 } | 282 } |
| 285 | 283 |
| 286 bool OperationRegistry::IsFileTransferOperation( | 284 bool OperationRegistry::IsFileTransferOperation( |
| 287 const Operation* operation) const { | 285 const Operation* operation) const { |
| 288 OperationType type = operation->progress_status().operation_type; | 286 OperationType type = operation->progress_status().operation_type; |
| 289 return type == OPERATION_UPLOAD || type == OPERATION_DOWNLOAD; | 287 return type == OPERATION_UPLOAD || type == OPERATION_DOWNLOAD; |
| 290 } | 288 } |
| 291 | 289 |
| 292 OperationRegistry::ProgressStatusList | 290 OperationProgressStatusList OperationRegistry::GetProgressStatusList() { |
| 293 OperationRegistry::GetProgressStatusList() { | |
| 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 295 | 292 |
| 296 ProgressStatusList status_list; | 293 OperationProgressStatusList status_list; |
| 297 for (OperationIDMap::const_iterator iter(&in_flight_operations_); | 294 for (OperationIDMap::const_iterator iter(&in_flight_operations_); |
| 298 !iter.IsAtEnd(); | 295 !iter.IsAtEnd(); |
| 299 iter.Advance()) { | 296 iter.Advance()) { |
| 300 const Operation* operation = iter.GetCurrentValue(); | 297 const Operation* operation = iter.GetCurrentValue(); |
| 301 if (IsFileTransferOperation(operation)) | 298 if (IsFileTransferOperation(operation)) |
| 302 status_list.push_back(operation->progress_status()); | 299 status_list.push_back(operation->progress_status()); |
| 303 } | 300 } |
| 304 return status_list; | 301 return status_list; |
| 305 } | 302 } |
| 306 | 303 |
| 307 bool OperationRegistry::ShouldNotifyStatusNow( | 304 bool OperationRegistry::ShouldNotifyStatusNow( |
| 308 const ProgressStatusList& list) { | 305 const OperationProgressStatusList& list) { |
| 309 if (!do_notification_frequency_control_) | 306 if (!do_notification_frequency_control_) |
| 310 return true; | 307 return true; |
| 311 | 308 |
| 312 base::Time now = base::Time::Now(); | 309 base::Time now = base::Time::Now(); |
| 313 | 310 |
| 314 // If it is a first event, or some time abnormality is detected, we should | 311 // If it is a first event, or some time abnormality is detected, we should |
| 315 // not skip this notification. | 312 // not skip this notification. |
| 316 if (last_notification_.is_null() || now < last_notification_) { | 313 if (last_notification_.is_null() || now < last_notification_) { |
| 317 last_notification_ = now; | 314 last_notification_ = now; |
| 318 return true; | 315 return true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 333 last_notification_ = now; | 330 last_notification_ = now; |
| 334 return true; | 331 return true; |
| 335 } | 332 } |
| 336 } | 333 } |
| 337 | 334 |
| 338 // Otherwise we can skip it. | 335 // Otherwise we can skip it. |
| 339 return false; | 336 return false; |
| 340 } | 337 } |
| 341 | 338 |
| 342 void OperationRegistry::NotifyStatusToObservers() { | 339 void OperationRegistry::NotifyStatusToObservers() { |
| 343 ProgressStatusList list(GetProgressStatusList()); | 340 OperationProgressStatusList list(GetProgressStatusList()); |
| 344 if (ShouldNotifyStatusNow(list)) | 341 if (ShouldNotifyStatusNow(list)) |
| 345 FOR_EACH_OBSERVER(Observer, observer_list_, OnProgressUpdate(list)); | 342 FOR_EACH_OBSERVER(Observer, observer_list_, OnProgressUpdate(list)); |
| 346 } | 343 } |
| 347 | 344 |
| 348 } // namespace gdata | 345 } // namespace gdata |
| OLD | NEW |