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.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 explicit ModemMessagingClientImpl(dbus::Bus
*bus
)
140 proxies_deleter_(&proxies_
) {
143 // ModemMessagingClient override.
144 virtual void SetSmsReceivedHandler(
145 const std::string
& service_name
,
146 const dbus::ObjectPath
& object_path
,
147 const SmsReceivedHandler
& handler
) OVERRIDE
{
148 GetProxy(service_name
, object_path
)->SetSmsReceivedHandler(handler
);
151 // ModemMessagingClient override.
152 virtual void ResetSmsReceivedHandler(
153 const std::string
& service_name
,
154 const dbus::ObjectPath
& object_path
) OVERRIDE
{
155 GetProxy(service_name
, object_path
)->ResetSmsReceivedHandler();
158 // ModemMessagingClient override.
159 virtual void Delete(const std::string
& service_name
,
160 const dbus::ObjectPath
& object_path
,
161 const dbus::ObjectPath
& sms_path
,
162 const DeleteCallback
& callback
) OVERRIDE
{
163 GetProxy(service_name
, object_path
)->Delete(sms_path
, callback
);
166 // ModemMessagingClient override.
167 virtual void List(const std::string
& service_name
,
168 const dbus::ObjectPath
& object_path
,
169 const ListCallback
& callback
) OVERRIDE
{
170 GetProxy(service_name
, object_path
)->List(callback
);
174 typedef std::map
<std::pair
<std::string
, std::string
>, ModemMessagingProxy
*>
177 // Returns a SMSProxy for the given service name and object path.
178 ModemMessagingProxy
* GetProxy(const std::string
& service_name
,
179 const dbus::ObjectPath
& object_path
) {
180 const ProxyMap::key_type
key(service_name
, object_path
.value());
181 ProxyMap::iterator it
= proxies_
.find(key
);
182 if (it
!= proxies_
.end())
185 // There is no proxy for the service_name and object_path, create it.
186 ModemMessagingProxy
* proxy
187 = new ModemMessagingProxy(bus_
, service_name
, object_path
);
188 proxies_
.insert(ProxyMap::value_type(key
, proxy
));
194 STLValueDeleter
<ProxyMap
> proxies_deleter_
;
196 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl
);
199 class CHROMEOS_EXPORT ModemMessagingClientStubImpl
200 : public ModemMessagingClient
{
202 ModemMessagingClientStubImpl() {}
203 virtual ~ModemMessagingClientStubImpl() {}
205 // ModemMessagingClient override.
206 virtual void SetSmsReceivedHandler(
207 const std::string
& service_name
,
208 const dbus::ObjectPath
& object_path
,
209 const SmsReceivedHandler
& handler
) OVERRIDE
{
210 DCHECK(sms_received_handler_
.is_null());
211 sms_received_handler_
= handler
;
214 // ModemMessagingClient override.
215 virtual void ResetSmsReceivedHandler(
216 const std::string
& service_name
,
217 const dbus::ObjectPath
& object_path
) OVERRIDE
{
218 sms_received_handler_
.Reset();
221 // ModemMessagingClient override.
222 virtual void Delete(const std::string
& service_name
,
223 const dbus::ObjectPath
& object_path
,
224 const dbus::ObjectPath
& sms_path
,
225 const DeleteCallback
& callback
) OVERRIDE
{
226 std::vector
<dbus::ObjectPath
>::iterator
it(
227 find(message_paths_
.begin(), message_paths_
.end(), sms_path
));
228 if (it
!= message_paths_
.end())
229 message_paths_
.erase(it
);
233 // ModemMessagingClient override.
234 virtual void List(const std::string
& service_name
,
235 const dbus::ObjectPath
& object_path
,
236 const ListCallback
& callback
) OVERRIDE
{
237 // This entire ModemMessagingClientStubImpl is for testing.
238 // Calling List with |service_name| equal to "AddSMS" allows unit
239 // tests to confirm that the sms_received_handler is functioning.
240 if (service_name
== "AddSMS") {
241 std::vector
<dbus::ObjectPath
> no_paths
;
242 const dbus::ObjectPath
kSmsPath("/SMS/0");
243 message_paths_
.push_back(kSmsPath
);
244 if (!sms_received_handler_
.is_null())
245 sms_received_handler_
.Run(kSmsPath
, true);
246 callback
.Run(no_paths
);
248 callback
.Run(message_paths_
);
253 SmsReceivedHandler sms_received_handler_
;
254 std::vector
<dbus::ObjectPath
> message_paths_
;
256 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientStubImpl
);
261 ////////////////////////////////////////////////////////////////////////////////
262 // ModemMessagingClient
264 ModemMessagingClient::ModemMessagingClient() {}
266 ModemMessagingClient::~ModemMessagingClient() {}
270 ModemMessagingClient
* ModemMessagingClient::Create(
271 DBusClientImplementationType type
,
273 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
) {
274 return new ModemMessagingClientImpl(bus
);
276 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
277 return new ModemMessagingClientStubImpl();
281 } // namespace chromeos