| 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 #ifndef CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ |
| 6 #define CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/id_map.h" | 13 #include "base/id_map.h" |
| 14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 | 16 |
| 17 namespace gdata { | 17 namespace gdata { |
| 18 | 18 |
| 19 // Unique ID to identify each operation. |
| 20 typedef int32 OperationID; |
| 21 |
| 22 // Enumeration type for indicating the direction of the operation. |
| 23 enum OperationType { |
| 24 OPERATION_UPLOAD, |
| 25 OPERATION_DOWNLOAD, |
| 26 OPERATION_OTHER, |
| 27 }; |
| 28 |
| 29 // Enumeration type for indicating the state of the transfer. |
| 30 enum OperationTransferState { |
| 31 OPERATION_NOT_STARTED, |
| 32 OPERATION_STARTED, |
| 33 OPERATION_IN_PROGRESS, |
| 34 OPERATION_COMPLETED, |
| 35 OPERATION_FAILED, |
| 36 OPERATION_SUSPENDED, |
| 37 }; |
| 38 |
| 39 // Returns string representations of the operation type and state, which are |
| 40 // exposed to the private extension API as in: |
| 41 // operation.chrome/common/extensions/api/file_browser_private.json |
| 42 std::string OperationTypeToString(OperationType type); |
| 43 std::string OperationTransferStateToString(OperationTransferState state); |
| 44 |
| 45 // Structure that packs progress information of each operation. |
| 46 struct OperationProgressStatus { |
| 47 OperationProgressStatus(OperationType type, const FilePath& file_path); |
| 48 // For debugging |
| 49 std::string DebugString() const; |
| 50 |
| 51 OperationID operation_id; |
| 52 |
| 53 // Type of the operation: upload/download. |
| 54 OperationType operation_type; |
| 55 // GData path of the file dealt with the current operation. |
| 56 FilePath file_path; |
| 57 // Current state of the transfer; |
| 58 OperationTransferState transfer_state; |
| 59 // The time when the operation is initiated. |
| 60 base::Time start_time; |
| 61 // Current fraction of progress of the operation. |
| 62 int64 progress_current; |
| 63 // Expected total number of bytes to be transferred in the operation. |
| 64 // -1 if no expectation is available (yet). |
| 65 int64 progress_total; |
| 66 }; |
| 67 typedef std::vector<OperationProgressStatus> OperationProgressStatusList; |
| 68 |
| 69 |
| 19 // This class tracks all the in-flight GData operation objects and manage their | 70 // This class tracks all the in-flight GData operation objects and manage their |
| 20 // lifetime. | 71 // lifetime. |
| 21 class OperationRegistry { | 72 class OperationRegistry { |
| 22 public: | 73 public: |
| 23 OperationRegistry(); | 74 OperationRegistry(); |
| 24 ~OperationRegistry(); | 75 ~OperationRegistry(); |
| 25 | 76 |
| 26 // Unique ID to identify each operation. | |
| 27 typedef int32 OperationID; | |
| 28 | |
| 29 // Enumeration type for indicating the direction of the operation. | |
| 30 enum OperationType { | |
| 31 OPERATION_UPLOAD, | |
| 32 OPERATION_DOWNLOAD, | |
| 33 OPERATION_OTHER, | |
| 34 }; | |
| 35 | |
| 36 enum OperationTransferState { | |
| 37 OPERATION_NOT_STARTED, | |
| 38 OPERATION_STARTED, | |
| 39 OPERATION_IN_PROGRESS, | |
| 40 OPERATION_COMPLETED, | |
| 41 OPERATION_FAILED, | |
| 42 OPERATION_SUSPENDED, | |
| 43 }; | |
| 44 | |
| 45 // Returns string representations of the operation type and state, which are | |
| 46 // exposed to the private extension API as in: | |
| 47 // operation.chrome/common/extensions/api/file_browser_private.json | |
| 48 static std::string OperationTypeToString(OperationType type); | |
| 49 static std::string OperationTransferStateToString( | |
| 50 OperationTransferState state); | |
| 51 | |
| 52 // Structure that packs progress information of each operation. | |
| 53 struct ProgressStatus { | |
| 54 ProgressStatus(OperationType type, const FilePath& file_path); | |
| 55 // For debugging | |
| 56 std::string DebugString() const; | |
| 57 | |
| 58 OperationID operation_id; | |
| 59 | |
| 60 // Type of the operation: upload/download. | |
| 61 OperationType operation_type; | |
| 62 // GData path of the file dealt with the current operation. | |
| 63 FilePath file_path; | |
| 64 // Current state of the transfer; | |
| 65 OperationTransferState transfer_state; | |
| 66 // The time when the operation is initiated. | |
| 67 base::Time start_time; | |
| 68 // Current fraction of progress of the operation. | |
| 69 int64 progress_current; | |
| 70 // Expected total number of bytes to be transferred in the operation. | |
| 71 // -1 if no expectation is available (yet). | |
| 72 int64 progress_total; | |
| 73 }; | |
| 74 typedef std::vector<ProgressStatus> ProgressStatusList; | |
| 75 | |
| 76 // Observer interface for listening changes in the active set of operations. | 77 // Observer interface for listening changes in the active set of operations. |
| 77 class Observer { | 78 class Observer { |
| 78 public: | 79 public: |
| 79 // Called when a GData operation started, made some progress, or finished. | 80 // Called when a GData operation started, made some progress, or finished. |
| 80 virtual void OnProgressUpdate(const ProgressStatusList& list) = 0; | 81 virtual void OnProgressUpdate(const OperationProgressStatusList& list) = 0; |
| 81 // Called when GData authentication failed. | 82 // Called when GData authentication failed. |
| 82 virtual void OnAuthenticationFailed() {} | 83 virtual void OnAuthenticationFailed() {} |
| 83 protected: | 84 protected: |
| 84 virtual ~Observer() {} | 85 virtual ~Observer() {} |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 // Base class for operations that this registry class can maintain. | 88 // Base class for operations that this registry class can maintain. |
| 88 // NotifyStart() passes the ownership of the Operation object to the registry. | 89 // NotifyStart() passes the ownership of the Operation object to the registry. |
| 89 // In particular, calling NotifyFinish() causes the registry to delete the | 90 // In particular, calling NotifyFinish() causes the registry to delete the |
| 90 // Operation object itself. | 91 // Operation object itself. |
| 91 class Operation { | 92 class Operation { |
| 92 public: | 93 public: |
| 93 explicit Operation(OperationRegistry* registry); | 94 explicit Operation(OperationRegistry* registry); |
| 94 Operation(OperationRegistry* registry, | 95 Operation(OperationRegistry* registry, |
| 95 OperationType type, | 96 OperationType type, |
| 96 const FilePath& file_path); | 97 const FilePath& file_path); |
| 97 virtual ~Operation(); | 98 virtual ~Operation(); |
| 98 | 99 |
| 99 // Cancels the ongoing operation. NotifyFinish() is called and the Operation | 100 // Cancels the ongoing operation. NotifyFinish() is called and the Operation |
| 100 // object is deleted once the cancellation is done in DoCancel(). | 101 // object is deleted once the cancellation is done in DoCancel(). |
| 101 void Cancel(); | 102 void Cancel(); |
| 102 | 103 |
| 103 // Retrieves the current progress status of the operation. | 104 // Retrieves the current progress status of the operation. |
| 104 const ProgressStatus& progress_status() const { return progress_status_; } | 105 const OperationProgressStatus& progress_status() const { |
| 106 return progress_status_; |
| 107 } |
| 105 | 108 |
| 106 protected: | 109 protected: |
| 107 // Notifies the registry about current status. | 110 // Notifies the registry about current status. |
| 108 void NotifyStart(); | 111 void NotifyStart(); |
| 109 void NotifyProgress(int64 current, int64 total); | 112 void NotifyProgress(int64 current, int64 total); |
| 110 void NotifyFinish(OperationTransferState status); | 113 void NotifyFinish(OperationTransferState status); |
| 111 // Notifies suspend/resume, used for chunked upload operations. | 114 // Notifies suspend/resume, used for chunked upload operations. |
| 112 // The initial upload operation should issue "start" "progress"* "suspend". | 115 // The initial upload operation should issue "start" "progress"* "suspend". |
| 113 // The subsequent operations will call "resume" "progress"* "suspend", | 116 // The subsequent operations will call "resume" "progress"* "suspend", |
| 114 // and the last one will do "resume" "progress"* "finish". | 117 // and the last one will do "resume" "progress"* "finish". |
| 115 // In other words, "suspend" is similar to "finish" except it lasts to live | 118 // In other words, "suspend" is similar to "finish" except it lasts to live |
| 116 // until the next "resume" comes. "Resume" is similar to "start", except | 119 // until the next "resume" comes. "Resume" is similar to "start", except |
| 117 // that it removes the existing "suspend" operation. | 120 // that it removes the existing "suspend" operation. |
| 118 void NotifySuspend(); | 121 void NotifySuspend(); |
| 119 void NotifyResume(); | 122 void NotifyResume(); |
| 120 // Notifies that authentication has failed. | 123 // Notifies that authentication has failed. |
| 121 void NotifyAuthFailed(); | 124 void NotifyAuthFailed(); |
| 122 | 125 |
| 123 private: | 126 private: |
| 124 // Does the cancellation. | 127 // Does the cancellation. |
| 125 virtual void DoCancel() = 0; | 128 virtual void DoCancel() = 0; |
| 126 | 129 |
| 127 OperationRegistry* const registry_; | 130 OperationRegistry* const registry_; |
| 128 ProgressStatus progress_status_; | 131 OperationProgressStatus progress_status_; |
| 129 }; | 132 }; |
| 130 | 133 |
| 131 // Cancels all in-flight operations. | 134 // Cancels all in-flight operations. |
| 132 void CancelAll(); | 135 void CancelAll(); |
| 133 | 136 |
| 134 // Cancels ongoing operation for a given virtual |file_path|. Returns true if | 137 // Cancels ongoing operation for a given virtual |file_path|. Returns true if |
| 135 // the operation was found and canceled. | 138 // the operation was found and canceled. |
| 136 bool CancelForFilePath(const FilePath& file_path); | 139 bool CancelForFilePath(const FilePath& file_path); |
| 137 | 140 |
| 138 // Obtains the list of currently active operations. | 141 // Obtains the list of currently active operations. |
| 139 ProgressStatusList GetProgressStatusList(); | 142 OperationProgressStatusList GetProgressStatusList(); |
| 140 | 143 |
| 141 // Sets an observer. The registry do NOT own observers; before destruction | 144 // Sets an observer. The registry do NOT own observers; before destruction |
| 142 // they need to be removed from the registry. | 145 // they need to be removed from the registry. |
| 143 void AddObserver(Observer* observer); | 146 void AddObserver(Observer* observer); |
| 144 void RemoveObserver(Observer* observer); | 147 void RemoveObserver(Observer* observer); |
| 145 | 148 |
| 146 // Disables the notification suppression for testing purpose. | 149 // Disables the notification suppression for testing purpose. |
| 147 void DisableNotificationFrequencyControlForTest(); | 150 void DisableNotificationFrequencyControlForTest(); |
| 148 | 151 |
| 149 private: | 152 private: |
| 150 // Handlers for notifications from Operations. | 153 // Handlers for notifications from Operations. |
| 151 friend class Operation; | 154 friend class Operation; |
| 152 // Notifies that an operation has started. This method passes the ownership of | 155 // Notifies that an operation has started. This method passes the ownership of |
| 153 // the operation to the registry. A fresh operation ID is returned to *id. | 156 // the operation to the registry. A fresh operation ID is returned to *id. |
| 154 void OnOperationStart(Operation* operation, OperationID* id); | 157 void OnOperationStart(Operation* operation, OperationID* id); |
| 155 void OnOperationProgress(OperationID operation); | 158 void OnOperationProgress(OperationID operation); |
| 156 void OnOperationFinish(OperationID operation); | 159 void OnOperationFinish(OperationID operation); |
| 157 void OnOperationSuspend(OperationID operation); | 160 void OnOperationSuspend(OperationID operation); |
| 158 void OnOperationResume(Operation* operation, ProgressStatus* new_status); | 161 void OnOperationResume(Operation* operation, |
| 162 OperationProgressStatus* new_status); |
| 159 void OnOperationAuthFailed(); | 163 void OnOperationAuthFailed(); |
| 160 | 164 |
| 161 bool IsFileTransferOperation(const Operation* operation) const; | 165 bool IsFileTransferOperation(const Operation* operation) const; |
| 162 | 166 |
| 163 // Controls the frequency of notifications, not to flood the listeners with | 167 // Controls the frequency of notifications, not to flood the listeners with |
| 164 // too many events. | 168 // too many events. |
| 165 bool ShouldNotifyStatusNow(const ProgressStatusList& list); | 169 bool ShouldNotifyStatusNow(const OperationProgressStatusList& list); |
| 166 // Sends notifications to the observers after checking that the frequency is | 170 // Sends notifications to the observers after checking that the frequency is |
| 167 // not too high by ShouldNotifyStatusNow. | 171 // not too high by ShouldNotifyStatusNow. |
| 168 void NotifyStatusToObservers(); | 172 void NotifyStatusToObservers(); |
| 169 | 173 |
| 170 typedef IDMap<Operation, IDMapOwnPointer> OperationIDMap; | 174 typedef IDMap<Operation, IDMapOwnPointer> OperationIDMap; |
| 171 OperationIDMap in_flight_operations_; | 175 OperationIDMap in_flight_operations_; |
| 172 ObserverList<Observer> observer_list_; | 176 ObserverList<Observer> observer_list_; |
| 173 base::Time last_notification_; | 177 base::Time last_notification_; |
| 174 bool do_notification_frequency_control_; | 178 bool do_notification_frequency_control_; |
| 175 | 179 |
| 176 DISALLOW_COPY_AND_ASSIGN(OperationRegistry); | 180 DISALLOW_COPY_AND_ASSIGN(OperationRegistry); |
| 177 }; | 181 }; |
| 178 | 182 |
| 179 } // namespace gdata | 183 } // namespace gdata |
| 180 | 184 |
| 181 #endif // CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ | 185 #endif // CHROME_BROWSER_GOOGLE_APIS_OPERATION_REGISTRY_H_ |
| OLD | NEW |