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/sms_client.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chromeos/chromeos_switches.h"
19 #include "dbus/message.h"
20 #include "dbus/object_proxy.h"
21 #include "dbus/values_util.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
28 // SMSClient is used to communicate with the
29 // org.freedesktop.ModemManager1.SMS service. All methods should be
30 // called from the origin thread (UI thread) which initializes the
31 // DBusThreadManager instance.
32 class SMSClientImpl
: public SMSClient
{
34 SMSClientImpl() : bus_(NULL
), weak_ptr_factory_(this) {}
36 virtual ~SMSClientImpl() {}
38 // Calls GetAll method. |callback| is called after the method call succeeds.
39 virtual void GetAll(const std::string
& service_name
,
40 const dbus::ObjectPath
& object_path
,
41 const GetAllCallback
& callback
) OVERRIDE
{
42 dbus::ObjectProxy
*proxy
= bus_
->GetObjectProxy(service_name
, object_path
);
43 dbus::MethodCall
method_call(dbus::kDBusPropertiesInterface
,
44 dbus::kDBusPropertiesGetAll
);
45 dbus::MessageWriter
writer(&method_call
);
46 writer
.AppendString(modemmanager::kModemManager1SmsInterface
);
47 proxy
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
48 base::Bind(&SMSClientImpl::OnGetAll
,
49 weak_ptr_factory_
.GetWeakPtr(),
54 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
59 // Handles responses of GetAll method calls.
60 void OnGetAll(const GetAllCallback
& callback
, dbus::Response
* response
) {
62 // Must invoke the callback, even if there is no message.
63 base::DictionaryValue empty_dictionary
;
64 callback
.Run(empty_dictionary
);
67 dbus::MessageReader
reader(response
);
68 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(&reader
));
69 base::DictionaryValue
* dictionary_value
= NULL
;
70 if (!value
.get() || !value
->GetAsDictionary(&dictionary_value
)) {
71 LOG(WARNING
) << "Invalid response: " << response
->ToString();
72 base::DictionaryValue empty_dictionary
;
73 callback
.Run(empty_dictionary
);
76 callback
.Run(*dictionary_value
);
81 // Note: This should remain the last member so it'll be destroyed and
82 // invalidate its weak pointers before any other members are destroyed.
83 base::WeakPtrFactory
<SMSClientImpl
> weak_ptr_factory_
;
85 DISALLOW_COPY_AND_ASSIGN(SMSClientImpl
);
88 class SMSClientStubImpl
: public SMSClient
{
90 SMSClientStubImpl() : weak_ptr_factory_(this) {}
91 virtual ~SMSClientStubImpl() {}
93 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{}
95 virtual void GetAll(const std::string
& service_name
,
96 const dbus::ObjectPath
& object_path
,
97 const GetAllCallback
& callback
) OVERRIDE
{
98 if (!CommandLine::ForCurrentProcess()->HasSwitch(
99 chromeos::switches::kSmsTestMessages
))
102 // Ownership passed to callback
103 base::DictionaryValue
*sms
= new base::DictionaryValue();
104 sms
->SetString("Number", "000-000-0000");
105 sms
->SetString("Text",
106 "SMSClientStubImpl: Test Message: " + object_path
.value());
107 sms
->SetString("Timestamp", "Fri Jun 8 13:26:04 EDT 2012");
109 // Run callback asynchronously.
110 if (callback
.is_null())
112 base::MessageLoop::current()->PostTask(
114 base::Bind(&SMSClientStubImpl::OnGetAll
,
115 weak_ptr_factory_
.GetWeakPtr(),
121 void OnGetAll(base::DictionaryValue
*sms
,
122 const GetAllCallback
& callback
) {
126 base::WeakPtrFactory
<SMSClientStubImpl
> weak_ptr_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl
);
133 ////////////////////////////////////////////////////////////////////////////////
136 SMSClient::SMSClient() {}
138 SMSClient::~SMSClient() {}
142 SMSClient
* SMSClient::Create(DBusClientImplementationType type
) {
143 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
) {
144 return new SMSClientImpl();
146 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
147 return new SMSClientStubImpl();
150 } // namespace chromeos