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"
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"
15 #include "dbus/message.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
23 // A class which makes method calls for SMS services via the
24 // org.freedesktop.ModemManager1.Messaging object.
25 class ModemMessagingProxy
{
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
)
35 proxy_(bus
->GetObjectProxy(service_name
, object_path
)),
36 service_name_(service_name
),
37 weak_ptr_factory_(this) {
38 proxy_
->ConnectToSignal(
39 modemmanager::kModemManager1MessagingInterface
,
40 modemmanager::kSMSAddedSignal
,
41 base::Bind(&ModemMessagingProxy::OnSmsAdded
,
42 weak_ptr_factory_
.GetWeakPtr()),
43 base::Bind(&ModemMessagingProxy::OnSignalConnected
,
44 weak_ptr_factory_
.GetWeakPtr()));
46 virtual ~ModemMessagingProxy() {}
48 // Sets SmsReceived signal handler.
49 void SetSmsReceivedHandler(const SmsReceivedHandler
& handler
) {
50 DCHECK(sms_received_handler_
.is_null());
51 sms_received_handler_
= handler
;
54 // Resets SmsReceived signal handler.
55 void ResetSmsReceivedHandler() {
56 sms_received_handler_
.Reset();
59 // Calls Delete method.
60 void Delete(const dbus::ObjectPath
& message_path
,
61 const DeleteCallback
& callback
) {
62 dbus::MethodCall
method_call(modemmanager::kModemManager1MessagingInterface
,
63 modemmanager::kSMSDeleteFunction
);
64 dbus::MessageWriter
writer(&method_call
);
65 writer
.AppendObjectPath(message_path
);
66 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
67 base::Bind(&ModemMessagingProxy::OnDelete
,
68 weak_ptr_factory_
.GetWeakPtr(),
73 virtual void List(const ListCallback
& callback
) {
74 dbus::MethodCall
method_call(modemmanager::kModemManager1MessagingInterface
,
75 modemmanager::kSMSListFunction
);
76 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
77 base::Bind(&ModemMessagingProxy::OnList
,
78 weak_ptr_factory_
.GetWeakPtr(),
83 // Handles SmsAdded signal.
84 void OnSmsAdded(dbus::Signal
* signal
) {
85 dbus::ObjectPath message_path
;
86 bool complete
= false;
87 dbus::MessageReader
reader(signal
);
88 if (!reader
.PopObjectPath(&message_path
) ||
89 !reader
.PopBool(&complete
)) {
90 LOG(ERROR
) << "Invalid signal: " << signal
->ToString();
93 if (!sms_received_handler_
.is_null()) {
94 sms_received_handler_
.Run(message_path
, complete
);
98 // Handles responses of Delete method calls.
99 void OnDelete(const DeleteCallback
& callback
, dbus::Response
* response
) {
105 // Handles responses of List method calls.
106 void OnList(const ListCallback
& callback
, dbus::Response
* response
) {
109 dbus::MessageReader
reader(response
);
110 std::vector
<dbus::ObjectPath
> sms_paths
;
111 if (!reader
.PopArrayOfObjectPaths(&sms_paths
))
112 LOG(WARNING
) << "Invalid response: " << response
->ToString();
113 callback
.Run(sms_paths
);
116 // Handles the result of signal connection setup.
117 void OnSignalConnected(const std::string
& interface
,
118 const std::string
& signal
,
120 LOG_IF(ERROR
, !succeeded
) << "Connect to " << interface
<< " "
121 << signal
<< " failed.";
125 dbus::ObjectProxy
* proxy_
;
126 std::string service_name_
;
127 SmsReceivedHandler sms_received_handler_
;
129 // Note: This should remain the last member so it'll be destroyed and
130 // invalidate its weak pointers before any other members are destroyed.
131 base::WeakPtrFactory
<ModemMessagingProxy
> weak_ptr_factory_
;
133 DISALLOW_COPY_AND_ASSIGN(ModemMessagingProxy
);
136 class CHROMEOS_EXPORT ModemMessagingClientImpl
: public ModemMessagingClient
{
138 ModemMessagingClientImpl()
140 proxies_deleter_(&proxies_
) {
143 virtual void SetSmsReceivedHandler(
144 const std::string
& service_name
,
145 const dbus::ObjectPath
& object_path
,
146 const SmsReceivedHandler
& handler
) OVERRIDE
{
147 GetProxy(service_name
, object_path
)->SetSmsReceivedHandler(handler
);
150 virtual void ResetSmsReceivedHandler(
151 const std::string
& service_name
,
152 const dbus::ObjectPath
& object_path
) OVERRIDE
{
153 GetProxy(service_name
, object_path
)->ResetSmsReceivedHandler();
156 virtual void Delete(const std::string
& service_name
,
157 const dbus::ObjectPath
& object_path
,
158 const dbus::ObjectPath
& sms_path
,
159 const DeleteCallback
& callback
) OVERRIDE
{
160 GetProxy(service_name
, object_path
)->Delete(sms_path
, callback
);
163 virtual void List(const std::string
& service_name
,
164 const dbus::ObjectPath
& object_path
,
165 const ListCallback
& callback
) OVERRIDE
{
166 GetProxy(service_name
, object_path
)->List(callback
);
170 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
175 typedef std::map
<std::pair
<std::string
, std::string
>, ModemMessagingProxy
*>
178 // Returns a SMSProxy for the given service name and object path.
179 ModemMessagingProxy
* GetProxy(const std::string
& service_name
,
180 const dbus::ObjectPath
& object_path
) {
181 const ProxyMap::key_type
key(service_name
, object_path
.value());
182 ProxyMap::iterator it
= proxies_
.find(key
);
183 if (it
!= proxies_
.end())
186 // There is no proxy for the service_name and object_path, create it.
187 ModemMessagingProxy
* proxy
188 = new ModemMessagingProxy(bus_
, service_name
, object_path
);
189 proxies_
.insert(ProxyMap::value_type(key
, proxy
));
195 STLValueDeleter
<ProxyMap
> proxies_deleter_
;
197 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl
);
202 ////////////////////////////////////////////////////////////////////////////////
203 // ModemMessagingClient
205 ModemMessagingClient::ModemMessagingClient() {}
207 ModemMessagingClient::~ModemMessagingClient() {}
211 ModemMessagingClient
* ModemMessagingClient::Create() {
212 return new ModemMessagingClientImpl();
216 } // namespace chromeos