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

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

Powered by Google App Engine
This is Rietveld 408576698