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