1 // Copyright 2013 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 GOOGLE_APIS_GCM_GCM_CLIENT_H_ 6 #define GOOGLE_APIS_GCM_GCM_CLIENT_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "google_apis/gcm/base/gcm_export.h" 14 15 namespace base { 16 class TaskRunner; 17 } 18 19 namespace gcm { 20 21 // Interface that encapsulates the network communications with the Google Cloud 22 // Messaging server. This interface is not supposed to be thread-safe. 23 class GCM_EXPORT GCMClient { 24 public: 25 enum Result { 26 // Successful operation. 27 SUCCESS, 28 // Invalid parameter. 29 INVALID_PARAMETER, 30 // Previous asynchronous operation is still pending to finish. Certain 31 // operation, like register, is only allowed one at a time. 32 ASYNC_OPERATION_PENDING, 33 // Network socket error. 34 NETWORK_ERROR, 35 // Problem at the server. 36 SERVER_ERROR, 37 // Exceeded the specified TTL during message sending. 38 TTL_EXCEEDED, 39 // Other errors. 40 UNKNOWN_ERROR 41 }; 42 43 // Message data consisting of key-value pairs. 44 typedef std::map<std::string, std::string> MessageData; 45 46 // Message to be delivered to the other party. 47 struct GCM_EXPORT OutgoingMessage { 48 OutgoingMessage(); 49 ~OutgoingMessage(); 50 51 // Message ID. 52 std::string id; 53 // In seconds. 54 int time_to_live; 55 MessageData data; 56 }; 57 58 // Message being received from the other party. 59 struct GCM_EXPORT IncomingMessage { 60 IncomingMessage(); 61 ~IncomingMessage(); 62 63 MessageData data; 64 }; 65 66 // The check-in info for the user. Returned by the server. 67 struct GCM_EXPORT CheckInInfo { 68 CheckInInfo() : android_id(0), secret(0) {} 69 bool IsValid() const { return android_id != 0 && secret != 0; } 70 void Reset() { 71 android_id = 0; 72 secret = 0; 73 } 74 75 uint64 android_id; 76 uint64 secret; 77 }; 78 79 // A delegate interface that allows the GCMClient instance to interact with 80 // its caller, i.e. notifying asynchronous event. 81 class Delegate { 82 public: 83 // Called when the user has been checked in successfully or an error occurs. 84 // |checkin_info|: valid if the checkin completed successfully. 85 // |result|: the type of the error if an error occured, success otherwise. 86 virtual void OnCheckInFinished(const CheckInInfo& checkin_info, 87 Result result) = 0; 88 89 // Called when the registration completed successfully or an error occurs. 90 // |app_id|: application ID. 91 // |registration_id|: non-empty if the registration completed successfully. 92 // |result|: the type of the error if an error occured, success otherwise. 93 virtual void OnRegisterFinished(const std::string& app_id, 94 const std::string& registration_id, 95 Result result) = 0; 96 97 // Called when the message is scheduled to send successfully or an error 98 // occurs. 99 // |app_id|: application ID. 100 // |message_id|: ID of the message being sent. 101 // |result|: the type of the error if an error occured, success otherwise. 102 virtual void OnSendFinished(const std::string& app_id, 103 const std::string& message_id, 104 Result result) = 0; 105 106 // Called when a message has been received. 107 // |app_id|: application ID. 108 // |message|: message received. 109 virtual void OnMessageReceived(const std::string& app_id, 110 const IncomingMessage& message) = 0; 111 112 // Called when some messages have been deleted from the server. 113 // |app_id|: application ID. 114 virtual void OnMessagesDeleted(const std::string& app_id) = 0; 115 116 // Called when a message failed to send to the server. 117 // |app_id|: application ID. 118 // |message_id|: ID of the message being sent. 119 // |result|: the type of the error if an error occured, success otherwise. 120 virtual void OnMessageSendError(const std::string& app_id, 121 const std::string& message_id, 122 Result result) = 0; 123 124 // Returns the checkin info associated with this user. The delegate class 125 // is expected to persist the checkin info that is provided by 126 // OnCheckInFinished. 127 virtual CheckInInfo GetCheckInInfo() const = 0; 128 129 // Called when the loading from the persistent store is done. The loading 130 // is triggered asynchronously when GCMClient is created. 131 virtual void OnLoadingCompleted() = 0; 132 133 // Returns a task runner for file operations that may block. This is used 134 // in writing to or reading from the persistent store. 135 virtual base::TaskRunner* GetFileTaskRunner() = 0; 136 }; 137 138 // Returns the single instance. Multiple profiles share the same client 139 // that makes use of the same MCS connection. 140 static GCMClient* Get(); 141 142 // Passes a mocked instance for testing purpose. 143 static void SetForTesting(GCMClient* client); 144 145 // Checks in the user to use GCM. If the device has not been checked in, it 146 // will be done first. 147 // |username|: the username (email address) used to check in with the server. 148 // |delegate|: the delegate whose methods will be called asynchronously in 149 // response to events and messages. 150 virtual void CheckIn(const std::string& username, Delegate* delegate) = 0; 151 152 // Registers the application for GCM. Delegate::OnRegisterFinished will be 153 // called asynchronously upon completion. 154 // |username|: the username (email address) passed in CheckIn. 155 // |app_id|: application ID. 156 // |cert|: SHA-1 of public key of the application, in base16 format. 157 // |sender_ids|: list of IDs of the servers that are allowed to send the 158 // messages to the application. These IDs are assigned by the 159 // Google API Console. 160 virtual void Register(const std::string& username, 161 const std::string& app_id, 162 const std::string& cert, 163 const std::vector<std::string>& sender_ids) = 0; 164 165 // Unregisters the application from GCM when it is uninstalled. 166 // Delegate::OnUnregisterFinished will be called asynchronously upon 167 // completion. 168 // |username|: the username (email address) passed in CheckIn. 169 // |app_id|: application ID. 170 virtual void Unregister(const std::string& username, 171 const std::string& app_id) = 0; 172 173 // Sends a message to a given receiver. Delegate::OnSendFinished will be 174 // called asynchronously upon completion. 175 // |username|: the username (email address) passed in CheckIn. 176 // |app_id|: application ID. 177 // |receiver_id|: registration ID of the receiver party. 178 // |message|: message to be sent. 179 virtual void Send(const std::string& username, 180 const std::string& app_id, 181 const std::string& receiver_id, 182 const OutgoingMessage& message) = 0; 183 184 // Returns true if the loading from the persistent store is still in progress. 185 virtual bool IsLoading() const = 0; 186 187 protected: 188 virtual ~GCMClient() {} 189 }; 190 191 } // namespace gcm 192 193 #endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_ 194