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