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/threading/thread_checker.h"
15 #include "components/gcm_driver/default_gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
21 class GCMConnectionObserver
;
22 struct AccountMapping
;
24 // Bridge between GCM users in Chrome and the platform-specific implementation.
27 typedef std::map
<std::string
, GCMAppHandler
*> GCMAppHandlerMap
;
28 typedef base::Callback
<void(const std::string
& registration_id
,
29 GCMClient::Result result
)> RegisterCallback
;
30 typedef base::Callback
<void(const std::string
& message_id
,
31 GCMClient::Result result
)> SendCallback
;
32 typedef base::Callback
<void(GCMClient::Result result
)> UnregisterCallback
;
33 typedef base::Callback
<void(const GCMClient::GCMStatistics
& stats
)>
34 GetGCMStatisticsCallback
;
36 // Returns true if the GCM is allowed for all users.
37 static bool IsAllowedForAllUsers();
42 // Registers |sender_id| for an app. A registration ID will be returned by
44 // |app_id|: application ID.
45 // |sender_ids|: list of IDs of the servers that are allowed to send the
46 // messages to the application. These IDs are assigned by the
47 // Google API Console.
48 // |callback|: to be called once the asynchronous operation is done.
49 void Register(const std::string
& app_id
,
50 const std::vector
<std::string
>& sender_ids
,
51 const RegisterCallback
& callback
);
53 // Unregisters an app from using GCM.
54 // |app_id|: application ID.
55 // |callback|: to be called once the asynchronous operation is done.
56 void Unregister(const std::string
& app_id
,
57 const UnregisterCallback
& callback
);
59 // Sends a message to a given receiver.
60 // |app_id|: application ID.
61 // |receiver_id|: registration ID of the receiver party.
62 // |message|: message to be sent.
63 // |callback|: to be called once the asynchronous operation is done.
64 void Send(const std::string
& app_id
,
65 const std::string
& receiver_id
,
66 const GCMClient::OutgoingMessage
& message
,
67 const SendCallback
& callback
);
69 const GCMAppHandlerMap
& app_handlers() const { return app_handlers_
; }
71 // This method must be called before destroying the GCMDriver. Once it has
72 // been called, no other GCMDriver methods may be used.
73 virtual void Shutdown();
75 // Call this method when the user signs in to a GAIA account.
76 // TODO(jianli): To be removed when sign-in enforcement is dropped.
77 virtual void OnSignedIn() = 0;
79 // Removes all the cached and persisted GCM data. If the GCM service is
80 // restarted after the purge, a new Android ID will be obtained.
81 virtual void Purge() = 0;
83 // Adds a handler for a given app.
84 virtual void AddAppHandler(const std::string
& app_id
, GCMAppHandler
* handler
);
86 // Remove the handler for a given app.
87 virtual void RemoveAppHandler(const std::string
& app_id
);
89 // Returns the handler for the given app.
90 GCMAppHandler
* GetAppHandler(const std::string
& app_id
);
92 // Adds a connection state observer.
93 virtual void AddConnectionObserver(GCMConnectionObserver
* observer
) = 0;
95 // Removes a connection state observer.
96 virtual void RemoveConnectionObserver(GCMConnectionObserver
* observer
) = 0;
98 // Enables/disables GCM service.
99 virtual void Enable() = 0;
100 virtual void Disable() = 0;
102 // For testing purpose. Always NULL on Android.
103 virtual GCMClient
* GetGCMClientForTesting() const = 0;
105 // Returns true if the service was started.
106 virtual bool IsStarted() const = 0;
108 // Returns true if the gcm client has an open and active connection.
109 virtual bool IsConnected() const = 0;
111 // Get GCM client internal states and statistics.
112 // If clear_logs is true then activity logs will be cleared before the stats
114 virtual void GetGCMStatistics(const GetGCMStatisticsCallback
& callback
,
115 bool clear_logs
) = 0;
117 // Enables/disables GCM activity recording, and then returns the stats.
118 virtual void SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
121 // Updates the |account_mapping| information in persistent store.
122 virtual void UpdateAccountMapping(const AccountMapping
& account_mapping
) = 0;
124 // Removes the account mapping information reated to |account_id| from
126 virtual void RemoveAccountMapping(const std::string
& account_id
) = 0;
129 // Ensures that the GCM service starts (if necessary conditions are met).
130 virtual GCMClient::Result
EnsureStarted() = 0;
132 // Platform-specific implementation of Register.
133 virtual void RegisterImpl(const std::string
& app_id
,
134 const std::vector
<std::string
>& sender_ids
) = 0;
136 // Platform-specific implementation of Unregister.
137 virtual void UnregisterImpl(const std::string
& app_id
) = 0;
139 // Platform-specific implementation of Send.
140 virtual void SendImpl(const std::string
& app_id
,
141 const std::string
& receiver_id
,
142 const GCMClient::OutgoingMessage
& message
) = 0;
144 // Runs the Register callback.
145 void RegisterFinished(const std::string
& app_id
,
146 const std::string
& registration_id
,
147 GCMClient::Result result
);
149 // Runs the Unregister callback.
150 void UnregisterFinished(const std::string
& app_id
,
151 GCMClient::Result result
);
153 // Runs the Send callback.
154 void SendFinished(const std::string
& app_id
,
155 const std::string
& message_id
,
156 GCMClient::Result result
);
158 bool HasRegisterCallback(const std::string
& app_id
);
160 void ClearCallbacks();
163 // Should be called when an app with |app_id| is trying to un/register.
164 // Checks whether another un/registration is in progress.
165 bool IsAsyncOperationPending(const std::string
& app_id
) const;
167 // Callback map (from app_id to callback) for Register.
168 std::map
<std::string
, RegisterCallback
> register_callbacks_
;
170 // Callback map (from app_id to callback) for Unregister.
171 std::map
<std::string
, UnregisterCallback
> unregister_callbacks_
;
173 // Callback map (from <app_id, message_id> to callback) for Send.
174 std::map
<std::pair
<std::string
, std::string
>, SendCallback
> send_callbacks_
;
176 // App handler map (from app_id to handler pointer).
177 // The handler is not owned.
178 GCMAppHandlerMap app_handlers_
;
180 // The default handler when no app handler can be found in the map.
181 DefaultGCMAppHandler default_app_handler_
;
183 DISALLOW_COPY_AND_ASSIGN(GCMDriver
);
188 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_