blob: 9280c68914b4564b5ba9b32b112570e24f8ccd01 [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]e2a4a8012014-02-07 22:32:5215template <class T> class scoped_refptr;
16
17namespace base {
18class FilePath;
19class SequencedTaskRunner;
20}
21
22namespace checkin_proto {
23class ChromeBuildProto;
24}
25
26namespace net {
27class URLRequestContextGetter;
28}
29
[email protected]e4097c82013-11-08 00:16:1230namespace 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.
34class GCM_EXPORT GCMClient {
35 public:
36 enum Result {
37 // Successful operation.
38 SUCCESS,
[email protected]b16a7c52013-11-20 01:18:5939 // Invalid parameter.
40 INVALID_PARAMETER,
[email protected]c7f7b532014-01-24 07:24:4541 // Profile not signed in.
42 NOT_SIGNED_IN,
[email protected]8c91586b42014-02-15 05:22:5143 // Certificate was missing. Certain operation, like register, requires it.
44 CERTIFICATE_MISSING,
[email protected]b16a7c52013-11-20 01:18:5945 // 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]e4097c82013-11-08 00:16:1248 // 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]b16a7c52013-11-20 01:18:5959 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1260
61 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5962 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1263 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]b16a7c52013-11-20 01:18:5974 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1275 IncomingMessage();
76 ~IncomingMessage();
77
78 MessageData data;
79 };
80
[email protected]e4097c82013-11-08 00:16:1281 // A delegate interface that allows the GCMClient instance to interact with
82 // its caller, i.e. notifying asynchronous event.
83 class Delegate {
84 public:
[email protected]e4097c82013-11-08 00:16:1285 // 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]e4007042014-02-15 20:34:2893 // Called when the unregistration completed.
94 // |app_id|: application ID.
95 // |success|: indicates whether unregistration request was successful.
96 virtual void OnUnregisterFinished(const std::string& app_id,
97 bool success) = 0;
98
[email protected]e4097c82013-11-08 00:16:1299 // 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]e4097c82013-11-08 00:16:12110 // |message|: message received.
111 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12112 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
[email protected]86625df2014-01-31 03:47:58126 // Called when the GCM becomes ready. To get to this state, GCMClient
127 // finished loading from the GCM store and retrieved the device check-in
128 // from the server if it hadn't yet.
129 virtual void OnGCMReady() = 0;
[email protected]e4097c82013-11-08 00:16:12130 };
131
[email protected]0db118222014-01-22 01:37:59132 GCMClient();
133 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04134
[email protected]e2a4a8012014-02-07 22:32:52135 // Begins initialization of the GCM Client.
[email protected]5799d052014-02-12 20:47:39136 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
137 // |store_path|: path to the GCM store.
[email protected]495a7db92014-02-22 07:49:59138 // |account_ids|: account IDs to be related to the device when checking in.
[email protected]5799d052014-02-12 20:47:39139 // |blocking_task_runner|: for running blocking file tasks.
140 // |url_request_context_getter|: for url requests.
141 // |delegate|: the delegate whose methods will be called asynchronously in
142 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52143 virtual void Initialize(
144 const checkin_proto::ChromeBuildProto& chrome_build_proto,
145 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:59146 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:52147 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
148 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39149 url_request_context_getter,
150 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52151
[email protected]5799d052014-02-12 20:47:39152 // Checks out of the GCM service. This will erase all the cached and persisted
153 // data.
154 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12155
156 // Registers the application for GCM. Delegate::OnRegisterFinished will be
157 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12158 // |app_id|: application ID.
159 // |cert|: SHA-1 of public key of the application, in base16 format.
160 // |sender_ids|: list of IDs of the servers that are allowed to send the
161 // messages to the application. These IDs are assigned by the
162 // Google API Console.
[email protected]5799d052014-02-12 20:47:39163 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12164 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.
[email protected]e4097c82013-11-08 00:16:12170 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39171 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12172
173 // Sends a message to a given receiver. Delegate::OnSendFinished will be
174 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12175 // |app_id|: application ID.
176 // |receiver_id|: registration ID of the receiver party.
177 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39178 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12179 const std::string& receiver_id,
180 const OutgoingMessage& message) = 0;
181
[email protected]86625df2014-01-31 03:47:58182 // Returns true if GCM becomes ready.
183 virtual bool IsReady() const = 0;
[email protected]e4097c82013-11-08 00:16:12184};
185
186} // namespace gcm
187
188#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_