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.
5 #ifndef GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_
6 #define GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/stl_util.h"
14 #include "base/time/default_clock.h"
15 #include "google_apis/gcm/base/mcs_message.h"
16 #include "google_apis/gcm/engine/gcm_store.h"
17 #include "google_apis/gcm/engine/mcs_client.h"
18 #include "google_apis/gcm/gcm_client.h"
19 #include "google_apis/gcm/protocol/android_checkin.pb.h"
20 #include "net/base/net_log.h"
21 #include "net/url_request/url_request_context_getter.h"
25 class SequencedTaskRunner
;
29 class HttpNetworkSession
;
30 class URLRequestContextGetter
;
36 class ConnectionFactory
;
37 class GCMClientImplTest
;
40 // 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.
44 class GCM_EXPORT GCMClientImpl
: public GCMClient
{
47 virtual ~GCMClientImpl();
49 // Begins initialization of the GCM Client.
51 const checkin_proto::ChromeBuildProto
& chrome_build_proto
,
52 const base::FilePath
& path
,
53 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
,
54 const scoped_refptr
<net::URLRequestContextGetter
>&
55 url_request_context_getter
);
57 // Overridden from GCMClient:
58 virtual void SetUserDelegate(const std::string
& username
,
59 Delegate
* delegate
) OVERRIDE
;
60 virtual void CheckIn(const std::string
& username
) OVERRIDE
;
61 virtual void Register(const std::string
& username
,
62 const std::string
& app_id
,
63 const std::string
& cert
,
64 const std::vector
<std::string
>& sender_ids
) OVERRIDE
;
65 virtual void Unregister(const std::string
& username
,
66 const std::string
& app_id
) OVERRIDE
;
67 virtual void Send(const std::string
& username
,
68 const std::string
& app_id
,
69 const std::string
& receiver_id
,
70 const OutgoingMessage
& message
) OVERRIDE
;
71 virtual bool IsLoading() const OVERRIDE
;
74 // State representation of the GCMClient.
78 // GCM store loading is in progress.
80 // Initial device checkin is in progress.
81 INITIAL_DEVICE_CHECKIN
,
82 // Ready to accept requests.
86 // Collection of pending checkin requests. Keys are serial numbers of the
87 // users as assigned by the user_list_. Values are pending checkin requests to
88 // obtain android IDs and security tokens for the users.
89 typedef std::map
<int64
, CheckinRequest
*> PendingCheckins
;
91 friend class GCMClientImplTest
;
93 // Callbacks for the MCSClient.
94 // Receives messages and dispatches them to relevant user delegates.
95 void OnMessageReceivedFromMCS(const gcm::MCSMessage
& message
);
96 // Receives confirmation of sent messages or information about errors.
97 void OnMessageSentToMCS(int64 user_serial_number
,
98 const std::string
& app_id
,
99 const std::string
& message_id
,
100 MCSClient::MessageSendStatus status
);
101 // Receives information about mcs_client_ errors.
104 // Runs after GCM Store load is done to trigger continuation of the
106 void OnLoadCompleted(const GCMStore::LoadResult
& result
);
107 // Initializes mcs_client_, which handles the connection to MCS.
108 void InitializeMCSClient(const GCMStore::LoadResult
& result
);
109 // Complets the first time device checkin.
110 void OnFirstTimeDeviceCheckinCompleted(const CheckinInfo
& checkin_info
);
111 // Starts a login on mcs_client_.
112 void StartMCSLogin();
113 // Resets state to before initialization.
116 // Startes a checkin request for a user with specified |serial_number|.
117 // Checkin info can be invalid, in which case it is considered a first time
119 void StartCheckin(int64 user_serial_number
,
120 const CheckinInfo
& checkin_info
);
121 // Completes the checkin request for the specified |serial_number|.
122 // |android_id| and |security_token| are expected to be non-zero or an error
123 // is triggered. Function also cleans up the pending checkin.
124 void OnCheckinCompleted(int64 user_serial_number
,
126 uint64 security_token
);
127 // Completes the checkin request for a device (serial number of 0).
128 void OnDeviceCheckinCompleted(const CheckinInfo
& checkin_info
);
130 // Callback for persisting device credentials in the |gcm_store_|.
131 void SetDeviceCredentialsCallback(bool success
);
133 // Callback for setting a delegate on a |user_list_|. Informs that the
134 // delegate with matching |username| was assigned a |user_serial_number|.
135 void SetDelegateCompleted(const std::string
& username
,
136 int64 user_serial_number
);
138 // Handles incoming data message and dispatches it the a relevant user
140 void HandleIncomingMessage(const gcm::MCSMessage
& message
);
142 // Fires OnMessageReceived event on |delegate|, with specified |app_id| and
143 // |incoming_message|.
144 void NotifyDelegateOnMessageReceived(GCMClient::Delegate
* delegate
,
145 const std::string
& app_id
,
146 const IncomingMessage
& incoming_message
);
148 // State of the GCM Client Implementation.
151 // Device checkin info (android ID and security token used by device).
152 CheckinInfo device_checkin_info_
;
154 // Clock used for timing of retry logic.
155 base::DefaultClock clock_
;
157 // Information about the chrome build.
158 // TODO(fgorski): Check if it can be passed in constructor and made const.
159 checkin_proto::ChromeBuildProto chrome_build_proto_
;
161 // Persistent data store for keeping device credentials, messages and user to
162 // serial number mappings.
163 scoped_ptr
<GCMStore
> gcm_store_
;
165 // Keeps the mappings of user's serial numbers and assigns new serial numbers
166 // once a user delegate is added for the first time.
167 scoped_ptr
<UserList
> user_list_
;
169 scoped_refptr
<net::HttpNetworkSession
> network_session_
;
170 net::BoundNetLog net_log_
;
171 scoped_ptr
<ConnectionFactory
> connection_factory_
;
172 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
174 // Controls receiving and sending of packets and reliable message queueing.
175 scoped_ptr
<MCSClient
> mcs_client_
;
177 // Currently pending checkins. GCMClientImpl owns the CheckinRequests.
178 PendingCheckins pending_checkins_
;
179 STLValueDeleter
<PendingCheckins
> pending_checkins_deleter_
;
181 DISALLOW_COPY_AND_ASSIGN(GCMClientImpl
);
186 #endif // GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_