OLD | NEW |
| (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 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/file_path.h" | |
13 #include "base/id_map.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/time.h" | |
16 | |
17 namespace gdata { | |
18 | |
19 // This class tracks all the in-flight GData operation objects and manage their | |
20 // lifetime. | |
21 class GDataOperationRegistry { | |
22 public: | |
23 GDataOperationRegistry(); | |
24 ~GDataOperationRegistry(); | |
25 | |
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 class Observer { | |
78 public: | |
79 // Called when a GData operation started, made some progress, or finished. | |
80 virtual void OnProgressUpdate(const ProgressStatusList& list) = 0; | |
81 // Called when GData authentication failed. | |
82 virtual void OnAuthenticationFailed() {} | |
83 protected: | |
84 virtual ~Observer() {} | |
85 }; | |
86 | |
87 // Base class for operations that this registry class can maintain. | |
88 // NotifyStart() passes the ownership of the Operation object to the registry. | |
89 // In particular, calling NotifyFinish() causes the registry to delete the | |
90 // Operation object itself. | |
91 class Operation { | |
92 public: | |
93 explicit Operation(GDataOperationRegistry* registry); | |
94 Operation(GDataOperationRegistry* registry, | |
95 OperationType type, | |
96 const FilePath& file_path); | |
97 virtual ~Operation(); | |
98 | |
99 // Cancels the ongoing operation. NotifyFinish() is called and the Operation | |
100 // object is deleted once the cancellation is done in DoCancel(). | |
101 void Cancel(); | |
102 | |
103 // Retrieves the current progress status of the operation. | |
104 const ProgressStatus& progress_status() const { return progress_status_; } | |
105 | |
106 protected: | |
107 // Notifies the registry about current status. | |
108 void NotifyStart(); | |
109 void NotifyProgress(int64 current, int64 total); | |
110 void NotifyFinish(OperationTransferState status); | |
111 // Notifies suspend/resume, used for chunked upload operations. | |
112 // The initial upload operation should issue "start" "progress"* "suspend". | |
113 // The subsequent operations will call "resume" "progress"* "suspend", | |
114 // and the last one will do "resume" "progress"* "finish". | |
115 // 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 | |
117 // that it removes the existing "suspend" operation. | |
118 void NotifySuspend(); | |
119 void NotifyResume(); | |
120 // Notifies that authentication has failed. | |
121 void NotifyAuthFailed(); | |
122 | |
123 private: | |
124 // Does the cancellation. | |
125 virtual void DoCancel() = 0; | |
126 | |
127 GDataOperationRegistry* const registry_; | |
128 ProgressStatus progress_status_; | |
129 }; | |
130 | |
131 // Cancels all in-flight operations. | |
132 void CancelAll(); | |
133 | |
134 // Cancels ongoing operation for a given virtual |file_path|. Returns true if | |
135 // the operation was found and canceled. | |
136 bool CancelForFilePath(const FilePath& file_path); | |
137 | |
138 // Obtains the list of currently active operations. | |
139 ProgressStatusList GetProgressStatusList(); | |
140 | |
141 // Sets an observer. The registry do NOT own observers; before destruction | |
142 // they need to be removed from the registry. | |
143 void AddObserver(Observer* observer); | |
144 void RemoveObserver(Observer* observer); | |
145 | |
146 // Disables the notification suppression for testing purpose. | |
147 void DisableNotificationFrequencyControlForTest(); | |
148 | |
149 private: | |
150 // Handlers for notifications from Operations. | |
151 friend class Operation; | |
152 // 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. | |
154 void OnOperationStart(Operation* operation, OperationID* id); | |
155 void OnOperationProgress(OperationID operation); | |
156 void OnOperationFinish(OperationID operation); | |
157 void OnOperationSuspend(OperationID operation); | |
158 void OnOperationResume(Operation* operation, ProgressStatus* new_status); | |
159 void OnOperationAuthFailed(); | |
160 | |
161 bool IsFileTransferOperation(const Operation* operation) const; | |
162 | |
163 // Controls the frequency of notifications, not to flood the listeners with | |
164 // too many events. | |
165 bool ShouldNotifyStatusNow(const ProgressStatusList& list); | |
166 // Sends notifications to the observers after checking that the frequency is | |
167 // not too high by ShouldNotifyStatusNow. | |
168 void NotifyStatusToObservers(); | |
169 | |
170 typedef IDMap<Operation, IDMapOwnPointer> OperationIDMap; | |
171 OperationIDMap in_flight_operations_; | |
172 ObserverList<Observer> observer_list_; | |
173 base::Time last_notification_; | |
174 bool do_notification_frequency_control_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(GDataOperationRegistry); | |
177 }; | |
178 | |
179 } // namespace gdata | |
180 | |
181 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATION_REGISTRY_H_ | |
OLD | NEW |