Chromium Code Reviews| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 url_request_context_getter, | 132 url_request_context_getter, |
| 132 Delegate* delegate) { | 133 Delegate* delegate) { |
| 133 DCHECK_EQ(UNINITIALIZED, state_); | 134 DCHECK_EQ(UNINITIALIZED, state_); |
| 134 DCHECK(url_request_context_getter); | 135 DCHECK(url_request_context_getter); |
| 135 DCHECK(delegate); | 136 DCHECK(delegate); |
| 136 | 137 |
| 137 chrome_build_proto_.CopyFrom(chrome_build_proto); | 138 chrome_build_proto_.CopyFrom(chrome_build_proto); |
| 138 url_request_context_getter_ = url_request_context_getter; | 139 url_request_context_getter_ = url_request_context_getter; |
| 139 | 140 |
| 140 gcm_store_.reset(new GCMStoreImpl(false, path, blocking_task_runner)); | 141 gcm_store_.reset(new GCMStoreImpl(false, path, blocking_task_runner)); |
| 141 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted, | |
| 142 weak_ptr_factory_.GetWeakPtr())); | |
| 143 | 142 |
| 144 delegate_ = delegate; | 143 delegate_ = delegate; |
| 145 | 144 |
| 146 // |mcs_client_| might already be set for testing at this point. No need to | 145 // |mcs_client_| might already be set for testing at this point. No need to |
| 147 // create a |connection_factory_|. | 146 // create a |connection_factory_|. |
| 148 if (!mcs_client_.get()) { | 147 if (!mcs_client_.get()) { |
| 149 const net::HttpNetworkSession::Params* network_session_params = | 148 const net::HttpNetworkSession::Params* network_session_params = |
| 150 url_request_context_getter->GetURLRequestContext()-> | 149 url_request_context_getter->GetURLRequestContext()-> |
| 151 GetNetworkSessionParams(); | 150 GetNetworkSessionParams(); |
| 152 DCHECK(network_session_params); | 151 DCHECK(network_session_params); |
| 153 network_session_ = new net::HttpNetworkSession(*network_session_params); | 152 network_session_ = new net::HttpNetworkSession(*network_session_params); |
| 154 connection_factory_.reset(new ConnectionFactoryImpl( | 153 connection_factory_.reset(new ConnectionFactoryImpl( |
| 155 GURL(kMCSEndpoint), | 154 GURL(kMCSEndpoint), |
| 156 kDefaultBackoffPolicy, | 155 kDefaultBackoffPolicy, |
| 157 network_session_, | 156 network_session_, |
| 158 net_log_.net_log())); | 157 net_log_.net_log())); |
| 159 mcs_client_.reset(new MCSClient(chrome_build_proto.chrome_version(), | 158 mcs_client_.reset(new MCSClient(chrome_build_proto.chrome_version(), |
| 160 clock_.get(), | 159 clock_.get(), |
| 161 connection_factory_.get(), | 160 connection_factory_.get(), |
| 162 gcm_store_.get())); | 161 gcm_store_.get())); |
| 163 } | 162 } |
| 164 | 163 |
| 164 state_ = INITIALIZED; | |
| 165 } | |
| 166 | |
| 167 void GCMClientImpl::CheckIn() { | |
| 168 DCHECK_EQ(INITIALIZED, state_); | |
| 169 | |
| 170 // Once the loading is completed, the check-in will be initiated. | |
| 171 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted, | |
| 172 weak_ptr_factory_.GetWeakPtr())); | |
| 165 state_ = LOADING; | 173 state_ = LOADING; |
| 166 } | 174 } |
| 167 | 175 |
| 168 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { | 176 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
| 169 DCHECK_EQ(LOADING, state_); | 177 DCHECK_EQ(LOADING, state_); |
| 170 | 178 |
| 171 if (!result->success) { | 179 if (!result->success) { |
| 172 ResetState(); | 180 ResetState(); |
| 173 return; | 181 return; |
| 174 } | 182 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 } | 279 } |
| 272 | 280 |
| 273 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { | 281 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| 274 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 282 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 275 DCHECK(success); | 283 DCHECK(success); |
| 276 } | 284 } |
| 277 | 285 |
| 278 void GCMClientImpl::CheckOut() { | 286 void GCMClientImpl::CheckOut() { |
| 279 delegate_ = NULL; | 287 delegate_ = NULL; |
| 280 device_checkin_info_.Reset(); | 288 device_checkin_info_.Reset(); |
| 281 mcs_client_->Destroy(); // This will also destroy GCM store. | |
| 282 mcs_client_.reset(); | 289 mcs_client_.reset(); |
| 290 gcm_store_->Destroy(base::Bind(&GCMClientImpl::OnGCMStoreDestroyed, | |
| 291 weak_ptr_factory_.GetWeakPtr())); | |
| 283 checkin_request_.reset(); | 292 checkin_request_.reset(); |
| 284 pending_registrations_.clear(); | 293 pending_registrations_.clear(); |
| 285 } | 294 } |
| 286 | 295 |
| 287 void GCMClientImpl::Register(const std::string& app_id, | 296 void GCMClientImpl::Register(const std::string& app_id, |
| 288 const std::string& cert, | 297 const std::string& cert, |
| 289 const std::vector<std::string>& sender_ids) { | 298 const std::vector<std::string>& sender_ids) { |
| 290 DCHECK_EQ(state_, READY); | 299 DCHECK_EQ(state_, READY); |
| 291 RegistrationRequest::RequestInfo request_info( | 300 RegistrationRequest::RequestInfo request_info( |
| 292 device_checkin_info_.android_id, | 301 device_checkin_info_.android_id, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 delegate_->OnUnregisterFinished(app_id, status); | 370 delegate_->OnUnregisterFinished(app_id, status); |
| 362 | 371 |
| 363 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id); | 372 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id); |
| 364 if (iter == pending_unregistrations_.end()) | 373 if (iter == pending_unregistrations_.end()) |
| 365 return; | 374 return; |
| 366 | 375 |
| 367 delete iter->second; | 376 delete iter->second; |
| 368 pending_unregistrations_.erase(iter); | 377 pending_unregistrations_.erase(iter); |
| 369 } | 378 } |
| 370 | 379 |
| 380 void GCMClientImpl::OnGCMStoreDestroyed(bool success) { | |
| 381 LOG_IF(ERROR, !success) << "GCM store failed to be destroyed!"; | |
|
Alexei Svitkine (slow)
2014/02/21 18:55:40
I think non-debug build log messages are discourag
jianli
2014/02/21 20:24:06
Done.
| |
| 382 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success); | |
| 383 } | |
| 384 | |
| 371 void GCMClientImpl::Send(const std::string& app_id, | 385 void GCMClientImpl::Send(const std::string& app_id, |
| 372 const std::string& receiver_id, | 386 const std::string& receiver_id, |
| 373 const OutgoingMessage& message) { | 387 const OutgoingMessage& message) { |
| 374 DCHECK_EQ(state_, READY); | 388 DCHECK_EQ(state_, READY); |
| 375 | 389 |
| 376 mcs_proto::DataMessageStanza stanza; | 390 mcs_proto::DataMessageStanza stanza; |
| 377 stanza.set_ttl(message.time_to_live); | 391 stanza.set_ttl(message.time_to_live); |
| 378 stanza.set_sent(clock_->Now().ToInternalValue() / | 392 stanza.set_sent(clock_->Now().ToInternalValue() / |
| 379 base::Time::kMicrosecondsPerSecond); | 393 base::Time::kMicrosecondsPerSecond); |
| 380 stanza.set_id(message.id); | 394 stanza.set_id(message.id); |
| 381 stanza.set_from(kSendMessageFromValue); | 395 stanza.set_from(kSendMessageFromValue); |
| 382 stanza.set_to(receiver_id); | 396 stanza.set_to(receiver_id); |
| 383 stanza.set_category(app_id); | 397 stanza.set_category(app_id); |
| 384 | 398 |
| 385 for (MessageData::const_iterator iter = message.data.begin(); | 399 for (MessageData::const_iterator iter = message.data.begin(); |
| 386 iter != message.data.end(); | 400 iter != message.data.end(); |
| 387 ++iter) { | 401 ++iter) { |
| 388 mcs_proto::AppData* app_data = stanza.add_app_data(); | 402 mcs_proto::AppData* app_data = stanza.add_app_data(); |
| 389 app_data->set_key(iter->first); | 403 app_data->set_key(iter->first); |
| 390 app_data->set_value(iter->second); | 404 app_data->set_value(iter->second); |
| 391 } | 405 } |
| 392 | 406 |
| 393 MCSMessage mcs_message(stanza); | 407 MCSMessage mcs_message(stanza); |
| 394 DVLOG(1) << "MCS message size: " << mcs_message.size(); | 408 DVLOG(1) << "MCS message size: " << mcs_message.size(); |
| 395 mcs_client_->SendMessage(mcs_message); | 409 mcs_client_->SendMessage(mcs_message); |
| 396 } | 410 } |
| 397 | 411 |
| 398 bool GCMClientImpl::IsReady() const { | |
| 399 return state_ == READY; | |
| 400 } | |
| 401 | |
| 402 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { | 412 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { |
| 403 switch (message.tag()) { | 413 switch (message.tag()) { |
| 404 case kLoginResponseTag: | 414 case kLoginResponseTag: |
| 405 DVLOG(1) << "Login response received by GCM Client. Ignoring."; | 415 DVLOG(1) << "Login response received by GCM Client. Ignoring."; |
| 406 return; | 416 return; |
| 407 case kDataMessageStanzaTag: | 417 case kDataMessageStanzaTag: |
| 408 DVLOG(1) << "A downstream message received. Processing..."; | 418 DVLOG(1) << "A downstream message received. Processing..."; |
| 409 HandleIncomingMessage(message); | 419 HandleIncomingMessage(message); |
| 410 return; | 420 return; |
| 411 default: | 421 default: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 if (iter != incoming_message.data.end()) | 498 if (iter != incoming_message.data.end()) |
| 489 message_id = iter->second; | 499 message_id = iter->second; |
| 490 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR); | 500 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR); |
| 491 } | 501 } |
| 492 | 502 |
| 493 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { | 503 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { |
| 494 mcs_client_ = mcs_client.Pass(); | 504 mcs_client_ = mcs_client.Pass(); |
| 495 } | 505 } |
| 496 | 506 |
| 497 } // namespace gcm | 507 } // namespace gcm |
| OLD | NEW |