sms_observer.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2017 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/system/network/sms_observer.h"
  5. #include <memory>
  6. #include "ash/constants/notifier_catalogs.h"
  7. #include "ash/public/cpp/notification_utils.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/system/tray/tray_constants.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/values.h"
  13. #include "chromeos/ash/components/network/network_event_log.h"
  14. #include "chromeos/ash/components/network/network_handler.h"
  15. #include "ui/gfx/paint_vector_icon.h"
  16. #include "ui/message_center/message_center.h"
  17. namespace ash {
  18. const char SmsObserver::kNotificationPrefix[] = "chrome://network/sms";
  19. namespace {
  20. const char kNotifierSms[] = "ash.sms";
  21. // Send the |message| to notification center to display to users. Note that each
  22. // notification will be assigned with different |message_id| as notification id.
  23. void ShowNotification(const base::Value* message,
  24. const std::string& message_text,
  25. const std::string& message_number,
  26. int message_id) {
  27. message_center::MessageCenter* message_center =
  28. message_center::MessageCenter::Get();
  29. if (!message_center)
  30. return;
  31. std::unique_ptr<message_center::Notification> notification;
  32. // TODO(estade): should SMS notifications really be shown to all users?
  33. notification = ash::CreateSystemNotification(
  34. message_center::NOTIFICATION_TYPE_SIMPLE,
  35. SmsObserver::kNotificationPrefix + std::to_string(message_id),
  36. base::ASCIIToUTF16(message_number),
  37. base::CollapseWhitespace(base::UTF8ToUTF16(message_text),
  38. false /* trim_sequences_with_line_breaks */),
  39. std::u16string(), GURL(),
  40. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  41. kNotifierSms, NotificationCatalogName::kSMS),
  42. message_center::RichNotificationData(), nullptr, kNotificationSmsSyncIcon,
  43. message_center::SystemNotificationWarningLevel::NORMAL);
  44. message_center->AddNotification(std::move(notification));
  45. }
  46. } // namespace
  47. SmsObserver::SmsObserver() {
  48. // TODO(armansito): SMS could be a special case for cellular that requires a
  49. // user (perhaps the owner) to be logged in. If that is the case, then an
  50. // additional check should be done before subscribing for SMS notifications.
  51. if (NetworkHandler::IsInitialized())
  52. NetworkHandler::Get()->network_sms_handler()->AddObserver(this);
  53. }
  54. SmsObserver::~SmsObserver() {
  55. if (NetworkHandler::IsInitialized()) {
  56. NetworkHandler::Get()->network_sms_handler()->RemoveObserver(this);
  57. }
  58. }
  59. void SmsObserver::MessageReceived(const base::Value& message) {
  60. const std::string* message_text =
  61. message.FindStringKey(NetworkSmsHandler::kTextKey);
  62. if (!message_text) {
  63. NET_LOG(ERROR) << "SMS message contains no content.";
  64. return;
  65. }
  66. // TODO(armansito): A message might be due to a special "Message Waiting"
  67. // state that the message is in. Once SMS handling moves to shill, such
  68. // messages should be filtered there so that this check becomes unnecessary.
  69. if (message_text->empty()) {
  70. NET_LOG(DEBUG) << "SMS has empty content text. Ignoring.";
  71. return;
  72. }
  73. const std::string* message_number =
  74. message.FindStringKey(NetworkSmsHandler::kNumberKey);
  75. if (!message_number) {
  76. NET_LOG(DEBUG) << "SMS contains no number. Ignoring.";
  77. return;
  78. }
  79. NET_LOG(DEBUG) << "Received SMS from: " << *message_number
  80. << " with text: " << *message_text;
  81. message_id_++;
  82. ShowNotification(&message, *message_text, *message_number, message_id_);
  83. }
  84. } // namespace ash