[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_ |
| 6 | #define GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_ |
| 7 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 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" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 18 | #include "google_apis/gcm/gcm_client.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 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" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 22 | |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 23 | namespace base { |
| 24 | class FilePath; |
| 25 | class SequencedTaskRunner; |
| 26 | } // namespace base |
| 27 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 28 | namespace net { |
| 29 | class HttpNetworkSession; |
| 30 | class URLRequestContextGetter; |
| 31 | } |
| 32 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 33 | namespace gcm { |
| 34 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 35 | class CheckinRequest; |
| 36 | class ConnectionFactory; |
| 37 | class GCMClientImplTest; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 38 | class UserList; |
| 39 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 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. |
[email protected] | 0db11822 | 2014-01-22 01:37:59 | [diff] [blame] | 44 | class GCM_EXPORT GCMClientImpl : public GCMClient { |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 45 | public: |
| 46 | GCMClientImpl(); |
| 47 | virtual ~GCMClientImpl(); |
| 48 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 49 | // Begins initialization of the GCM Client. |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 50 | void Initialize( |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 51 | const checkin_proto::ChromeBuildProto& chrome_build_proto, |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 52 | const base::FilePath& path, |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 53 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, |
| 54 | const scoped_refptr<net::URLRequestContextGetter>& |
| 55 | url_request_context_getter); |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 56 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 57 | // Overridden from GCMClient: |
[email protected] | 1b1c3cd | 2013-12-17 18:40:04 | [diff] [blame] | 58 | virtual void SetUserDelegate(const std::string& username, |
| 59 | Delegate* delegate) OVERRIDE; |
| 60 | virtual void CheckIn(const std::string& username) OVERRIDE; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 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; |
| 72 | |
| 73 | private: |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 74 | // State representation of the GCMClient. |
| 75 | enum State { |
| 76 | // Uninitialized. |
| 77 | UNINITIALIZED, |
| 78 | // GCM store loading is in progress. |
| 79 | LOADING, |
| 80 | // Initial device checkin is in progress. |
| 81 | INITIAL_DEVICE_CHECKIN, |
| 82 | // Ready to accept requests. |
| 83 | READY, |
| 84 | }; |
| 85 | |
| 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; |
| 90 | |
| 91 | friend class GCMClientImplTest; |
| 92 | |
| 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. |
| 102 | void OnMCSError(); |
| 103 | |
| 104 | // Runs after GCM Store load is done to trigger continuation of the |
| 105 | // initialization. |
| 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. |
| 114 | void ResetState(); |
| 115 | |
| 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 |
| 118 | // checkin. |
| 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, |
| 125 | uint64 android_id, |
| 126 | uint64 security_token); |
| 127 | // Completes the checkin request for a device (serial number of 0). |
| 128 | void OnDeviceCheckinCompleted(const CheckinInfo& checkin_info); |
| 129 | |
| 130 | // Callback for persisting device credentials in the |gcm_store_|. |
| 131 | void SetDeviceCredentialsCallback(bool success); |
| 132 | |
| 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); |
| 137 | |
| 138 | // Handles incoming data message and dispatches it the a relevant user |
| 139 | // delegate. |
| 140 | void HandleIncomingMessage(const gcm::MCSMessage& message); |
| 141 | |
| 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); |
| 147 | |
| 148 | // State of the GCM Client Implementation. |
| 149 | State state_; |
| 150 | |
| 151 | // Device checkin info (android ID and security token used by device). |
| 152 | CheckinInfo device_checkin_info_; |
| 153 | |
| 154 | // Clock used for timing of retry logic. |
| 155 | base::DefaultClock clock_; |
| 156 | |
| 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_; |
| 160 | |
| 161 | // Persistent data store for keeping device credentials, messages and user to |
| 162 | // serial number mappings. |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 163 | scoped_ptr<GCMStore> gcm_store_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 164 | |
| 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. |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 167 | scoped_ptr<UserList> user_list_; |
| 168 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame^] | 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_; |
| 173 | |
| 174 | // Controls receiving and sending of packets and reliable message queueing. |
| 175 | scoped_ptr<MCSClient> mcs_client_; |
| 176 | |
| 177 | // Currently pending checkins. GCMClientImpl owns the CheckinRequests. |
| 178 | PendingCheckins pending_checkins_; |
| 179 | STLValueDeleter<PendingCheckins> pending_checkins_deleter_; |
| 180 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(GCMClientImpl); |
| 182 | }; |
| 183 | |
| 184 | } // namespace gcm |
| 185 | |
| 186 | #endif // GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_ |