Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / sms_observer.cc
blob431fc8725b9f781b93a9aca262e55b9b070b1840
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"
7 #include "ash/shell.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"
17 namespace chromeos {
19 SmsObserver::SmsObserver(Profile* profile)
20 : profile_(profile) {
21 DCHECK(profile_);
22 UpdateObservers(chromeos::CrosLibrary::Get()->GetNetworkLibrary());
25 SmsObserver::~SmsObserver() {
26 NetworkLibrary* library = chromeos::CrosLibrary::Get()->GetNetworkLibrary();
27 library->RemoveNetworkManagerObserver(this);
28 DisconnectAll();
31 void SmsObserver::UpdateObservers(NetworkLibrary* library) {
32 // Guard against calls to libcros (http://crosbug.com/17863).
33 if (!CrosLibrary::Get()->libcros_loaded())
34 return;
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();) {
40 bool found = false;
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()) {
44 found = true;
45 break;
48 if (!found) {
49 VLOG(1) << "Remove SMS monitor for " << it_observer->first;
50 delete it_observer->second;
51 observers_.erase(it_observer++);
52 } else {
53 ++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();
64 continue;
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));
74 } else {
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())
83 return;
85 for (ObserversMap::iterator it = observers_.begin();
86 it != observers_.end(); ++it) {
87 VLOG(1) << "Remove SMS monitor for " << it->first;
88 delete it->second;
90 observers_.clear();
93 void SmsObserver::OnNetworkManagerChanged(NetworkLibrary* library) {
94 UpdateObservers(library);
97 void SmsObserver::OnNewMessage(const std::string& modem_device_path,
98 const SMS& message) {
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())
108 return;
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