Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / gcm_driver / gcm_driver.h
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/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"
18
19 namespace gcm {
20
21 class GCMAppHandler;
22 class GCMConnectionObserver;
23 struct AccountMapping;
24
25 // Bridge between GCM users in Chrome and the platform-specific implementation.
26 class GCMDriver {
27  public:
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;
36
37   // Returns true if the GCM is allowed for all users.
38   static bool IsAllowedForAllUsers();
39
40   GCMDriver();
41   virtual ~GCMDriver();
42
43   // Registers |sender_id| for an app. A registration ID will be returned by
44   // the GCM server.
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);
53
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);
59
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);
69
70   const GCMAppHandlerMap& app_handlers() const { return app_handlers_; }
71
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();
75
76   // Called when the user signs in to or out of a GAIA account.
77   virtual void OnSignedIn() = 0;
78   virtual void OnSignedOut() = 0;
79
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;
83
84   // Adds a handler for a given app.
85   virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler);
86
87   // Remove the handler for a given app.
88   virtual void RemoveAppHandler(const std::string& app_id);
89
90   // Returns the handler for the given app.
91   GCMAppHandler* GetAppHandler(const std::string& app_id);
92
93   // Adds a connection state observer.
94   virtual void AddConnectionObserver(GCMConnectionObserver* observer) = 0;
95
96   // Removes a connection state observer.
97   virtual void RemoveConnectionObserver(GCMConnectionObserver* observer) = 0;
98
99   // Enables/disables GCM service.
100   virtual void Enable() = 0;
101   virtual void Disable() = 0;
102
103   // For testing purpose. Always NULL on Android.
104   virtual GCMClient* GetGCMClientForTesting() const = 0;
105
106   // Returns true if the service was started.
107   virtual bool IsStarted() const = 0;
108
109   // Returns true if the gcm client has an open and active connection.
110   virtual bool IsConnected() const = 0;
111
112   // Get GCM client internal states and statistics.
113   // If clear_logs is true then activity logs will be cleared before the stats
114   // are returned.
115   virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
116                                 bool clear_logs) = 0;
117
118   // Enables/disables GCM activity recording, and then returns the stats.
119   virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback,
120                                bool recording) = 0;
121
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
126   //                   tokens.
127   virtual void SetAccountTokens(
128       const std::vector<GCMClient::AccountTokenInfo>& account_tokens) = 0;
129
130   // Updates the |account_mapping| information in persistent store.
131   virtual void UpdateAccountMapping(const AccountMapping& account_mapping) = 0;
132
133   // Removes the account mapping information reated to |account_id| from
134   // persistent store.
135   virtual void RemoveAccountMapping(const std::string& account_id) = 0;
136
137   // Getter and setter of last token fetch time.
138   virtual base::Time GetLastTokenFetchTime() = 0;
139   virtual void SetLastTokenFetchTime(const base::Time& time) = 0;
140
141  protected:
142   // Ensures that the GCM service starts (if necessary conditions are met).
143   virtual GCMClient::Result EnsureStarted() = 0;
144
145   // Platform-specific implementation of Register.
146   virtual void RegisterImpl(const std::string& app_id,
147                             const std::vector<std::string>& sender_ids) = 0;
148
149   // Platform-specific implementation of Unregister.
150   virtual void UnregisterImpl(const std::string& app_id) = 0;
151
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;
156
157   // Runs the Register callback.
158   void RegisterFinished(const std::string& app_id,
159                         const std::string& registration_id,
160                         GCMClient::Result result);
161
162   // Runs the Unregister callback.
163   void UnregisterFinished(const std::string& app_id,
164                           GCMClient::Result result);
165
166   // Runs the Send callback.
167   void SendFinished(const std::string& app_id,
168                     const std::string& message_id,
169                     GCMClient::Result result);
170
171   bool HasRegisterCallback(const std::string& app_id);
172
173   void ClearCallbacks();
174
175  private:
176   // Called after unregistration completes in order to trigger the pending
177   // registration.
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);
183
184   // Callback map (from app_id to callback) for Register.
185   std::map<std::string, RegisterCallback> register_callbacks_;
186
187   // Callback map (from app_id to callback) for Unregister.
188   std::map<std::string, UnregisterCallback> unregister_callbacks_;
189
190   // Callback map (from <app_id, message_id> to callback) for Send.
191   std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
192
193   // App handler map (from app_id to handler pointer).
194   // The handler is not owned.
195   GCMAppHandlerMap app_handlers_;
196
197   // The default handler when no app handler can be found in the map.
198   DefaultGCMAppHandler default_app_handler_;
199
200   base::WeakPtrFactory<GCMDriver> weak_ptr_factory_;
201
202   DISALLOW_COPY_AND_ASSIGN(GCMDriver);
203 };
204
205 }  // namespace gcm
206
207 #endif  // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_