| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "google_apis/gcm/gcm_client_impl.h" | 5 #include "google_apis/gcm/gcm_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram.h" |
| 12 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 13 #include "base/time/default_clock.h" | 14 #include "base/time/default_clock.h" |
| 14 #include "google_apis/gcm/base/mcs_message.h" | 15 #include "google_apis/gcm/base/mcs_message.h" |
| 15 #include "google_apis/gcm/base/mcs_util.h" | 16 #include "google_apis/gcm/base/mcs_util.h" |
| 16 #include "google_apis/gcm/engine/checkin_request.h" | 17 #include "google_apis/gcm/engine/checkin_request.h" |
| 17 #include "google_apis/gcm/engine/connection_factory_impl.h" | 18 #include "google_apis/gcm/engine/connection_factory_impl.h" |
| 18 #include "google_apis/gcm/engine/gcm_store_impl.h" | 19 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 19 #include "google_apis/gcm/engine/mcs_client.h" | 20 #include "google_apis/gcm/engine/mcs_client.h" |
| 20 #include "google_apis/gcm/engine/registration_request.h" | 21 #include "google_apis/gcm/engine/registration_request.h" |
| 21 #include "google_apis/gcm/engine/unregistration_request.h" | 22 #include "google_apis/gcm/engine/unregistration_request.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 Delegate* delegate) { | 134 Delegate* delegate) { |
| 134 DCHECK_EQ(UNINITIALIZED, state_); | 135 DCHECK_EQ(UNINITIALIZED, state_); |
| 135 DCHECK(url_request_context_getter); | 136 DCHECK(url_request_context_getter); |
| 136 DCHECK(delegate); | 137 DCHECK(delegate); |
| 137 | 138 |
| 138 chrome_build_proto_.CopyFrom(chrome_build_proto); | 139 chrome_build_proto_.CopyFrom(chrome_build_proto); |
| 139 url_request_context_getter_ = url_request_context_getter; | 140 url_request_context_getter_ = url_request_context_getter; |
| 140 account_ids_ = account_ids; | 141 account_ids_ = account_ids; |
| 141 | 142 |
| 142 gcm_store_.reset(new GCMStoreImpl(false, path, blocking_task_runner)); | 143 gcm_store_.reset(new GCMStoreImpl(false, path, blocking_task_runner)); |
| 143 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted, | |
| 144 weak_ptr_factory_.GetWeakPtr())); | |
| 145 | 144 |
| 146 delegate_ = delegate; | 145 delegate_ = delegate; |
| 147 | 146 |
| 148 // |mcs_client_| might already be set for testing at this point. No need to | 147 state_ = INITIALIZED; |
| 149 // create a |connection_factory_|. | 148 } |
| 150 if (!mcs_client_.get()) { | |
| 151 const net::HttpNetworkSession::Params* network_session_params = | |
| 152 url_request_context_getter->GetURLRequestContext()-> | |
| 153 GetNetworkSessionParams(); | |
| 154 DCHECK(network_session_params); | |
| 155 network_session_ = new net::HttpNetworkSession(*network_session_params); | |
| 156 connection_factory_.reset(new ConnectionFactoryImpl( | |
| 157 GURL(kMCSEndpoint), | |
| 158 kDefaultBackoffPolicy, | |
| 159 network_session_, | |
| 160 net_log_.net_log())); | |
| 161 mcs_client_.reset(new MCSClient(chrome_build_proto.chrome_version(), | |
| 162 clock_.get(), | |
| 163 connection_factory_.get(), | |
| 164 gcm_store_.get())); | |
| 165 } | |
| 166 | 149 |
| 150 void GCMClientImpl::Load() { |
| 151 DCHECK_EQ(INITIALIZED, state_); |
| 152 |
| 153 // Once the loading is completed, the check-in will be initiated. |
| 154 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted, |
| 155 weak_ptr_factory_.GetWeakPtr())); |
| 167 state_ = LOADING; | 156 state_ = LOADING; |
| 168 } | 157 } |
| 169 | 158 |
| 170 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { | 159 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
| 171 DCHECK_EQ(LOADING, state_); | 160 DCHECK_EQ(LOADING, state_); |
| 172 | 161 |
| 173 if (!result->success) { | 162 if (!result->success) { |
| 174 ResetState(); | 163 ResetState(); |
| 175 return; | 164 return; |
| 176 } | 165 } |
| 177 | 166 |
| 178 device_checkin_info_.android_id = result->device_android_id; | 167 device_checkin_info_.android_id = result->device_android_id; |
| 179 device_checkin_info_.secret = result->device_security_token; | 168 device_checkin_info_.secret = result->device_security_token; |
| 180 InitializeMCSClient(result.Pass()); | 169 InitializeMCSClient(result.Pass()); |
| 181 if (!device_checkin_info_.IsValid()) { | 170 if (!device_checkin_info_.IsValid()) { |
| 182 device_checkin_info_.Reset(); | 171 device_checkin_info_.Reset(); |
| 183 state_ = INITIAL_DEVICE_CHECKIN; | 172 state_ = INITIAL_DEVICE_CHECKIN; |
| 184 StartCheckin(device_checkin_info_); | 173 StartCheckin(device_checkin_info_); |
| 185 return; | 174 return; |
| 186 } | 175 } |
| 187 | 176 |
| 188 OnReady(); | 177 OnReady(); |
| 189 } | 178 } |
| 190 | 179 |
| 191 void GCMClientImpl::InitializeMCSClient( | 180 void GCMClientImpl::InitializeMCSClient( |
| 192 scoped_ptr<GCMStore::LoadResult> result) { | 181 scoped_ptr<GCMStore::LoadResult> result) { |
| 182 // |mcs_client_| might already be set for testing at this point. No need to |
| 183 // create a |connection_factory_|. |
| 184 if (!mcs_client_.get()) { |
| 185 const net::HttpNetworkSession::Params* network_session_params = |
| 186 url_request_context_getter_->GetURLRequestContext()-> |
| 187 GetNetworkSessionParams(); |
| 188 DCHECK(network_session_params); |
| 189 network_session_ = new net::HttpNetworkSession(*network_session_params); |
| 190 connection_factory_.reset(new ConnectionFactoryImpl( |
| 191 GURL(kMCSEndpoint), |
| 192 kDefaultBackoffPolicy, |
| 193 network_session_, |
| 194 net_log_.net_log())); |
| 195 mcs_client_.reset(new MCSClient(chrome_build_proto_.chrome_version(), |
| 196 clock_.get(), |
| 197 connection_factory_.get(), |
| 198 gcm_store_.get())); |
| 199 } |
| 200 |
| 193 mcs_client_->Initialize( | 201 mcs_client_->Initialize( |
| 194 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), | 202 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), |
| 195 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, | 203 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, |
| 196 weak_ptr_factory_.GetWeakPtr()), | 204 weak_ptr_factory_.GetWeakPtr()), |
| 197 base::Bind(&GCMClientImpl::OnMessageSentToMCS, | 205 base::Bind(&GCMClientImpl::OnMessageSentToMCS, |
| 198 weak_ptr_factory_.GetWeakPtr()), | 206 weak_ptr_factory_.GetWeakPtr()), |
| 199 result.Pass()); | 207 result.Pass()); |
| 200 } | 208 } |
| 201 | 209 |
| 202 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted( | 210 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 279 } |
| 272 } | 280 } |
| 273 } | 281 } |
| 274 | 282 |
| 275 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { | 283 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| 276 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 284 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 277 DCHECK(success); | 285 DCHECK(success); |
| 278 } | 286 } |
| 279 | 287 |
| 280 void GCMClientImpl::CheckOut() { | 288 void GCMClientImpl::CheckOut() { |
| 281 delegate_ = NULL; | |
| 282 device_checkin_info_.Reset(); | 289 device_checkin_info_.Reset(); |
| 283 mcs_client_->Destroy(); // This will also destroy GCM store. | |
| 284 mcs_client_.reset(); | 290 mcs_client_.reset(); |
| 291 gcm_store_->Destroy(base::Bind(&GCMClientImpl::OnGCMStoreDestroyed, |
| 292 weak_ptr_factory_.GetWeakPtr())); |
| 285 checkin_request_.reset(); | 293 checkin_request_.reset(); |
| 286 pending_registrations_.clear(); | 294 pending_registrations_.clear(); |
| 295 state_ = INITIALIZED; |
| 287 } | 296 } |
| 288 | 297 |
| 289 void GCMClientImpl::Register(const std::string& app_id, | 298 void GCMClientImpl::Register(const std::string& app_id, |
| 290 const std::string& cert, | 299 const std::string& cert, |
| 291 const std::vector<std::string>& sender_ids) { | 300 const std::vector<std::string>& sender_ids) { |
| 292 DCHECK_EQ(state_, READY); | 301 DCHECK_EQ(state_, READY); |
| 293 RegistrationRequest::RequestInfo request_info( | 302 RegistrationRequest::RequestInfo request_info( |
| 294 device_checkin_info_.android_id, | 303 device_checkin_info_.android_id, |
| 295 device_checkin_info_.secret, | 304 device_checkin_info_.secret, |
| 296 app_id, | 305 app_id, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 delegate_->OnUnregisterFinished(app_id, status); | 372 delegate_->OnUnregisterFinished(app_id, status); |
| 364 | 373 |
| 365 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id); | 374 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id); |
| 366 if (iter == pending_unregistrations_.end()) | 375 if (iter == pending_unregistrations_.end()) |
| 367 return; | 376 return; |
| 368 | 377 |
| 369 delete iter->second; | 378 delete iter->second; |
| 370 pending_unregistrations_.erase(iter); | 379 pending_unregistrations_.erase(iter); |
| 371 } | 380 } |
| 372 | 381 |
| 382 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { |
| 383 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; |
| 384 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); |
| 385 } |
| 386 |
| 373 void GCMClientImpl::Send(const std::string& app_id, | 387 void GCMClientImpl::Send(const std::string& app_id, |
| 374 const std::string& receiver_id, | 388 const std::string& receiver_id, |
| 375 const OutgoingMessage& message) { | 389 const OutgoingMessage& message) { |
| 376 DCHECK_EQ(state_, READY); | 390 DCHECK_EQ(state_, READY); |
| 377 | 391 |
| 378 mcs_proto::DataMessageStanza stanza; | 392 mcs_proto::DataMessageStanza stanza; |
| 379 stanza.set_ttl(message.time_to_live); | 393 stanza.set_ttl(message.time_to_live); |
| 380 stanza.set_sent(clock_->Now().ToInternalValue() / | 394 stanza.set_sent(clock_->Now().ToInternalValue() / |
| 381 base::Time::kMicrosecondsPerSecond); | 395 base::Time::kMicrosecondsPerSecond); |
| 382 stanza.set_id(message.id); | 396 stanza.set_id(message.id); |
| 383 stanza.set_from(kSendMessageFromValue); | 397 stanza.set_from(kSendMessageFromValue); |
| 384 stanza.set_to(receiver_id); | 398 stanza.set_to(receiver_id); |
| 385 stanza.set_category(app_id); | 399 stanza.set_category(app_id); |
| 386 | 400 |
| 387 for (MessageData::const_iterator iter = message.data.begin(); | 401 for (MessageData::const_iterator iter = message.data.begin(); |
| 388 iter != message.data.end(); | 402 iter != message.data.end(); |
| 389 ++iter) { | 403 ++iter) { |
| 390 mcs_proto::AppData* app_data = stanza.add_app_data(); | 404 mcs_proto::AppData* app_data = stanza.add_app_data(); |
| 391 app_data->set_key(iter->first); | 405 app_data->set_key(iter->first); |
| 392 app_data->set_value(iter->second); | 406 app_data->set_value(iter->second); |
| 393 } | 407 } |
| 394 | 408 |
| 395 MCSMessage mcs_message(stanza); | 409 MCSMessage mcs_message(stanza); |
| 396 DVLOG(1) << "MCS message size: " << mcs_message.size(); | 410 DVLOG(1) << "MCS message size: " << mcs_message.size(); |
| 397 mcs_client_->SendMessage(mcs_message); | 411 mcs_client_->SendMessage(mcs_message); |
| 398 } | 412 } |
| 399 | 413 |
| 400 bool GCMClientImpl::IsReady() const { | |
| 401 return state_ == READY; | |
| 402 } | |
| 403 | |
| 404 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { | 414 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { |
| 405 switch (message.tag()) { | 415 switch (message.tag()) { |
| 406 case kLoginResponseTag: | 416 case kLoginResponseTag: |
| 407 DVLOG(1) << "Login response received by GCM Client. Ignoring."; | 417 DVLOG(1) << "Login response received by GCM Client. Ignoring."; |
| 408 return; | 418 return; |
| 409 case kDataMessageStanzaTag: | 419 case kDataMessageStanzaTag: |
| 410 DVLOG(1) << "A downstream message received. Processing..."; | 420 DVLOG(1) << "A downstream message received. Processing..."; |
| 411 HandleIncomingMessage(message); | 421 HandleIncomingMessage(message); |
| 412 return; | 422 return; |
| 413 default: | 423 default: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 if (iter != incoming_message.data.end()) | 500 if (iter != incoming_message.data.end()) |
| 491 message_id = iter->second; | 501 message_id = iter->second; |
| 492 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR); | 502 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR); |
| 493 } | 503 } |
| 494 | 504 |
| 495 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { | 505 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { |
| 496 mcs_client_ = mcs_client.Pass(); | 506 mcs_client_ = mcs_client.Pass(); |
| 497 } | 507 } |
| 498 | 508 |
| 499 } // namespace gcm | 509 } // namespace gcm |
| OLD | NEW |