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_ipconfig_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_ipconfig_client_stub.h"
12 #include "chromeos/dbus/shill_property_changed_observer.h"
14 #include "dbus/message.h"
15 #include "dbus/object_path.h"
16 #include "dbus/object_proxy.h"
17 #include "dbus/values_util.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
24 // The ShillIPConfigClient implementation.
25 class ShillIPConfigClientImpl
: public ShillIPConfigClient
{
27 ShillIPConfigClientImpl();
29 ////////////////////////////////////
30 // ShillIPConfigClient overrides.
31 virtual void AddPropertyChangedObserver(
32 const dbus::ObjectPath
& ipconfig_path
,
33 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
34 GetHelper(ipconfig_path
)->AddPropertyChangedObserver(observer
);
37 virtual void RemovePropertyChangedObserver(
38 const dbus::ObjectPath
& ipconfig_path
,
39 ShillPropertyChangedObserver
* observer
) OVERRIDE
{
40 GetHelper(ipconfig_path
)->RemovePropertyChangedObserver(observer
);
42 virtual void Refresh(const dbus::ObjectPath
& ipconfig_path
,
43 const VoidDBusMethodCallback
& callback
) OVERRIDE
;
44 virtual void GetProperties(const dbus::ObjectPath
& ipconfig_path
,
45 const DictionaryValueCallback
& callback
) OVERRIDE
;
46 virtual void SetProperty(const dbus::ObjectPath
& ipconfig_path
,
47 const std::string
& name
,
48 const base::Value
& value
,
49 const VoidDBusMethodCallback
& callback
) OVERRIDE
;
50 virtual void ClearProperty(const dbus::ObjectPath
& ipconfig_path
,
51 const std::string
& name
,
52 const VoidDBusMethodCallback
& callback
) OVERRIDE
;
53 virtual void Remove(const dbus::ObjectPath
& ipconfig_path
,
54 const VoidDBusMethodCallback
& callback
) OVERRIDE
;
57 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
62 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
64 // Returns the corresponding ShillClientHelper for the profile.
65 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& ipconfig_path
) {
66 HelperMap::iterator it
= helpers_
.find(ipconfig_path
.value());
67 if (it
!= helpers_
.end())
70 // There is no helper for the profile, create it.
71 dbus::ObjectProxy
* object_proxy
=
72 bus_
->GetObjectProxy(shill::kFlimflamServiceName
, ipconfig_path
);
73 ShillClientHelper
* helper
= new ShillClientHelper(object_proxy
);
74 helper
->MonitorPropertyChanged(shill::kFlimflamIPConfigInterface
);
75 helpers_
.insert(HelperMap::value_type(ipconfig_path
.value(), helper
));
81 STLValueDeleter
<HelperMap
> helpers_deleter_
;
83 DISALLOW_COPY_AND_ASSIGN(ShillIPConfigClientImpl
);
86 ShillIPConfigClientImpl::ShillIPConfigClientImpl()
88 helpers_deleter_(&helpers_
) {
91 void ShillIPConfigClientImpl::GetProperties(
92 const dbus::ObjectPath
& ipconfig_path
,
93 const DictionaryValueCallback
& callback
) {
94 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
95 shill::kGetPropertiesFunction
);
96 GetHelper(ipconfig_path
)->CallDictionaryValueMethod(&method_call
, callback
);
99 void ShillIPConfigClientImpl::Refresh(
100 const dbus::ObjectPath
& ipconfig_path
,
101 const VoidDBusMethodCallback
& callback
) {
102 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
103 shill::kRefreshFunction
);
104 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
107 void ShillIPConfigClientImpl::SetProperty(
108 const dbus::ObjectPath
& ipconfig_path
,
109 const std::string
& name
,
110 const base::Value
& value
,
111 const VoidDBusMethodCallback
& callback
) {
112 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
113 shill::kSetPropertyFunction
);
114 dbus::MessageWriter
writer(&method_call
);
115 writer
.AppendString(name
);
116 // IPConfig supports writing basic type and string array properties.
117 switch (value
.GetType()) {
118 case base::Value::TYPE_LIST
: {
119 const base::ListValue
* list_value
= NULL
;
120 value
.GetAsList(&list_value
);
121 dbus::MessageWriter
variant_writer(NULL
);
122 writer
.OpenVariant("as", &variant_writer
);
123 dbus::MessageWriter
array_writer(NULL
);
124 variant_writer
.OpenArray("s", &array_writer
);
125 for (base::ListValue::const_iterator it
= list_value
->begin();
126 it
!= list_value
->end();
128 DLOG_IF(ERROR
, (*it
)->GetType() != base::Value::TYPE_STRING
)
129 << "Unexpected type " << (*it
)->GetType();
131 (*it
)->GetAsString(&str
);
132 array_writer
.AppendString(str
);
134 variant_writer
.CloseContainer(&array_writer
);
135 writer
.CloseContainer(&variant_writer
);
137 case base::Value::TYPE_BOOLEAN
:
138 case base::Value::TYPE_INTEGER
:
139 case base::Value::TYPE_DOUBLE
:
140 case base::Value::TYPE_STRING
:
141 dbus::AppendBasicTypeValueDataAsVariant(&writer
, value
);
144 DLOG(ERROR
) << "Unexpected type " << value
.GetType();
146 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
149 void ShillIPConfigClientImpl::ClearProperty(
150 const dbus::ObjectPath
& ipconfig_path
,
151 const std::string
& name
,
152 const VoidDBusMethodCallback
& callback
) {
153 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
154 shill::kClearPropertyFunction
);
155 dbus::MessageWriter
writer(&method_call
);
156 writer
.AppendString(name
);
157 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
160 void ShillIPConfigClientImpl::Remove(
161 const dbus::ObjectPath
& ipconfig_path
,
162 const VoidDBusMethodCallback
& callback
) {
163 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
164 shill::kRemoveConfigFunction
);
165 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
170 ShillIPConfigClient::ShillIPConfigClient() {}
172 ShillIPConfigClient::~ShillIPConfigClient() {}
175 ShillIPConfigClient
* ShillIPConfigClient::Create(
176 DBusClientImplementationType type
) {
177 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
178 return new ShillIPConfigClientImpl();
179 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
180 return new ShillIPConfigClientStub();
183 } // namespace chromeos