OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/dbus/flimflam_ipconfig_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "dbus/bus.h" | |
10 #include "dbus/message.h" | |
11 #include "dbus/object_path.h" | |
12 #include "dbus/object_proxy.h" | |
13 #include "dbus/values_util.h" | |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 namespace { | |
19 | |
20 // The FlimflamIPConfigClient implementation. | |
21 class FlimflamIPConfigClientImpl : public FlimflamIPConfigClient { | |
22 public: | |
23 explicit FlimflamIPConfigClientImpl(dbus::Bus* bus); | |
24 | |
25 // FlimflamIPConfigClient override. | |
26 virtual void SetPropertyChangedHandler( | |
27 const PropertyChangedHandler& handler) OVERRIDE; | |
28 | |
29 // FlimflamIPConfigClient override. | |
30 virtual void ResetPropertyChangedHandler() OVERRIDE; | |
31 // FlimflamIPConfigClient override. | |
32 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE; | |
33 // FlimflamIPConfigClient override. | |
34 virtual void SetProperty(const std::string& name, | |
35 const base::Value& value, | |
36 const VoidCallback& callback) OVERRIDE; | |
37 // FlimflamIPConfigClient override. | |
38 virtual void ClearProperty(const std::string& name, | |
39 const VoidCallback& callback) OVERRIDE; | |
40 // FlimflamIPConfigClient override. | |
41 virtual void Remove(const VoidCallback& callback) OVERRIDE; | |
42 | |
43 private: | |
44 dbus::ObjectProxy* proxy_; | |
45 FlimflamClientHelper helper_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientImpl); | |
48 }; | |
49 | |
50 FlimflamIPConfigClientImpl::FlimflamIPConfigClientImpl(dbus::Bus* bus) | |
51 : proxy_(bus->GetObjectProxy( | |
52 flimflam::kFlimflamServiceName, | |
53 dbus::ObjectPath(flimflam::kFlimflamServicePath))), | |
54 helper_(proxy_) { | |
55 helper_.MonitorPropertyChanged(flimflam::kFlimflamIPConfigInterface); | |
56 } | |
57 | |
58 void FlimflamIPConfigClientImpl::SetPropertyChangedHandler( | |
59 const PropertyChangedHandler& handler) { | |
60 helper_.SetPropertyChangedHandler(handler); | |
61 } | |
62 | |
63 void FlimflamIPConfigClientImpl::ResetPropertyChangedHandler() { | |
64 helper_.ResetPropertyChangedHandler(); | |
65 } | |
66 | |
67 // FlimflamIPConfigClient override. | |
68 void FlimflamIPConfigClientImpl::GetProperties( | |
69 const DictionaryValueCallback& callback) { | |
70 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
71 flimflam::kGetPropertiesFunction); | |
72 helper_.CallDictionaryValueMethod(&method_call, callback); | |
73 } | |
74 | |
75 // FlimflamIPConfigClient override. | |
76 void FlimflamIPConfigClientImpl::SetProperty(const std::string& name, | |
77 const base::Value& value, | |
78 const VoidCallback& callback) { | |
79 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
80 flimflam::kSetPropertyFunction); | |
81 dbus::MessageWriter writer(&method_call); | |
82 writer.AppendString(name); | |
83 // IPConfig supports writing basic type and string array properties. | |
84 switch (value.GetType()) { | |
85 case base::Value::TYPE_LIST: { | |
86 const base::ListValue* list_value = NULL; | |
87 value.GetAsList(&list_value); | |
88 dbus::MessageWriter variant_writer(NULL); | |
89 writer.OpenVariant("as", &variant_writer); | |
90 dbus::MessageWriter array_writer(NULL); | |
91 variant_writer.OpenArray("s", &array_writer); | |
92 for (base::ListValue::const_iterator it = list_value->begin(); | |
93 it != list_value->end(); | |
94 ++it) { | |
95 DLOG_IF(ERROR, (*it)->GetType() != base::Value::TYPE_STRING) | |
96 << "Unexpected type " << (*it)->GetType(); | |
97 std::string str; | |
98 (*it)->GetAsString(&str); | |
99 array_writer.AppendString(str); | |
100 } | |
101 variant_writer.CloseContainer(&array_writer); | |
102 writer.CloseContainer(&variant_writer); | |
103 } | |
104 case base::Value::TYPE_BOOLEAN: | |
105 case base::Value::TYPE_INTEGER: | |
106 case base::Value::TYPE_DOUBLE: | |
107 case base::Value::TYPE_STRING: | |
108 dbus::AppendBasicTypeValueDataAsVariant(&writer, value); | |
109 break; | |
110 default: | |
111 DLOG(ERROR) << "Unexpected type " << value.GetType(); | |
112 } | |
113 helper_.CallVoidMethod(&method_call, callback); | |
114 } | |
115 | |
116 // FlimflamIPConfigClient override. | |
117 void FlimflamIPConfigClientImpl::ClearProperty(const std::string& name, | |
118 const VoidCallback& callback) { | |
119 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
120 flimflam::kClearPropertyFunction); | |
121 dbus::MessageWriter writer(&method_call); | |
122 writer.AppendString(name); | |
123 helper_.CallVoidMethod(&method_call, callback); | |
124 } | |
125 | |
126 // FlimflamIPConfigClient override. | |
127 void FlimflamIPConfigClientImpl::Remove(const VoidCallback& callback) { | |
128 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
129 flimflam::kRemoveConfigFunction); | |
130 helper_.CallVoidMethod(&method_call, callback); | |
131 } | |
132 | |
133 // A stub implementation of FlimflamIPConfigClient. | |
134 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient { | |
135 public: | |
136 FlimflamIPConfigClientStubImpl() : weak_ptr_factory_(this) {} | |
137 | |
138 virtual ~FlimflamIPConfigClientStubImpl() {} | |
139 | |
140 // FlimflamIPConfigClient override. | |
141 virtual void SetPropertyChangedHandler( | |
142 const PropertyChangedHandler& handler) OVERRIDE {} | |
143 | |
144 // FlimflamIPConfigClient override. | |
145 virtual void ResetPropertyChangedHandler() OVERRIDE {} | |
146 | |
147 // FlimflamIPConfigClient override. | |
148 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | |
149 MessageLoop::current()->PostTask( | |
150 FROM_HERE, base::Bind(&FlimflamIPConfigClientStubImpl::PassProperties, | |
151 weak_ptr_factory_.GetWeakPtr(), | |
152 callback)); | |
153 } | |
154 | |
155 // FlimflamIPConfigClient override. | |
156 virtual void SetProperty(const std::string& name, | |
157 const base::Value& value, | |
158 const VoidCallback& callback) OVERRIDE { | |
159 MessageLoop::current()->PostTask( | |
160 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
161 } | |
162 | |
163 // FlimflamIPConfigClient override. | |
164 virtual void ClearProperty(const std::string& name, | |
165 const VoidCallback& callback) OVERRIDE { | |
166 MessageLoop::current()->PostTask( | |
167 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
168 } | |
169 | |
170 // FlimflamIPConfigClient override. | |
171 virtual void Remove(const VoidCallback& callback) OVERRIDE { | |
172 MessageLoop::current()->PostTask( | |
173 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | |
174 } | |
175 | |
176 private: | |
177 // Runs callback with |properties_|. | |
178 void PassProperties(const DictionaryValueCallback& callback) const { | |
179 callback.Run(DBUS_METHOD_CALL_SUCCESS, properties_); | |
180 } | |
181 | |
182 base::WeakPtrFactory<FlimflamIPConfigClientStubImpl> weak_ptr_factory_; | |
183 base::DictionaryValue properties_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl); | |
186 }; | |
187 | |
188 } // namespace | |
189 | |
190 FlimflamIPConfigClient::FlimflamIPConfigClient() {} | |
191 | |
192 FlimflamIPConfigClient::~FlimflamIPConfigClient() {} | |
193 | |
194 // static | |
195 FlimflamIPConfigClient* FlimflamIPConfigClient::Create( | |
196 DBusClientImplementationType type, | |
197 dbus::Bus* bus) { | |
198 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
199 return new FlimflamIPConfigClientImpl(bus); | |
200 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
201 return new FlimflamIPConfigClientStubImpl(); | |
202 } | |
203 | |
204 } // namespace chromeos | |
OLD | NEW |