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 RequestGCMStatisticsCallback; | 45 RequestGCMStatisticsCallback; |
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 | 49 |
69 // Returns the GCM enabled state. | 50 virtual void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); |
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 | |
81 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 virtual void Shutdown(); | |
jianli
2014/04/18 01:49:44
GCMProfileService is inherited from both GCMServic
bartfab (slow)
2014/04/22 17:51:07
Done.
| |
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. If it has not been created | 102 // Get GCM client internal states and statistics. If it has not been created |
133 // then stats won't be modified. | 103 // then stats won't be modified. |
134 void RequestGCMStatistics(RequestGCMStatisticsCallback callback); | 104 void RequestGCMStatistics(RequestGCMStatisticsCallback callback); |
135 | 105 |
106 // IdentityProvider::Observer: | |
107 virtual void OnLogin() OVERRIDE; | |
108 virtual void OnLogout() OVERRIDE; | |
Munjal (Google)
2014/04/17 18:51:25
We should remove dependency on login / logout conc
bartfab (slow)
2014/04/22 17:51:07
As commented by Jian, he is working on a design th
| |
109 | |
110 protected: | |
111 virtual bool StartAutomatically() const = 0; | |
jianli
2014/04/18 01:49:44
CanStartAutomatically?
bartfab (slow)
2014/04/22 17:51:07
GCM always can start automatically. The question i
| |
112 | |
113 virtual base::FilePath GetStorePath() const = 0; | |
114 | |
115 virtual scoped_refptr<net::URLRequestContextGetter> | |
116 GetURLRequestContextGetter() const = 0; | |
117 | |
118 // Resets the GCMClient instance. | |
119 void ResetGCMClient(); | |
120 | |
121 scoped_ptr<IdentityProvider> identity_provider_; | |
122 | |
136 private: | 123 private: |
137 friend class GCMProfileServiceTestConsumer; | 124 friend class TestGCMServiceWrapper; |
138 friend class extensions::ExtensionGCMAppHandlerTest; | 125 friend class extensions::ExtensionGCMAppHandlerTest; |
139 | 126 |
140 class DelayedTaskController; | 127 class DelayedTaskController; |
141 class IOWorker; | 128 class IOWorker; |
142 | 129 |
143 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | 130 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; |
144 | 131 |
145 // Overridden from content::NotificationObserver: | 132 // Ensures that the GCMClient is loaded and the GCM check-in is done if the |
146 virtual void Observe(int type, | 133 // |identity_provider_| is able to supply an account ID. |
147 const content::NotificationSource& source, | |
148 const content::NotificationDetails& details) OVERRIDE; | |
149 | |
150 // Overridden from SigninManagerBase::Observer: | |
151 virtual void GoogleSigninSucceeded(const std::string& username, | |
152 const std::string& password) OVERRIDE; | |
153 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; | |
154 | |
155 // Ensures that the GCMClient is loaded and the GCM check-in is done when | |
156 // the profile was signed in. | |
157 void EnsureLoaded(); | 134 void EnsureLoaded(); |
158 | 135 |
159 // Remove cached data when GCM service is stopped. | 136 // Remove cached data when GCM service is stopped. |
160 void RemoveCachedData(); | 137 void RemoveCachedData(); |
161 | 138 |
162 // Checks out of GCM when the profile has been signed out. This will erase | 139 // Checks out of GCM and erases any cached and persisted data. |
163 // all the cached and persisted data. | |
164 void CheckOut(); | 140 void CheckOut(); |
165 | 141 |
166 // Resets the GCMClient instance. This is called when the profile is being | |
167 // destroyed. | |
168 void ResetGCMClient(); | |
169 | |
170 // Ensures that the app is ready for GCM functions and events. | 142 // Ensures that the app is ready for GCM functions and events. |
171 GCMClient::Result EnsureAppReady(const std::string& app_id); | 143 GCMClient::Result EnsureAppReady(const std::string& app_id); |
172 | 144 |
173 // Should be called when an app with |app_id| is trying to un/register. | 145 // Should be called when an app with |app_id| is trying to un/register. |
174 // Checks whether another un/registration is in progress. | 146 // Checks whether another un/registration is in progress. |
175 bool IsAsyncOperationPending(const std::string& app_id) const; | 147 bool IsAsyncOperationPending(const std::string& app_id) const; |
176 | 148 |
177 void DoRegister(const std::string& app_id, | 149 void DoRegister(const std::string& app_id, |
178 const std::vector<std::string>& sender_ids); | 150 const std::vector<std::string>& sender_ids); |
179 void DoUnregister(const std::string& app_id); | 151 void DoUnregister(const std::string& app_id); |
(...skipping 14 matching lines...) Expand all Loading... | |
194 void MessagesDeleted(const std::string& app_id); | 166 void MessagesDeleted(const std::string& app_id); |
195 void MessageSendError(const std::string& app_id, | 167 void MessageSendError(const std::string& app_id, |
196 const GCMClient::SendErrorDetails& send_error_details); | 168 const GCMClient::SendErrorDetails& send_error_details); |
197 void GCMClientReady(); | 169 void GCMClientReady(); |
198 | 170 |
199 // Returns the handler for the given app. | 171 // Returns the handler for the given app. |
200 GCMAppHandler* GetAppHandler(const std::string& app_id); | 172 GCMAppHandler* GetAppHandler(const std::string& app_id); |
201 | 173 |
202 void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 174 void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
203 | 175 |
204 // The profile which owns this object. | |
205 Profile* profile_; | |
206 | |
207 // Flag to indicate if GCMClient is ready. | 176 // Flag to indicate if GCMClient is ready. |
208 bool gcm_client_ready_; | 177 bool gcm_client_ready_; |
209 | 178 |
210 // The username of the signed-in profile. | 179 // The account ID that this service is responsible for. Empty when the service |
211 std::string username_; | 180 // is not running. |
212 | 181 std::string account_id_; |
213 content::NotificationRegistrar registrar_; | |
214 | 182 |
215 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 183 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
216 | 184 |
217 // For all the work occured in IO thread. | 185 // For all the work occurring on the IO thread. Must be destroyed on the IO |
218 scoped_refptr<IOWorker> io_worker_; | 186 // thread. |
187 scoped_ptr<IOWorker> io_worker_; | |
219 | 188 |
220 // App handler map (from app_id to handler pointer). | 189 // App handler map (from app_id to handler pointer). |
221 // The handler is not owned. | 190 // The handler is not owned. |
222 GCMAppHandlerMap app_handlers_; | 191 GCMAppHandlerMap app_handlers_; |
223 | 192 |
224 // The default handler when no app handler can be found in the map. | 193 // The default handler when no app handler can be found in the map. |
225 DefaultGCMAppHandler default_app_handler_; | 194 DefaultGCMAppHandler default_app_handler_; |
226 | 195 |
227 // Callback map (from app_id to callback) for Register. | 196 // Callback map (from app_id to callback) for Register. |
228 std::map<std::string, RegisterCallback> register_callbacks_; | 197 std::map<std::string, RegisterCallback> register_callbacks_; |
229 | 198 |
230 // Callback map (from app_id to callback) for Unregister. | 199 // Callback map (from app_id to callback) for Unregister. |
231 std::map<std::string, UnregisterCallback> unregister_callbacks_; | 200 std::map<std::string, UnregisterCallback> unregister_callbacks_; |
232 | 201 |
233 // Callback map (from <app_id, message_id> to callback) for Send. | 202 // Callback map (from <app_id, message_id> to callback) for Send. |
234 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | 203 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; |
235 | 204 |
236 // Callback for RequestGCMStatistics. | 205 // Callback for RequestGCMStatistics. |
237 RequestGCMStatisticsCallback request_gcm_statistics_callback_; | 206 RequestGCMStatisticsCallback request_gcm_statistics_callback_; |
238 | 207 |
239 // Used to pass a weak pointer to the IO worker. | 208 // Used to pass a weak pointer to the IO worker. |
240 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_; | 209 base::WeakPtrFactory<GCMService> weak_ptr_factory_; |
241 | 210 |
242 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); | 211 DISALLOW_COPY_AND_ASSIGN(GCMService); |
243 }; | 212 }; |
244 | 213 |
245 } // namespace gcm | 214 } // namespace gcm |
246 | 215 |
247 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 216 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_SERVICE_H_ |
OLD | NEW |