[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> |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 10 | #include <vector> |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 11 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 16 | #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] | b4dd023 | 2014-02-08 02:37:31 | [diff] [blame] | 19 | #include "google_apis/gcm/engine/registration_request.h" |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 20 | #include "google_apis/gcm/gcm_client.h" |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 21 | #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] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 24 | |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 25 | namespace base { |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 26 | class Clock; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 27 | } // namespace base |
| 28 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 29 | namespace net { |
| 30 | class HttpNetworkSession; |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 31 | } // namespace net |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 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] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 38 | class UnregistrationRequest; |
[email protected] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 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 | |
| 49 | // Overridden from GCMClient: |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 50 | virtual void Initialize( |
| 51 | const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 52 | const base::FilePath& store_path, |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 53 | const std::vector<std::string>& account_ids, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 54 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 55 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 56 | url_request_context_getter, |
| 57 | Delegate* delegate) OVERRIDE; |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame^] | 58 | virtual void Load() OVERRIDE; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 59 | virtual void CheckOut() OVERRIDE; |
| 60 | virtual void Register(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 61 | const std::string& cert, |
| 62 | const std::vector<std::string>& sender_ids) OVERRIDE; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 63 | virtual void Unregister(const std::string& app_id) OVERRIDE; |
| 64 | virtual void Send(const std::string& app_id, |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 65 | const std::string& receiver_id, |
| 66 | const OutgoingMessage& message) OVERRIDE; |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 67 | |
| 68 | private: |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 69 | // State representation of the GCMClient. |
| 70 | enum State { |
| 71 | // Uninitialized. |
| 72 | UNINITIALIZED, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame^] | 73 | // Initialized, |
| 74 | INITIALIZED, |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 75 | // 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] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 83 | // 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] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 91 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 92 | uint64 android_id; |
| 93 | uint64 secret; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 96 | // Collection of pending registration requests. Keys are app IDs, while values |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 97 | // are pending registration requests to obtain a registration ID for |
| 98 | // requesting application. |
| 99 | typedef std::map<std::string, RegistrationRequest*> |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 100 | PendingRegistrations; |
| 101 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 102 | // 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] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 108 | 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] | 2809af7 | 2014-01-25 09:23:57 | [diff] [blame] | 123 | void OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 124 | // Initializes mcs_client_, which handles the connection to MCS. |
[email protected] | 2809af7 | 2014-01-25 09:23:57 | [diff] [blame] | 125 | void InitializeMCSClient(scoped_ptr<GCMStore::LoadResult> result); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 126 | // 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] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 132 | // Sets state to ready. This will initiate the MCS login and notify the |
| 133 | // delegates. |
| 134 | void OnReady(); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 135 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 136 | // Starts a first time device checkin. |
| 137 | void StartCheckin(const CheckinInfo& checkin_info); |
| 138 | // Completes the device checkin request. |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 139 | // |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] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 141 | void OnCheckinCompleted(uint64 android_id, |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 142 | uint64 security_token); |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 143 | |
| 144 | // Callback for persisting device credentials in the |gcm_store_|. |
| 145 | void SetDeviceCredentialsCallback(bool success); |
| 146 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 147 | // Completes the registration request. |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 148 | void OnRegisterCompleted(const std::string& app_id, |
[email protected] | b4dd023 | 2014-02-08 02:37:31 | [diff] [blame] | 149 | RegistrationRequest::Status status, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 150 | const std::string& registration_id); |
| 151 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 152 | // Completes the unregistration request. |
| 153 | void OnUnregisterCompleted(const std::string& app_id, bool status); |
| 154 | |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame^] | 155 | // Completes the GCM store destroy request. |
| 156 | void OnGCMStoreDestroyed(bool success); |
| 157 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 158 | // Handles incoming data message and dispatches it the a relevant user |
| 159 | // delegate. |
| 160 | void HandleIncomingMessage(const gcm::MCSMessage& message); |
| 161 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 162 | // 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] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 174 | |
| 175 | // State of the GCM Client Implementation. |
| 176 | State state_; |
| 177 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 178 | Delegate* delegate_; |
| 179 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 180 | // Device checkin info (android ID and security token used by device). |
| 181 | CheckinInfo device_checkin_info_; |
| 182 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 183 | // Clock used for timing of retry logic. Passed in for testing. Owned by |
| 184 | // GCMClientImpl. |
| 185 | scoped_ptr<base::Clock> clock_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 186 | |
| 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] | 79994f4 | 2014-01-16 16:05:36 | [diff] [blame] | 193 | scoped_ptr<GCMStore> gcm_store_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 194 | |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 195 | 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] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 203 | scoped_ptr<CheckinRequest> checkin_request_; |
[email protected] | 495a7db9 | 2014-02-22 07:49:59 | [diff] [blame] | 204 | std::vector<std::string> account_ids_; |
[email protected] | b83122a9 | 2014-01-22 21:29:29 | [diff] [blame] | 205 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 206 | // Currently pending registrations. GCMClientImpl owns the |
| 207 | // RegistrationRequests. |
| 208 | PendingRegistrations pending_registrations_; |
| 209 | STLValueDeleter<PendingRegistrations> pending_registrations_deleter_; |
| 210 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 211 | // Currently pending unregistrations. GCMClientImpl owns the |
| 212 | // UnregistrationRequests. |
| 213 | PendingUnregistrations pending_unregistrations_; |
| 214 | STLValueDeleter<PendingUnregistrations> pending_unregistrations_deleter_; |
| 215 | |
[email protected] | 955e0ff | 2014-01-31 20:42:12 | [diff] [blame] | 216 | // Factory for creating references in callbacks. |
| 217 | base::WeakPtrFactory<GCMClientImpl> weak_ptr_factory_; |
| 218 | |
[email protected] | e4097c8 | 2013-11-08 00:16:12 | [diff] [blame] | 219 | DISALLOW_COPY_AND_ASSIGN(GCMClientImpl); |
| 220 | }; |
| 221 | |
| 222 | } // namespace gcm |
| 223 | |
| 224 | #endif // GOOGLE_APIS_GCM_GCM_CLIENT_IMPL_H_ |