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