Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: chrome/browser/services/gcm/gcm_profile_service.h

Issue 225403021: Extract Profile-independent GCMService from GCMProfileService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Provide GCMService with the list of all accounts. It does use it after all. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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" 10 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h" 12 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/ref_counted.h" 13 #include "chrome/browser/services/gcm/gcm_service.h"
16 #include "base/memory/weak_ptr.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" 15 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h" 16 #include "content/public/browser/notification_registrar.h"
22 #include "google_apis/gcm/gcm_client.h"
23 17
24 class Profile; 18 class Profile;
25 19
26 namespace base {
27 class Value;
28 }
29
30 namespace extensions {
31 class ExtensionGCMAppHandlerTest;
32 }
33
34 namespace user_prefs { 20 namespace user_prefs {
35 class PrefRegistrySyncable; 21 class PrefRegistrySyncable;
36 } 22 }
37 23
38 namespace gcm { 24 namespace gcm {
39 25
40 class GCMAppHandler;
41 class GCMClientFactory; 26 class GCMClientFactory;
42 class GCMProfileServiceTestConsumer;
43 27
44 // Acts as a bridge between GCM API and GCMClient layer. It is profile based. 28 // A specialization of GCMService that is a KeyedService.
jianli 2014/04/17 00:33:19 A specialization of GCMService that is tied with a
bartfab (slow) 2014/04/17 14:20:01 Done.
45 class GCMProfileService : public KeyedService, 29 class GCMProfileService : public GCMService,
46 public content::NotificationObserver, 30 public KeyedService,
47 public SigninManagerBase::Observer { 31 public content::NotificationObserver {
48 public: 32 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 RequestGCMStatisticsCallback;
56
57 // Any change made to this enum should have corresponding change in the 33 // Any change made to this enum should have corresponding change in the
58 // GetGCMEnabledStateString(...) function. 34 // GetGCMEnabledStateString(...) function.
59 enum GCMEnabledState { 35 enum GCMEnabledState {
60 // GCM is always enabled. GCMClient will always load and connect with GCM. 36 // GCM is always enabled. GCMClient will always load and connect with GCM.
61 ALWAYS_ENABLED, 37 ALWAYS_ENABLED,
62 // GCM is only enabled for apps. GCMClient will start to load and connect 38 // GCM is only enabled for apps. GCMClient will start to load and connect
63 // with GCM only when GCM API is used. 39 // with GCM only when GCM API is used.
64 ENABLED_FOR_APPS, 40 ENABLED_FOR_APPS,
65 // GCM is always disabled. GCMClient will never load and connect with GCM. 41 // GCM is always disabled. GCMClient will never load and connect with GCM.
66 ALWAYS_DISABLED 42 ALWAYS_DISABLED
67 }; 43 };
68 44
69 // Returns the GCM enabled state. 45 // Returns the GCM enabled state.
70 static GCMEnabledState GetGCMEnabledState(Profile* profile); 46 static GCMEnabledState GetGCMEnabledState(Profile* profile);
71 47
72 // Returns text representation of a GCMEnabledState enum entry. 48 // Returns text representation of a GCMEnabledState enum entry.
73 static std::string GetGCMEnabledStateString(GCMEnabledState state); 49 static std::string GetGCMEnabledStateString(GCMEnabledState state);
74 50
75 // Register profile-specific prefs for GCM. 51 // Register profile-specific prefs for GCM.
76 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 52 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
77 53
78 explicit GCMProfileService(Profile* profile); 54 explicit GCMProfileService(Profile* profile);
79 virtual ~GCMProfileService(); 55 virtual ~GCMProfileService();
80 56
81 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); 57 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory);
82 58
83 void Start();
84
85 void Stop();
86
87 // KeyedService implementation.
88 virtual void Shutdown() OVERRIDE;
89
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. 59 // Returns the user name if the profile is signed in.
127 std::string SignedInUserName() const; 60 std::string SignedInUserName() const;
128 61
129 // Returns true if the gcm client is ready. 62 // Overridden from KeyedService:
130 bool IsGCMClientReady() const; 63 virtual void Shutdown() OVERRIDE;
131
132 // Get GCM client internal states and statistics. If it has not been created
133 // then stats won't be modified.
134 void RequestGCMStatistics(RequestGCMStatisticsCallback callback);
135
136 private:
137 friend class GCMProfileServiceTestConsumer;
138 friend class extensions::ExtensionGCMAppHandlerTest;
139
140 class DelayedTaskController;
141 class IOWorker;
142
143 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
144 64
145 // Overridden from content::NotificationObserver: 65 // Overridden from content::NotificationObserver:
146 virtual void Observe(int type, 66 virtual void Observe(int type,
147 const content::NotificationSource& source, 67 const content::NotificationSource& source,
148 const content::NotificationDetails& details) OVERRIDE; 68 const content::NotificationDetails& details) OVERRIDE;
149 69
150 // Overridden from SigninManagerBase::Observer: 70 protected:
151 virtual void GoogleSigninSucceeded(const std::string& username, 71 // Overridden from GCMService:
152 const std::string& password) OVERRIDE; 72 virtual bool IsAlwaysEnabled() OVERRIDE;
153 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; 73 virtual base::FilePath GetStorePath() OVERRIDE;
74 virtual scoped_refptr<net::URLRequestContextGetter>
75 GetURLRequestContextGetter() OVERRIDE;
154 76
155 // Ensures that the GCMClient is loaded and the GCM check-in is done when 77 private:
156 // the profile was signed in.
157 void EnsureLoaded();
158
159 // Remove cached data when GCM service is stopped.
160 void RemoveCachedData();
161
162 // Checks out of GCM when the profile has been signed out. This will erase
163 // all the cached and persisted data.
164 void CheckOut();
165
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.
171 GCMClient::Result EnsureAppReady(const std::string& app_id);
172
173 // Should be called when an app with |app_id| is trying to un/register.
174 // Checks whether another un/registration is in progress.
175 bool IsAsyncOperationPending(const std::string& app_id) const;
176
177 void DoRegister(const std::string& app_id,
178 const std::vector<std::string>& sender_ids);
179 void DoUnregister(const std::string& app_id);
180 void DoSend(const std::string& app_id,
181 const std::string& receiver_id,
182 const GCMClient::OutgoingMessage& message);
183
184 // Callbacks posted from IO thread to UI thread.
185 void RegisterFinished(const std::string& app_id,
186 const std::string& registration_id,
187 GCMClient::Result result);
188 void UnregisterFinished(const std::string& app_id, GCMClient::Result result);
189 void SendFinished(const std::string& app_id,
190 const std::string& message_id,
191 GCMClient::Result result);
192 void MessageReceived(const std::string& app_id,
193 GCMClient::IncomingMessage message);
194 void MessagesDeleted(const std::string& app_id);
195 void MessageSendError(const std::string& app_id,
196 const GCMClient::SendErrorDetails& send_error_details);
197 void GCMClientReady();
198
199 // Returns the handler for the given app.
200 GCMAppHandler* GetAppHandler(const std::string& app_id);
201
202 void RequestGCMStatisticsFinished(GCMClient::GCMStatistics stats);
203
204 // The profile which owns this object. 78 // The profile which owns this object.
205 Profile* profile_; 79 Profile* profile_;
206 80
207 // Flag to indicate if GCMClient is ready.
208 bool gcm_client_ready_;
209
210 // The username of the signed-in profile.
211 std::string username_;
212
213 content::NotificationRegistrar registrar_; 81 content::NotificationRegistrar registrar_;
214 82
215 scoped_ptr<DelayedTaskController> delayed_task_controller_;
216
217 // For all the work occured in IO thread.
218 scoped_refptr<IOWorker> io_worker_;
219
220 // App handler map (from app_id to handler pointer).
221 // The handler is not owned.
222 GCMAppHandlerMap app_handlers_;
223
224 // The default handler when no app handler can be found in the map.
225 DefaultGCMAppHandler default_app_handler_;
226
227 // Callback map (from app_id to callback) for Register.
228 std::map<std::string, RegisterCallback> register_callbacks_;
229
230 // Callback map (from app_id to callback) for Unregister.
231 std::map<std::string, UnregisterCallback> unregister_callbacks_;
232
233 // Callback map (from <app_id, message_id> to callback) for Send.
234 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
235
236 // Callback for RequestGCMStatistics.
237 RequestGCMStatisticsCallback request_gcm_statistics_callback_;
238
239 // Used to pass a weak pointer to the IO worker.
240 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_;
241
242 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); 83 DISALLOW_COPY_AND_ASSIGN(GCMProfileService);
243 }; 84 };
244 85
245 } // namespace gcm 86 } // namespace gcm
246 87
247 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ 88 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698