gcm_driver.h revision 1675a649fd7a8b3cb80ffddae2dc181f122353c5
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.
4
5#ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
6#define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
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"
17
18namespace gcm {
19
20class GCMAppHandler;
21class GCMConnectionObserver;
22struct AccountMapping;
23
24// Bridge between GCM users in Chrome and the platform-specific implementation.
25class 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;
35
36  // Returns true if the GCM is allowed for all users.
37  static bool IsAllowedForAllUsers();
38
39  GCMDriver();
40  virtual ~GCMDriver();
41
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);
52
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);
58
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);
68
69  const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
70
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();
74
75  // Called when the user signs in to or out of a GAIA account.
76  virtual void OnSignedIn() = 0;
77  virtual void OnSignedOut() = 0;
78
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;
82
83  // Adds a handler for a given app.
84  virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
85
86  // Remove the handler for a given app.
87  virtual void RemoveAppHandler(const std::string& app_id);
88
89  // Returns the handler for the given app.
90  GCMAppHandler* GetAppHandler(const std::string& app_id);
91
92  // Adds a connection state observer.
93  virtual void AddConnectionObserver(GCMConnectionObserver* observer) = 0;
94
95  // Removes a connection state observer.
96  virtual void RemoveConnectionObserver(GCMConnectionObserver* observer) = 0;
97
98  // Enables/disables GCM service.
99  virtual void Enable() = 0;
100  virtual void Disable() = 0;
101
102  // For testing purpose. Always NULL on Android.
103  virtual GCMClient* GetGCMClientForTesting() const = 0;
104
105  // Returns true if the service was started.
106  virtual bool IsStarted() const = 0;
107
108  // Returns true if the gcm client has an open and active connection.
109  virtual bool IsConnected() const = 0;
110
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;
116
117  // Enables/disables GCM activity recording, and then returns the stats.
118  virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
119                               bool recording) = 0;
120
121  // Updates the |account_mapping| information in persistent store.
122  virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
123
124  // Removes the account mapping information reated to |account_id| from
125  // persistent store.
126  virtual void RemoveAccountMapping(const std::string& account_id) = 0;
127
128 protected:
129  // Ensures that the GCM service starts (if necessary conditions are met).
130  virtual GCMClient::Result EnsureStarted() = 0;
131
132  // Platform-specific implementation of Register.
133  virtual void RegisterImpl(const std::string& app_id,
134                            const std::vector<std::string>& sender_ids) = 0;
135
136  // Platform-specific implementation of Unregister.
137  virtual void UnregisterImpl(const std::string& app_id) = 0;
138
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;
143
144  // Runs the Register callback.
145  void RegisterFinished(const std::string& app_id,
146                        const std::string& registration_id,
147                        GCMClient::Result result);
148
149  // Runs the Unregister callback.
150  void UnregisterFinished(const std::string& app_id,
151                          GCMClient::Result result);
152
153  // Runs the Send callback.
154  void SendFinished(const std::string& app_id,
155                    const std::string& message_id,
156                    GCMClient::Result result);
157
158  bool HasRegisterCallback(const std::string& app_id);
159
160  void ClearCallbacks();
161
162 private:
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;
166
167  // Callback map (from app_id to callback) for Register.
168  std::map<std::string, RegisterCallback> register_callbacks_;
169
170  // Callback map (from app_id to callback) for Unregister.
171  std::map<std::string, UnregisterCallback> unregister_callbacks_;
172
173  // Callback map (from <app_id, message_id> to callback) for Send.
174  std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
175
176  // App handler map (from app_id to handler pointer).
177  // The handler is not owned.
178  GCMAppHandlerMap app_handlers_;
179
180  // The default handler when no app handler can be found in the map.
181  DefaultGCMAppHandler default_app_handler_;
182
183  DISALLOW_COPY_AND_ASSIGN(GCMDriver);
184};
185
186}  // namespace gcm
187
188#endif  // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
189