OLD | NEW |
---|---|
(Empty) | |
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" | |
5 | |
6 #include <map> | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/stringprintf.h" | |
14 #include "base/stl_util.h" | |
15 #include "base/values.h" | |
16 #include "dbus/bus.h" | |
17 #include "dbus/message.h" | |
18 #include "dbus/object_proxy.h" | |
19 #include "dbus/values_util.h" | |
20 #include "third_party/cros_system_api/dbus/service_constants.h" | |
21 | |
22 namespace chromeos { | |
23 | |
24 namespace { | |
25 | |
26 // A class which makes method calls for SMS services via the | |
27 // org.freedesktop.ModemManager1.Sms object using the DBusProperties | |
28 // interface. | |
29 class SMSProxy { | |
30 public: | |
31 typedef base::Callback<void(const base::DictionaryValue& sms)> GetAllCallback; | |
32 | |
33 SMSProxy(dbus::Bus* bus, | |
34 const std::string& service_name, | |
35 const dbus::ObjectPath& object_path) | |
36 : proxy_(bus->GetObjectProxy(service_name, object_path)), | |
37 weak_ptr_factory_(this) { | |
38 } | |
39 | |
40 // Gets all the properties of the SMS. | |
41 void GetAll(const GetAllCallback& callback) { | |
42 dbus::MethodCall method_call(dbus::kDBusPropertiesInterface, | |
43 dbus::kDBusPropertiesGetAll); | |
44 dbus::MessageWriter writer(&method_call); | |
45 writer.AppendString(modemmanager::kModemManager1SmsInterface); | |
46 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
47 base::Bind(&SMSProxy::OnGetAll, | |
48 weak_ptr_factory_.GetWeakPtr(), | |
49 callback)); | |
50 } | |
51 | |
52 private: | |
53 // Handles responses of GetAll method calls. | |
54 void OnGetAll(const GetAllCallback& callback, dbus::Response* response) { | |
55 if (!response) { | |
56 // Must invoke the callback, even if there is no message. | |
57 callback.Run(base::DictionaryValue()); | |
58 return; | |
59 } | |
60 dbus::MessageReader reader(response); | |
61 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
62 base::DictionaryValue* dictionary_value = NULL; | |
63 if (!value.get() || !value->GetAsDictionary(&dictionary_value)) { | |
64 LOG(WARNING) << "Invalid response: " << response->ToString(); | |
65 callback.Run(base::DictionaryValue()); | |
66 return; | |
67 } | |
68 callback.Run(*dictionary_value); | |
69 } | |
70 | |
71 void OnSignalConnected(const std::string& interface, | |
hashimoto
2012/06/06 07:19:44
No need to have this method.
Jason Glasgow
2012/06/06 20:54:20
Done.
| |
72 const std::string& signal, | |
73 bool successed) { | |
74 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " | |
75 << signal << " failed."; | |
76 } | |
77 | |
78 dbus::ObjectProxy* proxy_; | |
79 base::WeakPtrFactory<SMSProxy> weak_ptr_factory_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(SMSProxy); | |
82 }; | |
83 | |
84 // SMSClient is used to communicate with the | |
85 // org.freedesktop.ModemManager1.SMS service. All methods should be | |
86 // called from the origin thread (UI thread) which initializes the | |
87 // DBusThreadManager instance. | |
88 class SMSClientImpl : public SMSClient { | |
89 public: | |
90 explicit SMSClientImpl(dbus::Bus* bus) : bus_(bus), weak_ptr_factory_(this) {} | |
91 virtual ~SMSClientImpl() {} | |
92 | |
93 // Calls GetAll method. |callback| is called after the method call succeeds. | |
94 virtual void GetAll(const std::string& service_name, | |
95 const dbus::ObjectPath& object_path, | |
96 const GetAllCallback& callback) OVERRIDE { | |
97 // The callback passed to GetAll owns the |proxy| pointer. | |
98 SMSProxy *proxy = new SMSProxy(bus_, service_name, object_path); | |
99 CHECK(proxy); | |
hashimoto
2012/06/06 07:19:44
No need to CHECK
Jason Glasgow
2012/06/06 20:54:20
Done.
| |
100 proxy->GetAll(base::Bind(&SMSClientImpl::OnGetAll, | |
101 weak_ptr_factory_.GetWeakPtr(), | |
102 base::Owned(proxy), callback)); | |
hashimoto
2012/06/06 07:19:44
First I thought this trick (deleting |proxy| on On
Jason Glasgow
2012/06/06 20:54:20
I'm still working on a solution....
On 2012/06/06
| |
103 } | |
104 | |
105 private: | |
106 void OnGetAll(SMSProxy *proxy, | |
107 const GetAllCallback& callback, | |
108 const base::DictionaryValue& sms) { | |
109 callback.Run(sms); | |
110 } | |
111 | |
112 dbus::Bus* bus_; | |
113 base::WeakPtrFactory<SMSClientImpl> weak_ptr_factory_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(SMSClientImpl); | |
116 }; | |
117 | |
118 class SMSClientStubImpl : public SMSClient { | |
119 public: | |
120 SMSClientStubImpl() {} | |
121 virtual ~SMSClientStubImpl() {} | |
122 | |
123 virtual void GetAll(const std::string& service_name, | |
124 const dbus::ObjectPath& object_path, | |
125 const GetAllCallback& callback) OVERRIDE { | |
126 // TODO(jglasgow): implement for unit tests | |
127 } | |
128 | |
129 private: | |
130 DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl); | |
131 }; | |
132 | |
133 } // namespace | |
134 | |
135 //////////////////////////////////////////////////////////////////////////////// | |
136 // SMSClient | |
137 | |
138 SMSClient::SMSClient() {} | |
139 | |
140 SMSClient::~SMSClient() {} | |
141 | |
142 | |
143 // static | |
144 SMSClient* SMSClient::Create(DBusClientImplementationType type, | |
145 dbus::Bus* bus) { | |
146 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | |
147 return new SMSClientImpl(bus); | |
148 } | |
149 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
150 return new SMSClientStubImpl(); | |
151 } | |
152 | |
153 } // namespace chromeos | |
OLD | NEW |