OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
7 | 7 |
8 #include <map> | |
9 #include <string> | 8 #include <string> |
10 | 9 |
11 #include "base/basictypes.h" | |
12 #include "base/callback.h" | |
13 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
14 #include "base/gtest_prod_util.h" | 11 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 12 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 13 #include "chrome/browser/services/gcm/gcm_service.h" |
17 #include "chrome/browser/services/gcm/default_gcm_app_handler.h" | |
18 #include "components/keyed_service/core/keyed_service.h" | 14 #include "components/keyed_service/core/keyed_service.h" |
19 #include "components/signin/core/browser/signin_manager_base.h" | |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 #include "google_apis/gcm/gcm_client.h" | |
23 | 15 |
24 class Profile; | 16 class Profile; |
25 | 17 |
26 namespace base { | |
27 class Value; | |
28 } | |
29 | |
30 namespace extensions { | |
31 class ExtensionGCMAppHandlerTest; | |
32 } | |
33 | |
34 namespace user_prefs { | 18 namespace user_prefs { |
35 class PrefRegistrySyncable; | 19 class PrefRegistrySyncable; |
36 } | 20 } |
37 | 21 |
38 namespace gcm { | 22 namespace gcm { |
39 | 23 |
40 class GCMAppHandler; | 24 // A specialization of GCMService that is tied to a Profile. |
41 class GCMClientFactory; | 25 class GCMProfileService : public GCMService, public KeyedService { |
42 class GCMProfileServiceTestConsumer; | |
43 | |
44 // Acts as a bridge between GCM API and GCMClient layer. It is profile based. | |
45 class GCMProfileService : public KeyedService, | |
46 public content::NotificationObserver, | |
47 public SigninManagerBase::Observer { | |
48 public: | 26 public: |
49 typedef base::Callback<void(const std::string& registration_id, | |
50 GCMClient::Result result)> RegisterCallback; | |
51 typedef base::Callback<void(const std::string& message_id, | |
52 GCMClient::Result result)> SendCallback; | |
53 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; | |
54 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> | |
55 GetGCMStatisticsCallback; | |
56 | |
57 // Any change made to this enum should have corresponding change in the | 27 // Any change made to this enum should have corresponding change in the |
58 // GetGCMEnabledStateString(...) function. | 28 // GetGCMEnabledStateString(...) function. |
59 enum GCMEnabledState { | 29 enum GCMEnabledState { |
60 // GCM is always enabled. GCMClient will always load and connect with GCM. | 30 // GCM is always enabled. GCMClient will always load and connect with GCM. |
61 ALWAYS_ENABLED, | 31 ALWAYS_ENABLED, |
62 // GCM is only enabled for apps. GCMClient will start to load and connect | 32 // GCM is only enabled for apps. GCMClient will start to load and connect |
63 // with GCM only when GCM API is used. | 33 // with GCM only when GCM API is used. |
64 ENABLED_FOR_APPS, | 34 ENABLED_FOR_APPS, |
65 // GCM is always disabled. GCMClient will never load and connect with GCM. | 35 // GCM is always disabled. GCMClient will never load and connect with GCM. |
66 ALWAYS_DISABLED | 36 ALWAYS_DISABLED |
67 }; | 37 }; |
68 | 38 |
69 // Returns the GCM enabled state. | 39 // Returns the GCM enabled state. |
70 static GCMEnabledState GetGCMEnabledState(Profile* profile); | 40 static GCMEnabledState GetGCMEnabledState(Profile* profile); |
71 | 41 |
72 // Returns text representation of a GCMEnabledState enum entry. | 42 // Returns text representation of a GCMEnabledState enum entry. |
73 static std::string GetGCMEnabledStateString(GCMEnabledState state); | 43 static std::string GetGCMEnabledStateString(GCMEnabledState state); |
74 | 44 |
75 // Register profile-specific prefs for GCM. | 45 // Register profile-specific prefs for GCM. |
76 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 46 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
77 | 47 |
78 explicit GCMProfileService(Profile* profile); | 48 explicit GCMProfileService(Profile* profile); |
79 virtual ~GCMProfileService(); | 49 virtual ~GCMProfileService(); |
80 | 50 |
81 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); | 51 // KeyedService: |
82 | |
83 void Start(); | |
84 | |
85 void Stop(); | |
86 | |
87 // KeyedService implementation. | |
88 virtual void Shutdown() OVERRIDE; | 52 virtual void Shutdown() OVERRIDE; |
89 | 53 |
90 // Adds a handler for a given app. | |
91 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); | |
92 | |
93 // Remove the handler for a given app. | |
94 virtual void RemoveAppHandler(const std::string& app_id); | |
95 | |
96 // Registers |sender_id| for an app. A registration ID will be returned by | |
97 // the GCM server. | |
98 // |app_id|: application ID. | |
99 // |sender_ids|: list of IDs of the servers that are allowed to send the | |
100 // messages to the application. These IDs are assigned by the | |
101 // Google API Console. | |
102 // |callback|: to be called once the asynchronous operation is done. | |
103 virtual void Register(const std::string& app_id, | |
104 const std::vector<std::string>& sender_ids, | |
105 RegisterCallback callback); | |
106 | |
107 // Unregisters an app from using GCM. | |
108 // |app_id|: application ID. | |
109 // |callback|: to be called once the asynchronous operation is done. | |
110 virtual void Unregister(const std::string& app_id, | |
111 UnregisterCallback callback); | |
112 | |
113 // Sends a message to a given receiver. | |
114 // |app_id|: application ID. | |
115 // |receiver_id|: registration ID of the receiver party. | |
116 // |message|: message to be sent. | |
117 // |callback|: to be called once the asynchronous operation is done. | |
118 virtual void Send(const std::string& app_id, | |
119 const std::string& receiver_id, | |
120 const GCMClient::OutgoingMessage& message, | |
121 SendCallback callback); | |
122 | |
123 // For testing purpose. | |
124 GCMClient* GetGCMClientForTesting() const; | |
125 | |
126 // Returns the user name if the profile is signed in. | 54 // Returns the user name if the profile is signed in. |
127 std::string SignedInUserName() const; | 55 std::string SignedInUserName() const; |
128 | 56 |
129 // Returns true if the gcm client is ready. | 57 protected: |
130 bool IsGCMClientReady() const; | 58 // Overridden from GCMService: |
131 | 59 virtual bool ShouldStartAutomatically() const OVERRIDE; |
132 // Get GCM client internal states and statistics. | 60 virtual base::FilePath GetStorePath() const OVERRIDE; |
133 // If clear_logs is true then activity logs will be cleared before the stats | 61 virtual scoped_refptr<net::URLRequestContextGetter> |
134 // are returned. | 62 GetURLRequestContextGetter() const OVERRIDE; |
135 void GetGCMStatistics(GetGCMStatisticsCallback callback, | |
136 bool clear_logs); | |
137 | |
138 // Enables/disables GCM activity recording, and then returns the stats. | |
139 void SetGCMRecording(GetGCMStatisticsCallback callback, bool recording); | |
140 | 63 |
141 private: | 64 private: |
142 friend class GCMProfileServiceTestConsumer; | |
143 friend class extensions::ExtensionGCMAppHandlerTest; | |
144 | |
145 class DelayedTaskController; | |
146 class IOWorker; | |
147 | |
148 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | |
149 | |
150 // Overridden from content::NotificationObserver: | |
151 virtual void Observe(int type, | |
152 const content::NotificationSource& source, | |
153 const content::NotificationDetails& details) OVERRIDE; | |
154 | |
155 // Overridden from SigninManagerBase::Observer: | |
156 virtual void GoogleSigninSucceeded(const std::string& username, | |
157 const std::string& password) OVERRIDE; | |
158 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; | |
159 | |
160 // Ensures that the GCMClient is loaded and the GCM check-in is done when | |
161 // the profile was signed in. | |
162 void EnsureLoaded(); | |
163 | |
164 // Remove cached data when GCM service is stopped. | |
165 void RemoveCachedData(); | |
166 | |
167 // Checks out of GCM when the profile has been signed out. This will erase | |
168 // all the cached and persisted data. | |
169 void CheckOut(); | |
170 | |
171 // Resets the GCMClient instance. This is called when the profile is being | |
172 // destroyed. | |
173 void ResetGCMClient(); | |
174 | |
175 // Ensures that the app is ready for GCM functions and events. | |
176 GCMClient::Result EnsureAppReady(const std::string& app_id); | |
177 | |
178 // Should be called when an app with |app_id| is trying to un/register. | |
179 // Checks whether another un/registration is in progress. | |
180 bool IsAsyncOperationPending(const std::string& app_id) const; | |
181 | |
182 void DoRegister(const std::string& app_id, | |
183 const std::vector<std::string>& sender_ids); | |
184 void DoUnregister(const std::string& app_id); | |
185 void DoSend(const std::string& app_id, | |
186 const std::string& receiver_id, | |
187 const GCMClient::OutgoingMessage& message); | |
188 | |
189 // Callbacks posted from IO thread to UI thread. | |
190 void RegisterFinished(const std::string& app_id, | |
191 const std::string& registration_id, | |
192 GCMClient::Result result); | |
193 void UnregisterFinished(const std::string& app_id, GCMClient::Result result); | |
194 void SendFinished(const std::string& app_id, | |
195 const std::string& message_id, | |
196 GCMClient::Result result); | |
197 void MessageReceived(const std::string& app_id, | |
198 GCMClient::IncomingMessage message); | |
199 void MessagesDeleted(const std::string& app_id); | |
200 void MessageSendError(const std::string& app_id, | |
201 const GCMClient::SendErrorDetails& send_error_details); | |
202 void GCMClientReady(); | |
203 | |
204 // Returns the handler for the given app. | |
205 GCMAppHandler* GetAppHandler(const std::string& app_id); | |
206 | |
207 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | |
208 | |
209 // The profile which owns this object. | 65 // The profile which owns this object. |
210 Profile* profile_; | 66 Profile* profile_; |
211 | 67 |
212 // Flag to indicate if GCMClient is ready. | |
213 bool gcm_client_ready_; | |
214 | |
215 // The username of the signed-in profile. | |
216 std::string username_; | |
217 | |
218 content::NotificationRegistrar registrar_; | |
219 | |
220 scoped_ptr<DelayedTaskController> delayed_task_controller_; | |
221 | |
222 // For all the work occured in IO thread. | |
223 scoped_refptr<IOWorker> io_worker_; | |
224 | |
225 // App handler map (from app_id to handler pointer). | |
226 // The handler is not owned. | |
227 GCMAppHandlerMap app_handlers_; | |
228 | |
229 // The default handler when no app handler can be found in the map. | |
230 DefaultGCMAppHandler default_app_handler_; | |
231 | |
232 // Callback map (from app_id to callback) for Register. | |
233 std::map<std::string, RegisterCallback> register_callbacks_; | |
234 | |
235 // Callback map (from app_id to callback) for Unregister. | |
236 std::map<std::string, UnregisterCallback> unregister_callbacks_; | |
237 | |
238 // Callback map (from <app_id, message_id> to callback) for Send. | |
239 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | |
240 | |
241 // Callback for GetGCMStatistics. | |
242 GetGCMStatisticsCallback request_gcm_statistics_callback_; | |
243 | |
244 // Used to pass a weak pointer to the IO worker. | |
245 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_; | |
246 | |
247 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); | 68 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); |
248 }; | 69 }; |
249 | 70 |
250 } // namespace gcm | 71 } // namespace gcm |
251 | 72 |
252 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 73 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
OLD | NEW |