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