OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 COMPONENTS_GCM_GCM_DRIVER_H_ | 5 #ifndef COMPONENTS_GCM_GCM_DRIVER_H_ |
6 #define COMPONENTS_GCM_GCM_DRIVER_H_ | 6 #define COMPONENTS_GCM_GCM_DRIVER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | |
13 #include "base/callback.h" | 12 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 13 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "components/gcm/default_gcm_app_handler.h" | |
19 #include "google_apis/gaia/identity_provider.h" | |
20 #include "google_apis/gcm/gcm_client.h" | 15 #include "google_apis/gcm/gcm_client.h" |
21 | 16 |
22 namespace base { | 17 namespace base { |
23 class FilePath; | |
24 class MessageLoopProxy; | 18 class MessageLoopProxy; |
25 } | 19 } |
26 | 20 |
27 namespace net { | |
28 class URLRequestContextGetter; | |
29 } | |
30 | |
31 namespace gcm { | 21 namespace gcm { |
32 | 22 |
33 class GCMAppHandler; | 23 class GCMAppHandler; |
34 class GCMClientFactory; | |
35 | 24 |
36 // A bridge between the GCM users in Chrome and the GCMClient layer. | 25 // A bridge between the GCM users in Chrome and the GCMClient layer. |
37 class GCMDriver : public IdentityProvider::Observer { | 26 class GCMDriver { |
38 public: | 27 public: |
39 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | 28 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; |
40 typedef base::Callback<void(const std::string& registration_id, | 29 typedef base::Callback<void(const std::string& registration_id, |
41 GCMClient::Result result)> RegisterCallback; | 30 GCMClient::Result result)> RegisterCallback; |
42 typedef base::Callback<void(const std::string& message_id, | 31 typedef base::Callback<void(const std::string& message_id, |
43 GCMClient::Result result)> SendCallback; | 32 GCMClient::Result result)> SendCallback; |
44 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; | 33 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; |
45 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> | 34 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> |
46 GetGCMStatisticsCallback; | 35 GetGCMStatisticsCallback; |
47 | 36 |
48 GCMDriver(const checkin_proto::ChromeBuildProto& chrome_build_proto, | 37 GCMDriver(const scoped_refptr<base::MessageLoopProxy>& ui_thread); |
jianli
2014/05/08 00:26:48
I don't think you need to pass a ui thread to the
johnme
2014/05/08 16:08:02
Done.
| |
49 scoped_ptr<GCMClientFactory> gcm_client_factory, | |
50 scoped_ptr<IdentityProvider> identity_provider, | |
51 const base::FilePath& store_path, | |
52 const scoped_refptr<net::URLRequestContextGetter>& request_context, | |
53 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
54 const scoped_refptr<base::MessageLoopProxy>& ui_thread, | |
55 const scoped_refptr<base::MessageLoopProxy>& io_thread); | |
56 virtual ~GCMDriver(); | 38 virtual ~GCMDriver(); |
57 | 39 |
58 // Enables/disables GCM service. When GCM is disabled, the GCM service | 40 // Enables/disables GCM service. When GCM is disabled, the GCM service |
59 // will always be stopped. Otherwise, the GCM service will be started | 41 // will always be stopped. Otherwise, the GCM service will be started |
60 // on demand, i.e., some app is asking for the service. | 42 // on demand, i.e., some app is asking for the service. |
61 void Enable(); | 43 virtual void Enable() = 0; |
62 void Disable(); | 44 virtual void Disable() = 0; |
63 | 45 |
64 // Shuts down the GCM service completely. This will tell all the apps | 46 // Shuts down the GCM service completely. This will tell all the apps |
65 // to shut them down. After that, no GCM service could be consumed. | 47 // to shut them down. After that, no GCM service could be consumed. |
66 virtual void Shutdown(); | 48 virtual void Shutdown(); |
67 | 49 |
68 // Adds a handler for a given app. It is OK to call this again to update the | 50 // Adds a handler for a given app. It is OK to call this again to update the |
69 // handler. | 51 // handler. |
70 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); | 52 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); |
71 | 53 |
72 // Removes the handler for a given app. | 54 // Removes the handler for a given app. |
73 virtual void RemoveAppHandler(const std::string& app_id); | 55 virtual void RemoveAppHandler(const std::string& app_id); |
74 | 56 |
75 // Registers |sender_id| for an app. A registration ID will be returned by | 57 // Registers |sender_id| for an app. A registration ID will be returned by |
76 // the GCM server. | 58 // the GCM server. |
77 // |app_id|: application ID. | 59 // |app_id|: application ID. |
78 // |sender_ids|: list of IDs of the servers that are allowed to send the | 60 // |sender_ids|: list of IDs of the servers that are allowed to send the |
79 // messages to the application. These IDs are assigned by the | 61 // messages to the application. These IDs are assigned by the |
80 // Google API Console. | 62 // Google API Console. |
81 // |callback|: to be called once the asynchronous operation is done. | 63 // |callback|: to be called once the asynchronous operation is done. |
82 virtual void Register(const std::string& app_id, | 64 virtual void Register(const std::string& app_id, |
83 const std::vector<std::string>& sender_ids, | 65 const std::vector<std::string>& sender_ids, |
84 const RegisterCallback& callback); | 66 const RegisterCallback& callback) = 0; |
85 | 67 |
86 // Unregisters an app from using GCM. | 68 // Unregisters an app from using GCM. |
87 // |app_id|: application ID. | 69 // |app_id|: application ID. |
88 // |callback|: to be called once the asynchronous operation is done. | 70 // |callback|: to be called once the asynchronous operation is done. |
89 virtual void Unregister(const std::string& app_id, | 71 virtual void Unregister(const std::string& app_id, |
90 const UnregisterCallback& callback); | 72 const UnregisterCallback& callback) = 0; |
91 | 73 |
92 // Sends a message to a given receiver. | 74 // Sends a message to a given receiver. |
93 // |app_id|: application ID. | 75 // |app_id|: application ID. |
94 // |receiver_id|: registration ID of the receiver party. | 76 // |receiver_id|: registration ID of the receiver party. |
95 // |message|: message to be sent. | 77 // |message|: message to be sent. |
96 // |callback|: to be called once the asynchronous operation is done. | 78 // |callback|: to be called once the asynchronous operation is done. |
97 virtual void Send(const std::string& app_id, | 79 virtual void Send(const std::string& app_id, |
98 const std::string& receiver_id, | 80 const std::string& receiver_id, |
99 const GCMClient::OutgoingMessage& message, | 81 const GCMClient::OutgoingMessage& message, |
100 const SendCallback& callback); | 82 const SendCallback& callback) = 0; |
101 | 83 |
102 // For testing purpose. | 84 // For testing purpose. Always NULL on Android. |
103 GCMClient* GetGCMClientForTesting() const; | 85 virtual GCMClient* GetGCMClientForTesting() const = 0; |
104 | 86 |
105 // Returns true if the service was started. | 87 // Returns true if the service was started. |
106 bool IsStarted() const; | 88 virtual bool IsStarted() const = 0; |
107 | 89 |
108 // Returns true if the gcm client is ready. | 90 // Returns true if the gcm client is ready. |
109 bool IsGCMClientReady() const; | 91 virtual bool IsGCMClientReady() const = 0; |
110 | 92 |
111 std::string SignedInUserName() const; | 93 virtual std::string SignedInUserName() const = 0; |
112 | 94 |
113 // Get GCM client internal states and statistics. | 95 // Get GCM client internal states and statistics. |
114 // If clear_logs is true then activity logs will be cleared before the stats | 96 // If clear_logs is true then activity logs will be cleared before the stats |
115 // are returned. | 97 // are returned. |
116 void GetGCMStatistics(GetGCMStatisticsCallback callback, bool clear_logs); | 98 virtual void GetGCMStatistics(GetGCMStatisticsCallback callback, |
99 bool clear_logs) = 0; | |
117 | 100 |
118 // Enables/disables GCM activity recording, and then returns the stats. | 101 // Enables/disables GCM activity recording, and then returns the stats. |
119 void SetGCMRecording(GetGCMStatisticsCallback callback, bool recording); | 102 virtual void SetGCMRecording(GetGCMStatisticsCallback callback, |
120 | 103 bool recording) = 0; |
121 // IdentityProvider::Observer: | |
122 virtual void OnActiveAccountLogin() OVERRIDE; | |
123 virtual void OnActiveAccountLogout() OVERRIDE; | |
124 | 104 |
125 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } | 105 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } |
126 | 106 |
127 protected: | 107 protected: |
128 // Used for constructing fake GCMDriver for testing purpose. | 108 // Used for constructing fake GCMDriver for testing purpose. |
129 GCMDriver(); | 109 GCMDriver(); |
130 | 110 |
111 scoped_refptr<base::MessageLoopProxy> ui_thread_; | |
112 | |
131 private: | 113 private: |
132 class DelayedTaskController; | |
133 class IOWorker; | |
134 | |
135 // Ensures that the GCM service starts when all of the following conditions | |
136 // satisfy: | |
137 // 1) GCM is enabled. | |
138 // 2) The identity provider is able to supply an account ID. | |
139 // 3) An app requests to consume the service by register the app handler. | |
140 GCMClient::Result EnsureStarted(); | |
141 | |
142 // Stops the GCM service. It can be restarted by calling EnsureStarted again. | |
143 void Stop(); | |
144 | |
145 // Remove cached data when GCM service is stopped. | |
146 void RemoveCachedData(); | |
147 | |
148 // Checks out of GCM and erases any cached and persisted data. | |
149 void CheckOut(); | |
150 | |
151 // Should be called when an app with |app_id| is trying to un/register. | |
152 // Checks whether another un/registration is in progress. | |
153 bool IsAsyncOperationPending(const std::string& app_id) const; | |
154 | |
155 void DoRegister(const std::string& app_id, | |
156 const std::vector<std::string>& sender_ids); | |
157 void DoUnregister(const std::string& app_id); | |
158 void DoSend(const std::string& app_id, | |
159 const std::string& receiver_id, | |
160 const GCMClient::OutgoingMessage& message); | |
161 | |
162 // Callbacks posted from IO thread to UI thread. | |
163 void RegisterFinished(const std::string& app_id, | |
164 const std::string& registration_id, | |
165 GCMClient::Result result); | |
166 void UnregisterFinished(const std::string& app_id, GCMClient::Result result); | |
167 void SendFinished(const std::string& app_id, | |
168 const std::string& message_id, | |
169 GCMClient::Result result); | |
170 void MessageReceived(const std::string& app_id, | |
171 GCMClient::IncomingMessage message); | |
172 void MessagesDeleted(const std::string& app_id); | |
173 void MessageSendError(const std::string& app_id, | |
174 const GCMClient::SendErrorDetails& send_error_details); | |
175 void GCMClientReady(); | |
176 | |
177 // Returns the handler for the given app. | |
178 GCMAppHandler* GetAppHandler(const std::string& app_id); | |
179 | |
180 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | |
181 | |
182 scoped_ptr<IdentityProvider> identity_provider_; | |
183 scoped_refptr<base::MessageLoopProxy> ui_thread_; | |
184 scoped_refptr<base::MessageLoopProxy> io_thread_; | |
185 | |
186 // Flag to indicate if GCM is enabled. | |
187 bool gcm_enabled_; | |
188 | |
189 // Flag to indicate if GCMClient is ready. | |
190 bool gcm_client_ready_; | |
191 | |
192 // The account ID that this service is responsible for. Empty when the service | |
193 // is not running. | |
194 std::string account_id_; | |
195 | |
196 scoped_ptr<DelayedTaskController> delayed_task_controller_; | |
197 | |
198 // For all the work occurring on the IO thread. Must be destroyed on the IO | |
199 // thread. | |
200 scoped_ptr<IOWorker> io_worker_; | |
201 | |
202 // App handler map (from app_id to handler pointer). | 114 // App handler map (from app_id to handler pointer). |
203 // The handler is not owned. | 115 // The handler is not owned. |
204 GCMAppHandlerMap app_handlers_; | 116 GCMAppHandlerMap app_handlers_; |
205 | 117 |
206 // The default handler when no app handler can be found in the map. | |
207 DefaultGCMAppHandler default_app_handler_; | |
208 | |
209 // Callback map (from app_id to callback) for Register. | |
210 std::map<std::string, RegisterCallback> register_callbacks_; | |
211 | |
212 // Callback map (from app_id to callback) for Unregister. | |
213 std::map<std::string, UnregisterCallback> unregister_callbacks_; | |
214 | |
215 // Callback map (from <app_id, message_id> to callback) for Send. | |
216 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | |
217 | |
218 // Callback for GetGCMStatistics. | |
219 GetGCMStatisticsCallback request_gcm_statistics_callback_; | |
220 | |
221 // Used to pass a weak pointer to the IO worker. | |
222 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; | |
223 | |
224 DISALLOW_COPY_AND_ASSIGN(GCMDriver); | 118 DISALLOW_COPY_AND_ASSIGN(GCMDriver); |
225 }; | 119 }; |
226 | 120 |
227 } // namespace gcm | 121 } // namespace gcm |
228 | 122 |
229 #endif // COMPONENTS_GCM_GCM_DRIVER_H_ | 123 #endif // COMPONENTS_GCM_GCM_DRIVER_H_ |
OLD | NEW |