message_receiver_impl.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2020 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. #include "ash/components/phonehub/message_receiver_impl.h"
  5. #include <netinet/in.h>
  6. #include <stdint.h>
  7. #include <string>
  8. #include "ash/components/phonehub/proto/phonehub_api.pb.h"
  9. #include "ash/components/phonehub/util/histogram_util.h"
  10. #include "ash/constants/ash_features.h"
  11. #include "base/logging.h"
  12. namespace ash {
  13. namespace phonehub {
  14. namespace {
  15. std::string GetMessageTypeName(proto::MessageType message_type) {
  16. switch (message_type) {
  17. case proto::MessageType::PHONE_STATUS_SNAPSHOT:
  18. return "PHONE_STATUS_SNAPSHOT";
  19. case proto::MessageType::PHONE_STATUS_UPDATE:
  20. return "PHONE_STATUS_UPDATE";
  21. case proto::MessageType::UPDATE_NOTIFICATION_MODE_RESPONSE:
  22. return "UPDATE_NOTIFICATION_MODE_RESPONSE";
  23. case proto::MessageType::RING_DEVICE_RESPONSE:
  24. return "RING_DEVICE_RESPONSE";
  25. case proto::MessageType::UPDATE_BATTERY_MODE_RESPONSE:
  26. return "UPDATE_BATTERY_MODE_RESPONSE";
  27. case proto::MessageType::DISMISS_NOTIFICATION_RESPONSE:
  28. return "DISMISS_NOTIFICATION_RESPONSE";
  29. case proto::MessageType::NOTIFICATION_INLINE_REPLY_RESPONSE:
  30. return "NOTIFICATION_INLINE_REPLY_RESPONSE";
  31. case proto::MessageType::SHOW_NOTIFICATION_ACCESS_SETUP_RESPONSE:
  32. return "SHOW_NOTIFICATION_ACCESS_SETUP_RESPONSE";
  33. case proto::MessageType::FETCH_CAMERA_ROLL_ITEMS_RESPONSE:
  34. return "FETCH_CAMERA_ROLL_ITEMS_RESPONSE";
  35. case proto::MessageType::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE:
  36. return "FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE";
  37. case proto::MessageType::FEATURE_SETUP_RESPONSE:
  38. return "FEATURE_SETUP_RESPONSE";
  39. default:
  40. return "UNKOWN_MESSAGE";
  41. }
  42. }
  43. } // namespace
  44. MessageReceiverImpl::MessageReceiverImpl(
  45. secure_channel::ConnectionManager* connection_manager)
  46. : connection_manager_(connection_manager) {
  47. DCHECK(connection_manager_);
  48. connection_manager_->AddObserver(this);
  49. }
  50. MessageReceiverImpl::~MessageReceiverImpl() {
  51. connection_manager_->RemoveObserver(this);
  52. }
  53. void MessageReceiverImpl::OnMessageReceived(const std::string& payload) {
  54. // The first two bytes of |payload| is reserved for the header
  55. // proto::MessageType.
  56. uint16_t* ptr =
  57. reinterpret_cast<uint16_t*>(const_cast<char*>(payload.data()));
  58. proto::MessageType message_type =
  59. static_cast<proto::MessageType>(ntohs(*ptr));
  60. PA_LOG(INFO) << "MessageReceiver received a "
  61. << GetMessageTypeName(message_type) << " message.";
  62. util::LogMessageResult(message_type,
  63. util::PhoneHubMessageResult::kResponseReceived);
  64. // Decode the proto message if the message is something we want to notify to
  65. // clients.
  66. if (message_type == proto::MessageType::PHONE_STATUS_SNAPSHOT) {
  67. proto::PhoneStatusSnapshot snapshot_proto;
  68. // Serialized proto is after the first two bytes of |payload|.
  69. if (!snapshot_proto.ParseFromString(payload.substr(2))) {
  70. PA_LOG(ERROR) << "OnMessageReceived() could not deserialize the "
  71. << "PhoneStatusSnapshot proto message.";
  72. return;
  73. }
  74. NotifyPhoneStatusSnapshotReceived(snapshot_proto);
  75. return;
  76. }
  77. if (message_type == proto::MessageType::PHONE_STATUS_UPDATE) {
  78. proto::PhoneStatusUpdate update_proto;
  79. // Serialized proto is after the first two bytes of |payload|.
  80. if (!update_proto.ParseFromString(payload.substr(2))) {
  81. PA_LOG(ERROR) << "OnMessageReceived() could not deserialize the "
  82. << "PhoneStatusUpdate proto message.";
  83. return;
  84. }
  85. NotifyPhoneStatusUpdateReceived(update_proto);
  86. return;
  87. }
  88. if (features::IsPhoneHubFeatureSetupErrorHandlingEnabled() &&
  89. message_type == proto::MessageType::FEATURE_SETUP_RESPONSE) {
  90. proto::FeatureSetupResponse response;
  91. // Serialized proto is after the first two bytes of |payload|.
  92. if (!response.ParseFromString(payload.substr(2))) {
  93. PA_LOG(ERROR) << "OnMessageReceived() could not deserialize the "
  94. << "FeatureSetupResponse proto message.";
  95. return;
  96. }
  97. NotifyFeatureSetupResponseReceived(response);
  98. }
  99. if (features::IsPhoneHubCameraRollEnabled() &&
  100. message_type == proto::MessageType::FETCH_CAMERA_ROLL_ITEMS_RESPONSE) {
  101. proto::FetchCameraRollItemsResponse response;
  102. // Serialized proto is after the first two bytes of |payload|.
  103. if (!response.ParseFromString(payload.substr(2))) {
  104. PA_LOG(ERROR) << "OnMessageReceived() could not deserialize the "
  105. << "FetchCameraRollItemsResponse proto message.";
  106. return;
  107. }
  108. NotifyFetchCameraRollItemsResponseReceived(response);
  109. return;
  110. }
  111. if (features::IsPhoneHubCameraRollEnabled() &&
  112. message_type ==
  113. proto::MessageType::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE) {
  114. proto::FetchCameraRollItemDataResponse response;
  115. // Serialized proto is after the first two bytes of |payload|.
  116. if (!response.ParseFromString(payload.substr(2))) {
  117. PA_LOG(ERROR) << "OnMessageReceived() could not deserialize the "
  118. << "FetchCameraRollItemDataResponse proto message.";
  119. return;
  120. }
  121. NotifyFetchCameraRollItemDataResponseReceived(response);
  122. return;
  123. }
  124. }
  125. } // namespace phonehub
  126. } // namespace ash