[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] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 15 | template <class T> class scoped_refptr; |
| 16 | |
| 17 | namespace base { |
| 18 | class FilePath; |
| 19 | class SequencedTaskRunner; |
| 20 | } |
| 21 | |
| 22 | namespace checkin_proto { |
| 23 | class ChromeBuildProto; |
| 24 | } |
| 25 | |
| 26 | namespace net { |
| 27 | class URLRequestContextGetter; |
| 28 | } |
| 29 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 30 | namespace gcm { |
| 31 | |
| 32 | // Interface that encapsulates the network communications with the Google Cloud |
| 33 | // Messaging server. This interface is not supposed to be thread-safe. |
| 34 | class GCM_EXPORT GCMClient { |
| 35 | public: |
| 36 | enum Result { |
| 37 | // Successful operation. |
| 38 | SUCCESS, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 39 | // Invalid parameter. |
| 40 | INVALID_PARAMETER, |
[email protected] | c7f7b53 | 2014-01-24 07:24:45 | [diff] [blame] | 41 | // Profile not signed in. |
| 42 | NOT_SIGNED_IN, |
[email protected] | 8c91586b4 | 2014-02-15 05:22:51 | [diff] [blame] | 43 | // Certificate was missing. Certain operation, like register, requires it. |
| 44 | CERTIFICATE_MISSING, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 45 | // Previous asynchronous operation is still pending to finish. Certain |
| 46 | // operation, like register, is only allowed one at a time. |
| 47 | ASYNC_OPERATION_PENDING, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 48 | // Network socket error. |
| 49 | NETWORK_ERROR, |
| 50 | // Problem at the server. |
| 51 | SERVER_ERROR, |
| 52 | // Exceeded the specified TTL during message sending. |
| 53 | TTL_EXCEEDED, |
| 54 | // Other errors. |
| 55 | UNKNOWN_ERROR |
| 56 | }; |
| 57 | |
| 58 | // Message data consisting of key-value pairs. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 59 | typedef std::map<std::string, std::string> MessageData; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 60 | |
| 61 | // Message to be delivered to the other party. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 62 | struct GCM_EXPORT OutgoingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 63 | OutgoingMessage(); |
| 64 | ~OutgoingMessage(); |
| 65 | |
| 66 | // Message ID. |
| 67 | std::string id; |
| 68 | // In seconds. |
| 69 | int time_to_live; |
| 70 | MessageData data; |
| 71 | }; |
| 72 | |
| 73 | // Message being received from the other party. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 74 | struct GCM_EXPORT IncomingMessage { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 75 | IncomingMessage(); |
| 76 | ~IncomingMessage(); |
| 77 | |
| 78 | MessageData data; |
[email protected] | 40113aa | 2014-02-28 21:37:56 | [diff] [blame] | 79 | std::string collapse_key; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 80 | }; |
| 81 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 82 | // A delegate interface that allows the GCMClient instance to interact with |
| 83 | // its caller, i.e. notifying asynchronous event. |
| 84 | class Delegate { |
| 85 | public: |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 86 | // Called when the registration completed successfully or an error occurs. |
| 87 | // |app_id|: application ID. |
| 88 | // |registration_id|: non-empty if the registration completed successfully. |
| 89 | // |result|: the type of the error if an error occured, success otherwise. |
| 90 | virtual void OnRegisterFinished(const std::string& app_id, |
| 91 | const std::string& registration_id, |
| 92 | Result result) = 0; |
| 93 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 94 | // Called when the unregistration completed. |
| 95 | // |app_id|: application ID. |
| 96 | // |success|: indicates whether unregistration request was successful. |
| 97 | virtual void OnUnregisterFinished(const std::string& app_id, |
| 98 | bool success) = 0; |
| 99 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 100 | // Called when the message is scheduled to send successfully or an error |
| 101 | // occurs. |
| 102 | // |app_id|: application ID. |
| 103 | // |message_id|: ID of the message being sent. |
| 104 | // |result|: the type of the error if an error occured, success otherwise. |
| 105 | virtual void OnSendFinished(const std::string& app_id, |
| 106 | const std::string& message_id, |
| 107 | Result result) = 0; |
| 108 | |
| 109 | // Called when a message has been received. |
| 110 | // |app_id|: application ID. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 111 | // |message|: message received. |
| 112 | virtual void OnMessageReceived(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 113 | const IncomingMessage& message) = 0; |
| 114 | |
| 115 | // Called when some messages have been deleted from the server. |
| 116 | // |app_id|: application ID. |
| 117 | virtual void OnMessagesDeleted(const std::string& app_id) = 0; |
| 118 | |
| 119 | // Called when a message failed to send to the server. |
| 120 | // |app_id|: application ID. |
| 121 | // |message_id|: ID of the message being sent. |
| 122 | // |result|: the type of the error if an error occured, success otherwise. |
| 123 | virtual void OnMessageSendError(const std::string& app_id, |
| 124 | const std::string& message_id, |
| 125 | Result result) = 0; |
| 126 | |
[email protected] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 127 | // Called when the GCM becomes ready. To get to this state, GCMClient |
| 128 | // finished loading from the GCM store and retrieved the device check-in |
| 129 | // from the server if it hadn't yet. |
| 130 | virtual void OnGCMReady() = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 131 | }; |
| 132 | |
[email protected] | 0db11822 | 2014-01-22 01:37:59 | [diff] [blame] | 133 | GCMClient(); |
| 134 | virtual ~GCMClient(); |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 135 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 136 | // Begins initialization of the GCM Client. This will not trigger a |
| 137 | // connection. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 138 | // |chrome_build_proto|: chrome info, i.e., version, channel and etc. |
| 139 | // |store_path|: path to the GCM store. |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 140 | // |account_ids|: account IDs to be related to the device when checking in. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 141 | // |blocking_task_runner|: for running blocking file tasks. |
| 142 | // |url_request_context_getter|: for url requests. |
| 143 | // |delegate|: the delegate whose methods will be called asynchronously in |
| 144 | // response to events and messages. |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 145 | virtual void Initialize( |
| 146 | const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 147 | const base::FilePath& store_path, |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 148 | const std::vector<std::string>& account_ids, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 149 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 150 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 151 | url_request_context_getter, |
| 152 | Delegate* delegate) = 0; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 153 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 154 | // Loads the data from the persistent store. This will automatically kick off |
| 155 | // the check-in if the check-in info is not found in the store. |
[email protected] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame^] | 156 | // TODO(jianli): consider renaming this name to Start. |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 157 | virtual void Load() = 0; |
| 158 | |
[email protected] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame^] | 159 | // Stops using the GCM service. This will not erase the persisted data. |
| 160 | virtual void Stop() = 0; |
| 161 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 162 | // Checks out of the GCM service. This will erase all the cached and persisted |
| 163 | // data. |
| 164 | virtual void CheckOut() = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 165 | |
| 166 | // Registers the application for GCM. Delegate::OnRegisterFinished will be |
| 167 | // called asynchronously upon completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 168 | // |app_id|: application ID. |
| 169 | // |cert|: SHA-1 of public key of the application, in base16 format. |
| 170 | // |sender_ids|: list of IDs of the servers that are allowed to send the |
| 171 | // messages to the application. These IDs are assigned by the |
| 172 | // Google API Console. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 173 | virtual void Register(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 174 | const std::string& cert, |
| 175 | const std::vector<std::string>& sender_ids) = 0; |
| 176 | |
| 177 | // Unregisters the application from GCM when it is uninstalled. |
| 178 | // Delegate::OnUnregisterFinished will be called asynchronously upon |
| 179 | // completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 180 | // |app_id|: application ID. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 181 | virtual void Unregister(const std::string& app_id) = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 182 | |
| 183 | // Sends a message to a given receiver. Delegate::OnSendFinished will be |
| 184 | // called asynchronously upon completion. |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 185 | // |app_id|: application ID. |
| 186 | // |receiver_id|: registration ID of the receiver party. |
| 187 | // |message|: message to be sent. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 188 | virtual void Send(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 189 | const std::string& receiver_id, |
| 190 | const OutgoingMessage& message) = 0; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace gcm |
| 194 | |
| 195 | #endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_ |