1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/sms_observer.h"
8 #include "ash/system/chromeos/network/sms_observer.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "ash/system/tray/system_tray_notifier.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/chromeos/cros/cros_library.h"
13 #include "grit/generated_resources.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
19 SmsObserver::SmsObserver(Profile
* profile
)
22 UpdateObservers(chromeos::CrosLibrary::Get()->GetNetworkLibrary());
25 SmsObserver::~SmsObserver() {
26 NetworkLibrary
* library
= chromeos::CrosLibrary::Get()->GetNetworkLibrary();
27 library
->RemoveNetworkManagerObserver(this);
31 void SmsObserver::UpdateObservers(NetworkLibrary
* library
) {
32 // Guard against calls to libcros (http://crosbug.com/17863).
33 if (!CrosLibrary::Get()->libcros_loaded())
36 const CellularNetworkVector
& networks
= library
->cellular_networks();
37 // Remove monitors for networks that are not in the list anymore.
38 for (ObserversMap::iterator it_observer
= observers_
.begin();
39 it_observer
!= observers_
.end();) {
41 for (CellularNetworkVector::const_iterator it_network
= networks
.begin();
42 it_network
!= networks
.end(); ++it_network
) {
43 if (it_observer
->first
== (*it_network
)->device_path()) {
49 VLOG(1) << "Remove SMS monitor for " << it_observer
->first
;
50 delete it_observer
->second
;
51 observers_
.erase(it_observer
++);
57 // Add monitors for new networks.
58 for (CellularNetworkVector::const_iterator it_network
= networks
.begin();
59 it_network
!= networks
.end(); ++it_network
) {
60 const std::string
& device_path((*it_network
)->device_path());
61 if (device_path
.empty()) {
62 LOG(WARNING
) << "Cellular Network has empty device path: "
63 << (*it_network
)->name();
66 ObserversMap::iterator it_observer
= observers_
.find(device_path
);
67 if (it_observer
== observers_
.end()) {
68 VLOG(1) << "Add SMS monitor for " << device_path
;
69 CrosNetworkWatcher
* watcher
=
70 CrosMonitorSMS(device_path
,
71 base::Bind(&SmsObserver::OnNewMessage
,
72 base::Unretained(this)));
73 observers_
.insert(ObserversMap::value_type(device_path
, watcher
));
75 VLOG(1) << "Already has SMS monitor for " << device_path
;
80 void SmsObserver::DisconnectAll() {
81 // Guard against calls to libcros (http://crosbug.com/17863).
82 if (!CrosLibrary::Get()->libcros_loaded())
85 for (ObserversMap::iterator it
= observers_
.begin();
86 it
!= observers_
.end(); ++it
) {
87 VLOG(1) << "Remove SMS monitor for " << it
->first
;
93 void SmsObserver::OnNetworkManagerChanged(NetworkLibrary
* library
) {
94 UpdateObservers(library
);
97 void SmsObserver::OnNewMessage(const std::string
& modem_device_path
,
99 VLOG(1) << "New message notification from " << message
.number
100 << " text: " << message
.text
;
102 // Don't show empty messages. Such messages most probably "Message Waiting
103 // Indication" and it should be determined by data-coding-scheme,
104 // see crosbug.com/27883. But this field is not exposed from libcros.
105 // TODO(dpokuhin): when libcros refactoring done, implement check for
106 // "Message Waiting Indication" to filter out such messages.
107 if (message
.text
.empty())
110 // Add an Ash SMS notification. TODO(stevenjb): Replace this code with
111 // NetworkSmsHandler when fixed: crbug.com/133416.
112 base::DictionaryValue dict
;
113 dict
.SetString(ash::kSmsNumberKey
, message
.number
);
114 dict
.SetString(ash::kSmsTextKey
, message
.text
);
115 ash::Shell::GetInstance()->system_tray_notifier()->NotifyAddSmsMessage(dict
);
118 } // namespace chromeos