Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver.h
blob572c02d8b793d035901b76021e924d4dbd04f9de
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/gcm_driver/common/gcm_messages.h"
18 #include "components/gcm_driver/crypto/gcm_encryption_provider.h"
19 #include "components/gcm_driver/default_gcm_app_handler.h"
20 #include "components/gcm_driver/gcm_client.h"
22 namespace base {
23 class FilePath;
24 class SequencedTaskRunner;
27 namespace gcm {
29 class GCMAppHandler;
30 class GCMConnectionObserver;
31 struct AccountMapping;
33 // Provides the InstanceID support via GCMDriver.
34 class InstanceIDHandler {
35 public:
36 typedef base::Callback<void(const std::string& token,
37 GCMClient::Result result)> GetTokenCallback;
38 typedef base::Callback<void(GCMClient::Result result)> DeleteTokenCallback;
39 typedef base::Callback<void(const std::string& instance_id,
40 const std::string& extra_data)>
41 GetInstanceIDDataCallback;
43 InstanceIDHandler();
44 virtual ~InstanceIDHandler();
46 // Delete all tokens assoicated with |app_id|.
47 void DeleteAllTokensForApp(const std::string& app_id,
48 const DeleteTokenCallback& callback);
50 // Token service.
51 virtual void GetToken(const std::string& app_id,
52 const std::string& authorized_entity,
53 const std::string& scope,
54 const std::map<std::string, std::string>& options,
55 const GetTokenCallback& callback) = 0;
56 virtual void DeleteToken(const std::string& app_id,
57 const std::string& authorized_entity,
58 const std::string& scope,
59 const DeleteTokenCallback& callback) = 0;
61 // Persistence support.
62 virtual void AddInstanceIDData(const std::string& app_id,
63 const std::string& instance_id,
64 const std::string& extra_data) = 0;
65 virtual void RemoveInstanceIDData(const std::string& app_id) = 0;
66 virtual void GetInstanceIDData(
67 const std::string& app_id,
68 const GetInstanceIDDataCallback& callback) = 0;
70 private:
71 DISALLOW_COPY_AND_ASSIGN(InstanceIDHandler);
74 // Bridge between GCM users in Chrome and the platform-specific implementation.
75 class GCMDriver {
76 public:
77 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
78 typedef base::Callback<void(const std::string& registration_id,
79 GCMClient::Result result)> RegisterCallback;
80 typedef base::Callback<void(const std::string& message_id,
81 GCMClient::Result result)> SendCallback;
82 typedef base::Callback<void(const std::string&)> GetPublicKeyCallback;
83 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
84 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
85 GetGCMStatisticsCallback;
87 GCMDriver(
88 const base::FilePath& store_path,
89 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
90 virtual ~GCMDriver();
92 // Registers |sender_ids| for an app. A registration ID will be returned by
93 // the GCM server. On Android, only a single sender ID is supported, but
94 // instead multiple simultaneous registrations are allowed.
95 // |app_id|: application ID.
96 // |sender_ids|: list of IDs of the servers that are allowed to send the
97 // messages to the application. These IDs are assigned by the
98 // Google API Console.
99 // |callback|: to be called once the asynchronous operation is done.
100 void Register(const std::string& app_id,
101 const std::vector<std::string>& sender_ids,
102 const RegisterCallback& callback);
104 // Unregisters all sender_ids for an app. Only works on non-Android.
105 // |app_id|: application ID.
106 // |callback|: to be called once the asynchronous operation is done.
107 void Unregister(const std::string& app_id,
108 const UnregisterCallback& callback);
110 // Unregisters an (app_id, sender_id) pair from using GCM. Only works on
111 // Android.
112 // TODO(jianli): Switch to using GCM's unsubscribe API.
113 // |app_id|: application ID.
114 // |sender_id|: the sender ID that was passed when registering.
115 // |callback|: to be called once the asynchronous operation is done.
116 void UnregisterWithSenderId(const std::string& app_id,
117 const std::string& sender_id,
118 const UnregisterCallback& callback);
120 // Sends a message to a given receiver.
121 // |app_id|: application ID.
122 // |receiver_id|: registration ID of the receiver party.
123 // |message|: message to be sent.
124 // |callback|: to be called once the asynchronous operation is done.
125 void Send(const std::string& app_id,
126 const std::string& receiver_id,
127 const OutgoingMessage& message,
128 const SendCallback& callback);
130 // Get the public encryption key associated with |app_id|. If no keys have
131 // been associated with |app_id| yet, they will be created. The |callback|
132 // will be invoked when it is available.
133 void GetPublicKey(const std::string& app_id,
134 const GetPublicKeyCallback& callback);
136 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
138 // This method must be called before destroying the GCMDriver. Once it has
139 // been called, no other GCMDriver methods may be used.
140 virtual void Shutdown();
142 // Called when the user signs in to or out of a GAIA account.
143 virtual void OnSignedIn() = 0;
144 virtual void OnSignedOut() = 0;
146 // Adds a handler for a given app.
147 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
149 // Remove the handler for a given app.
150 virtual void RemoveAppHandler(const std::string& app_id);
152 // Returns the handler for the given app.
153 GCMAppHandler* GetAppHandler(const std::string& app_id);
155 // Adds a connection state observer.
156 virtual void AddConnectionObserver(GCMConnectionObserver* observer) = 0;
158 // Removes a connection state observer.
159 virtual void RemoveConnectionObserver(GCMConnectionObserver* observer) = 0;
161 // Enables/disables GCM service.
162 virtual void Enable() = 0;
163 virtual void Disable() = 0;
165 // For testing purpose. Always NULL on Android.
166 virtual GCMClient* GetGCMClientForTesting() const = 0;
168 // Returns true if the service was started.
169 virtual bool IsStarted() const = 0;
171 // Returns true if the gcm client has an open and active connection.
172 virtual bool IsConnected() const = 0;
174 // Get GCM client internal states and statistics.
175 // If clear_logs is true then activity logs will be cleared before the stats
176 // are returned.
177 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
178 bool clear_logs) = 0;
180 // Enables/disables GCM activity recording, and then returns the stats.
181 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
182 bool recording) = 0;
184 // sets a list of signed in accounts with OAuth2 access tokens, when GCMDriver
185 // works in context of a signed in entity (e.g. browser profile where user is
186 // signed into sync).
187 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
188 // tokens.
189 virtual void SetAccountTokens(
190 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) = 0;
192 // Updates the |account_mapping| information in persistent store.
193 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
195 // Removes the account mapping information reated to |account_id| from
196 // persistent store.
197 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
199 // Getter and setter of last token fetch time.
200 virtual base::Time GetLastTokenFetchTime() = 0;
201 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
203 // Sets whether or not GCM should try to wake the system from suspend in order
204 // to send a heartbeat message.
205 virtual void WakeFromSuspendForHeartbeat(bool wake) = 0;
207 // Supports InstanceID handling.
208 virtual InstanceIDHandler* GetInstanceIDHandler() = 0;
210 // Adds or removes a custom client requested heartbeat interval. If multiple
211 // components set that setting, the lowest setting will be used. If the
212 // setting is outside of GetMax/MinClientHeartbeatIntervalMs() it will be
213 // ignored. If a new setting is less than the currently used, the connection
214 // will be reset with the new heartbeat. Client that no longer require
215 // aggressive heartbeats, should remove their requested interval. Heartbeats
216 // set this way survive connection/Chrome restart.
218 // GCM Driver can decide to postpone the action until Client is properly
219 // initialized, hence this setting can be called at any time.
221 // Server can overwrite the setting to a different value.
223 // |scope| is used to identify the component that requests a custom interval
224 // to be set, and allows that component to later revoke the setting.
225 // |interval_ms| should be between 2 minues and 15 minues (28 minues on
226 // cellular networks). For details check
227 // GetMin/MaxClientHeartbeatItnervalMs() in HeartbeatManager.
228 virtual void AddHeartbeatInterval(const std::string& scope,
229 int interval_ms) = 0;
230 virtual void RemoveHeartbeatInterval(const std::string& scope) = 0;
232 protected:
233 // Ensures that the GCM service starts (if necessary conditions are met).
234 virtual GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) = 0;
236 // Platform-specific implementation of Register.
237 virtual void RegisterImpl(const std::string& app_id,
238 const std::vector<std::string>& sender_ids) = 0;
240 // Platform-specific implementation of Unregister.
241 virtual void UnregisterImpl(const std::string& app_id) = 0;
243 // Platform-specific implementation of UnregisterWithSenderId.
244 virtual void UnregisterWithSenderIdImpl(const std::string& app_id,
245 const std::string& sender_id);
247 // Platform-specific implementation of Send.
248 virtual void SendImpl(const std::string& app_id,
249 const std::string& receiver_id,
250 const OutgoingMessage& message) = 0;
252 // Runs the Register callback.
253 void RegisterFinished(const std::string& app_id,
254 const std::string& registration_id,
255 GCMClient::Result result);
257 // Runs the Unregister callback.
258 void UnregisterFinished(const std::string& app_id,
259 GCMClient::Result result);
261 // Runs the Send callback.
262 void SendFinished(const std::string& app_id,
263 const std::string& message_id,
264 GCMClient::Result result);
266 bool HasRegisterCallback(const std::string& app_id);
268 void ClearCallbacks();
270 private:
271 // Common code shared by Unregister and UnregisterWithSenderId.
272 void UnregisterInternal(const std::string& app_id,
273 const std::string* sender_id,
274 const UnregisterCallback& callback);
276 // Called after unregistration completes in order to trigger the pending
277 // registration.
278 void RegisterAfterUnregister(
279 const std::string& app_id,
280 const std::vector<std::string>& normalized_sender_ids,
281 const UnregisterCallback& unregister_callback,
282 GCMClient::Result result);
284 // Callback map (from app_id to callback) for Register.
285 std::map<std::string, RegisterCallback> register_callbacks_;
287 // Callback map (from app_id to callback) for Unregister.
288 std::map<std::string, UnregisterCallback> unregister_callbacks_;
290 // Callback map (from <app_id, message_id> to callback) for Send.
291 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
293 // The encryption provider, used for key management and decryption of
294 // encrypted, incoming messages.
295 GCMEncryptionProvider encryption_provider_;
297 // App handler map (from app_id to handler pointer).
298 // The handler is not owned.
299 GCMAppHandlerMap app_handlers_;
301 // The default handler when no app handler can be found in the map.
302 DefaultGCMAppHandler default_app_handler_;
304 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_;
306 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
309 } // namespace gcm
311 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_