blob: 02a243b493e7189524c24b07d6f8e94832a4611e [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]b16a7c52013-11-20 01:18:5943 // Previous asynchronous operation is still pending to finish. Certain
44 // operation, like register, is only allowed one at a time.
45 ASYNC_OPERATION_PENDING,
[email protected]e4097c82013-11-08 00:16:1246 // Network socket error.
47 NETWORK_ERROR,
48 // Problem at the server.
49 SERVER_ERROR,
50 // Exceeded the specified TTL during message sending.
51 TTL_EXCEEDED,
52 // Other errors.
53 UNKNOWN_ERROR
54 };
55
56 // Message data consisting of key-value pairs.
[email protected]b16a7c52013-11-20 01:18:5957 typedef std::map<std::string, std::string> MessageData;
[email protected]e4097c82013-11-08 00:16:1258
59 // Message to be delivered to the other party.
[email protected]b16a7c52013-11-20 01:18:5960 struct GCM_EXPORT OutgoingMessage {
[email protected]e4097c82013-11-08 00:16:1261 OutgoingMessage();
62 ~OutgoingMessage();
63
64 // Message ID.
65 std::string id;
66 // In seconds.
67 int time_to_live;
68 MessageData data;
69 };
70
71 // Message being received from the other party.
[email protected]b16a7c52013-11-20 01:18:5972 struct GCM_EXPORT IncomingMessage {
[email protected]e4097c82013-11-08 00:16:1273 IncomingMessage();
74 ~IncomingMessage();
75
76 MessageData data;
77 };
78
[email protected]e4097c82013-11-08 00:16:1279 // A delegate interface that allows the GCMClient instance to interact with
80 // its caller, i.e. notifying asynchronous event.
81 class Delegate {
82 public:
[email protected]e4097c82013-11-08 00:16:1283 // Called when the registration completed successfully or an error occurs.
84 // |app_id|: application ID.
85 // |registration_id|: non-empty if the registration completed successfully.
86 // |result|: the type of the error if an error occured, success otherwise.
87 virtual void OnRegisterFinished(const std::string& app_id,
88 const std::string& registration_id,
89 Result result) = 0;
90
[email protected]e4097c82013-11-08 00:16:1291 // Called when the message is scheduled to send successfully or an error
92 // occurs.
93 // |app_id|: application ID.
94 // |message_id|: ID of the message being sent.
95 // |result|: the type of the error if an error occured, success otherwise.
96 virtual void OnSendFinished(const std::string& app_id,
97 const std::string& message_id,
98 Result result) = 0;
99
100 // Called when a message has been received.
101 // |app_id|: application ID.
[email protected]e4097c82013-11-08 00:16:12102 // |message|: message received.
103 virtual void OnMessageReceived(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12104 const IncomingMessage& message) = 0;
105
106 // Called when some messages have been deleted from the server.
107 // |app_id|: application ID.
108 virtual void OnMessagesDeleted(const std::string& app_id) = 0;
109
110 // Called when a message failed to send to the server.
111 // |app_id|: application ID.
112 // |message_id|: ID of the message being sent.
113 // |result|: the type of the error if an error occured, success otherwise.
114 virtual void OnMessageSendError(const std::string& app_id,
115 const std::string& message_id,
116 Result result) = 0;
117
[email protected]86625df2014-01-31 03:47:58118 // Called when the GCM becomes ready. To get to this state, GCMClient
119 // finished loading from the GCM store and retrieved the device check-in
120 // from the server if it hadn't yet.
121 virtual void OnGCMReady() = 0;
[email protected]e4097c82013-11-08 00:16:12122 };
123
[email protected]0db118222014-01-22 01:37:59124 GCMClient();
125 virtual ~GCMClient();
[email protected]1b1c3cd2013-12-17 18:40:04126
[email protected]e2a4a8012014-02-07 22:32:52127 // Begins initialization of the GCM Client.
[email protected]5799d052014-02-12 20:47:39128 // |chrome_build_proto|: chrome info, i.e., version, channel and etc.
129 // |store_path|: path to the GCM store.
130 // |blocking_task_runner|: for running blocking file tasks.
131 // |url_request_context_getter|: for url requests.
132 // |delegate|: the delegate whose methods will be called asynchronously in
133 // response to events and messages.
[email protected]e2a4a8012014-02-07 22:32:52134 virtual void Initialize(
135 const checkin_proto::ChromeBuildProto& chrome_build_proto,
136 const base::FilePath& store_path,
137 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
138 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:39139 url_request_context_getter,
140 Delegate* delegate) = 0;
[email protected]e2a4a8012014-02-07 22:32:52141
[email protected]5799d052014-02-12 20:47:39142 // Checks out of the GCM service. This will erase all the cached and persisted
143 // data.
144 virtual void CheckOut() = 0;
[email protected]e4097c82013-11-08 00:16:12145
146 // Registers the application for GCM. Delegate::OnRegisterFinished will be
147 // called asynchronously upon completion.
[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.
[email protected]5799d052014-02-12 20:47:39153 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12154 const std::string& cert,
155 const std::vector<std::string>& sender_ids) = 0;
156
157 // Unregisters the application from GCM when it is uninstalled.
158 // Delegate::OnUnregisterFinished will be called asynchronously upon
159 // completion.
[email protected]e4097c82013-11-08 00:16:12160 // |app_id|: application ID.
[email protected]5799d052014-02-12 20:47:39161 virtual void Unregister(const std::string& app_id) = 0;
[email protected]e4097c82013-11-08 00:16:12162
163 // Sends a message to a given receiver. Delegate::OnSendFinished will be
164 // called asynchronously upon completion.
[email protected]e4097c82013-11-08 00:16:12165 // |app_id|: application ID.
166 // |receiver_id|: registration ID of the receiver party.
167 // |message|: message to be sent.
[email protected]5799d052014-02-12 20:47:39168 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:12169 const std::string& receiver_id,
170 const OutgoingMessage& message) = 0;
171
[email protected]86625df2014-01-31 03:47:58172 // Returns true if GCM becomes ready.
173 virtual bool IsReady() const = 0;
[email protected]e4097c82013-11-08 00:16:12174};
175
176} // namespace gcm
177
178#endif // GOOGLE_APIS_GCM_GCM_CLIENT_H_