[GCM] Passing GCMClient::AccountTokenInfo list to GCMDriver
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver.h
blob34391acaf7a514976811cdbd54fad776963b9455
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/threading/thread_checker.h"
15 #include "components/gcm_driver/default_gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
18 namespace gcm {
20 class GCMAppHandler;
21 class GCMConnectionObserver;
22 struct AccountMapping;
24 // Bridge between GCM users in Chrome and the platform-specific implementation.
25 class GCMDriver {
26 public:
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();
39 GCMDriver();
40 virtual ~GCMDriver();
42 // Registers |sender_id| for an app. A registration ID will be returned by
43 // the GCM server.
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
113 // are returned.
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,
119 bool recording) = 0;
121 // sets a list of signed in accounts with OAuth2 access tokens, when GCMDriver
122 // works in context of a signed in entity (e.g. browser profile where user is
123 // signed into sync).
124 // |account_tokens|: list of email addresses, account IDs and OAuth2 access
125 // tokens.
126 virtual void SetAccountTokens(
127 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) = 0;
129 // Updates the |account_mapping| information in persistent store.
130 virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
132 // Removes the account mapping information reated to |account_id| from
133 // persistent store.
134 virtual void RemoveAccountMapping(const std::string& account_id) = 0;
136 protected:
137 // Ensures that the GCM service starts (if necessary conditions are met).
138 virtual GCMClient::Result EnsureStarted() = 0;
140 // Platform-specific implementation of Register.
141 virtual void RegisterImpl(const std::string& app_id,
142 const std::vector<std::string>& sender_ids) = 0;
144 // Platform-specific implementation of Unregister.
145 virtual void UnregisterImpl(const std::string& app_id) = 0;
147 // Platform-specific implementation of Send.
148 virtual void SendImpl(const std::string& app_id,
149 const std::string& receiver_id,
150 const GCMClient::OutgoingMessage& message) = 0;
152 // Runs the Register callback.
153 void RegisterFinished(const std::string& app_id,
154 const std::string& registration_id,
155 GCMClient::Result result);
157 // Runs the Unregister callback.
158 void UnregisterFinished(const std::string& app_id,
159 GCMClient::Result result);
161 // Runs the Send callback.
162 void SendFinished(const std::string& app_id,
163 const std::string& message_id,
164 GCMClient::Result result);
166 bool HasRegisterCallback(const std::string& app_id);
168 void ClearCallbacks();
170 private:
171 // Should be called when an app with |app_id| is trying to un/register.
172 // Checks whether another un/registration is in progress.
173 bool IsAsyncOperationPending(const std::string& app_id) const;
175 // Callback map (from app_id to callback) for Register.
176 std::map<std::string, RegisterCallback> register_callbacks_;
178 // Callback map (from app_id to callback) for Unregister.
179 std::map<std::string, UnregisterCallback> unregister_callbacks_;
181 // Callback map (from <app_id, message_id> to callback) for Send.
182 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
184 // App handler map (from app_id to handler pointer).
185 // The handler is not owned.
186 GCMAppHandlerMap app_handlers_;
188 // The default handler when no app handler can be found in the map.
189 DefaultGCMAppHandler default_app_handler_;
191 DISALLOW_COPY_AND_ASSIGN(GCMDriver);
194 } // namespace gcm
196 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_