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 void Shutdown(); |
jianli
2014/04/17 00:33:19
The name Shutdown is same as the one defined in GC
bartfab (slow)
2014/04/17 14:20:01
Oops, GCMService::Shutdown() should have been virt
| |
88 virtual void Shutdown() OVERRIDE; | |
89 | 57 |
90 // Adds a handler for a given app. | 58 // Adds a handler for a given app. |
91 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); | 59 void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); |
92 | 60 |
93 // Remove the handler for a given app. | 61 // Remove the handler for a given app. |
94 virtual void RemoveAppHandler(const std::string& app_id); | 62 void RemoveAppHandler(const std::string& app_id); |
95 | 63 |
96 // Registers |sender_id| for an app. A registration ID will be returned by | 64 // Registers |sender_id| for an app. A registration ID will be returned by |
97 // the GCM server. | 65 // the GCM server. |
98 // |app_id|: application ID. | 66 // |app_id|: application ID. |
99 // |sender_ids|: list of IDs of the servers that are allowed to send the | 67 // |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 | 68 // messages to the application. These IDs are assigned by the |
101 // Google API Console. | 69 // Google API Console. |
102 // |callback|: to be called once the asynchronous operation is done. | 70 // |callback|: to be called once the asynchronous operation is done. |
103 virtual void Register(const std::string& app_id, | 71 virtual void Register(const std::string& app_id, |
104 const std::vector<std::string>& sender_ids, | 72 const std::vector<std::string>& sender_ids, |
(...skipping 11 matching lines...) Expand all Loading... | |
116 // |message|: message to be sent. | 84 // |message|: message to be sent. |
117 // |callback|: to be called once the asynchronous operation is done. | 85 // |callback|: to be called once the asynchronous operation is done. |
118 virtual void Send(const std::string& app_id, | 86 virtual void Send(const std::string& app_id, |
119 const std::string& receiver_id, | 87 const std::string& receiver_id, |
120 const GCMClient::OutgoingMessage& message, | 88 const GCMClient::OutgoingMessage& message, |
121 SendCallback callback); | 89 SendCallback callback); |
122 | 90 |
123 // For testing purpose. | 91 // For testing purpose. |
124 GCMClient* GetGCMClientForTesting() const; | 92 GCMClient* GetGCMClientForTesting() const; |
125 | 93 |
126 // Returns the user name if the profile is signed in. | 94 // Returns true if the service was started. |
127 std::string SignedInUserName() const; | 95 bool IsStarted() const; |
128 | 96 |
129 // Returns true if the gcm client is ready. | 97 // Returns true if the gcm client is ready. |
130 bool IsGCMClientReady() const; | 98 bool IsGCMClientReady() const; |
131 | 99 |
132 // Get GCM client internal states and statistics. If it has not been created | 100 // Get GCM client internal states and statistics. If it has not been created |
133 // then stats won't be modified. | 101 // then stats won't be modified. |
134 void RequestGCMStatistics(RequestGCMStatisticsCallback callback); | 102 void RequestGCMStatistics(RequestGCMStatisticsCallback callback); |
135 | 103 |
104 // IdentityProvider::Observer: | |
105 virtual void OnLogin() OVERRIDE; | |
106 virtual void OnLogout() OVERRIDE; | |
107 | |
108 protected: | |
109 virtual bool IsAlwaysEnabled() = 0; | |
jianli
2014/04/17 00:33:19
nit: add const modifier
Also, I think IsEnabled s
bartfab (slow)
2014/04/17 14:20:01
const: Done.
Renaming: I think "IsEnabled" is not
| |
110 | |
111 virtual base::FilePath GetStorePath() = 0; | |
jianli
2014/04/17 00:33:19
nit: add const modifier
bartfab (slow)
2014/04/17 14:20:01
Done.
| |
112 | |
113 virtual scoped_refptr<net::URLRequestContextGetter> | |
jianli
2014/04/17 00:33:19
ditto
bartfab (slow)
2014/04/17 14:20:01
Done.
| |
114 GetURLRequestContextGetter() = 0; | |
115 | |
116 // Resets the GCMClient instance. | |
117 void ResetGCMClient(); | |
118 | |
119 scoped_ptr<IdentityProvider> identity_provider_; | |
120 | |
136 private: | 121 private: |
137 friend class GCMProfileServiceTestConsumer; | 122 friend class TestGCMServiceWrapper; |
138 friend class extensions::ExtensionGCMAppHandlerTest; | 123 friend class extensions::ExtensionGCMAppHandlerTest; |
139 | 124 |
140 class DelayedTaskController; | 125 class DelayedTaskController; |
141 class IOWorker; | 126 class IOWorker; |
142 | 127 |
143 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | 128 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; |
144 | 129 |
145 // Overridden from content::NotificationObserver: | 130 // Ensures that the GCMClient is loaded and the GCM check-in is done if the |
146 virtual void Observe(int type, | 131 // |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(); | 132 void EnsureLoaded(); |
158 | 133 |
159 // Remove cached data when GCM service is stopped. | 134 // Remove cached data when GCM service is stopped. |
160 void RemoveCachedData(); | 135 void RemoveCachedData(); |
161 | 136 |
162 // Checks out of GCM when the profile has been signed out. This will erase | 137 // Checks out of GCM and erases any cached and persisted data. |
163 // all the cached and persisted data. | |
164 void CheckOut(); | 138 void CheckOut(); |
165 | 139 |
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. | 140 // Ensures that the app is ready for GCM functions and events. |
171 GCMClient::Result EnsureAppReady(const std::string& app_id); | 141 GCMClient::Result EnsureAppReady(const std::string& app_id); |
172 | 142 |
173 // Should be called when an app with |app_id| is trying to un/register. | 143 // Should be called when an app with |app_id| is trying to un/register. |
174 // Checks whether another un/registration is in progress. | 144 // Checks whether another un/registration is in progress. |
175 bool IsAsyncOperationPending(const std::string& app_id) const; | 145 bool IsAsyncOperationPending(const std::string& app_id) const; |
176 | 146 |
177 void DoRegister(const std::string& app_id, | 147 void DoRegister(const std::string& app_id, |
178 const std::vector<std::string>& sender_ids); | 148 const std::vector<std::string>& sender_ids); |
179 void DoUnregister(const std::string& app_id); | 149 void DoUnregister(const std::string& app_id); |
(...skipping 14 matching lines...) Expand all Loading... | |
194 void MessagesDeleted(const std::string& app_id); | 164 void MessagesDeleted(const std::string& app_id); |
195 void MessageSendError(const std::string& app_id, | 165 void MessageSendError(const std::string& app_id, |
196 const GCMClient::SendErrorDetails& send_error_details); | 166 const GCMClient::SendErrorDetails& send_error_details); |
197 void GCMClientReady(); | 167 void GCMClientReady(); |
198 | 168 |
199 // Returns the handler for the given app. | 169 // Returns the handler for the given app. |
200 GCMAppHandler* GetAppHandler(const std::string& app_id); | 170 GCMAppHandler* GetAppHandler(const std::string& app_id); |
201 | 171 |
202 void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 172 void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
203 | 173 |
204 // The profile which owns this object. | |
205 Profile* profile_; | |
206 | |
207 // Flag to indicate if GCMClient is ready. | 174 // Flag to indicate if GCMClient is ready. |
208 bool gcm_client_ready_; | 175 bool gcm_client_ready_; |
209 | 176 |
210 // The username of the signed-in profile. | 177 // The account ID that this service is responsible for. Empty when the service |
211 std::string username_; | 178 // is not running. |
212 | 179 std::string account_id_; |
213 content::NotificationRegistrar registrar_; | |
214 | 180 |
215 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 181 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
216 | 182 |
217 // For all the work occured in IO thread. | 183 // For all the work occurring on the IO thread. Must be destroyed on the IO |
218 scoped_refptr<IOWorker> io_worker_; | 184 // thread. |
185 scoped_ptr<IOWorker> io_worker_; | |
219 | 186 |
220 // App handler map (from app_id to handler pointer). | 187 // App handler map (from app_id to handler pointer). |
221 // The handler is not owned. | 188 // The handler is not owned. |
222 GCMAppHandlerMap app_handlers_; | 189 GCMAppHandlerMap app_handlers_; |
223 | 190 |
224 // The default handler when no app handler can be found in the map. | 191 // The default handler when no app handler can be found in the map. |
225 DefaultGCMAppHandler default_app_handler_; | 192 DefaultGCMAppHandler default_app_handler_; |
226 | 193 |
227 // Callback map (from app_id to callback) for Register. | 194 // Callback map (from app_id to callback) for Register. |
228 std::map<std::string, RegisterCallback> register_callbacks_; | 195 std::map<std::string, RegisterCallback> register_callbacks_; |
229 | 196 |
230 // Callback map (from app_id to callback) for Unregister. | 197 // Callback map (from app_id to callback) for Unregister. |
231 std::map<std::string, UnregisterCallback> unregister_callbacks_; | 198 std::map<std::string, UnregisterCallback> unregister_callbacks_; |
232 | 199 |
233 // Callback map (from <app_id, message_id> to callback) for Send. | 200 // Callback map (from <app_id, message_id> to callback) for Send. |
234 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | 201 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; |
235 | 202 |
236 // Callback for RequestGCMStatistics. | 203 // Callback for RequestGCMStatistics. |
237 RequestGCMStatisticsCallback request_gcm_statistics_callback_; | 204 RequestGCMStatisticsCallback request_gcm_statistics_callback_; |
238 | 205 |
239 // Used to pass a weak pointer to the IO worker. | 206 // Used to pass a weak pointer to the IO worker. |
240 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_; | 207 base::WeakPtrFactory<GCMService> weak_ptr_factory_; |
241 | 208 |
242 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); | 209 DISALLOW_COPY_AND_ASSIGN(GCMService); |
243 }; | 210 }; |
244 | 211 |
245 } // namespace gcm | 212 } // namespace gcm |
246 | 213 |
247 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 214 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_SERVICE_H_ |
OLD | NEW |