message_sender_impl.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_sender_impl.h"
  5. #include <netinet/in.h>
  6. #include "ash/components/phonehub/util/histogram_util.h"
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/services/secure_channel/public/cpp/client/connection_manager.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "base/strings/strcat.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. namespace ash {
  13. namespace phonehub {
  14. namespace {
  15. std::string SerializeMessage(proto::MessageType message_type,
  16. const google::protobuf::MessageLite* request) {
  17. // Add two space characters, followed by the serialized proto.
  18. std::string message = base::StrCat({" ", request->SerializeAsString()});
  19. // Replace the first two characters with |message_type| as a 16-bit int.
  20. uint16_t* ptr =
  21. reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
  22. *ptr = htons(static_cast<uint16_t>(message_type));
  23. return message;
  24. }
  25. } // namespace
  26. MessageSenderImpl::MessageSenderImpl(
  27. secure_channel::ConnectionManager* connection_manager)
  28. : connection_manager_(connection_manager) {
  29. DCHECK(connection_manager_);
  30. }
  31. MessageSenderImpl::~MessageSenderImpl() = default;
  32. void MessageSenderImpl::SendCrosState(bool notification_setting_enabled,
  33. bool camera_roll_setting_enabled) {
  34. proto::NotificationSetting is_notification_enabled =
  35. notification_setting_enabled
  36. ? proto::NotificationSetting::NOTIFICATIONS_ON
  37. : proto::NotificationSetting::NOTIFICATIONS_OFF;
  38. proto::CameraRollSetting is_camera_roll_enabled =
  39. camera_roll_setting_enabled ? proto::CameraRollSetting::CAMERA_ROLL_ON
  40. : proto::CameraRollSetting::CAMERA_ROLL_OFF;
  41. proto::CrosState request;
  42. request.set_notification_setting(is_notification_enabled);
  43. request.set_camera_roll_setting(is_camera_roll_enabled);
  44. if (features::IsPhoneHubMonochromeNotificationIconsEnabled()) {
  45. // Updated Chromebooks should always use the new flag, but a flag is still
  46. // necessary to identify end-of-support Chromebooks so the phone can know
  47. // to send backwards-compatible messages.
  48. request.set_notification_icon_styling(
  49. proto::NotificationIconStyling::ICON_STYLE_MONOCHROME_SMALL_ICON);
  50. }
  51. SendMessage(proto::MessageType::PROVIDE_CROS_STATE, &request);
  52. }
  53. void MessageSenderImpl::SendUpdateNotificationModeRequest(
  54. bool do_not_disturb_enabled) {
  55. proto::NotificationMode notification_mode =
  56. do_not_disturb_enabled ? proto::NotificationMode::DO_NOT_DISTURB_ON
  57. : proto::NotificationMode::DO_NOT_DISTURB_OFF;
  58. proto::UpdateNotificationModeRequest request;
  59. request.set_notification_mode(notification_mode);
  60. SendMessage(proto::MessageType::UPDATE_NOTIFICATION_MODE_REQUEST, &request);
  61. }
  62. void MessageSenderImpl::SendUpdateBatteryModeRequest(
  63. bool battery_saver_mode_enabled) {
  64. proto::BatteryMode battery_mode = battery_saver_mode_enabled
  65. ? proto::BatteryMode::BATTERY_SAVER_ON
  66. : proto::BatteryMode::BATTERY_SAVER_OFF;
  67. proto::UpdateBatteryModeRequest request;
  68. request.set_battery_mode(battery_mode);
  69. SendMessage(proto::MessageType::UPDATE_BATTERY_MODE_REQUEST, &request);
  70. }
  71. void MessageSenderImpl::SendDismissNotificationRequest(
  72. int64_t notification_id) {
  73. proto::DismissNotificationRequest request;
  74. request.set_notification_id(notification_id);
  75. SendMessage(proto::MessageType::DISMISS_NOTIFICATION_REQUEST, &request);
  76. }
  77. void MessageSenderImpl::SendNotificationInlineReplyRequest(
  78. int64_t notification_id,
  79. const std::u16string& reply_text) {
  80. proto::NotificationInlineReplyRequest request;
  81. request.set_notification_id(notification_id);
  82. request.set_reply_text(base::UTF16ToUTF8(reply_text));
  83. SendMessage(proto::MessageType::NOTIFICATION_INLINE_REPLY_REQUEST, &request);
  84. }
  85. void MessageSenderImpl::SendShowNotificationAccessSetupRequest() {
  86. proto::ShowNotificationAccessSetupRequest request;
  87. SendMessage(proto::MessageType::SHOW_NOTIFICATION_ACCESS_SETUP_REQUEST,
  88. &request);
  89. }
  90. void MessageSenderImpl::SendFeatureSetupRequest(bool camera_roll,
  91. bool notifications) {
  92. proto::FeatureSetupRequest request;
  93. request.set_camera_roll_setup_requested(camera_roll);
  94. request.set_notification_setup_requested(notifications);
  95. SendMessage(proto::MessageType::FEATURE_SETUP_REQUEST, &request);
  96. }
  97. void MessageSenderImpl::SendRingDeviceRequest(bool device_ringing_enabled) {
  98. proto::FindMyDeviceRingStatus ringing_enabled =
  99. device_ringing_enabled ? proto::FindMyDeviceRingStatus::RINGING
  100. : proto::FindMyDeviceRingStatus::NOT_RINGING;
  101. proto::RingDeviceRequest request;
  102. request.set_ring_status(ringing_enabled);
  103. SendMessage(proto::MessageType::RING_DEVICE_REQUEST, &request);
  104. }
  105. void MessageSenderImpl::SendFetchCameraRollItemsRequest(
  106. const proto::FetchCameraRollItemsRequest& request) {
  107. SendMessage(proto::MessageType::FETCH_CAMERA_ROLL_ITEMS_REQUEST, &request);
  108. }
  109. void MessageSenderImpl::SendFetchCameraRollItemDataRequest(
  110. const proto::FetchCameraRollItemDataRequest& request) {
  111. SendMessage(proto::MessageType::FETCH_CAMERA_ROLL_ITEM_DATA_REQUEST,
  112. &request);
  113. }
  114. void MessageSenderImpl::SendInitiateCameraRollItemTransferRequest(
  115. const proto::InitiateCameraRollItemTransferRequest& request) {
  116. SendMessage(proto::MessageType::INITIATE_CAMERA_ROLL_ITEM_TRANSFER_REQUEST,
  117. &request);
  118. }
  119. void MessageSenderImpl::SendMessage(
  120. proto::MessageType message_type,
  121. const google::protobuf::MessageLite* request) {
  122. connection_manager_->SendMessage(SerializeMessage(message_type, request));
  123. UMA_HISTOGRAM_ENUMERATION("PhoneHub.Usage.SentMessageTypeCount", message_type,
  124. proto::MessageType_MAX);
  125. util::LogMessageResult(message_type,
  126. util::PhoneHubMessageResult::kRequestAttempted);
  127. }
  128. } // namespace phonehub
  129. } // namespace ash