blob: 72b86bab65f49ba9162472ec4f1e57395f802479 [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_IMPL_H_
6#define GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_
7
[email protected]b83122a92014-01-22 21:29:298#include <map>
9#include <string>
[email protected]848b1b62014-01-30 23:51:0410#include <vector>
[email protected]b83122a92014-01-22 21:29:2911
[email protected]e4097c82013-11-08 00:16:1212#include "base/compiler_specific.h"
[email protected]79994f42014-01-16 16:05:3613#include "base/memory/ref_counted.h"
[email protected]955e0ff2014-01-31 20:42:1214#include "base/memory/weak_ptr.h"
[email protected]b83122a92014-01-22 21:29:2915#include "base/stl_util.h"
[email protected]b83122a92014-01-22 21:29:2916#include "google_apis/gcm/base/mcs_message.h"
17#include "google_apis/gcm/engine/gcm_store.h"
18#include "google_apis/gcm/engine/mcs_client.h"
[email protected]b4dd0232014-02-08 02:37:3119#include "google_apis/gcm/engine/registration_request.h"
[email protected]e4097c82013-11-08 00:16:1220#include "google_apis/gcm/gcm_client.h"
[email protected]b83122a92014-01-22 21:29:2921#include "google_apis/gcm/protocol/android_checkin.pb.h"
22#include "net/base/net_log.h"
23#include "net/url_request/url_request_context_getter.h"
[email protected]e4097c82013-11-08 00:16:1224
[email protected]79994f42014-01-16 16:05:3625namespace base {
[email protected]955e0ff2014-01-31 20:42:1226class Clock;
[email protected]79994f42014-01-16 16:05:3627} // namespace base
28
[email protected]b83122a92014-01-22 21:29:2929namespace net {
30class HttpNetworkSession;
[email protected]955e0ff2014-01-31 20:42:1231} // namespace net
[email protected]b83122a92014-01-22 21:29:2932
[email protected]e4097c82013-11-08 00:16:1233namespace gcm {
34
[email protected]b83122a92014-01-22 21:29:2935class CheckinRequest;
36class ConnectionFactory;
37class GCMClientImplTest;
[email protected]e4007042014-02-15 20:34:2838class UnregistrationRequest;
[email protected]79994f42014-01-16 16:05:3639
[email protected]b83122a92014-01-22 21:29:2940// Implements the GCM Client. It is used to coordinate MCS Client (communication
41// with MCS) and other pieces of GCM infrastructure like Registration and
42// Checkins. It also allows for registering user delegates that host
43// applications that send and receive messages.
[email protected]0db118222014-01-22 01:37:5944class GCM_EXPORT GCMClientImpl : public GCMClient {
[email protected]e4097c82013-11-08 00:16:1245 public:
46 GCMClientImpl();
47 virtual ~GCMClientImpl();
48
49 // Overridden from GCMClient:
[email protected]e2a4a8012014-02-07 22:32:5250 virtual void Initialize(
51 const checkin_proto::ChromeBuildProto& chrome_build_proto,
52 const base::FilePath& store_path,
[email protected]495a7db92014-02-22 07:49:5953 const std::vector<std::string>& account_ids,
[email protected]e2a4a8012014-02-07 22:32:5254 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
55 const scoped_refptr<net::URLRequestContextGetter>&
[email protected]5799d052014-02-12 20:47:3956 url_request_context_getter,
57 Delegate* delegate) OVERRIDE;
[email protected]d3a4b2e2014-02-27 13:46:5458 virtual void Load() OVERRIDE;
[email protected]5799d052014-02-12 20:47:3959 virtual void CheckOut() OVERRIDE;
60 virtual void Register(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:1261 const std::string& cert,
62 const std::vector<std::string>& sender_ids) OVERRIDE;
[email protected]5799d052014-02-12 20:47:3963 virtual void Unregister(const std::string& app_id) OVERRIDE;
64 virtual void Send(const std::string& app_id,
[email protected]e4097c82013-11-08 00:16:1265 const std::string& receiver_id,
66 const OutgoingMessage& message) OVERRIDE;
[email protected]e4097c82013-11-08 00:16:1267
68 private:
[email protected]b83122a92014-01-22 21:29:2969 // State representation of the GCMClient.
70 enum State {
71 // Uninitialized.
72 UNINITIALIZED,
[email protected]d3a4b2e2014-02-27 13:46:5473 // Initialized,
74 INITIALIZED,
[email protected]b83122a92014-01-22 21:29:2975 // GCM store loading is in progress.
76 LOADING,
77 // Initial device checkin is in progress.
78 INITIAL_DEVICE_CHECKIN,
79 // Ready to accept requests.
80 READY,
81 };
82
[email protected]5799d052014-02-12 20:47:3983 // The check-in info for the user. Returned by the server.
84 struct GCM_EXPORT CheckinInfo {
85 CheckinInfo() : android_id(0), secret(0) {}
86 bool IsValid() const { return android_id != 0 && secret != 0; }
87 void Reset() {
88 android_id = 0;
89 secret = 0;
90 }
[email protected]b83122a92014-01-22 21:29:2991
[email protected]5799d052014-02-12 20:47:3992 uint64 android_id;
93 uint64 secret;
[email protected]848b1b62014-01-30 23:51:0494 };
95
[email protected]e4007042014-02-15 20:34:2896 // Collection of pending registration requests. Keys are app IDs, while values
[email protected]5799d052014-02-12 20:47:3997 // are pending registration requests to obtain a registration ID for
98 // requesting application.
99 typedef std::map<std::string, RegistrationRequest*>
[email protected]848b1b62014-01-30 23:51:04100 PendingRegistrations;
101
[email protected]e4007042014-02-15 20:34:28102 // Collection of pending unregistration requests. Keys are app IDs, while
103 // values are pending unregistration requests to disable the registration ID
104 // currently assigned to the application.
105 typedef std::map<std::string, UnregistrationRequest*>
106 PendingUnregistrations;
107
[email protected]b83122a92014-01-22 21:29:29108 friend class GCMClientImplTest;
109
110 // Callbacks for the MCSClient.
111 // Receives messages and dispatches them to relevant user delegates.
112 void OnMessageReceivedFromMCS(const gcm::MCSMessage& message);
113 // Receives confirmation of sent messages or information about errors.
114 void OnMessageSentToMCS(int64 user_serial_number,
115 const std::string& app_id,
116 const std::string& message_id,
117 MCSClient::MessageSendStatus status);
118 // Receives information about mcs_client_ errors.
119 void OnMCSError();
120
121 // Runs after GCM Store load is done to trigger continuation of the
122 // initialization.
[email protected]2809af72014-01-25 09:23:57123 void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29124 // Initializes mcs_client_, which handles the connection to MCS.
[email protected]2809af72014-01-25 09:23:57125 void InitializeMCSClient(scoped_ptr<GCMStore::LoadResult> result);
[email protected]b83122a92014-01-22 21:29:29126 // Complets the first time device checkin.
127 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo& checkin_info);
128 // Starts a login on mcs_client_.
129 void StartMCSLogin();
130 // Resets state to before initialization.
131 void ResetState();
[email protected]86625df2014-01-31 03:47:58132 // Sets state to ready. This will initiate the MCS login and notify the
133 // delegates.
134 void OnReady();
[email protected]b83122a92014-01-22 21:29:29135
[email protected]5799d052014-02-12 20:47:39136 // Starts a first time device checkin.
137 void StartCheckin(const CheckinInfo& checkin_info);
138 // Completes the device checkin request.
[email protected]b83122a92014-01-22 21:29:29139 // |android_id| and |security_token| are expected to be non-zero or an error
140 // is triggered. Function also cleans up the pending checkin.
[email protected]5799d052014-02-12 20:47:39141 void OnCheckinCompleted(uint64 android_id,
[email protected]b83122a92014-01-22 21:29:29142 uint64 security_token);
[email protected]b83122a92014-01-22 21:29:29143
144 // Callback for persisting device credentials in the |gcm_store_|.
145 void SetDeviceCredentialsCallback(bool success);
146
[email protected]848b1b62014-01-30 23:51:04147 // Completes the registration request.
[email protected]5799d052014-02-12 20:47:39148 void OnRegisterCompleted(const std::string& app_id,
[email protected]b4dd0232014-02-08 02:37:31149 RegistrationRequest::Status status,
[email protected]848b1b62014-01-30 23:51:04150 const std::string& registration_id);
151
[email protected]e4007042014-02-15 20:34:28152 // Completes the unregistration request.
153 void OnUnregisterCompleted(const std::string& app_id, bool status);
154
[email protected]d3a4b2e2014-02-27 13:46:54155 // Completes the GCM store destroy request.
156 void OnGCMStoreDestroyed(bool success);
157
[email protected]b83122a92014-01-22 21:29:29158 // Handles incoming data message and dispatches it the a relevant user
159 // delegate.
160 void HandleIncomingMessage(const gcm::MCSMessage& message);
161
[email protected]848b1b62014-01-30 23:51:04162 // Fires OnMessageSendError event on |delegate|, with specified |app_id| and
163 // message ID obtained from |incoming_message| if one is available.
164 void NotifyDelegateOnMessageSendError(
165 GCMClient::Delegate* delegate,
166 const std::string& app_id,
167 const IncomingMessage& incoming_message);
168
169 // For testing purpose only.
170 // Sets an |mcs_client_| for testing. Takes the ownership of |mcs_client|.
171 // TODO(fgorski): Remove this method. Create GCMEngineFactory that will create
172 // components of the engine.
173 void SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client);
[email protected]b83122a92014-01-22 21:29:29174
175 // State of the GCM Client Implementation.
176 State state_;
177
[email protected]5799d052014-02-12 20:47:39178 Delegate* delegate_;
179
[email protected]b83122a92014-01-22 21:29:29180 // Device checkin info (android ID and security token used by device).
181 CheckinInfo device_checkin_info_;
182
[email protected]848b1b62014-01-30 23:51:04183 // Clock used for timing of retry logic. Passed in for testing. Owned by
184 // GCMClientImpl.
185 scoped_ptr<base::Clock> clock_;
[email protected]b83122a92014-01-22 21:29:29186
187 // Information about the chrome build.
188 // TODO(fgorski): Check if it can be passed in constructor and made const.
189 checkin_proto::ChromeBuildProto chrome_build_proto_;
190
191 // Persistent data store for keeping device credentials, messages and user to
192 // serial number mappings.
[email protected]79994f42014-01-16 16:05:36193 scoped_ptr<GCMStore> gcm_store_;
[email protected]b83122a92014-01-22 21:29:29194
[email protected]b83122a92014-01-22 21:29:29195 scoped_refptr<net::HttpNetworkSession> network_session_;
196 net::BoundNetLog net_log_;
197 scoped_ptr<ConnectionFactory> connection_factory_;
198 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
199
200 // Controls receiving and sending of packets and reliable message queueing.
201 scoped_ptr<MCSClient> mcs_client_;
202
[email protected]5799d052014-02-12 20:47:39203 scoped_ptr<CheckinRequest> checkin_request_;
[email protected]495a7db92014-02-22 07:49:59204 std::vector<std::string> account_ids_;
[email protected]b83122a92014-01-22 21:29:29205
[email protected]848b1b62014-01-30 23:51:04206 // Currently pending registrations. GCMClientImpl owns the
207 // RegistrationRequests.
208 PendingRegistrations pending_registrations_;
209 STLValueDeleter<PendingRegistrations> pending_registrations_deleter_;
210
[email protected]e4007042014-02-15 20:34:28211 // Currently pending unregistrations. GCMClientImpl owns the
212 // UnregistrationRequests.
213 PendingUnregistrations pending_unregistrations_;
214 STLValueDeleter<PendingUnregistrations> pending_unregistrations_deleter_;
215
[email protected]955e0ff2014-01-31 20:42:12216 // Factory for creating references in callbacks.
217 base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_;
218
[email protected]e4097c82013-11-08 00:16:12219 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl);
220};
221
222} // namespace gcm
223
224#endif // GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_