[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 | |
| 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, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame^] | 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, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 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. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame^] | 44 | typedef std::map<std::string, std::string> MessageData; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 45 | |
| 46 | // Message to be delivered to the other party. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame^] | 47 | struct GCM_EXPORT OutgoingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 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. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame^] | 59 | struct GCM_EXPORT IncomingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 60 | IncomingMessage(); |
| 61 | ~IncomingMessage(); |
| 62 | |
| 63 | MessageData data; |
| 64 | }; |
| 65 | |
| 66 | // The check-in info for the user. Returned by the server. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame^] | 67 | struct GCM_EXPORT CheckInInfo { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 68 | CheckInInfo() : android_id(0), secret(0) {} |
| 69 | bool IsValid() const { return android_id != 0 && secret != 0; } |
| 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. |
| 80 | // |checkin_info|: valid if the checkin completed successfully. |
| 81 | // |result|: the type of the error if an error occured, success otherwise. |
| 82 | virtual void OnAddUserFinished(const CheckInInfo& checkin_info, |
| 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 | |
| 93 | // Called when the unregistration completed successfully or an error occurs. |
| 94 | // |app_id|: application ID. |
| 95 | // |result|: the type of the error if an error occured, success otherwise. |
| 96 | virtual void OnUnregisterFinished(const std::string& app_id, |
| 97 | Result result) = 0; |
| 98 | |
| 99 | // Called when the message is scheduled to send successfully or an error |
| 100 | // occurs. |
| 101 | // |app_id|: application ID. |
| 102 | // |message_id|: ID of the message being sent. |
| 103 | // |result|: the type of the error if an error occured, success otherwise. |
| 104 | virtual void OnSendFinished(const std::string& app_id, |
| 105 | const std::string& message_id, |
| 106 | Result result) = 0; |
| 107 | |
| 108 | // Called when a message has been received. |
| 109 | // |app_id|: application ID. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 110 | // |message|: message received. |
| 111 | virtual void OnMessageReceived(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 112 | const IncomingMessage& message) = 0; |
| 113 | |
| 114 | // Called when some messages have been deleted from the server. |
| 115 | // |app_id|: application ID. |
| 116 | virtual void OnMessagesDeleted(const std::string& app_id) = 0; |
| 117 | |
| 118 | // Called when a message failed to send to the server. |
| 119 | // |app_id|: application ID. |
| 120 | // |message_id|: ID of the message being sent. |
| 121 | // |result|: the type of the error if an error occured, success otherwise. |
| 122 | virtual void OnMessageSendError(const std::string& app_id, |
| 123 | const std::string& message_id, |
| 124 | Result result) = 0; |
| 125 | |
| 126 | // Returns the checkin info associated with this user. The delegate class |
| 127 | // is expected to persist the checkin info that is provided by |
| 128 | // OnAddUserFinished. |
| 129 | virtual CheckInInfo GetCheckInInfo() const = 0; |
| 130 | |
| 131 | // Called when the loading from the persistent store is done. The loading |
| 132 | // is triggered asynchronously when GCMClient is created. |
| 133 | virtual void OnLoadingCompleted() = 0; |
| 134 | |
| 135 | // Returns a task runner for file operations that may block. This is used |
| 136 | // in writing to or reading from the persistent store. |
| 137 | virtual base::TaskRunner* GetFileTaskRunner() = 0; |
| 138 | }; |
| 139 | |
| 140 | // Returns the single instance. Multiple profiles share the same client |
| 141 | // that makes use of the same MCS connection. |
| 142 | static GCMClient* Get(); |
| 143 | |
| 144 | // Passes a mocked instance for testing purpose. |
| 145 | static void SetForTesting(GCMClient* client); |
| 146 | |
| 147 | // Checks in the user to use GCM. If the device has not been checked in, it |
| 148 | // will be done first. |
| 149 | // |username|: the username (email address) used to check in with the server. |
| 150 | // |delegate|: the delegate whose methods will be called asynchronously in |
| 151 | // response to events and messages. |
| 152 | virtual void AddUser(const std::string& username, Delegate* delegate) = 0; |
| 153 | |
| 154 | // Registers the application for GCM. Delegate::OnRegisterFinished will be |
| 155 | // called asynchronously upon completion. |
| 156 | // |username|: the username (email address) passed in AddUser. |
| 157 | // |app_id|: application ID. |
| 158 | // |cert|: SHA-1 of public key of the application, in base16 format. |
| 159 | // |sender_ids|: list of IDs of the servers that are allowed to send the |
| 160 | // messages to the application. These IDs are assigned by the |
| 161 | // Google API Console. |
| 162 | virtual void Register(const std::string& username, |
| 163 | const std::string& app_id, |
| 164 | const std::string& cert, |
| 165 | const std::vector<std::string>& sender_ids) = 0; |
| 166 | |
| 167 | // Unregisters the application from GCM when it is uninstalled. |
| 168 | // Delegate::OnUnregisterFinished will be called asynchronously upon |
| 169 | // completion. |
| 170 | // |username|: the username (email address) passed in AddUser. |
| 171 | // |app_id|: application ID. |
| 172 | virtual void Unregister(const std::string& username, |
| 173 | const std::string& app_id) = 0; |
| 174 | |
| 175 | // Sends a message to a given receiver. Delegate::OnSendFinished will be |
| 176 | // called asynchronously upon completion. |
| 177 | // |username|: the username (email address) passed in AddUser. |
| 178 | // |app_id|: application ID. |
| 179 | // |receiver_id|: registration ID of the receiver party. |
| 180 | // |message|: message to be sent. |
| 181 | virtual void Send(const std::string& username, |
| 182 | const std::string& app_id, |
| 183 | const std::string& receiver_id, |
| 184 | const OutgoingMessage& message) = 0; |
| 185 | |
| 186 | // Returns true if the loading from the persistent store is still in progress. |
| 187 | virtual bool IsLoading() const = 0; |
| 188 | |
| 189 | protected: |
| 190 | virtual ~GCMClient() {} |
| 191 | }; |
| 192 | |
| 193 | } // namespace gcm |
| 194 | |
| 195 | #endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_ |