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/chromeos/chromeos_version.h"
9 #include "base/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/values.h"
12 #include "chromeos/dbus/shill_property_changed_observer.h"
13 #include "chromeos/dbus/shill_service_client_stub.h"
15 #include "dbus/message.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
23 #ifndef DBUS_ERROR_UNKNOWN_OBJECT
24 // The linux_chromeos ASAN builder has an older version of dbus-protocol.h
25 // so make sure this is defined.
26 #define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject"
29 // Error callback for GetProperties.
30 void OnGetPropertiesError(
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.GetProperties for: " +
37 service_path
.value() + ": " + error_name
+ ": " + error_message
;
39 // Suppress ERROR messages for UnknownMethod/Object" since this can
40 // happen under normal conditions. See crbug.com/130660 and crbug.com/222210.
41 if (error_name
== DBUS_ERROR_UNKNOWN_METHOD
||
42 error_name
== DBUS_ERROR_UNKNOWN_OBJECT
)
43 VLOG(1) << log_string
;
45 LOG(ERROR
) << log_string
;
47 base::DictionaryValue empty_dictionary
;
48 callback
.Run(DBUS_METHOD_CALL_FAILURE
, empty_dictionary
);
51 // The ShillServiceClient implementation.
52 class ShillServiceClientImpl
: public ShillServiceClient
{
54 explicit ShillServiceClientImpl(dbus::Bus
* bus
)
56 helpers_deleter_(&helpers_
) {
59 /////////////////////////////////////
60 // ShillServiceClient overrides.
61 virtual void AddPropertyChangedObserver(
62 const dbus::ObjectPath
& service_path
,
63 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
64 GetHelper(service_path
)->AddPropertyChangedObserver(observer
);
67 virtual void RemovePropertyChangedObserver(
68 const dbus::ObjectPath
& service_path
,
69 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
70 GetHelper(service_path
)->RemovePropertyChangedObserver(observer
);
73 virtual void GetProperties(const dbus::ObjectPath
& service_path
,
74 const DictionaryValueCallback
& callback
) OVERRIDE
{
75 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
76 flimflam::kGetPropertiesFunction
);
77 GetHelper(service_path
)->CallDictionaryValueMethodWithErrorCallback(
79 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
),
80 base::Bind(&OnGetPropertiesError
, 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 ClearProperty(const dbus::ObjectPath
& service_path
,
99 const std::string
& name
,
100 const base::Closure
& callback
,
101 const ErrorCallback
& error_callback
) OVERRIDE
{
102 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
103 flimflam::kClearPropertyFunction
);
104 dbus::MessageWriter
writer(&method_call
);
105 writer
.AppendString(name
);
106 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
112 virtual void ClearProperties(const dbus::ObjectPath
& service_path
,
113 const std::vector
<std::string
>& names
,
114 const ListValueCallback
& callback
,
115 const ErrorCallback
& error_callback
) OVERRIDE
{
116 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
117 shill::kClearPropertiesFunction
);
118 dbus::MessageWriter
writer(&method_call
);
119 writer
.AppendArrayOfStrings(names
);
120 GetHelper(service_path
)->CallListValueMethodWithErrorCallback(
126 virtual void Connect(const dbus::ObjectPath
& service_path
,
127 const base::Closure
& callback
,
128 const ErrorCallback
& error_callback
) OVERRIDE
{
129 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
130 flimflam::kConnectFunction
);
131 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(
132 &method_call
, callback
, error_callback
);
135 virtual void Disconnect(const dbus::ObjectPath
& service_path
,
136 const base::Closure
& callback
,
137 const ErrorCallback
& error_callback
) OVERRIDE
{
138 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
139 flimflam::kDisconnectFunction
);
140 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
145 virtual void Remove(const dbus::ObjectPath
& service_path
,
146 const base::Closure
& callback
,
147 const ErrorCallback
& error_callback
) OVERRIDE
{
148 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
149 flimflam::kRemoveServiceFunction
);
150 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
155 virtual void ActivateCellularModem(
156 const dbus::ObjectPath
& service_path
,
157 const std::string
& carrier
,
158 const base::Closure
& callback
,
159 const ErrorCallback
& error_callback
) OVERRIDE
{
160 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
161 flimflam::kActivateCellularModemFunction
);
162 dbus::MessageWriter
writer(&method_call
);
163 writer
.AppendString(carrier
);
164 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
169 virtual void CompleteCellularActivation(
170 const dbus::ObjectPath
& service_path
,
171 const base::Closure
& callback
,
172 const ErrorCallback
& error_callback
) OVERRIDE
{
173 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
174 shill::kCompleteCellularActivationFunction
);
175 dbus::MessageWriter
writer(&method_call
);
176 GetHelper(service_path
)->CallVoidMethodWithErrorCallback(&method_call
,
181 virtual bool CallActivateCellularModemAndBlock(
182 const dbus::ObjectPath
& service_path
,
183 const std::string
& carrier
) OVERRIDE
{
184 dbus::MethodCall
method_call(flimflam::kFlimflamServiceInterface
,
185 flimflam::kActivateCellularModemFunction
);
186 dbus::MessageWriter
writer(&method_call
);
187 writer
.AppendString(carrier
);
188 return GetHelper(service_path
)->CallVoidMethodAndBlock(&method_call
);
191 virtual ShillServiceClient::TestInterface
* GetTestInterface() OVERRIDE
{
196 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
198 // Returns the corresponding ShillClientHelper for the profile.
199 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& service_path
) {
200 HelperMap::iterator it
= helpers_
.find(service_path
.value());
201 if (it
!= helpers_
.end())
204 // There is no helper for the profile, create it.
205 dbus::ObjectProxy
* object_proxy
=
206 bus_
->GetObjectProxy(flimflam::kFlimflamServiceName
, service_path
);
207 ShillClientHelper
* helper
= new ShillClientHelper(bus_
, object_proxy
);
208 helper
->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface
);
209 helpers_
.insert(HelperMap::value_type(service_path
.value(), helper
));
215 STLValueDeleter
<HelperMap
> helpers_deleter_
;
217 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl
);
222 ShillServiceClient::ShillServiceClient() {}
224 ShillServiceClient::~ShillServiceClient() {}
227 ShillServiceClient
* ShillServiceClient::Create(
228 DBusClientImplementationType type
,
230 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
231 return new ShillServiceClientImpl(bus
);
232 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
233 return new ShillServiceClientStub();
236 } // namespace chromeos