gcm_driver.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 "google_apis/gcm/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 // Adds a handler for a given app. 71 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); 72 73 // Remove the handler for a given app. 74 virtual void RemoveAppHandler(const std::string& app_id); 75 76 // Enables/disables GCM service. 77 virtual void Enable() = 0; 78 virtual void Disable() = 0; 79 80 // For testing purpose. Always NULL on Android. 81 virtual GCMClient* GetGCMClientForTesting() const = 0; 82 83 // Returns true if the service was started. 84 virtual bool IsStarted() const = 0; 85 86 // Returns true if the gcm client is ready. 87 virtual bool IsGCMClientReady() const = 0; 88 89 // Get GCM client internal states and statistics. 90 // If clear_logs is true then activity logs will be cleared before the stats 91 // are returned. 92 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, 93 bool clear_logs) = 0; 94 95 // Enables/disables GCM activity recording, and then returns the stats. 96 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, 97 bool recording) = 0; 98 99 // Returns the user name if the profile is signed in. Empty string otherwise. 100 virtual std::string SignedInUserName() const = 0; 101 102 protected: 103 // Ensures that the GCM service starts (if necessary conditions are met). 104 virtual GCMClient::Result EnsureStarted() = 0; 105 106 // Platform-specific implementation of Register. 107 virtual void RegisterImpl(const std::string& app_id, 108 const std::vector<std::string>& sender_ids) = 0; 109 110 // Platform-specific implementation of Unregister. 111 virtual void UnregisterImpl(const std::string& app_id) = 0; 112 113 // Platform-specific implementation of Send. 114 virtual void SendImpl(const std::string& app_id, 115 const std::string& receiver_id, 116 const GCMClient::OutgoingMessage& message) = 0; 117 118 // Runs the Register callback. 119 void RegisterFinished(const std::string& app_id, 120 const std::string& registration_id, 121 GCMClient::Result result); 122 123 // Runs the Unregister callback. 124 void UnregisterFinished(const std::string& app_id, 125 GCMClient::Result result); 126 127 // Runs the Send callback. 128 void SendFinished(const std::string& app_id, 129 const std::string& message_id, 130 GCMClient::Result result); 131 132 bool HasRegisterCallback(const std::string& app_id); 133 134 void ClearCallbacks(); 135 136 // Returns the handler for the given app. 137 GCMAppHandler* GetAppHandler(const std::string& app_id); 138 139 private: 140 // Should be called when an app with |app_id| is trying to un/register. 141 // Checks whether another un/registration is in progress. 142 bool IsAsyncOperationPending(const std::string& app_id) const; 143 144 // Callback map (from app_id to callback) for Register. 145 std::map<std::string, RegisterCallback> register_callbacks_; 146 147 // Callback map (from app_id to callback) for Unregister. 148 std::map<std::string, UnregisterCallback> unregister_callbacks_; 149 150 // Callback map (from <app_id, message_id> to callback) for Send. 151 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; 152 153 // App handler map (from app_id to handler pointer). 154 // The handler is not owned. 155 GCMAppHandlerMap app_handlers_; 156 157 // The default handler when no app handler can be found in the map. 158 DefaultGCMAppHandler default_app_handler_; 159 160 DISALLOW_COPY_AND_ASSIGN(GCMDriver); 161}; 162 163} // namespace gcm 164 165#endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ 166