Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / fake_shill_device_client.cc
1 // Copyright 2013 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 "chromeos/dbus/fake_shill_device_client.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/shill_manager_client.h"
13 #include "chromeos/dbus/shill_property_changed_observer.h"
14 #include "chromeos/network/shill_property_util.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "dbus/values_util.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21
22 namespace chromeos {
23
24 namespace {
25
26 void ErrorFunction(const std::string& device_path,
27                    const std::string& error_name,
28                    const std::string& error_message) {
29   LOG(ERROR) << "Shill Error for: " << device_path
30              << ": " << error_name << " : " << error_message;
31 }
32
33 void PostDeviceNotFoundError(
34     const ShillDeviceClient::ErrorCallback& error_callback) {
35   std::string error_message("Failed");
36   base::MessageLoop::current()->PostTask(
37       FROM_HERE,
38       base::Bind(error_callback, shill::kErrorResultNotFound, error_message));
39 }
40
41 }  // namespace
42
43 FakeShillDeviceClient::FakeShillDeviceClient()
44     : tdls_busy_count_(0),
45       weak_ptr_factory_(this) {
46 }
47
48 FakeShillDeviceClient::~FakeShillDeviceClient() {
49   STLDeleteContainerPairSecondPointers(
50       observer_list_.begin(), observer_list_.end());
51 }
52
53 // ShillDeviceClient overrides.
54
55 void FakeShillDeviceClient::Init(dbus::Bus* bus) {}
56
57 void FakeShillDeviceClient::AddPropertyChangedObserver(
58     const dbus::ObjectPath& device_path,
59     ShillPropertyChangedObserver* observer) {
60   GetObserverList(device_path).AddObserver(observer);
61 }
62
63 void FakeShillDeviceClient::RemovePropertyChangedObserver(
64     const dbus::ObjectPath& device_path,
65     ShillPropertyChangedObserver* observer) {
66   GetObserverList(device_path).RemoveObserver(observer);
67 }
68
69 void FakeShillDeviceClient::GetProperties(
70     const dbus::ObjectPath& device_path,
71     const DictionaryValueCallback& callback) {
72   base::MessageLoop::current()->PostTask(
73       FROM_HERE,
74       base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties,
75                  weak_ptr_factory_.GetWeakPtr(),
76                  device_path, callback));
77 }
78
79 void FakeShillDeviceClient::ProposeScan(
80     const dbus::ObjectPath& device_path,
81     const VoidDBusMethodCallback& callback) {
82   PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
83 }
84
85 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path,
86                                         const std::string& name,
87                                         const base::Value& value,
88                                         const base::Closure& callback,
89                                         const ErrorCallback& error_callback) {
90   base::DictionaryValue* device_properties = NULL;
91   if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
92                                                        &device_properties)) {
93     PostDeviceNotFoundError(error_callback);
94     return;
95   }
96   device_properties->SetWithoutPathExpansion(name, value.DeepCopy());
97   base::MessageLoop::current()->PostTask(
98       FROM_HERE,
99       base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged,
100                  weak_ptr_factory_.GetWeakPtr(), device_path, name));
101   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
102 }
103
104 void FakeShillDeviceClient::ClearProperty(
105     const dbus::ObjectPath& device_path,
106     const std::string& name,
107     const VoidDBusMethodCallback& callback) {
108   base::DictionaryValue* device_properties = NULL;
109   if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
110                                                        &device_properties)) {
111     PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE);
112     return;
113   }
114   device_properties->RemoveWithoutPathExpansion(name, NULL);
115   PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
116 }
117
118 void FakeShillDeviceClient::AddIPConfig(
119     const dbus::ObjectPath& device_path,
120     const std::string& method,
121     const ObjectPathDBusMethodCallback& callback) {
122   base::MessageLoop::current()->PostTask(FROM_HERE,
123                                          base::Bind(callback,
124                                                     DBUS_METHOD_CALL_SUCCESS,
125                                                     dbus::ObjectPath()));
126 }
127
128 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path,
129                                        const std::string& pin,
130                                        bool require,
131                                        const base::Closure& callback,
132                                        const ErrorCallback& error_callback) {
133   if (!stub_devices_.HasKey(device_path.value())) {
134     PostDeviceNotFoundError(error_callback);
135     return;
136   }
137   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
138 }
139
140 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path,
141                                      const std::string& pin,
142                                      const base::Closure& callback,
143                                      const ErrorCallback& error_callback) {
144   if (!stub_devices_.HasKey(device_path.value())) {
145     PostDeviceNotFoundError(error_callback);
146     return;
147   }
148   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
149 }
150
151 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path,
152                                        const std::string& puk,
153                                        const std::string& pin,
154                                        const base::Closure& callback,
155                                        const ErrorCallback& error_callback) {
156   if (!stub_devices_.HasKey(device_path.value())) {
157     PostDeviceNotFoundError(error_callback);
158     return;
159   }
160   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
161 }
162
163 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path,
164                                       const std::string& old_pin,
165                                       const std::string& new_pin,
166                                       const base::Closure& callback,
167                                       const ErrorCallback& error_callback) {
168   if (!stub_devices_.HasKey(device_path.value())) {
169     PostDeviceNotFoundError(error_callback);
170     return;
171   }
172   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
173 }
174
175 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path,
176                                      const std::string& network_id,
177                                      const base::Closure& callback,
178                                      const ErrorCallback& error_callback) {
179   if (!stub_devices_.HasKey(device_path.value())) {
180     PostDeviceNotFoundError(error_callback);
181     return;
182   }
183   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
184 }
185
186 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path,
187                                        const std::string& carrier,
188                                        const base::Closure& callback,
189                                        const ErrorCallback& error_callback) {
190   if (!stub_devices_.HasKey(device_path.value())) {
191     PostDeviceNotFoundError(error_callback);
192     return;
193   }
194   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
195 }
196
197 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path,
198                                   const base::Closure& callback,
199                                   const ErrorCallback& error_callback) {
200   if (!stub_devices_.HasKey(device_path.value())) {
201     PostDeviceNotFoundError(error_callback);
202     return;
203   }
204   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
205 }
206
207 void FakeShillDeviceClient::PerformTDLSOperation(
208     const dbus::ObjectPath& device_path,
209     const std::string& operation,
210     const std::string& peer,
211     const StringCallback& callback,
212     const ErrorCallback& error_callback) {
213   if (!stub_devices_.HasKey(device_path.value())) {
214     PostDeviceNotFoundError(error_callback);
215     return;
216   }
217   if (tdls_busy_count_) {
218     --tdls_busy_count_;
219     std::string error_message("In-Progress");
220     base::MessageLoop::current()->PostTask(
221         FROM_HERE,
222         base::Bind(error_callback,
223                    shill::kErrorResultInProgress, error_message));
224     return;
225   }
226   std::string result;
227   if (operation == shill::kTDLSStatusOperation)
228     result = shill::kTDLSConnectedState;
229   base::MessageLoop::current()->PostTask(FROM_HERE,
230                                          base::Bind(callback, result));
231 }
232
233 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() {
234   return this;
235 }
236
237 // ShillDeviceClient::TestInterface overrides.
238
239 void FakeShillDeviceClient::AddDevice(const std::string& device_path,
240                                       const std::string& type,
241                                       const std::string& object_path) {
242   DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
243       AddDevice(device_path);
244
245   base::DictionaryValue* properties = GetDeviceProperties(device_path);
246   properties->SetWithoutPathExpansion(shill::kTypeProperty,
247                                       base::Value::CreateStringValue(type));
248   properties->SetWithoutPathExpansion(
249       shill::kDBusObjectProperty, base::Value::CreateStringValue(object_path));
250   properties->SetWithoutPathExpansion(
251       shill::kDBusServiceProperty,
252       base::Value::CreateStringValue(modemmanager::kModemManager1));
253   if (NetworkTypePattern::Cellular().MatchesType(type)) {
254     properties->SetWithoutPathExpansion(shill::kCellularAllowRoamingProperty,
255                                         new base::FundamentalValue(false));
256   }
257 }
258
259 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) {
260   DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
261       RemoveDevice(device_path);
262
263   stub_devices_.RemoveWithoutPathExpansion(device_path, NULL);
264 }
265
266 void FakeShillDeviceClient::ClearDevices() {
267   DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
268       ClearDevices();
269
270   stub_devices_.Clear();
271 }
272
273 void FakeShillDeviceClient::SetDeviceProperty(const std::string& device_path,
274                                               const std::string& name,
275                                               const base::Value& value) {
276   VLOG(1) << "SetDeviceProperty: " << device_path
277           << ": " << name << " = " << value;
278   SetProperty(dbus::ObjectPath(device_path), name, value,
279               base::Bind(&base::DoNothing),
280               base::Bind(&ErrorFunction, device_path));
281 }
282
283 std::string FakeShillDeviceClient::GetDevicePathForType(
284     const std::string& type) {
285   for (base::DictionaryValue::Iterator iter(stub_devices_);
286        !iter.IsAtEnd(); iter.Advance()) {
287     const base::DictionaryValue* properties = NULL;
288     if (!iter.value().GetAsDictionary(&properties))
289       continue;
290     std::string prop_type;
291     if (!properties->GetStringWithoutPathExpansion(
292             shill::kTypeProperty, &prop_type) ||
293         prop_type != type)
294       continue;
295     return iter.key();
296   }
297   return std::string();
298 }
299
300 void FakeShillDeviceClient::PassStubDeviceProperties(
301     const dbus::ObjectPath& device_path,
302     const DictionaryValueCallback& callback) const {
303   const base::DictionaryValue* device_properties = NULL;
304   if (!stub_devices_.GetDictionaryWithoutPathExpansion(
305       device_path.value(), &device_properties)) {
306     base::DictionaryValue empty_dictionary;
307     callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
308     return;
309   }
310   callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties);
311 }
312
313 // Posts a task to run a void callback with status code |status|.
314 void FakeShillDeviceClient::PostVoidCallback(
315     const VoidDBusMethodCallback& callback,
316     DBusMethodCallStatus status) {
317   base::MessageLoop::current()->PostTask(FROM_HERE,
318                                          base::Bind(callback, status));
319 }
320
321 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
322     const dbus::ObjectPath& device_path,
323     const std::string& property) {
324   base::DictionaryValue* dict = NULL;
325   std::string path = device_path.value();
326   if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) {
327     LOG(ERROR) << "Notify for unknown service: " << path;
328     return;
329   }
330   base::Value* value = NULL;
331   if (!dict->GetWithoutPathExpansion(property, &value)) {
332     LOG(ERROR) << "Notify for unknown property: "
333         << path << " : " << property;
334     return;
335   }
336   FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
337                     GetObserverList(device_path),
338                     OnPropertyChanged(property, *value));
339 }
340
341 base::DictionaryValue* FakeShillDeviceClient::GetDeviceProperties(
342     const std::string& device_path) {
343   base::DictionaryValue* properties = NULL;
344   if (!stub_devices_.GetDictionaryWithoutPathExpansion(
345       device_path, &properties)) {
346     properties = new base::DictionaryValue;
347     stub_devices_.SetWithoutPathExpansion(device_path, properties);
348   }
349   return properties;
350 }
351
352 FakeShillDeviceClient::PropertyObserverList&
353 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath& device_path) {
354   std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
355       observer_list_.find(device_path);
356   if (iter != observer_list_.end())
357     return *(iter->second);
358   PropertyObserverList* observer_list = new PropertyObserverList();
359   observer_list_[device_path] = observer_list;
360   return *observer_list;
361 }
362
363 }  // namespace chromeos