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_REQUEST_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_REQUEST_REGISTRY_H_ |
6 #define CHROME_BROWSER_GOOGLE_APIS_REQUEST_REGISTRY_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_REQUEST_REGISTRY_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/files/file_path.h" | |
10 #include "base/id_map.h" | 9 #include "base/id_map.h" |
11 #include "chrome/browser/google_apis/gdata_errorcode.h" | |
12 | 10 |
13 namespace google_apis { | 11 namespace google_apis { |
14 | 12 |
15 // Unique ID to identify each request. | 13 // Unique ID to identify each request. |
16 typedef int32 RequestID; | 14 typedef int32 RequestID; |
17 | 15 |
18 // Enumeration type for indicating the state of the transfer. | |
19 enum RequestTransferState { | |
20 REQUEST_NOT_STARTED, | |
21 REQUEST_STARTED, | |
22 REQUEST_IN_PROGRESS, | |
23 REQUEST_COMPLETED, | |
24 REQUEST_FAILED, | |
25 }; | |
26 | |
27 // Returns string representations of the request state. | |
28 std::string RequestTransferStateToString(RequestTransferState state); | |
29 | |
30 // Structure that packs progress information of each request. | |
31 struct RequestProgressStatus { | |
32 RequestProgressStatus(); | |
33 | |
34 RequestID request_id; | |
35 | |
36 // Current state of the transfer; | |
37 RequestTransferState transfer_state; | |
38 }; | |
39 | |
40 // This class tracks all the in-flight Google API requests and manage | 16 // This class tracks all the in-flight Google API requests and manage |
41 // their lifetime. | 17 // their lifetime. |
42 class RequestRegistry { | 18 class RequestRegistry { |
43 public: | 19 public: |
44 RequestRegistry(); | 20 RequestRegistry(); |
45 ~RequestRegistry(); | 21 ~RequestRegistry(); |
46 | 22 |
47 // Base class for requests that this registry class can maintain. | 23 // Base class for requests that this registry class can maintain. |
48 // NotifyStart() passes the ownership of the Request object to the registry. | 24 // NotifyStart() passes the ownership of the Request object to the registry. |
49 // In particular, calling NotifyFinish() causes the registry to delete the | 25 // In particular, calling NotifyFinish() causes the registry to delete the |
50 // Request object itself. | 26 // Request object itself. |
51 class Request { | 27 class Request { |
52 public: | 28 public: |
53 explicit Request(RequestRegistry* registry); | 29 explicit Request(RequestRegistry* registry); |
54 virtual ~Request(); | 30 virtual ~Request(); |
55 | 31 |
56 // Cancels the ongoing request. NotifyFinish() is called and the Request | |
57 // object is deleted once the cancellation is done in DoCancel(). | |
58 void Cancel(); | |
59 | |
60 // Retrieves the current progress status of the request. | |
61 const RequestProgressStatus& progress_status() const { | |
62 return progress_status_; | |
63 } | |
64 | |
65 protected: | 32 protected: |
66 // Notifies the registry about current status. | 33 // Notifies the registry about current status. |
67 void NotifyStart(); | 34 void NotifyStart(); |
68 void NotifyFinish(RequestTransferState status); | 35 void NotifyFinish(); |
69 | 36 |
70 private: | 37 private: |
71 // Does the cancellation. | |
72 virtual void DoCancel() = 0; | |
73 | |
74 RequestRegistry* const registry_; | 38 RequestRegistry* const registry_; |
75 RequestProgressStatus progress_status_; | 39 RequestID id_; |
76 }; | 40 }; |
77 | 41 |
78 // Cancels the specified request. | |
79 void CancelRequest(Request* request); | |
80 | |
81 private: | 42 private: |
82 // Handlers for notifications from Requests. | 43 // Handlers for notifications from Requests. |
83 friend class Request; | 44 friend class Request; |
84 // Notifies that an request has started. This method passes the ownership of | 45 // Notifies that an request has started. This method passes the ownership of |
85 // the request to the registry. A fresh request ID is returned to *id. | 46 // the request to the registry. A fresh request ID is returned to *id. |
86 void OnRequestStart(Request* request, RequestID* id); | 47 void OnRequestStart(Request* request, RequestID* id); |
87 void OnRequestFinish(RequestID request_id); | 48 void OnRequestFinish(RequestID request_id); |
88 | 49 |
89 typedef IDMap<Request, IDMapOwnPointer> RequestIDMap; | 50 typedef IDMap<Request, IDMapOwnPointer> RequestIDMap; |
90 RequestIDMap in_flight_requests_; | 51 RequestIDMap in_flight_requests_; |
91 | 52 |
92 DISALLOW_COPY_AND_ASSIGN(RequestRegistry); | 53 DISALLOW_COPY_AND_ASSIGN(RequestRegistry); |
93 }; | 54 }; |
94 | 55 |
95 } // namespace google_apis | 56 } // namespace google_apis |
96 | 57 |
97 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_REGISTRY_H_ | 58 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_REGISTRY_H_ |
OLD | NEW |