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 <deque> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
13 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/stringprintf.h" | |
14 #include "base/time/default_clock.h" | 17 #include "base/time/default_clock.h" |
15 #include "google_apis/gcm/base/mcs_message.h" | 18 #include "google_apis/gcm/base/mcs_message.h" |
16 #include "google_apis/gcm/base/mcs_util.h" | 19 #include "google_apis/gcm/base/mcs_util.h" |
17 #include "google_apis/gcm/engine/checkin_request.h" | 20 #include "google_apis/gcm/engine/checkin_request.h" |
18 #include "google_apis/gcm/engine/connection_factory_impl.h" | 21 #include "google_apis/gcm/engine/connection_factory_impl.h" |
19 #include "google_apis/gcm/engine/gcm_store_impl.h" | 22 #include "google_apis/gcm/engine/gcm_store_impl.h" |
20 #include "google_apis/gcm/engine/mcs_client.h" | 23 #include "google_apis/gcm/engine/mcs_client.h" |
21 #include "google_apis/gcm/engine/registration_request.h" | 24 #include "google_apis/gcm/engine/registration_request.h" |
22 #include "google_apis/gcm/engine/unregistration_request.h" | 25 #include "google_apis/gcm/engine/unregistration_request.h" |
26 #include "google_apis/gcm/gcm_stats_recorder.h" | |
23 #include "google_apis/gcm/protocol/mcs.pb.h" | 27 #include "google_apis/gcm/protocol/mcs.pb.h" |
24 #include "net/http/http_network_session.h" | 28 #include "net/http/http_network_session.h" |
25 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
26 #include "url/gurl.h" | 30 #include "url/gurl.h" |
27 | 31 |
28 namespace gcm { | 32 namespace gcm { |
29 | 33 |
30 namespace { | 34 namespace { |
31 | 35 |
32 // Backoff policy. Shared across reconnection logic and checkin/(un)registration | 36 // Backoff policy. Shared across reconnection logic and checkin/(un)registration |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 DCHECK(network_session_params); | 193 DCHECK(network_session_params); |
190 network_session_ = new net::HttpNetworkSession(*network_session_params); | 194 network_session_ = new net::HttpNetworkSession(*network_session_params); |
191 connection_factory_.reset(new ConnectionFactoryImpl( | 195 connection_factory_.reset(new ConnectionFactoryImpl( |
192 GURL(kMCSEndpoint), | 196 GURL(kMCSEndpoint), |
193 kDefaultBackoffPolicy, | 197 kDefaultBackoffPolicy, |
194 network_session_, | 198 network_session_, |
195 net_log_.net_log())); | 199 net_log_.net_log())); |
196 mcs_client_.reset(new MCSClient(chrome_build_proto_.chrome_version(), | 200 mcs_client_.reset(new MCSClient(chrome_build_proto_.chrome_version(), |
197 clock_.get(), | 201 clock_.get(), |
198 connection_factory_.get(), | 202 connection_factory_.get(), |
199 gcm_store_.get())); | 203 gcm_store_.get(), |
204 &recorder_)); | |
200 } | 205 } |
201 | 206 |
202 mcs_client_->Initialize( | 207 mcs_client_->Initialize( |
203 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), | 208 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), |
204 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, | 209 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, |
205 weak_ptr_factory_.GetWeakPtr()), | 210 weak_ptr_factory_.GetWeakPtr()), |
206 base::Bind(&GCMClientImpl::OnMessageSentToMCS, | 211 base::Bind(&GCMClientImpl::OnMessageSentToMCS, |
207 weak_ptr_factory_.GetWeakPtr()), | 212 weak_ptr_factory_.GetWeakPtr()), |
208 result.Pass()); | 213 result.Pass()); |
209 } | 214 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 | 248 |
244 void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { | 249 void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { |
245 checkin_request_.reset( | 250 checkin_request_.reset( |
246 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, | 251 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, |
247 weak_ptr_factory_.GetWeakPtr()), | 252 weak_ptr_factory_.GetWeakPtr()), |
248 kDefaultBackoffPolicy, | 253 kDefaultBackoffPolicy, |
249 chrome_build_proto_, | 254 chrome_build_proto_, |
250 checkin_info.android_id, | 255 checkin_info.android_id, |
251 checkin_info.secret, | 256 checkin_info.secret, |
252 account_ids_, | 257 account_ids_, |
253 url_request_context_getter_)); | 258 url_request_context_getter_, |
259 &recorder_)); | |
260 recorder_.RecordCheckin(checkin_info.android_id, GCMStatsRecorder::Initiated); | |
254 checkin_request_->Start(); | 261 checkin_request_->Start(); |
255 } | 262 } |
256 | 263 |
257 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, | 264 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
258 uint64 security_token) { | 265 uint64 security_token) { |
259 checkin_request_.reset(); | 266 checkin_request_.reset(); |
260 | 267 |
261 CheckinInfo checkin_info; | 268 CheckinInfo checkin_info; |
262 checkin_info.android_id = android_id; | 269 checkin_info.android_id = android_id; |
263 checkin_info.secret = security_token; | 270 checkin_info.secret = security_token; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 sender_ids); | 318 sender_ids); |
312 DCHECK_EQ(0u, pending_registrations_.count(app_id)); | 319 DCHECK_EQ(0u, pending_registrations_.count(app_id)); |
313 | 320 |
314 RegistrationRequest* registration_request = | 321 RegistrationRequest* registration_request = |
315 new RegistrationRequest(request_info, | 322 new RegistrationRequest(request_info, |
316 kDefaultBackoffPolicy, | 323 kDefaultBackoffPolicy, |
317 base::Bind(&GCMClientImpl::OnRegisterCompleted, | 324 base::Bind(&GCMClientImpl::OnRegisterCompleted, |
318 weak_ptr_factory_.GetWeakPtr(), | 325 weak_ptr_factory_.GetWeakPtr(), |
319 app_id), | 326 app_id), |
320 kMaxRegistrationRetries, | 327 kMaxRegistrationRetries, |
321 url_request_context_getter_); | 328 url_request_context_getter_, |
329 &recorder_); | |
322 pending_registrations_[app_id] = registration_request; | 330 pending_registrations_[app_id] = registration_request; |
331 recorder_.RecordRegister(app_id, GCMStatsRecorder::Initiated, sender_ids); | |
323 registration_request->Start(); | 332 registration_request->Start(); |
324 } | 333 } |
325 | 334 |
326 void GCMClientImpl::OnRegisterCompleted(const std::string& app_id, | 335 void GCMClientImpl::OnRegisterCompleted(const std::string& app_id, |
327 RegistrationRequest::Status status, | 336 RegistrationRequest::Status status, |
328 const std::string& registration_id) { | 337 const std::string& registration_id) { |
329 DCHECK(delegate_); | 338 DCHECK(delegate_); |
330 | 339 |
331 Result result; | 340 Result result; |
332 PendingRegistrations::iterator iter = pending_registrations_.find(app_id); | 341 PendingRegistrations::iterator iter = pending_registrations_.find(app_id); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 for (MessageData::const_iterator iter = message.data.begin(); | 415 for (MessageData::const_iterator iter = message.data.begin(); |
407 iter != message.data.end(); | 416 iter != message.data.end(); |
408 ++iter) { | 417 ++iter) { |
409 mcs_proto::AppData* app_data = stanza.add_app_data(); | 418 mcs_proto::AppData* app_data = stanza.add_app_data(); |
410 app_data->set_key(iter->first); | 419 app_data->set_key(iter->first); |
411 app_data->set_value(iter->second); | 420 app_data->set_value(iter->second); |
412 } | 421 } |
413 | 422 |
414 MCSMessage mcs_message(stanza); | 423 MCSMessage mcs_message(stanza); |
415 DVLOG(1) << "MCS message size: " << mcs_message.size(); | 424 DVLOG(1) << "MCS message size: " << mcs_message.size(); |
425 recorder_.RecordSend(app_id, GCMStatsRecorder::Initiated, receiver_id, | |
426 message.id, mcs_message.size()); | |
416 mcs_client_->SendMessage(mcs_message); | 427 mcs_client_->SendMessage(mcs_message); |
417 } | 428 } |
418 | 429 |
419 std::string GCMClientImpl::GetStateString() const { | 430 std::string GCMClientImpl::GetStateString() const { |
420 switch(state_) { | 431 switch(state_) { |
421 case GCMClientImpl::INITIALIZED: | 432 case GCMClientImpl::INITIALIZED: |
422 return "INITIALIZED"; | 433 return "INITIALIZED"; |
423 case GCMClientImpl::UNINITIALIZED: | 434 case GCMClientImpl::UNINITIALIZED: |
424 return "UNINITIALIZED"; | 435 return "UNINITIALIZED"; |
425 case GCMClientImpl::LOADING: | 436 case GCMClientImpl::LOADING: |
426 return "LOADING"; | 437 return "LOADING"; |
427 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: | 438 case GCMClientImpl::INITIAL_DEVICE_CHECKIN: |
428 return "INITIAL_DEVICE_CHECKIN"; | 439 return "INITIAL_DEVICE_CHECKIN"; |
429 case GCMClientImpl::READY: | 440 case GCMClientImpl::READY: |
430 return "READY"; | 441 return "READY"; |
431 default: | 442 default: |
432 NOTREACHED(); | 443 NOTREACHED(); |
433 return std::string(); | 444 return std::string(); |
434 } | 445 } |
435 } | 446 } |
436 | 447 |
448 void GCMClientImpl::SetRecording(bool recording) { | |
449 recorder_.SetRecording(recording); | |
450 } | |
451 | |
452 void GCMClientImpl::ClearActivityLogs() { | |
453 recorder_.Clear(); | |
454 } | |
455 | |
437 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { | 456 GCMClient::GCMStatistics GCMClientImpl::GetStatistics() const { |
438 GCMClient::GCMStatistics stats; | 457 GCMClient::GCMStatistics stats; |
458 stats.is_recording = recorder_.IsRecording(); | |
439 stats.gcm_client_state = GCMClientImpl::GetStateString(); | 459 stats.gcm_client_state = GCMClientImpl::GetStateString(); |
440 stats.connection_client_created = mcs_client_.get() != NULL; | 460 stats.connection_client_created = mcs_client_.get() != NULL; |
441 if (mcs_client_.get()) { | 461 if (mcs_client_.get()) { |
442 stats.connection_state = mcs_client_->GetStateString(); | 462 stats.connection_state = mcs_client_->GetStateString(); |
443 // TODO(juyik): add more statistics such as message metadata list, etc. | 463 stats.send_queue_size = mcs_client_->GetSendQueueSize(); |
464 stats.unacked_queue_size = mcs_client_->GetUnackedQueueSize(); | |
444 } | 465 } |
445 if (device_checkin_info_.android_id > 0) | 466 if (device_checkin_info_.android_id > 0) |
446 stats.android_id = device_checkin_info_.android_id; | 467 stats.android_id = device_checkin_info_.android_id; |
468 | |
469 for (std::deque<GCMStatsRecorder::CheckinActivity>::const_iterator it | |
470 = recorder_.CheckinActivities().begin(); | |
jianli
2014/03/18 23:53:23
nit: '=' should be moved to the end of previous li
juyik
2014/03/20 01:09:53
Done.
| |
471 it != recorder_.CheckinActivities().end(); | |
472 ++it) { | |
473 stats.checkins.push_back(*it); | |
474 } | |
475 for (std::deque<GCMStatsRecorder::RegisterActivity>::const_iterator it | |
476 = recorder_.RegisterActivities().begin(); | |
477 it != recorder_.RegisterActivities().end(); | |
478 ++it) { | |
479 stats.registers.push_back(*it); | |
480 } | |
481 for (std::deque<GCMStatsRecorder::SendMessageActivity>::const_iterator it | |
482 = recorder_.SendMessageActivities().begin(); | |
483 it != recorder_.SendMessageActivities().end(); | |
484 ++it) { | |
485 stats.sent_messages.push_back(*it); | |
486 } | |
487 for (std::deque<GCMStatsRecorder::ReceiveMessageActivity>::const_iterator it | |
488 = recorder_.ReceiveMessageActivities().begin(); | |
489 it != recorder_.ReceiveMessageActivities().end(); | |
490 ++it) { | |
491 stats.received_messages.push_back(*it); | |
492 } | |
447 return stats; | 493 return stats; |
448 } | 494 } |
449 | 495 |
450 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { | 496 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { |
451 switch (message.tag()) { | 497 switch (message.tag()) { |
452 case kLoginResponseTag: | 498 case kLoginResponseTag: |
453 DVLOG(1) << "Login response received by GCM Client. Ignoring."; | 499 DVLOG(1) << "Login response received by GCM Client. Ignoring."; |
454 return; | 500 return; |
455 case kDataMessageStanzaTag: | 501 case kDataMessageStanzaTag: |
456 DVLOG(1) << "A downstream message received. Processing..."; | 502 DVLOG(1) << "A downstream message received. Processing..."; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 if (iter != message_data.end()) { | 561 if (iter != message_data.end()) { |
516 message_type = DecodeMessageType(iter->second); | 562 message_type = DecodeMessageType(iter->second); |
517 message_data.erase(iter); | 563 message_data.erase(iter); |
518 } | 564 } |
519 | 565 |
520 switch (message_type) { | 566 switch (message_type) { |
521 case DATA_MESSAGE: | 567 case DATA_MESSAGE: |
522 HandleIncomingDataMessage(data_message_stanza, message_data); | 568 HandleIncomingDataMessage(data_message_stanza, message_data); |
523 break; | 569 break; |
524 case DELETED_MESSAGES: | 570 case DELETED_MESSAGES: |
571 recorder_.RecordReceiveWithDetails( | |
572 GCMStatsRecorder::Success, | |
573 data_message_stanza.from(), | |
574 data_message_stanza.category(), | |
575 "Received DELETED MESSAGE"); | |
525 delegate_->OnMessagesDeleted(data_message_stanza.category()); | 576 delegate_->OnMessagesDeleted(data_message_stanza.category()); |
526 break; | 577 break; |
527 case SEND_ERROR: | 578 case SEND_ERROR: |
528 HandleIncomingSendError(data_message_stanza, message_data); | 579 HandleIncomingSendError(data_message_stanza, message_data); |
529 break; | 580 break; |
530 case UNKNOWN: | 581 case UNKNOWN: |
531 default: // Treat default the same as UNKNOWN. | 582 default: // Treat default the same as UNKNOWN. |
532 DVLOG(1) << "Unknown message_type received. Message ignored. " | 583 DVLOG(1) << "Unknown message_type received. Message ignored. " |
533 << "App ID: " << data_message_stanza.category() << "."; | 584 << "App ID: " << data_message_stanza.category() << "."; |
585 recorder_.RecordReceiveWithDetails( | |
586 GCMStatsRecorder::Success, | |
587 data_message_stanza.from(), | |
588 data_message_stanza.category(), | |
589 "Unknown message type received"); | |
534 break; | 590 break; |
535 } | 591 } |
536 } | 592 } |
537 | 593 |
538 void GCMClientImpl::HandleIncomingDataMessage( | 594 void GCMClientImpl::HandleIncomingDataMessage( |
539 const mcs_proto::DataMessageStanza& data_message_stanza, | 595 const mcs_proto::DataMessageStanza& data_message_stanza, |
540 MessageData& message_data) { | 596 MessageData& message_data) { |
541 IncomingMessage incoming_message; | 597 IncomingMessage incoming_message; |
542 incoming_message.sender_id = data_message_stanza.from(); | 598 incoming_message.sender_id = data_message_stanza.from(); |
543 if (data_message_stanza.has_token()) | 599 if (data_message_stanza.has_token()) |
544 incoming_message.collapse_key = data_message_stanza.token(); | 600 incoming_message.collapse_key = data_message_stanza.token(); |
545 incoming_message.data = message_data; | 601 incoming_message.data = message_data; |
602 recorder_.RecordReceiveDataMessage(incoming_message.sender_id, | |
603 data_message_stanza.category(), | |
604 incoming_message.data.size()); | |
546 delegate_->OnMessageReceived(data_message_stanza.category(), | 605 delegate_->OnMessageReceived(data_message_stanza.category(), |
547 incoming_message); | 606 incoming_message); |
548 } | 607 } |
549 | 608 |
550 void GCMClientImpl::HandleIncomingSendError( | 609 void GCMClientImpl::HandleIncomingSendError( |
551 const mcs_proto::DataMessageStanza& data_message_stanza, | 610 const mcs_proto::DataMessageStanza& data_message_stanza, |
552 MessageData& message_data) { | 611 MessageData& message_data) { |
553 SendErrorDetails send_error_details; | 612 SendErrorDetails send_error_details; |
554 send_error_details.additional_data = message_data; | 613 send_error_details.additional_data = message_data; |
555 send_error_details.result = SERVER_ERROR; | 614 send_error_details.result = SERVER_ERROR; |
556 | 615 |
557 MessageData::iterator iter = | 616 MessageData::iterator iter = |
558 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 617 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
559 if (iter != send_error_details.additional_data.end()) { | 618 if (iter != send_error_details.additional_data.end()) { |
560 send_error_details.message_id = iter->second; | 619 send_error_details.message_id = iter->second; |
561 send_error_details.additional_data.erase(iter); | 620 send_error_details.additional_data.erase(iter); |
562 } | 621 } |
563 | 622 |
623 recorder_.RecordReceiveWithDetails( | |
624 GCMStatsRecorder::Error, | |
625 std::string(), | |
626 data_message_stanza.category(), | |
627 base::StringPrintf("Send nessage server error. Message id: %s", | |
628 send_error_details.message_id.c_str())); | |
564 delegate_->OnMessageSendError(data_message_stanza.category(), | 629 delegate_->OnMessageSendError(data_message_stanza.category(), |
565 send_error_details); | 630 send_error_details); |
566 } | 631 } |
567 | 632 |
568 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { | 633 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { |
569 mcs_client_ = mcs_client.Pass(); | 634 mcs_client_ = mcs_client.Pass(); |
570 } | 635 } |
571 | 636 |
572 } // namespace gcm | 637 } // namespace gcm |
OLD | NEW |