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;
37 // Returns true if the GCM is allowed for all users.
38 static bool IsAllowedForAllUsers();
43 // Registers |sender_id| for an app. A registration ID will be returned by
45 // |app_id|: application ID.
46 // |sender_ids|: list of IDs of the servers that are allowed to send the
47 // messages to the application. These IDs are assigned by the
48 // Google API Console.
49 // |callback|: to be called once the asynchronous operation is done.
50 void Register(const std::string& app_id,
51 const std::vector<std::string>& sender_ids,
52 const RegisterCallback& callback);
54 // Unregisters an app from using GCM.
55 // |app_id|: application ID.
56 // |callback|: to be called once the asynchronous operation is done.
57 void Unregister(const std::string& app_id,
58 const UnregisterCallback& callback);
60 // Sends a message to a given receiver.
61 // |app_id|: application ID.
62 // |receiver_id|: registration ID of the receiver party.
63 // |message|: message to be sent.
64 // |callback|: to be called once the asynchronous operation is done.
65 void Send(const std::string& app_id,
66 const std::string& receiver_id,
67 const GCMClient::OutgoingMessage& message,
68 const SendCallback& callback);
70 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
72 // This method must be called before destroying the GCMDriver. Once it has
73 // been called, no other GCMDriver methods may be used.
74 virtual void Shutdown();
76 // Called when the user signs in to or out of a GAIA account.
77 virtual void OnSignedIn() = 0;
78 virtual void OnSignedOut() = 0;
80 // Removes all the cached and persisted GCM data. If the GCM service is
81 // restarted after the purge, a new Android ID will be obtained.
82 virtual void Purge() = 0;
84 // Adds a handler for a given app.
85 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
87 // Remove the handler for a given app.
88 virtual void RemoveAppHandler(const std::string& app_id);
90 // Returns the handler for the given app.
91 GCMAppHandler* GetAppHandler(const std::string& app_id);
93 // Adds a connection state observer.
94 virtual void AddConnectionObserver(GCMConnectionObserver* observer) = 0;
96 // Removes a connection state observer.
97 virtual void RemoveConnectionObserver(GCMConnectionObserver* observer) = 0;
99 // Enables/disables GCM service.
100 virtual void Enable() = 0;
101 virtual void Disable() = 0;
103 // For testing purpose. Always NULL on Android.
104 virtual GCMClient* GetGCMClientForTesting() const = 0;
106 // Returns true if the service was started.
107 virtual bool IsStarted() const = 0;
109 // Returns true if the gcm client has an open and active connection.
110 virtual bool IsConnected() const = 0;
112 // Get GCM client internal states and statistics.
113 // If clear_logs is true then activity logs will be cleared before the stats
115 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
116 bool clear_logs) = 0;
118 // Enables/disables GCM activity recording, and then returns the stats.
119 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
122 // sets a list of signed in accounts with OAuth2 access tokens, when GCMDriver
123 // works in context of a signed in entity (e.g. browser profile where user is
124 // signed into sync).
125 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
127 virtual void SetAccountTokens(
128 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) = 0;
130 // Updates the |account_mapping| information in persistent store.
131 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
133 // Removes the account mapping information reated to |account_id| from
135 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
137 // Getter and setter of last token fetch time.
138 virtual base::Time GetLastTokenFetchTime() = 0;
139 virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
142 // Ensures that the GCM service starts (if necessary conditions are met).
143 virtual GCMClient::Result EnsureStarted() = 0;
145 // Platform-specific implementation of Register.
146 virtual void RegisterImpl(const std::string& app_id,
147 const std::vector<std::string>& sender_ids) = 0;
149 // Platform-specific implementation of Unregister.
150 virtual void UnregisterImpl(const std::string& app_id) = 0;
152 // Platform-specific implementation of Send.
153 virtual void SendImpl(const std::string& app_id,
154 const std::string& receiver_id,
155 const GCMClient::OutgoingMessage& message) = 0;
157 // Runs the Register callback.
158 void RegisterFinished(const std::string& app_id,
159 const std::string& registration_id,
160 GCMClient::Result result);
162 // Runs the Unregister callback.
163 void UnregisterFinished(const std::string& app_id,
164 GCMClient::Result result);
166 // Runs the Send callback.
167 void SendFinished(const std::string& app_id,
168 const std::string& message_id,
169 GCMClient::Result result);
171 bool HasRegisterCallback(const std::string& app_id);
173 void ClearCallbacks();
176 // Called after unregistration completes in order to trigger the pending
178 void RegisterAfterUnregister(
179 const std::string& app_id,
180 const std::vector<std::string>& normalized_sender_ids,
181 const UnregisterCallback& unregister_callback,
182 GCMClient::Result result);
184 // Callback map (from app_id to callback) for Register.
185 std::map<std::string, RegisterCallback> register_callbacks_;
187 // Callback map (from app_id to callback) for Unregister.
188 std::map<std::string, UnregisterCallback> unregister_callbacks_;
190 // Callback map (from <app_id, message_id> to callback) for Send.
191 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
193 // App handler map (from app_id to handler pointer).
194 // The handler is not owned.
195 GCMAppHandlerMap app_handlers_;
197 // The default handler when no app handler can be found in the map.
198 DefaultGCMAppHandler default_app_handler_;
200 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_;
202 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
207 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_