gcm_driver.h revision 116680a4aac90f2aa7413d9095a592090648e557
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;
21
22// Bridge between GCM users in Chrome and the platform-specific implementation.
23class GCMDriver {
24 public:
25  typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap;
26  typedef base::Callback<void(const std::string& registration_id,
27                              GCMClient::Result result)> RegisterCallback;
28  typedef base::Callback<void(const std::string& message_id,
29                              GCMClient::Result result)> SendCallback;
30  typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback;
31  typedef base::Callback<void(const GCMClient::GCMStatistics& stats)>
32      GetGCMStatisticsCallback;
33
34  // Returns true if the GCM is allowed for all users.
35  static bool IsAllowedForAllUsers();
36
37  GCMDriver();
38  virtual ~GCMDriver();
39
40  // Registers |sender_id| for an app. A registration ID will be returned by
41  // the GCM server.
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);
50
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);
56
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);
66
67  const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
68
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();
72
73  // Call this method when the user signs in to a GAIA account.
74  // TODO(jianli): To be removed when sign-in enforcement is dropped.
75  virtual void OnSignedIn() = 0;
76
77  // Removes all the cached and persisted GCM data. If the GCM service is
78  // restarted after the purge, a new Android ID will be obtained.
79  virtual void Purge() = 0;
80
81  // Adds a handler for a given app.
82  virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
83
84  // Remove the handler for a given app.
85  virtual void RemoveAppHandler(const std::string& app_id);
86
87  // Returns the handler for the given app.
88  GCMAppHandler* GetAppHandler(const std::string& app_id);
89
90  // Enables/disables GCM service.
91  virtual void Enable() = 0;
92  virtual void Disable() = 0;
93
94  // For testing purpose. Always NULL on Android.
95  virtual GCMClient* GetGCMClientForTesting() const = 0;
96
97  // Returns true if the service was started.
98  virtual bool IsStarted() const = 0;
99
100  // Returns true if the gcm client has an open and active connection.
101  virtual bool IsConnected() const = 0;
102
103  // Get GCM client internal states and statistics.
104  // If clear_logs is true then activity logs will be cleared before the stats
105  // are returned.
106  virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
107                                bool clear_logs) = 0;
108
109  // Enables/disables GCM activity recording, and then returns the stats.
110  virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
111                               bool recording) = 0;
112
113 protected:
114  // Ensures that the GCM service starts (if necessary conditions are met).
115  virtual GCMClient::Result EnsureStarted() = 0;
116
117  // Platform-specific implementation of Register.
118  virtual void RegisterImpl(const std::string& app_id,
119                            const std::vector<std::string>& sender_ids) = 0;
120
121  // Platform-specific implementation of Unregister.
122  virtual void UnregisterImpl(const std::string& app_id) = 0;
123
124  // Platform-specific implementation of Send.
125  virtual void SendImpl(const std::string& app_id,
126                        const std::string& receiver_id,
127                        const GCMClient::OutgoingMessage& message) = 0;
128
129  // Runs the Register callback.
130  void RegisterFinished(const std::string& app_id,
131                        const std::string& registration_id,
132                        GCMClient::Result result);
133
134  // Runs the Unregister callback.
135  void UnregisterFinished(const std::string& app_id,
136                          GCMClient::Result result);
137
138  // Runs the Send callback.
139  void SendFinished(const std::string& app_id,
140                    const std::string& message_id,
141                    GCMClient::Result result);
142
143  bool HasRegisterCallback(const std::string& app_id);
144
145  void ClearCallbacks();
146
147 private:
148  // Should be called when an app with |app_id| is trying to un/register.
149  // Checks whether another un/registration is in progress.
150  bool IsAsyncOperationPending(const std::string& app_id) const;
151
152  // Callback map (from app_id to callback) for Register.
153  std::map<std::string, RegisterCallback> register_callbacks_;
154
155  // Callback map (from app_id to callback) for Unregister.
156  std::map<std::string, UnregisterCallback> unregister_callbacks_;
157
158  // Callback map (from <app_id, message_id> to callback) for Send.
159  std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
160
161  // App handler map (from app_id to handler pointer).
162  // The handler is not owned.
163  GCMAppHandlerMap app_handlers_;
164
165  // The default handler when no app handler can be found in the map.
166  DefaultGCMAppHandler default_app_handler_;
167
168  DISALLOW_COPY_AND_ASSIGN(GCMDriver);
169};
170
171}  // namespace gcm
172
173#endif  // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_
174