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_
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "components/gcm_driver/default_gcm_app_handler.h"
17 #include "components/gcm_driver/gcm_client.h"
22 class GCMConnectionObserver
;
23 struct AccountMapping
;
25 // Bridge between GCM users in Chrome and the platform-specific implementation.
28 typedef std::map
<std::string
, GCMAppHandler
*> GCMAppHandlerMap
;
29 typedef base::Callback
<void(const std::string
& registration_id
,
30 GCMClient::Result result
)> RegisterCallback
;
31 typedef base::Callback
<void(const std::string
& message_id
,
32 GCMClient::Result result
)> SendCallback
;
33 typedef base::Callback
<void(GCMClient::Result result
)> UnregisterCallback
;
34 typedef base::Callback
<void(const GCMClient::GCMStatistics
& stats
)>
35 GetGCMStatisticsCallback
;
40 // Registers |sender_ids| for an app. A registration ID will be returned by
41 // the GCM server. On Android, only a single sender ID is supported, but
42 // instead multiple simultaneous registrations are allowed.
43 // |app_id|: application ID.
44 // |sender_ids|: list of IDs of the servers that are allowed to send the
45 // messages to the application. These IDs are assigned by the
46 // Google API Console.
47 // |callback|: to be called once the asynchronous operation is done.
48 void Register(const std::string
& app_id
,
49 const std::vector
<std::string
>& sender_ids
,
50 const RegisterCallback
& callback
);
52 // Unregisters all sender_ids for an app. Only works on non-Android.
53 // |app_id|: application ID.
54 // |callback|: to be called once the asynchronous operation is done.
55 void Unregister(const std::string
& app_id
,
56 const UnregisterCallback
& callback
);
58 // Unregisters an (app_id, sender_id) pair from using GCM. Only works on
60 // TODO(jianli): Switch to using GCM's unsubscribe API.
61 // |app_id|: application ID.
62 // |sender_id|: the sender ID that was passed when registering.
63 // |callback|: to be called once the asynchronous operation is done.
64 void UnregisterWithSenderId(const std::string
& app_id
,
65 const std::string
& sender_id
,
66 const UnregisterCallback
& callback
);
68 // Sends a message to a given receiver.
69 // |app_id|: application ID.
70 // |receiver_id|: registration ID of the receiver party.
71 // |message|: message to be sent.
72 // |callback|: to be called once the asynchronous operation is done.
73 void Send(const std::string
& app_id
,
74 const std::string
& receiver_id
,
75 const GCMClient::OutgoingMessage
& message
,
76 const SendCallback
& callback
);
78 const GCMAppHandlerMap
& app_handlers() const { return app_handlers_
; }
80 // This method must be called before destroying the GCMDriver. Once it has
81 // been called, no other GCMDriver methods may be used.
82 virtual void Shutdown();
84 // Called when the user signs in to or out of a GAIA account.
85 virtual void OnSignedIn() = 0;
86 virtual void OnSignedOut() = 0;
88 // Adds a handler for a given app.
89 virtual void AddAppHandler(const std::string
& app_id
, GCMAppHandler
* handler
);
91 // Remove the handler for a given app.
92 virtual void RemoveAppHandler(const std::string
& app_id
);
94 // Returns the handler for the given app.
95 GCMAppHandler
* GetAppHandler(const std::string
& app_id
);
97 // Adds a connection state observer.
98 virtual void AddConnectionObserver(GCMConnectionObserver
* observer
) = 0;
100 // Removes a connection state observer.
101 virtual void RemoveConnectionObserver(GCMConnectionObserver
* observer
) = 0;
103 // Enables/disables GCM service.
104 virtual void Enable() = 0;
105 virtual void Disable() = 0;
107 // For testing purpose. Always NULL on Android.
108 virtual GCMClient
* GetGCMClientForTesting() const = 0;
110 // Returns true if the service was started.
111 virtual bool IsStarted() const = 0;
113 // Returns true if the gcm client has an open and active connection.
114 virtual bool IsConnected() const = 0;
116 // Get GCM client internal states and statistics.
117 // If clear_logs is true then activity logs will be cleared before the stats
119 virtual void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
120 bool clear_logs
) = 0;
122 // Enables/disables GCM activity recording, and then returns the stats.
123 virtual void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
126 // sets a list of signed in accounts with OAuth2 access tokens, when GCMDriver
127 // works in context of a signed in entity (e.g. browser profile where user is
128 // signed into sync).
129 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
131 virtual void SetAccountTokens(
132 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
) = 0;
134 // Updates the |account_mapping| information in persistent store.
135 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
137 // Removes the account mapping information reated to |account_id| from
139 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
141 // Getter and setter of last token fetch time.
142 virtual base::Time
GetLastTokenFetchTime() = 0;
143 virtual void SetLastTokenFetchTime(const base::Time
& time
) = 0;
145 // Sets whether or not GCM should try to wake the system from suspend in order
146 // to send a heartbeat message.
147 virtual void WakeFromSuspendForHeartbeat(bool wake
) = 0;
150 // Ensures that the GCM service starts (if necessary conditions are met).
151 virtual GCMClient::Result
EnsureStarted(GCMClient::StartMode start_mode
) = 0;
153 // Platform-specific implementation of Register.
154 virtual void RegisterImpl(const std::string
& app_id
,
155 const std::vector
<std::string
>& sender_ids
) = 0;
157 // Platform-specific implementation of Unregister.
158 virtual void UnregisterImpl(const std::string
& app_id
) = 0;
160 // Platform-specific implementation of UnregisterWithSenderId.
161 virtual void UnregisterWithSenderIdImpl(const std::string
& app_id
,
162 const std::string
& sender_id
);
164 // Platform-specific implementation of Send.
165 virtual void SendImpl(const std::string
& app_id
,
166 const std::string
& receiver_id
,
167 const GCMClient::OutgoingMessage
& message
) = 0;
169 // Runs the Register callback.
170 void RegisterFinished(const std::string
& app_id
,
171 const std::string
& registration_id
,
172 GCMClient::Result result
);
174 // Runs the Unregister callback.
175 void UnregisterFinished(const std::string
& app_id
,
176 GCMClient::Result result
);
178 // Runs the Send callback.
179 void SendFinished(const std::string
& app_id
,
180 const std::string
& message_id
,
181 GCMClient::Result result
);
183 bool HasRegisterCallback(const std::string
& app_id
);
185 void ClearCallbacks();
188 // Common code shared by Unregister and UnregisterWithSenderId.
189 void UnregisterInternal(const std::string
& app_id
,
190 const std::string
* sender_id
,
191 const UnregisterCallback
& callback
);
193 // Called after unregistration completes in order to trigger the pending
195 void RegisterAfterUnregister(
196 const std::string
& app_id
,
197 const std::vector
<std::string
>& normalized_sender_ids
,
198 const UnregisterCallback
& unregister_callback
,
199 GCMClient::Result result
);
201 // Callback map (from app_id to callback) for Register.
202 std::map
<std::string
, RegisterCallback
> register_callbacks_
;
204 // Callback map (from app_id to callback) for Unregister.
205 std::map
<std::string
, UnregisterCallback
> unregister_callbacks_
;
207 // Callback map (from <app_id, message_id> to callback) for Send.
208 std::map
<std::pair
<std::string
, std::string
>, SendCallback
> send_callbacks_
;
210 // App handler map (from app_id to handler pointer).
211 // The handler is not owned.
212 GCMAppHandlerMap app_handlers_
;
214 // The default handler when no app handler can be found in the map.
215 DefaultGCMAppHandler default_app_handler_
;
217 base::WeakPtrFactory
<GCMDriver
> weak_ptr_factory_
;
219 DISALLOW_COPY_AND_ASSIGN(GCMDriver
);
224 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_