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

Side by Side Diff: components/gcm_driver/gcm_driver.h

Issue 278493002: Split GCMDriver into platform-specific implementations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed jianli's review comments Created 6 years, 6 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 2014 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 COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ 5 #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ 6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h"
13 #include "base/callback.h" 12 #include "base/callback.h"
14 #include "base/compiler_specific.h" 13 #include "base/macros.h"
15 #include "base/memory/ref_counted.h" 14 #include "base/threading/thread_checker.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/gcm_driver/default_gcm_app_handler.h" 15 #include "components/gcm_driver/default_gcm_app_handler.h"
19 #include "google_apis/gaia/identity_provider.h"
20 #include "google_apis/gcm/gcm_client.h" 16 #include "google_apis/gcm/gcm_client.h"
21 17
22 namespace base {
23 class FilePath;
24 class SequencedTaskRunner;
25 }
26
27 namespace extensions {
28 class ExtensionGCMAppHandlerTest;
29 }
30
31 namespace net {
32 class URLRequestContextGetter;
33 }
34
35 namespace gcm { 18 namespace gcm {
36 19
37 class GCMAppHandler; 20 class GCMAppHandler;
38 class GCMClientFactory;
39 21
40 // A bridge between the GCM users in Chrome and the GCMClient layer. 22 // Bridge between GCM users in Chrome and the platform-specific implementation.
41 class GCMDriver : public IdentityProvider::Observer { 23 class GCMDriver {
42 public: 24 public:
43 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; 25 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
44 typedef base::Callback<void(const std::string& registration_id, 26 typedef base::Callback<void(const std::string& registration_id,
45 GCMClient::Result result)> RegisterCallback; 27 GCMClient::Result result)> RegisterCallback;
46 typedef base::Callback<void(const std::string& message_id, 28 typedef base::Callback<void(const std::string& message_id,
47 GCMClient::Result result)> SendCallback; 29 GCMClient::Result result)> SendCallback;
48 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; 30 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
49 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> 31 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
50 GetGCMStatisticsCallback; 32 GetGCMStatisticsCallback;
51 33
52 GCMDriver( 34 GCMDriver();
53 scoped_ptr<GCMClientFactory> gcm_client_factory,
54 scoped_ptr<IdentityProvider> identity_provider,
55 const GCMClient::ChromeBuildInfo& chrome_build_info,
56 const base::FilePath& store_path,
57 const scoped_refptr<net::URLRequestContextGetter>& request_context,
58 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
59 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
60 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
61 virtual ~GCMDriver(); 35 virtual ~GCMDriver();
62 36
63 // Enables/disables GCM service.
64 void Enable();
65 void Disable();
66
67 // This method must be called before destroying the GCMDriver. Once it has 37 // This method must be called before destroying the GCMDriver. Once it has
68 // been called, no other GCMDriver methods may be used. 38 // been called, no other GCMDriver methods may be used.
69 virtual void Shutdown(); 39 virtual void Shutdown();
70 40
71 // Adds a handler for a given app. 41 // Adds a handler for a given app.
72 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); 42 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
73 43
74 // Remove the handler for a given app. 44 // Remove the handler for a given app.
75 virtual void RemoveAppHandler(const std::string& app_id); 45 virtual void RemoveAppHandler(const std::string& app_id);
76 46
47 // Enables/disables GCM service.
48 virtual void Enable() = 0;
49 virtual void Disable() = 0;
50
77 // Registers |sender_id| for an app. A registration ID will be returned by 51 // Registers |sender_id| for an app. A registration ID will be returned by
78 // the GCM server. 52 // the GCM server.
79 // |app_id|: application ID. 53 // |app_id|: application ID.
80 // |sender_ids|: list of IDs of the servers that are allowed to send the 54 // |sender_ids|: list of IDs of the servers that are allowed to send the
81 // messages to the application. These IDs are assigned by the 55 // messages to the application. These IDs are assigned by the
82 // Google API Console. 56 // Google API Console.
83 // |callback|: to be called once the asynchronous operation is done. 57 // |callback|: to be called once the asynchronous operation is done.
84 virtual void Register(const std::string& app_id, 58 virtual void Register(const std::string& app_id,
85 const std::vector<std::string>& sender_ids, 59 const std::vector<std::string>& sender_ids,
86 const RegisterCallback& callback); 60 const RegisterCallback& callback) = 0;
87 61
88 // Unregisters an app from using GCM. 62 // Unregisters an app from using GCM.
89 // |app_id|: application ID. 63 // |app_id|: application ID.
90 // |callback|: to be called once the asynchronous operation is done. 64 // |callback|: to be called once the asynchronous operation is done.
91 virtual void Unregister(const std::string& app_id, 65 virtual void Unregister(const std::string& app_id,
92 const UnregisterCallback& callback); 66 const UnregisterCallback& callback) = 0;
93 67
94 // Sends a message to a given receiver. 68 // Sends a message to a given receiver.
95 // |app_id|: application ID. 69 // |app_id|: application ID.
96 // |receiver_id|: registration ID of the receiver party. 70 // |receiver_id|: registration ID of the receiver party.
97 // |message|: message to be sent. 71 // |message|: message to be sent.
98 // |callback|: to be called once the asynchronous operation is done. 72 // |callback|: to be called once the asynchronous operation is done.
99 virtual void Send(const std::string& app_id, 73 virtual void Send(const std::string& app_id,
100 const std::string& receiver_id, 74 const std::string& receiver_id,
101 const GCMClient::OutgoingMessage& message, 75 const GCMClient::OutgoingMessage& message,
102 const SendCallback& callback); 76 const SendCallback& callback) = 0;
103 77
104 // For testing purpose. 78 // For testing purpose. Always NULL on Android.
105 GCMClient* GetGCMClientForTesting() const; 79 virtual GCMClient* GetGCMClientForTesting() const = 0;
106 80
107 // Returns true if the service was started. 81 // Returns true if the service was started.
108 bool IsStarted() const; 82 virtual bool IsStarted() const = 0;
109 83
110 // Returns true if the gcm client is ready. 84 // Returns true if the gcm client is ready.
111 bool IsGCMClientReady() const; 85 virtual bool IsGCMClientReady() const = 0;
112 86
113 // Get GCM client internal states and statistics. 87 // Get GCM client internal states and statistics.
114 // If clear_logs is true then activity logs will be cleared before the stats 88 // If clear_logs is true then activity logs will be cleared before the stats
115 // are returned. 89 // are returned.
116 void GetGCMStatistics(const GetGCMStatisticsCallback& callback, 90 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
117 bool clear_logs); 91 bool clear_logs) = 0;
118 92
119 // Enables/disables GCM activity recording, and then returns the stats. 93 // Enables/disables GCM activity recording, and then returns the stats.
120 void SetGCMRecording(const GetGCMStatisticsCallback& callback, 94 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
121 bool recording); 95 bool recording) = 0;
122 96
123 // Returns the user name if the profile is signed in. Empty string otherwise. 97 // Returns the user name if the profile is signed in. Empty string otherwise.
124 std::string SignedInUserName() const; 98 virtual std::string SignedInUserName() const = 0;
125
126 // IdentityProvider::Observer:
127 virtual void OnActiveAccountLogin() OVERRIDE;
128 virtual void OnActiveAccountLogout() OVERRIDE;
129 99
130 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } 100 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
131 101
132 protected: 102 protected:
133 // Used for constructing fake GCMDriver for testing purpose.
134 GCMDriver();
135
136 private:
137 class DelayedTaskController;
138 class IOWorker;
139
140 // Ensures that the GCM service starts when all of the following conditions
141 // satisfy:
142 // 1) GCM is enabled.
143 // 2) The identity provider is able to supply an account ID.
144 GCMClient::Result EnsureStarted();
145
146 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
147 void Stop();
148
149 // Remove cached data when GCM service is stopped.
150 void RemoveCachedData();
151
152 // Checks out of GCM and erases any cached and persisted data.
153 void CheckOut();
154
155 // Should be called when an app with |app_id| is trying to un/register.
156 // Checks whether another un/registration is in progress.
157 bool IsAsyncOperationPending(const std::string& app_id) const;
158
159 void DoRegister(const std::string& app_id,
160 const std::vector<std::string>& sender_ids);
161 void DoUnregister(const std::string& app_id);
162 void DoSend(const std::string& app_id,
163 const std::string& receiver_id,
164 const GCMClient::OutgoingMessage& message);
165
166 // Callbacks posted from IO thread to UI thread.
167 void RegisterFinished(const std::string& app_id,
168 const std::string& registration_id,
169 GCMClient::Result result);
170 void UnregisterFinished(const std::string& app_id, GCMClient::Result result);
171 void SendFinished(const std::string& app_id,
172 const std::string& message_id,
173 GCMClient::Result result);
174 void MessageReceived(const std::string& app_id,
175 GCMClient::IncomingMessage message);
176 void MessagesDeleted(const std::string& app_id);
177 void MessageSendError(const std::string& app_id,
178 const GCMClient::SendErrorDetails& send_error_details);
179 void GCMClientReady();
180
181 // Returns the handler for the given app. 103 // Returns the handler for the given app.
182 GCMAppHandler* GetAppHandler(const std::string& app_id); 104 GCMAppHandler* GetAppHandler(const std::string& app_id);
183 105
184 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); 106 // Ensure GCMDriver methods are all called from the same thread.
107 base::ThreadChecker thread_checker_;
185 108
186 // Flag to indicate if GCM is enabled. 109 private:
187 bool gcm_enabled_;
188
189 // Flag to indicate if GCMClient is ready.
190 bool gcm_client_ready_;
191
192 // The account ID that this service is responsible for. Empty when the service
193 // is not running.
194 std::string account_id_;
195
196 scoped_ptr<IdentityProvider> identity_provider_;
197 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
198 scoped_refptr<base::SequencedTaskRunner> io_thread_;
199
200 scoped_ptr<DelayedTaskController> delayed_task_controller_;
201
202 // For all the work occurring on the IO thread. Must be destroyed on the IO
203 // thread.
204 scoped_ptr<IOWorker> io_worker_;
205
206 // App handler map (from app_id to handler pointer). 110 // App handler map (from app_id to handler pointer).
207 // The handler is not owned. 111 // The handler is not owned.
208 GCMAppHandlerMap app_handlers_; 112 GCMAppHandlerMap app_handlers_;
209 113
210 // The default handler when no app handler can be found in the map. 114 // The default handler when no app handler can be found in the map.
211 DefaultGCMAppHandler default_app_handler_; 115 DefaultGCMAppHandler default_app_handler_;
212 116
213 // Callback map (from app_id to callback) for Register.
214 std::map<std::string, RegisterCallback> register_callbacks_;
215
216 // Callback map (from app_id to callback) for Unregister.
217 std::map<std::string, UnregisterCallback> unregister_callbacks_;
218
219 // Callback map (from <app_id, message_id> to callback) for Send.
220 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
221
222 // Callback for GetGCMStatistics.
223 GetGCMStatisticsCallback request_gcm_statistics_callback_;
224
225 // Used to pass a weak pointer to the IO worker.
226 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_;
227
228 DISALLOW_COPY_AND_ASSIGN(GCMDriver); 117 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
229 }; 118 };
230 119
231 } // namespace gcm 120 } // namespace gcm
232 121
233 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ 122 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698