Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: ash/common/system/chromeos/network/sms_observer.cc

Issue 2690543003: Send notification to users upon receiving sms messages (Closed)
Patch Set: address comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
James Cook 2017/02/15 19:49:48 nit: blank line below
22 // Send the |message| to notification center to display to users. Note that each
23 // notification will be assigned with different |message_id| as notification id.
24 void ShowNotification(const base::DictionaryValue* message,
25 const std::string& message_text,
26 const std::string& message_number,
27 int message_id) {
28 message_center::MessageCenter* message_center =
29 message_center::MessageCenter::Get();
30 if (!message_center)
31 return;
32
33 const char kNotificationId[] = "chrome://network/sms";
34 std::unique_ptr<message_center::Notification> notification;
35
36 notification = base::MakeUnique<message_center::Notification>(
37 message_center::NOTIFICATION_TYPE_SIMPLE,
38 kNotificationId + std::to_string(message_id),
39 base::ASCIIToUTF16(message_number), base::ASCIIToUTF16(message_text),
40 gfx::Image(gfx::CreateVectorIcon(
41 ash::kSystemMenuSmsIcon, ash::kMenuIconSize, ash::kMenuIconColor)),
42 base::string16(), GURL(),
43 message_center::NotifierId(message_center::NotifierId::APPLICATION,
44 ash::system_notifier::kNotifierSms),
45 message_center::RichNotificationData(), nullptr);
46 message_center->AddNotification(std::move(notification));
47 }
48
49 } // namespace
50
51 SmsObserver::SmsObserver() {
52 // TODO(armansito): SMS could be a special case for cellular that requires a
53 // user (perhaps the owner) to be logged in. If that is the case, then an
54 // additional check should be done before subscribing for SMS notifications.
55 if (NetworkHandler::IsInitialized())
56 NetworkHandler::Get()->network_sms_handler()->AddObserver(this);
57 }
58
59 SmsObserver::~SmsObserver() {
60 if (NetworkHandler::IsInitialized()) {
61 NetworkHandler::Get()->network_sms_handler()->RemoveObserver(this);
62 }
63 }
64
65 void SmsObserver::MessageReceived(const base::DictionaryValue& message) {
66 std::string message_text;
67 if (!message.GetStringWithoutPathExpansion(
68 chromeos::NetworkSmsHandler::kTextKey, &message_text)) {
69 NET_LOG(ERROR) << "SMS message contains no content.";
70 return;
71 }
72 // TODO(armansito): A message might be due to a special "Message Waiting"
73 // state that the message is in. Once SMS handling moves to shill, such
74 // messages should be filtered there so that this check becomes unnecessary.
75 if (message_text.empty()) {
76 NET_LOG(DEBUG) << "SMS has empty content text. Ignoring.";
77 return;
78 }
79 std::string message_number;
80 if (!message.GetStringWithoutPathExpansion(
81 chromeos::NetworkSmsHandler::kNumberKey, &message_number)) {
82 NET_LOG(DEBUG) << "SMS contains no number. Ignoring.";
83 return;
84 }
85
86 NET_LOG(DEBUG) << "Received SMS from: " << message_number
87 << " with text: " << message_text;
88 message_id_++;
89 ShowNotification(&message, message_text, message_number, message_id_);
90 }
91
92 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/network/sms_observer.h ('k') | ash/common/system/chromeos/network/sms_observer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698