blob: 914bb83cbe05df4af14544946f82f8bd89477f8f [file] [log] [blame]
[email protected]e4097c82013-11-08 00:16:121// 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]e4097c82013-11-08 00:16:1215namespace 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.
19class GCM_EXPORT GCMClient {
20 public:
21 enum Result {
22 // Successful operation.
23 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5924 // 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]e4097c82013-11-08 00:16:1229 // 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]b16a7c52013-11-20 01:18:5940 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1241
42 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5943 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1244 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]b16a7c52013-11-20 01:18:5955 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1256 IncomingMessage();
57 ~IncomingMessage();
58
59 MessageData data;
60 };
61
62 // The check-in info for the user. Returned by the server.
[email protected]cb9c0002014-01-09 01:37:0363 struct GCM_EXPORT CheckinInfo {
64 CheckinInfo() : android_id(0), secret(0) {}
[email protected]e4097c82013-11-08 00:16:1265 bool IsValid() const { return android_id != 0 && secret != 0; }
[email protected]eb44a8a2013-11-26 19:55:5366 void Reset() {
67 android_id = 0;
68 secret = 0;
69 }
[email protected]e4097c82013-11-08 00:16:1270
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]cb9c0002014-01-09 01:37:0380 // |checkin_info|: valid if the CheckIn(..) completed successfully.
[email protected]e4097c82013-11-08 00:16:1281 // |result|: the type of the error if an error occured, success otherwise.
[email protected]cb9c0002014-01-09 01:37:0382 virtual void OnCheckInFinished(const CheckinInfo& checkin_info,
[email protected]e4097c82013-11-08 00:16:1283 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]e4097c82013-11-08 00:16:1293 // 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]e4097c82013-11-08 00:16:12104 // |message|: message received.
105 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12106 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]eb44a8a2013-11-26 19:55:53122 // OnCheckInFinished.
[email protected]cb9c0002014-01-09 01:37:03123 virtual CheckinInfo GetCheckinInfo() const = 0;
[email protected]e4097c82013-11-08 00:16:12124
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]e4097c82013-11-08 00:16:12128 };
129
[email protected]0db118222014-01-22 01:37:59130 GCMClient();
131 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04132
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]e4097c82013-11-08 00:16:12139
[email protected]2abca5b2013-12-10 05:41:56140 // 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]1b1c3cd2013-12-17 18:40:04143 virtual void CheckIn(const std::string& username) = 0;
[email protected]e4097c82013-11-08 00:16:12144
145 // Registers the application for GCM. Delegate::OnRegisterFinished will be
146 // called asynchronously upon completion.
[email protected]cb9c0002014-01-09 01:37:03147 // |username|: the username (email address) passed in CheckIn(..).
[email protected]e4097c82013-11-08 00:16:12148 // |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]cb9c0002014-01-09 01:37:03161 // |username|: the username (email address) passed in CheckIn(..).
[email protected]e4097c82013-11-08 00:16:12162 // |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]cb9c0002014-01-09 01:37:03168 // |username|: the username (email address) passed in CheckIn(..).
[email protected]e4097c82013-11-08 00:16:12169 // |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]e4097c82013-11-08 00:16:12179};
180
181} // namespace gcm
182
183#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_