| 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" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 const int kMaxRegistrationRetries = 5; | 79 const int kMaxRegistrationRetries = 5; |
| 80 const char kMessageTypeDataMessage[] = "gcm"; | 80 const char kMessageTypeDataMessage[] = "gcm"; |
| 81 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; | 81 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| 82 const char kMessageTypeKey[] = "message_type"; | 82 const char kMessageTypeKey[] = "message_type"; |
| 83 const char kMessageTypeSendErrorKey[] = "send_error"; | 83 const char kMessageTypeSendErrorKey[] = "send_error"; |
| 84 const char kSendErrorMessageIdKey[] = "google.message_id"; | 84 const char kSendErrorMessageIdKey[] = "google.message_id"; |
| 85 const char kSendMessageFromValue[] = "gcm@chrome.com"; | 85 const char kSendMessageFromValue[] = "gcm@chrome.com"; |
| 86 const int64 kDefaultUserSerialNumber = 0LL; | 86 const int64 kDefaultUserSerialNumber = 0LL; |
| 87 | 87 |
| 88 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { | 88 Result ToGCMClientResult(MCSClient::MessageSendStatus status) { |
| 89 switch (status) { | 89 switch (status) { |
| 90 case MCSClient::QUEUED: | 90 case MCSClient::QUEUED: |
| 91 return GCMClient::SUCCESS; | 91 return RESULT_SUCCESS; |
| 92 case MCSClient::QUEUE_SIZE_LIMIT_REACHED: | 92 case MCSClient::QUEUE_SIZE_LIMIT_REACHED: |
| 93 return GCMClient::NETWORK_ERROR; | 93 return RESULT_NETWORK_ERROR; |
| 94 case MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: | 94 case MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: |
| 95 return GCMClient::NETWORK_ERROR; | 95 return RESULT_NETWORK_ERROR; |
| 96 case MCSClient::MESSAGE_TOO_LARGE: | 96 case MCSClient::MESSAGE_TOO_LARGE: |
| 97 return GCMClient::INVALID_PARAMETER; | 97 return RESULT_INVALID_PARAMETER; |
| 98 case MCSClient::NO_CONNECTION_ON_ZERO_TTL: | 98 case MCSClient::NO_CONNECTION_ON_ZERO_TTL: |
| 99 return GCMClient::NETWORK_ERROR; | 99 return RESULT_NETWORK_ERROR; |
| 100 case MCSClient::TTL_EXCEEDED: | 100 case MCSClient::TTL_EXCEEDED: |
| 101 return GCMClient::NETWORK_ERROR; | 101 return RESULT_NETWORK_ERROR; |
| 102 case MCSClient::SENT: | 102 case MCSClient::SENT: |
| 103 default: | 103 default: |
| 104 NOTREACHED(); | 104 NOTREACHED(); |
| 105 break; | 105 break; |
| 106 } | 106 } |
| 107 return GCMClientImpl::UNKNOWN_ERROR; | 107 return RESULT_UNKNOWN_ERROR; |
| 108 } | 108 } |
| 109 | 109 |
| 110 MessageType DecodeMessageType(const std::string& value) { | 110 MessageType DecodeMessageType(const std::string& value) { |
| 111 if (kMessageTypeDeletedMessagesKey == value) | 111 if (kMessageTypeDeletedMessagesKey == value) |
| 112 return DELETED_MESSAGES; | 112 return DELETED_MESSAGES; |
| 113 if (kMessageTypeSendErrorKey == value) | 113 if (kMessageTypeSendErrorKey == value) |
| 114 return SEND_ERROR; | 114 return SEND_ERROR; |
| 115 if (kMessageTypeDataMessage == value) | 115 if (kMessageTypeDataMessage == value) |
| 116 return DATA_MESSAGE; | 116 return DATA_MESSAGE; |
| 117 return UNKNOWN; | 117 return UNKNOWN; |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 const std::vector<std::string>& sender_ids) { | 422 const std::vector<std::string>& sender_ids) { |
| 423 DCHECK_EQ(state_, READY); | 423 DCHECK_EQ(state_, READY); |
| 424 | 424 |
| 425 // If the same sender ids is provided, return the cached registration ID | 425 // If the same sender ids is provided, return the cached registration ID |
| 426 // directly. | 426 // directly. |
| 427 RegistrationInfoMap::const_iterator registrations_iter = | 427 RegistrationInfoMap::const_iterator registrations_iter = |
| 428 registrations_.find(app_id); | 428 registrations_.find(app_id); |
| 429 if (registrations_iter != registrations_.end() && | 429 if (registrations_iter != registrations_.end() && |
| 430 registrations_iter->second->sender_ids == sender_ids) { | 430 registrations_iter->second->sender_ids == sender_ids) { |
| 431 delegate_->OnRegisterFinished( | 431 delegate_->OnRegisterFinished( |
| 432 app_id, registrations_iter->second->registration_id, SUCCESS); | 432 app_id, registrations_iter->second->registration_id, RESULT_SUCCESS); |
| 433 return; | 433 return; |
| 434 } | 434 } |
| 435 | 435 |
| 436 RegistrationRequest::RequestInfo request_info( | 436 RegistrationRequest::RequestInfo request_info( |
| 437 device_checkin_info_.android_id, | 437 device_checkin_info_.android_id, |
| 438 device_checkin_info_.secret, | 438 device_checkin_info_.secret, |
| 439 app_id, | 439 app_id, |
| 440 sender_ids); | 440 sender_ids); |
| 441 DCHECK_EQ(0u, pending_registration_requests_.count(app_id)); | 441 DCHECK_EQ(0u, pending_registration_requests_.count(app_id)); |
| 442 | 442 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 459 const std::string& app_id, | 459 const std::string& app_id, |
| 460 const std::vector<std::string>& sender_ids, | 460 const std::vector<std::string>& sender_ids, |
| 461 RegistrationRequest::Status status, | 461 RegistrationRequest::Status status, |
| 462 const std::string& registration_id) { | 462 const std::string& registration_id) { |
| 463 DCHECK(delegate_); | 463 DCHECK(delegate_); |
| 464 | 464 |
| 465 Result result; | 465 Result result; |
| 466 PendingRegistrationRequests::iterator iter = | 466 PendingRegistrationRequests::iterator iter = |
| 467 pending_registration_requests_.find(app_id); | 467 pending_registration_requests_.find(app_id); |
| 468 if (iter == pending_registration_requests_.end()) | 468 if (iter == pending_registration_requests_.end()) |
| 469 result = UNKNOWN_ERROR; | 469 result = RESULT_UNKNOWN_ERROR; |
| 470 else if (status == RegistrationRequest::INVALID_SENDER) | 470 else if (status == RegistrationRequest::INVALID_SENDER) |
| 471 result = INVALID_PARAMETER; | 471 result = RESULT_INVALID_PARAMETER; |
| 472 else if (registration_id.empty()) | 472 else if (registration_id.empty()) |
| 473 result = SERVER_ERROR; | 473 result = RESULT_SERVER_ERROR; |
| 474 else | 474 else |
| 475 result = SUCCESS; | 475 result = RESULT_SUCCESS; |
| 476 | 476 |
| 477 if (result == SUCCESS) { | 477 if (result == RESULT_SUCCESS) { |
| 478 // Cache it. | 478 // Cache it. |
| 479 linked_ptr<RegistrationInfo> registration(new RegistrationInfo); | 479 linked_ptr<RegistrationInfo> registration(new RegistrationInfo); |
| 480 registration->sender_ids = sender_ids; | 480 registration->sender_ids = sender_ids; |
| 481 registration->registration_id = registration_id; | 481 registration->registration_id = registration_id; |
| 482 registrations_[app_id] = registration; | 482 registrations_[app_id] = registration; |
| 483 | 483 |
| 484 // Save it in the persistent store. | 484 // Save it in the persistent store. |
| 485 gcm_store_->AddRegistration( | 485 gcm_store_->AddRegistration( |
| 486 app_id, | 486 app_id, |
| 487 registration, | 487 registration, |
| 488 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, | 488 base::Bind(&GCMClientImpl::UpdateRegistrationCallback, |
| 489 weak_ptr_factory_.GetWeakPtr())); | 489 weak_ptr_factory_.GetWeakPtr())); |
| 490 } | 490 } |
| 491 | 491 |
| 492 delegate_->OnRegisterFinished( | 492 delegate_->OnRegisterFinished( |
| 493 app_id, result == SUCCESS ? registration_id : std::string(), result); | 493 app_id, |
| 494 result == RESULT_SUCCESS ? registration_id : std::string(), |
| 495 result); |
| 494 | 496 |
| 495 if (iter != pending_registration_requests_.end()) { | 497 if (iter != pending_registration_requests_.end()) { |
| 496 delete iter->second; | 498 delete iter->second; |
| 497 pending_registration_requests_.erase(iter); | 499 pending_registration_requests_.erase(iter); |
| 498 } | 500 } |
| 499 } | 501 } |
| 500 | 502 |
| 501 void GCMClientImpl::Unregister(const std::string& app_id) { | 503 void GCMClientImpl::Unregister(const std::string& app_id) { |
| 502 DCHECK_EQ(state_, READY); | 504 DCHECK_EQ(state_, READY); |
| 503 if (pending_unregistration_requests_.count(app_id) == 1) | 505 if (pending_unregistration_requests_.count(app_id) == 1) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 529 unregistration_request->Start(); | 531 unregistration_request->Start(); |
| 530 } | 532 } |
| 531 | 533 |
| 532 void GCMClientImpl::OnUnregisterCompleted( | 534 void GCMClientImpl::OnUnregisterCompleted( |
| 533 const std::string& app_id, | 535 const std::string& app_id, |
| 534 UnregistrationRequest::Status status) { | 536 UnregistrationRequest::Status status) { |
| 535 DVLOG(1) << "Unregister completed for app: " << app_id | 537 DVLOG(1) << "Unregister completed for app: " << app_id |
| 536 << " with " << (status ? "success." : "failure."); | 538 << " with " << (status ? "success." : "failure."); |
| 537 delegate_->OnUnregisterFinished( | 539 delegate_->OnUnregisterFinished( |
| 538 app_id, | 540 app_id, |
| 539 status == UnregistrationRequest::SUCCESS ? SUCCESS : SERVER_ERROR); | 541 status == UnregistrationRequest::SUCCESS ? RESULT_SUCCESS |
| 542 : RESULT_SERVER_ERROR); |
| 540 | 543 |
| 541 PendingUnregistrationRequests::iterator iter = | 544 PendingUnregistrationRequests::iterator iter = |
| 542 pending_unregistration_requests_.find(app_id); | 545 pending_unregistration_requests_.find(app_id); |
| 543 if (iter == pending_unregistration_requests_.end()) | 546 if (iter == pending_unregistration_requests_.end()) |
| 544 return; | 547 return; |
| 545 | 548 |
| 546 delete iter->second; | 549 delete iter->second; |
| 547 pending_unregistration_requests_.erase(iter); | 550 pending_unregistration_requests_.erase(iter); |
| 548 } | 551 } |
| 549 | 552 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 // than |OnSendFinished|. SendErrorDetails.additional_data is left empty. | 657 // than |OnSendFinished|. SendErrorDetails.additional_data is left empty. |
| 655 // All other errors will be raised immediately, through asynchronous callback. | 658 // All other errors will be raised immediately, through asynchronous callback. |
| 656 // It is expected that TTL_EXCEEDED will be issued for a message that was | 659 // It is expected that TTL_EXCEEDED will be issued for a message that was |
| 657 // previously issued |OnSendFinished| with status SUCCESS. | 660 // previously issued |OnSendFinished| with status SUCCESS. |
| 658 // For now, we do not report that the message has been sent and acked | 661 // For now, we do not report that the message has been sent and acked |
| 659 // successfully. | 662 // successfully. |
| 660 // TODO(jianli): Consider adding UMA for this status. | 663 // TODO(jianli): Consider adding UMA for this status. |
| 661 if (status == MCSClient::TTL_EXCEEDED) { | 664 if (status == MCSClient::TTL_EXCEEDED) { |
| 662 SendErrorDetails send_error_details; | 665 SendErrorDetails send_error_details; |
| 663 send_error_details.message_id = message_id; | 666 send_error_details.message_id = message_id; |
| 664 send_error_details.result = GCMClient::TTL_EXCEEDED; | 667 send_error_details.result = RESULT_TTL_EXCEEDED; |
| 665 delegate_->OnMessageSendError(app_id, send_error_details); | 668 delegate_->OnMessageSendError(app_id, send_error_details); |
| 666 } else if (status != MCSClient::SENT) { | 669 } else if (status != MCSClient::SENT) { |
| 667 delegate_->OnSendFinished(app_id, message_id, ToGCMClientResult(status)); | 670 delegate_->OnSendFinished(app_id, message_id, ToGCMClientResult(status)); |
| 668 } | 671 } |
| 669 } | 672 } |
| 670 | 673 |
| 671 void GCMClientImpl::OnMCSError() { | 674 void GCMClientImpl::OnMCSError() { |
| 672 // TODO(fgorski): For now it replaces the initialization method. Long term it | 675 // TODO(fgorski): For now it replaces the initialization method. Long term it |
| 673 // should have an error or status passed in. | 676 // should have an error or status passed in. |
| 674 } | 677 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 incoming_message.collapse_key = data_message_stanza.token(); | 749 incoming_message.collapse_key = data_message_stanza.token(); |
| 747 incoming_message.data = message_data; | 750 incoming_message.data = message_data; |
| 748 delegate_->OnMessageReceived(app_id, incoming_message); | 751 delegate_->OnMessageReceived(app_id, incoming_message); |
| 749 } | 752 } |
| 750 | 753 |
| 751 void GCMClientImpl::HandleIncomingSendError( | 754 void GCMClientImpl::HandleIncomingSendError( |
| 752 const mcs_proto::DataMessageStanza& data_message_stanza, | 755 const mcs_proto::DataMessageStanza& data_message_stanza, |
| 753 MessageData& message_data) { | 756 MessageData& message_data) { |
| 754 SendErrorDetails send_error_details; | 757 SendErrorDetails send_error_details; |
| 755 send_error_details.additional_data = message_data; | 758 send_error_details.additional_data = message_data; |
| 756 send_error_details.result = SERVER_ERROR; | 759 send_error_details.result = RESULT_SERVER_ERROR; |
| 757 | 760 |
| 758 MessageData::iterator iter = | 761 MessageData::iterator iter = |
| 759 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 762 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
| 760 if (iter != send_error_details.additional_data.end()) { | 763 if (iter != send_error_details.additional_data.end()) { |
| 761 send_error_details.message_id = iter->second; | 764 send_error_details.message_id = iter->second; |
| 762 send_error_details.additional_data.erase(iter); | 765 send_error_details.additional_data.erase(iter); |
| 763 } | 766 } |
| 764 | 767 |
| 765 recorder_.RecordIncomingSendError( | 768 recorder_.RecordIncomingSendError( |
| 766 data_message_stanza.category(), | 769 data_message_stanza.category(), |
| 767 data_message_stanza.to(), | 770 data_message_stanza.to(), |
| 768 data_message_stanza.id()); | 771 data_message_stanza.id()); |
| 769 delegate_->OnMessageSendError(data_message_stanza.category(), | 772 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 770 send_error_details); | 773 send_error_details); |
| 771 } | 774 } |
| 772 | 775 |
| 773 } // namespace gcm | 776 } // namespace gcm |
| OLD | NEW |