Roll src/third_party/WebKit f0fc3a1:b92951d (svn 201688:201689)
[chromium-blink-merge.git] / chromeos / dbus / shill_ipconfig_client.cc
blob457cc5c9afd4eedd4480b146d2256b76cead7f37
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"
7 #include "base/bind.h"
8 #include "base/containers/scoped_ptr_map.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/values.h"
12 #include "chromeos/dbus/shill_property_changed_observer.h"
13 #include "dbus/bus.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"
20 namespace chromeos {
22 namespace {
24 // The ShillIPConfigClient implementation.
25 class ShillIPConfigClientImpl : public ShillIPConfigClient {
26 public:
27 ShillIPConfigClientImpl();
29 ////////////////////////////////////
30 // ShillIPConfigClient overrides.
31 void AddPropertyChangedObserver(
32 const dbus::ObjectPath& ipconfig_path,
33 ShillPropertyChangedObserver* observer) override {
34 GetHelper(ipconfig_path)->AddPropertyChangedObserver(observer);
37 void RemovePropertyChangedObserver(
38 const dbus::ObjectPath& ipconfig_path,
39 ShillPropertyChangedObserver* observer) override {
40 GetHelper(ipconfig_path)->RemovePropertyChangedObserver(observer);
42 void Refresh(const dbus::ObjectPath& ipconfig_path,
43 const VoidDBusMethodCallback& callback) override;
44 void GetProperties(const dbus::ObjectPath& ipconfig_path,
45 const DictionaryValueCallback& callback) override;
46 void SetProperty(const dbus::ObjectPath& ipconfig_path,
47 const std::string& name,
48 const base::Value& value,
49 const VoidDBusMethodCallback& callback) override;
50 void ClearProperty(const dbus::ObjectPath& ipconfig_path,
51 const std::string& name,
52 const VoidDBusMethodCallback& callback) override;
53 void Remove(const dbus::ObjectPath& ipconfig_path,
54 const VoidDBusMethodCallback& callback) override;
55 ShillIPConfigClient::TestInterface* GetTestInterface() override;
57 protected:
58 void Init(dbus::Bus* bus) override { bus_ = bus; }
60 private:
61 typedef base::ScopedPtrMap<std::string, scoped_ptr<ShillClientHelper>>
62 HelperMap;
64 // Returns the corresponding ShillClientHelper for the profile.
65 ShillClientHelper* GetHelper(const dbus::ObjectPath& ipconfig_path) {
66 HelperMap::const_iterator it = helpers_.find(ipconfig_path.value());
67 if (it != helpers_.end())
68 return it->second;
70 // There is no helper for the profile, create it.
71 dbus::ObjectProxy* object_proxy =
72 bus_->GetObjectProxy(shill::kFlimflamServiceName, ipconfig_path);
73 scoped_ptr<ShillClientHelper> helper(new ShillClientHelper(object_proxy));
74 helper->MonitorPropertyChanged(shill::kFlimflamIPConfigInterface);
75 ShillClientHelper* helper_ptr = helper.get();
76 helpers_.insert(ipconfig_path.value(), helper.Pass());
77 return helper_ptr;
80 dbus::Bus* bus_;
81 HelperMap helpers_;
83 DISALLOW_COPY_AND_ASSIGN(ShillIPConfigClientImpl);
86 ShillIPConfigClientImpl::ShillIPConfigClientImpl() : bus_(NULL) {
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();
125 ++it) {
126 DLOG_IF(ERROR, (*it)->GetType() != base::Value::TYPE_STRING)
127 << "Unexpected type " << (*it)->GetType();
128 std::string str;
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);
140 break;
141 default:
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() {
168 return NULL;
171 } // namespace
173 ShillIPConfigClient::ShillIPConfigClient() {}
175 ShillIPConfigClient::~ShillIPConfigClient() {}
177 // static
178 ShillIPConfigClient* ShillIPConfigClient::Create() {
179 return new ShillIPConfigClientImpl();
182 } // namespace chromeos