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

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

Issue 2583393002: Send notification to users upon receiving sms messages (Closed)
Patch Set: update return type 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 <utility>
6
7 #include "ash/common/material_design/material_design_controller.h"
8 #include "ash/common/metrics/user_metrics_action.h"
9 #include "ash/common/system/chromeos/network/sms_observer.h"
10 #include "ash/common/system/system_notifier.h"
11 #include "ash/common/system/tray/tray_constants.h"
12 #include "ash/common/wm_shell.h"
13 #include "ash/resources/vector_icons/vector_icons.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chromeos/network/network_event_log.h"
18 #include "chromeos/network/network_handler.h"
19 #include "grit/ash_resources.h"
20 #include "grit/ash_strings.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/gfx/paint_vector_icon.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/views/view.h"
26
27 namespace ash {
28
29 // Send the |message| to notification center to display to users. Note that each
30 // notification will be assigned with different |message_id| as notification id.
31 void SendMessage(const base::DictionaryValue* message,
32 std::string message_text,
33 std::string message_number,
34 int message_id) {
35 const char kNotificationId[] = "chrome://network/sms";
36 std::unique_ptr<message_center::Notification> notification;
37
38 notification.reset(new message_center::Notification(
39 message_center::NOTIFICATION_TYPE_SIMPLE,
40 kNotificationId + std::to_string(message_id),
41 base::ASCIIToUTF16(message_number), base::ASCIIToUTF16(message_text),
42 gfx::Image(
43 CreateVectorIcon(kSystemMenuSmsIcon, kMenuIconSize, kMenuIconColor)),
44 base::string16(), GURL(),
45 message_center::NotifierId(message_center::NotifierId::APPLICATION,
46 system_notifier::kNotifierSms),
47 message_center::RichNotificationData(), nullptr));
48 message_center::MessageCenter::Get()->AddNotification(
49 std::move(notification));
50 }
51
52 SmsObserver::SmsObserver() : message_id_(0) {
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 (chromeos::NetworkHandler::IsInitialized())
57 chromeos::NetworkHandler::Get()->network_sms_handler()->AddObserver(this);
58 }
59
60 SmsObserver::~SmsObserver() {
61 if (chromeos::NetworkHandler::IsInitialized()) {
62 chromeos::NetworkHandler::Get()->network_sms_handler()->RemoveObserver(
63 this);
64 }
65 }
66
67 void SmsObserver::MessageReceived(const base::DictionaryValue& message) {
68 std::string message_text;
69 if (!message.GetStringWithoutPathExpansion(
70 chromeos::NetworkSmsHandler::kTextKey, &message_text)) {
71 NET_LOG_ERROR("SMS message contains no content.", "");
72 return;
73 }
74 // TODO(armansito): A message might be due to a special "Message Waiting"
75 // state that the message is in. Once SMS handling moves to shill, such
76 // messages should be filtered there so that this check becomes unnecessary.
77 if (message_text.empty()) {
78 NET_LOG_DEBUG("SMS has empty content text. Ignoring.", "");
79 return;
80 }
81 std::string message_number;
82 if (!message.GetStringWithoutPathExpansion(
83 chromeos::NetworkSmsHandler::kNumberKey, &message_number)) {
84 NET_LOG_DEBUG("SMS contains no number. Ignoring.", "");
85 return;
86 }
87
88 NET_LOG_DEBUG(
89 "Received SMS from: " + message_number + " with text: " + message_text,
90 "");
91 message_id_++;
92 SendMessage(&message, message_text, message_number, message_id_);
93 }
94
95 } // 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