bluetooth: Shutdown BluetoothAdapter before DBus on ChromeOS.
[chromium-blink-merge.git] / chromeos / dbus / modem_messaging_client.cc
blobce4929dc9b91c8183783788b1764fdc255518918
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.
4 #include "chromeos/dbus/modem_messaging_client.h"
6 #include <map>
7 #include <utility>
9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h"
13 #include "base/values.h"
14 #include "dbus/bus.h"
15 #include "dbus/message.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 namespace {
23 // A class which makes method calls for SMS services via the
24 // org.freedesktop.ModemManager1.Messaging object.
25 class ModemMessagingProxy {
26 public:
27 typedef ModemMessagingClient::SmsReceivedHandler SmsReceivedHandler;
28 typedef ModemMessagingClient::ListCallback ListCallback;
29 typedef ModemMessagingClient::DeleteCallback DeleteCallback;
31 ModemMessagingProxy(dbus::Bus* bus,
32 const std::string& service_name,
33 const dbus::ObjectPath& object_path)
34 : proxy_(bus->GetObjectProxy(service_name, object_path)),
35 service_name_(service_name),
36 weak_ptr_factory_(this) {
37 proxy_->ConnectToSignal(
38 modemmanager::kModemManager1MessagingInterface,
39 modemmanager::kSMSAddedSignal,
40 base::Bind(&ModemMessagingProxy::OnSmsAdded,
41 weak_ptr_factory_.GetWeakPtr()),
42 base::Bind(&ModemMessagingProxy::OnSignalConnected,
43 weak_ptr_factory_.GetWeakPtr()));
45 virtual ~ModemMessagingProxy() {}
47 // Sets SmsReceived signal handler.
48 void SetSmsReceivedHandler(const SmsReceivedHandler& handler) {
49 DCHECK(sms_received_handler_.is_null());
50 sms_received_handler_ = handler;
53 // Resets SmsReceived signal handler.
54 void ResetSmsReceivedHandler() {
55 sms_received_handler_.Reset();
58 // Calls Delete method.
59 void Delete(const dbus::ObjectPath& message_path,
60 const DeleteCallback& callback) {
61 dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
62 modemmanager::kSMSDeleteFunction);
63 dbus::MessageWriter writer(&method_call);
64 writer.AppendObjectPath(message_path);
65 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
66 base::Bind(&ModemMessagingProxy::OnDelete,
67 weak_ptr_factory_.GetWeakPtr(),
68 callback));
71 // Calls List method.
72 virtual void List(const ListCallback& callback) {
73 dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
74 modemmanager::kSMSListFunction);
75 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
76 base::Bind(&ModemMessagingProxy::OnList,
77 weak_ptr_factory_.GetWeakPtr(),
78 callback));
81 private:
82 // Handles SmsAdded signal.
83 void OnSmsAdded(dbus::Signal* signal) {
84 dbus::ObjectPath message_path;
85 bool complete = false;
86 dbus::MessageReader reader(signal);
87 if (!reader.PopObjectPath(&message_path) ||
88 !reader.PopBool(&complete)) {
89 LOG(ERROR) << "Invalid signal: " << signal->ToString();
90 return;
92 if (!sms_received_handler_.is_null()) {
93 sms_received_handler_.Run(message_path, complete);
97 // Handles responses of Delete method calls.
98 void OnDelete(const DeleteCallback& callback, dbus::Response* response) {
99 if (!response)
100 return;
101 callback.Run();
104 // Handles responses of List method calls.
105 void OnList(const ListCallback& callback, dbus::Response* response) {
106 if (!response)
107 return;
108 dbus::MessageReader reader(response);
109 std::vector<dbus::ObjectPath> sms_paths;
110 if (!reader.PopArrayOfObjectPaths(&sms_paths))
111 LOG(WARNING) << "Invalid response: " << response->ToString();
112 callback.Run(sms_paths);
115 // Handles the result of signal connection setup.
116 void OnSignalConnected(const std::string& interface,
117 const std::string& signal,
118 bool succeeded) {
119 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " "
120 << signal << " failed.";
123 dbus::ObjectProxy* proxy_;
124 std::string service_name_;
125 SmsReceivedHandler sms_received_handler_;
127 // Note: This should remain the last member so it'll be destroyed and
128 // invalidate its weak pointers before any other members are destroyed.
129 base::WeakPtrFactory<ModemMessagingProxy> weak_ptr_factory_;
131 DISALLOW_COPY_AND_ASSIGN(ModemMessagingProxy);
134 class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient {
135 public:
136 ModemMessagingClientImpl()
137 : bus_(NULL),
138 proxies_deleter_(&proxies_) {
141 void SetSmsReceivedHandler(const std::string& service_name,
142 const dbus::ObjectPath& object_path,
143 const SmsReceivedHandler& handler) override {
144 GetProxy(service_name, object_path)->SetSmsReceivedHandler(handler);
147 void ResetSmsReceivedHandler(const std::string& service_name,
148 const dbus::ObjectPath& object_path) override {
149 GetProxy(service_name, object_path)->ResetSmsReceivedHandler();
152 void Delete(const std::string& service_name,
153 const dbus::ObjectPath& object_path,
154 const dbus::ObjectPath& sms_path,
155 const DeleteCallback& callback) override {
156 GetProxy(service_name, object_path)->Delete(sms_path, callback);
159 void List(const std::string& service_name,
160 const dbus::ObjectPath& object_path,
161 const ListCallback& callback) override {
162 GetProxy(service_name, object_path)->List(callback);
165 protected:
166 void Init(dbus::Bus* bus) override { bus_ = bus; };
168 private:
169 typedef std::map<std::pair<std::string, std::string>, ModemMessagingProxy*>
170 ProxyMap;
172 // Returns a SMSProxy for the given service name and object path.
173 ModemMessagingProxy* GetProxy(const std::string& service_name,
174 const dbus::ObjectPath& object_path) {
175 const ProxyMap::key_type key(service_name, object_path.value());
176 ProxyMap::iterator it = proxies_.find(key);
177 if (it != proxies_.end())
178 return it->second;
180 // There is no proxy for the service_name and object_path, create it.
181 ModemMessagingProxy* proxy
182 = new ModemMessagingProxy(bus_, service_name, object_path);
183 proxies_.insert(ProxyMap::value_type(key, proxy));
184 return proxy;
187 dbus::Bus* bus_;
188 ProxyMap proxies_;
189 STLValueDeleter<ProxyMap> proxies_deleter_;
191 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl);
194 } // namespace
196 ////////////////////////////////////////////////////////////////////////////////
197 // ModemMessagingClient
199 ModemMessagingClient::ModemMessagingClient() {}
201 ModemMessagingClient::~ModemMessagingClient() {}
204 // static
205 ModemMessagingClient* ModemMessagingClient::Create() {
206 return new ModemMessagingClientImpl();
210 } // namespace chromeos