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_property_changed_observer.h"
13 #include "dbus/message.h"
14 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h"
16 #include "dbus/values_util.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
23 // The ShillIPConfigClient implementation.
24 class ShillIPConfigClientImpl
: public ShillIPConfigClient
{
26 ShillIPConfigClientImpl();
28 ////////////////////////////////////
29 // ShillIPConfigClient overrides.
30 void AddPropertyChangedObserver(
31 const dbus::ObjectPath
& ipconfig_path
,
32 ShillPropertyChangedObserver
* observer
) override
{
33 GetHelper(ipconfig_path
)->AddPropertyChangedObserver(observer
);
36 void RemovePropertyChangedObserver(
37 const dbus::ObjectPath
& ipconfig_path
,
38 ShillPropertyChangedObserver
* observer
) override
{
39 GetHelper(ipconfig_path
)->RemovePropertyChangedObserver(observer
);
41 void Refresh(const dbus::ObjectPath
& ipconfig_path
,
42 const VoidDBusMethodCallback
& callback
) override
;
43 void GetProperties(const dbus::ObjectPath
& ipconfig_path
,
44 const DictionaryValueCallback
& callback
) override
;
45 void SetProperty(const dbus::ObjectPath
& ipconfig_path
,
46 const std::string
& name
,
47 const base::Value
& value
,
48 const VoidDBusMethodCallback
& callback
) override
;
49 void ClearProperty(const dbus::ObjectPath
& ipconfig_path
,
50 const std::string
& name
,
51 const VoidDBusMethodCallback
& callback
) override
;
52 void Remove(const dbus::ObjectPath
& ipconfig_path
,
53 const VoidDBusMethodCallback
& callback
) override
;
54 ShillIPConfigClient::TestInterface
* GetTestInterface() override
;
57 void Init(dbus::Bus
* bus
) override
{ bus_
= bus
; }
60 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
62 // Returns the corresponding ShillClientHelper for the profile.
63 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& ipconfig_path
) {
64 HelperMap::iterator it
= helpers_
.find(ipconfig_path
.value());
65 if (it
!= helpers_
.end())
68 // There is no helper for the profile, create it.
69 dbus::ObjectProxy
* object_proxy
=
70 bus_
->GetObjectProxy(shill::kFlimflamServiceName
, ipconfig_path
);
71 ShillClientHelper
* helper
= new ShillClientHelper(object_proxy
);
72 helper
->MonitorPropertyChanged(shill::kFlimflamIPConfigInterface
);
73 helpers_
.insert(HelperMap::value_type(ipconfig_path
.value(), helper
));
79 STLValueDeleter
<HelperMap
> helpers_deleter_
;
81 DISALLOW_COPY_AND_ASSIGN(ShillIPConfigClientImpl
);
84 ShillIPConfigClientImpl::ShillIPConfigClientImpl()
86 helpers_deleter_(&helpers_
) {
89 void ShillIPConfigClientImpl::GetProperties(
90 const dbus::ObjectPath
& ipconfig_path
,
91 const DictionaryValueCallback
& callback
) {
92 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
93 shill::kGetPropertiesFunction
);
94 GetHelper(ipconfig_path
)->CallDictionaryValueMethod(&method_call
, callback
);
97 void ShillIPConfigClientImpl::Refresh(
98 const dbus::ObjectPath
& ipconfig_path
,
99 const VoidDBusMethodCallback
& callback
) {
100 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
101 shill::kRefreshFunction
);
102 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
105 void ShillIPConfigClientImpl::SetProperty(
106 const dbus::ObjectPath
& ipconfig_path
,
107 const std::string
& name
,
108 const base::Value
& value
,
109 const VoidDBusMethodCallback
& callback
) {
110 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
111 shill::kSetPropertyFunction
);
112 dbus::MessageWriter
writer(&method_call
);
113 writer
.AppendString(name
);
114 // IPConfig supports writing basic type and string array properties.
115 switch (value
.GetType()) {
116 case base::Value::TYPE_LIST
: {
117 const base::ListValue
* list_value
= NULL
;
118 value
.GetAsList(&list_value
);
119 dbus::MessageWriter
variant_writer(NULL
);
120 writer
.OpenVariant("as", &variant_writer
);
121 dbus::MessageWriter
array_writer(NULL
);
122 variant_writer
.OpenArray("s", &array_writer
);
123 for (base::ListValue::const_iterator it
= list_value
->begin();
124 it
!= list_value
->end();
126 DLOG_IF(ERROR
, (*it
)->GetType() != base::Value::TYPE_STRING
)
127 << "Unexpected type " << (*it
)->GetType();
129 (*it
)->GetAsString(&str
);
130 array_writer
.AppendString(str
);
132 variant_writer
.CloseContainer(&array_writer
);
133 writer
.CloseContainer(&variant_writer
);
135 case base::Value::TYPE_BOOLEAN
:
136 case base::Value::TYPE_INTEGER
:
137 case base::Value::TYPE_DOUBLE
:
138 case base::Value::TYPE_STRING
:
139 dbus::AppendBasicTypeValueDataAsVariant(&writer
, value
);
142 DLOG(ERROR
) << "Unexpected type " << value
.GetType();
144 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
147 void ShillIPConfigClientImpl::ClearProperty(
148 const dbus::ObjectPath
& ipconfig_path
,
149 const std::string
& name
,
150 const VoidDBusMethodCallback
& callback
) {
151 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
152 shill::kClearPropertyFunction
);
153 dbus::MessageWriter
writer(&method_call
);
154 writer
.AppendString(name
);
155 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
158 void ShillIPConfigClientImpl::Remove(
159 const dbus::ObjectPath
& ipconfig_path
,
160 const VoidDBusMethodCallback
& callback
) {
161 dbus::MethodCall
method_call(shill::kFlimflamIPConfigInterface
,
162 shill::kRemoveConfigFunction
);
163 GetHelper(ipconfig_path
)->CallVoidMethod(&method_call
, callback
);
166 ShillIPConfigClient::TestInterface
*
167 ShillIPConfigClientImpl::GetTestInterface() {
173 ShillIPConfigClient::ShillIPConfigClient() {}
175 ShillIPConfigClient::~ShillIPConfigClient() {}
178 ShillIPConfigClient
* ShillIPConfigClient::Create() {
179 return new ShillIPConfigClientImpl();
182 } // namespace chromeos