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.
5 #include "chromeos/dbus/shill_service_client.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/shill_property_changed_observer.h"
12 #include "chromeos/dbus/shill_service_client_stub.h"
14 #include "dbus/message.h"
15 #include "dbus/object_proxy.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
22 #ifndef DBUS_ERROR_UNKNOWN_OBJECT
23 // The linux_chromeos ASAN builder has an older version of dbus-protocol.h
24 // so make sure this is defined.
25 #define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject"
28 // Error callback for GetProperties.
29 void OnGetDictionaryError(
30 const std::string
& method_name
,
31 const dbus::ObjectPath
& service_path
,
32 const ShillServiceClient::DictionaryValueCallback
& callback
,
33 const std::string
& error_name
,
34 const std::string
& error_message
) {
35 const std::string log_string
=
36 "Failed to call org.chromium.shill.Service." + method_name
+
37 " for: " + service_path
.value() + ": " +
38 error_name
+ ": " + error_message
;
40 // Suppress ERROR messages for UnknownMethod/Object" since this can
41 // happen under normal conditions. See crbug.com/130660 and crbug.com/222210.
42 if (error_name
== DBUS_ERROR_UNKNOWN_METHOD
||
43 error_name
== DBUS_ERROR_UNKNOWN_OBJECT
)
44 VLOG(1) << log_string
;
46 LOG(ERROR
) << log_string
;
48 base::DictionaryValue empty_dictionary
;
49 callback
.Run(DBUS_METHOD_CALL_FAILURE
, empty_dictionary
);
52 // The ShillServiceClient implementation.
53 class ShillServiceClientImpl
: public ShillServiceClient
{
55 explicit ShillServiceClientImpl()
57 helpers_deleter_(&helpers_
) {
60 virtual void AddPropertyChangedObserver(
61 const dbus::ObjectPath
& service_path
,
62 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
63 GetHelper(service_path
)->AddPropertyChangedObserver(observer
);
66 virtual void RemovePropertyChangedObserver(
67 const dbus::ObjectPath
& service_path
,
68 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
69 GetHelper(service_path
)->RemovePropertyChangedObserver(observer
);
72 virtual void GetProperties(const dbus::ObjectPath
& service_path
,
73 const DictionaryValueCallback
& callback
) OVERRIDE
{
74 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
75 flimflam::kGetPropertiesFunction
);
76 GetHelper(service_path
)->CallDictionaryValueMethodWithErrorCallback(
78 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
),
79 base::Bind(&OnGetDictionaryError
, "GetProperties",
80 service_path
, callback
));
83 virtual void SetProperty(const dbus::ObjectPath
& service_path
,
84 const std::string
& name
,
85 const base::Value
& value
,
86 const base::Closure
& callback
,
87 const ErrorCallback
& error_callback
) OVERRIDE
{
88 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
89 flimflam::kSetPropertyFunction
);
90 dbus::MessageWriter
writer(&method_call
);
91 writer
.AppendString(name
);
92 ShillClientHelper::AppendValueDataAsVariant(&writer
, value
);
93 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
98 virtual void SetProperties(const dbus::ObjectPath
& service_path
,
99 const base::DictionaryValue
& properties
,
100 const base::Closure
& callback
,
101 const ErrorCallback
& error_callback
) OVERRIDE
{
102 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
103 shill::kSetPropertiesFunction
);
104 dbus::MessageWriter
writer(&method_call
);
105 ShillClientHelper::AppendServicePropertiesDictionary(&writer
, properties
);
106 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
111 virtual void ClearProperty(const dbus::ObjectPath
& service_path
,
112 const std::string
& name
,
113 const base::Closure
& callback
,
114 const ErrorCallback
& error_callback
) OVERRIDE
{
115 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
116 flimflam::kClearPropertyFunction
);
117 dbus::MessageWriter
writer(&method_call
);
118 writer
.AppendString(name
);
119 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
125 virtual void ClearProperties(const dbus::ObjectPath
& service_path
,
126 const std::vector
<std::string
>& names
,
127 const ListValueCallback
& callback
,
128 const ErrorCallback
& error_callback
) OVERRIDE
{
129 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
130 shill::kClearPropertiesFunction
);
131 dbus::MessageWriter
writer(&method_call
);
132 writer
.AppendArrayOfStrings(names
);
133 GetHelper(service_path
)->CallListValueMethodWithErrorCallback(
139 virtual void Connect(const dbus::ObjectPath
& service_path
,
140 const base::Closure
& callback
,
141 const ErrorCallback
& error_callback
) OVERRIDE
{
142 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
143 flimflam::kConnectFunction
);
144 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(
145 &method_call
, callback
, error_callback
);
148 virtual void Disconnect(const dbus::ObjectPath
& service_path
,
149 const base::Closure
& callback
,
150 const ErrorCallback
& error_callback
) OVERRIDE
{
151 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
152 flimflam::kDisconnectFunction
);
153 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
158 virtual void Remove(const dbus::ObjectPath
& service_path
,
159 const base::Closure
& callback
,
160 const ErrorCallback
& error_callback
) OVERRIDE
{
161 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
162 flimflam::kRemoveServiceFunction
);
163 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
168 virtual void ActivateCellularModem(
169 const dbus::ObjectPath
& service_path
,
170 const std::string
& carrier
,
171 const base::Closure
& callback
,
172 const ErrorCallback
& error_callback
) OVERRIDE
{
173 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
174 flimflam::kActivateCellularModemFunction
);
175 dbus::MessageWriter
writer(&method_call
);
176 writer
.AppendString(carrier
);
177 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
182 virtual void CompleteCellularActivation(
183 const dbus::ObjectPath
& service_path
,
184 const base::Closure
& callback
,
185 const ErrorCallback
& error_callback
) OVERRIDE
{
186 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
187 shill::kCompleteCellularActivationFunction
);
188 dbus::MessageWriter
writer(&method_call
);
189 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
194 virtual void GetLoadableProfileEntries(
195 const dbus::ObjectPath
& service_path
,
196 const DictionaryValueCallback
& callback
) OVERRIDE
{
197 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
198 shill::kGetLoadableProfileEntriesFunction
);
199 GetHelper(service_path
)->CallDictionaryValueMethodWithErrorCallback(
201 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
),
202 base::Bind(&OnGetDictionaryError
, "GetLoadableProfileEntries",
203 service_path
, callback
));
206 virtual ShillServiceClient::TestInterface
* GetTestInterface() OVERRIDE
{
211 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
216 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
218 // Returns the corresponding ShillClientHelper for the profile.
219 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& service_path
) {
220 HelperMap::iterator it
= helpers_
.find(service_path
.value());
221 if (it
!= helpers_
.end())
224 // There is no helper for the profile, create it.
225 dbus::ObjectProxy
* object_proxy
=
226 bus_
->GetObjectProxy(flimflam::kFlimflamServiceName
, service_path
);
227 ShillClientHelper
* helper
= new ShillClientHelper(bus_
, object_proxy
);
228 helper
->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface
);
229 helpers_
.insert(HelperMap::value_type(service_path
.value(), helper
));
235 STLValueDeleter
<HelperMap
> helpers_deleter_
;
237 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl
);
242 ShillServiceClient::ShillServiceClient() {}
244 ShillServiceClient::~ShillServiceClient() {}
247 ShillServiceClient
* ShillServiceClient::Create(
248 DBusClientImplementationType type
) {
249 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
250 return new ShillServiceClientImpl();
251 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
252 return new ShillServiceClientStub();
255 } // namespace chromeos