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