Index: ash/common/system/chromeos/network/sms_observer.cc |
diff --git a/ash/common/system/chromeos/network/sms_observer.cc b/ash/common/system/chromeos/network/sms_observer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..804049cc0f75a8118bd89ff8064e0d1c89d94090 |
--- /dev/null |
+++ b/ash/common/system/chromeos/network/sms_observer.cc |
@@ -0,0 +1,84 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+#include "ash/common/system/chromeos/network/sms_observer.h" |
James Cook
2017/02/14 02:04:38
nit: blank line above
yiyix
2017/02/15 19:41:39
Done.
|
+ |
+#include "ash/common/system/system_notifier.h" |
+#include "ash/common/system/tray/tray_constants.h" |
+#include "ash/resources/vector_icons/vector_icons.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "chromeos/network/network_event_log.h" |
+#include "chromeos/network/network_handler.h" |
+#include "ui/gfx/paint_vector_icon.h" |
+#include "ui/message_center/message_center.h" |
+ |
+using chromeos::NetworkHandler; |
+ |
+// Send the |message| to notification center to display to users. Note that each |
+// notification will be assigned with different |message_id| as notification id. |
+void ShowNotification(const base::DictionaryValue* message, |
James Cook
2017/02/14 02:04:38
put in an anonymous namespace
What I usually do i
yiyix
2017/02/15 19:41:39
Thanks. I will adapt to this form.
|
+ const std::string& message_text, |
+ const std::string& message_number, |
+ int message_id) { |
+ const char kNotificationId[] = "chrome://network/sms"; |
+ std::unique_ptr<message_center::Notification> notification; |
+ |
+ notification = base::MakeUnique<message_center::Notification>( |
+ message_center::NOTIFICATION_TYPE_SIMPLE, |
+ kNotificationId + std::to_string(message_id), |
+ base::ASCIIToUTF16(message_number), base::ASCIIToUTF16(message_text), |
+ gfx::Image(gfx::CreateVectorIcon( |
+ ash::kSystemMenuSmsIcon, ash::kMenuIconSize, ash::kMenuIconColor)), |
+ base::string16(), GURL(), |
+ message_center::NotifierId(message_center::NotifierId::APPLICATION, |
+ ash::system_notifier::kNotifierSms), |
+ message_center::RichNotificationData(), nullptr); |
+ message_center::MessageCenter::Get()->AddNotification( |
stevenjb
2017/02/14 00:47:21
Now that this is owned by ash::Shell, I don't thin
yiyix
2017/02/15 19:41:39
would be a good check. Thanks.
|
+ std::move(notification)); |
+} |
+ |
+namespace ash { |
+ |
+SmsObserver::SmsObserver() { |
+ // TODO(armansito): SMS could be a special case for cellular that requires a |
+ // user (perhaps the owner) to be logged in. If that is the case, then an |
+ // additional check should be done before subscribing for SMS notifications. |
+ if (NetworkHandler::IsInitialized()) |
+ NetworkHandler::Get()->network_sms_handler()->AddObserver(this); |
+} |
+ |
+SmsObserver::~SmsObserver() { |
+ if (NetworkHandler::IsInitialized()) { |
+ NetworkHandler::Get()->network_sms_handler()->RemoveObserver(this); |
+ } |
+} |
+ |
+void SmsObserver::MessageReceived(const base::DictionaryValue& message) { |
+ std::string message_text; |
+ if (!message.GetStringWithoutPathExpansion( |
+ chromeos::NetworkSmsHandler::kTextKey, &message_text)) { |
+ NET_LOG(ERROR) << "SMS message contains no content."; |
+ return; |
+ } |
+ // TODO(armansito): A message might be due to a special "Message Waiting" |
+ // state that the message is in. Once SMS handling moves to shill, such |
+ // messages should be filtered there so that this check becomes unnecessary. |
+ if (message_text.empty()) { |
+ NET_LOG(DEBUG) << "SMS has empty content text. Ignoring."; |
+ return; |
+ } |
+ std::string message_number; |
+ if (!message.GetStringWithoutPathExpansion( |
+ chromeos::NetworkSmsHandler::kNumberKey, &message_number)) { |
+ NET_LOG(DEBUG) << "SMS contains no number. Ignoring."; |
+ return; |
+ } |
+ |
+ NET_LOG(DEBUG) << "Received SMS from: " << message_number |
+ << " with text: " << message_text; |
+ message_id_++; |
+ ShowNotification(&message, message_text, message_number, message_id_); |
+} |
+ |
+} // namespace ash |