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