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_id| for an app. A registration ID will be returned by
42 // |app_id|: application ID.
43 // |sender_ids|: list of IDs of the servers that are allowed to send the
44 // messages to the application. These IDs are assigned by the
45 // Google API Console.
46 // |callback|: to be called once the asynchronous operation is done.
47 void Register(const std::string
& app_id
,
48 const std::vector
<std::string
>& sender_ids
,
49 const RegisterCallback
& callback
);
51 // Unregisters an app from using GCM.
52 // |app_id|: application ID.
53 // |callback|: to be called once the asynchronous operation is done.
54 void Unregister(const std::string
& app_id
,
55 const UnregisterCallback
& callback
);
57 // Sends a message to a given receiver.
58 // |app_id|: application ID.
59 // |receiver_id|: registration ID of the receiver party.
60 // |message|: message to be sent.
61 // |callback|: to be called once the asynchronous operation is done.
62 void Send(const std::string
& app_id
,
63 const std::string
& receiver_id
,
64 const GCMClient::OutgoingMessage
& message
,
65 const SendCallback
& callback
);
67 const GCMAppHandlerMap
& app_handlers() const { return app_handlers_
; }
69 // This method must be called before destroying the GCMDriver. Once it has
70 // been called, no other GCMDriver methods may be used.
71 virtual void Shutdown();
73 // Called when the user signs in to or out of a GAIA account.
74 virtual void OnSignedIn() = 0;
75 virtual void OnSignedOut() = 0;
77 // Adds a handler for a given app.
78 virtual void AddAppHandler(const std::string
& app_id
, GCMAppHandler
* handler
);
80 // Remove the handler for a given app.
81 virtual void RemoveAppHandler(const std::string
& app_id
);
83 // Returns the handler for the given app.
84 GCMAppHandler
* GetAppHandler(const std::string
& app_id
);
86 // Adds a connection state observer.
87 virtual void AddConnectionObserver(GCMConnectionObserver
* observer
) = 0;
89 // Removes a connection state observer.
90 virtual void RemoveConnectionObserver(GCMConnectionObserver
* observer
) = 0;
92 // Enables/disables GCM service.
93 virtual void Enable() = 0;
94 virtual void Disable() = 0;
96 // For testing purpose. Always NULL on Android.
97 virtual GCMClient
* GetGCMClientForTesting() const = 0;
99 // Returns true if the service was started.
100 virtual bool IsStarted() const = 0;
102 // Returns true if the gcm client has an open and active connection.
103 virtual bool IsConnected() const = 0;
105 // Get GCM client internal states and statistics.
106 // If clear_logs is true then activity logs will be cleared before the stats
108 virtual void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
109 bool clear_logs
) = 0;
111 // Enables/disables GCM activity recording, and then returns the stats.
112 virtual void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
115 // sets a list of signed in accounts with OAuth2 access tokens, when GCMDriver
116 // works in context of a signed in entity (e.g. browser profile where user is
117 // signed into sync).
118 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
120 virtual void SetAccountTokens(
121 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
) = 0;
123 // Updates the |account_mapping| information in persistent store.
124 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
126 // Removes the account mapping information reated to |account_id| from
128 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
130 // Getter and setter of last token fetch time.
131 virtual base::Time
GetLastTokenFetchTime() = 0;
132 virtual void SetLastTokenFetchTime(const base::Time
& time
) = 0;
134 // Sets whether or not GCM should try to wake the system from suspend in order
135 // to send a heartbeat message.
136 virtual void WakeFromSuspendForHeartbeat(bool wake
) = 0;
139 // Ensures that the GCM service starts (if necessary conditions are met).
140 virtual GCMClient::Result
EnsureStarted(GCMClient::StartMode start_mode
) = 0;
142 // Platform-specific implementation of Register.
143 virtual void RegisterImpl(const std::string
& app_id
,
144 const std::vector
<std::string
>& sender_ids
) = 0;
146 // Platform-specific implementation of Unregister.
147 virtual void UnregisterImpl(const std::string
& app_id
) = 0;
149 // Platform-specific implementation of Send.
150 virtual void SendImpl(const std::string
& app_id
,
151 const std::string
& receiver_id
,
152 const GCMClient::OutgoingMessage
& message
) = 0;
154 // Runs the Register callback.
155 void RegisterFinished(const std::string
& app_id
,
156 const std::string
& registration_id
,
157 GCMClient::Result result
);
159 // Runs the Unregister callback.
160 void UnregisterFinished(const std::string
& app_id
,
161 GCMClient::Result result
);
163 // Runs the Send callback.
164 void SendFinished(const std::string
& app_id
,
165 const std::string
& message_id
,
166 GCMClient::Result result
);
168 bool HasRegisterCallback(const std::string
& app_id
);
170 void ClearCallbacks();
173 // Called after unregistration completes in order to trigger the pending
175 void RegisterAfterUnregister(
176 const std::string
& app_id
,
177 const std::vector
<std::string
>& normalized_sender_ids
,
178 const UnregisterCallback
& unregister_callback
,
179 GCMClient::Result result
);
181 // Callback map (from app_id to callback) for Register.
182 std::map
<std::string
, RegisterCallback
> register_callbacks_
;
184 // Callback map (from app_id to callback) for Unregister.
185 std::map
<std::string
, UnregisterCallback
> unregister_callbacks_
;
187 // Callback map (from <app_id, message_id> to callback) for Send.
188 std::map
<std::pair
<std::string
, std::string
>, SendCallback
> send_callbacks_
;
190 // App handler map (from app_id to handler pointer).
191 // The handler is not owned.
192 GCMAppHandlerMap app_handlers_
;
194 // The default handler when no app handler can be found in the map.
195 DefaultGCMAppHandler default_app_handler_
;
197 base::WeakPtrFactory
<GCMDriver
> weak_ptr_factory_
;
199 DISALLOW_COPY_AND_ASSIGN(GCMDriver
);
204 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_