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