summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/sms_observer.cc
blob: 6d7729107fe2d0d9b451997ae2096226f012bffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/sms_observer.h"

#include "ash/shell.h"
#include "ash/system/chromeos/network/sms_observer.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chromeos/network/cros_network_functions.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace chromeos {

SmsObserver::SmsObserver(Profile* profile)
    : profile_(profile) {
  DCHECK(profile_);
  UpdateObservers(chromeos::CrosLibrary::Get()->GetNetworkLibrary());
}

SmsObserver::~SmsObserver() {
  NetworkLibrary* library = chromeos::CrosLibrary::Get()->GetNetworkLibrary();
  library->RemoveNetworkManagerObserver(this);
  DisconnectAll();
}

void SmsObserver::UpdateObservers(NetworkLibrary* library) {
  // Guard against calls to libcros (http://crosbug.com/17863).
  if (!CrosLibrary::Get()->libcros_loaded())
    return;

  const CellularNetworkVector& networks = library->cellular_networks();
  // Remove monitors for networks that are not in the list anymore.
  for (ObserversMap::iterator it_observer = observers_.begin();
       it_observer != observers_.end();) {
    bool found = false;
    for (CellularNetworkVector::const_iterator it_network = networks.begin();
        it_network != networks.end(); ++it_network) {
      if (it_observer->first == (*it_network)->device_path()) {
        found = true;
        break;
      }
    }
    if (!found) {
      VLOG(1) << "Remove SMS monitor for " << it_observer->first;
      delete it_observer->second;
      observers_.erase(it_observer++);
    } else {
      ++it_observer;
    }
  }

  // Add monitors for new networks.
  for (CellularNetworkVector::const_iterator it_network = networks.begin();
       it_network != networks.end(); ++it_network) {
    const std::string& device_path((*it_network)->device_path());
    if (device_path.empty()) {
      LOG(WARNING) << "Cellular Network has empty device path: "
                   << (*it_network)->name();
      continue;
    }
    ObserversMap::iterator it_observer = observers_.find(device_path);
    if (it_observer == observers_.end()) {
      VLOG(1) << "Add SMS monitor for " << device_path;
      CrosNetworkWatcher* watcher =
          CrosMonitorSMS(device_path,
                         base::Bind(&SmsObserver::OnNewMessage,
                                    base::Unretained(this)));
      observers_.insert(ObserversMap::value_type(device_path, watcher));
    } else {
      VLOG(1) << "Already has SMS monitor for " << device_path;
    }
  }
}

void SmsObserver::DisconnectAll() {
  // Guard against calls to libcros (http://crosbug.com/17863).
  if (!CrosLibrary::Get()->libcros_loaded())
    return;

  for (ObserversMap::iterator it = observers_.begin();
       it != observers_.end(); ++it) {
    VLOG(1) << "Remove SMS monitor for " << it->first;
    delete it->second;
  }
  observers_.clear();
}

void SmsObserver::OnNetworkManagerChanged(NetworkLibrary* library) {
  UpdateObservers(library);
}

void SmsObserver::OnNewMessage(const std::string& modem_device_path,
                               const SMS& message) {
  VLOG(1) << "New message notification from " << message.number
          << " text: " << message.text;

  // Don't show empty messages. Such messages most probably "Message Waiting
  // Indication" and it should be determined by data-coding-scheme,
  // see crosbug.com/27883. But this field is not exposed from libcros.
  // TODO(dpokuhin): when libcros refactoring done, implement check for
  // "Message Waiting Indication" to filter out such messages.
  if (message.text.empty())
    return;

  // Add an Ash SMS notification. TODO(stevenjb): Replace this code with
  // NetworkSmsHandler when fixed: crbug.com/133416.
  base::DictionaryValue dict;
  dict.SetString(ash::kSmsNumberKey, message.number);
  dict.SetString(ash::kSmsTextKey, message.text);
  ash::Shell::GetInstance()->system_tray_notifier()->NotifyAddSmsMessage(dict);
}

}  // namespace chromeos