1 // Copyright (c) 2011 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 "chrome/browser/chromeos/sms_observer.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/chromeos/cros/cros_library.h"
9 #include "chrome/browser/chromeos/notifications/system_notification.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "grit/generated_resources.h"
12 #include "grit/theme_resources.h"
13 #include "third_party/cros/chromeos_network.h"
14 #include "ui/base/l10n/l10n_util.h"
15
16 namespace chromeos {
17
SmsObserver(Profile * profile)18 SmsObserver::SmsObserver(Profile* profile)
19 : profile_(profile) {
20 DCHECK(profile_);
21 if (!CrosLibrary::Get()->EnsureLoaded())
22 return;
23
24 UpdateObservers(chromeos::CrosLibrary::Get()->GetNetworkLibrary());
25 }
26
~SmsObserver()27 SmsObserver::~SmsObserver() {
28 NetworkLibrary* library = chromeos::CrosLibrary::Get()->GetNetworkLibrary();
29 library->RemoveNetworkManagerObserver(this);
30 DisconnectAll();
31 }
32
UpdateObservers(NetworkLibrary * library)33 void SmsObserver::UpdateObservers(NetworkLibrary* library) {
34 if (!CrosLibrary::Get()->EnsureLoaded())
35 return;
36
37 const CellularNetworkVector& networks = library->cellular_networks();
38 // Remove monitors for networks that are not in the list anymore.
39 for (ObserversMap::iterator it_observer = observers_.begin();
40 it_observer != observers_.end();) {
41 bool found = false;
42 for (CellularNetworkVector::const_iterator it_network = networks.begin();
43 it_network != networks.end(); ++it_network) {
44 if (it_observer->first == (*it_network)->device_path()) {
45 found = true;
46 break;
47 }
48 }
49 if (!found) {
50 VLOG(1) << "Remove SMS monitor for " << it_observer->first;
51 DisconnectSMSMonitor(it_observer->second);
52 observers_.erase(it_observer++);
53 } else {
54 ++it_observer;
55 }
56 }
57
58 // Add monitors for new networks.
59 for (CellularNetworkVector::const_iterator it_network = networks.begin();
60 it_network != networks.end(); ++it_network) {
61 ObserversMap::iterator it_observer =
62 observers_.find((*it_network)->device_path());
63 if (it_observer == observers_.end()) {
64 VLOG(1) << "Add SMS monitor for " << (*it_network)->device_path();
65 observers_.insert(ObserversMap::value_type((*it_network)->device_path(),
66 MonitorSMS((*it_network)->device_path().c_str(),
67 &StaticCallback, this)));
68 } else {
69 VLOG(1) << "Already has SMS monitor for " << (*it_network)->device_path();
70 }
71 }
72 }
73
DisconnectAll()74 void SmsObserver::DisconnectAll() {
75 if (!CrosLibrary::Get()->EnsureLoaded())
76 return;
77
78 for (ObserversMap::iterator it = observers_.begin();
79 it != observers_.end(); ++it) {
80 VLOG(1) << "Remove SMS monitor for " << it->first;
81 DisconnectSMSMonitor(it->second);
82 }
83 observers_.clear();
84 }
85
OnNetworkManagerChanged(NetworkLibrary * library)86 void SmsObserver::OnNetworkManagerChanged(NetworkLibrary* library) {
87 UpdateObservers(library);
88 }
89
90 // static
StaticCallback(void * object,const char * modem_device_path,const SMS * message)91 void SmsObserver::StaticCallback(void* object,
92 const char* modem_device_path,
93 const SMS* message) {
94 SmsObserver* monitor = static_cast<SmsObserver*>(object);
95 monitor->OnNewMessage(modem_device_path, message);
96 }
97
OnNewMessage(const char * modem_device_path,const SMS * message)98 void SmsObserver::OnNewMessage(const char* modem_device_path,
99 const SMS* message) {
100 VLOG(1) << "New message notification from " << message->number
101 << " text: " << message->text;
102
103 SystemNotification note(
104 profile_,
105 "incoming _sms.chromeos",
106 IDR_NOTIFICATION_SMS,
107 l10n_util::GetStringFUTF16(
108 IDS_SMS_NOTIFICATION_TITLE, UTF8ToUTF16(message->number)));
109
110 note.Show(UTF8ToUTF16(message->text), true, false);
111 }
112
113 } // namespace chromeos
114