Revert of Revert of Fix text clipping in CompositorFrameTime Display (patchset #1...
[chromium-blink-merge.git] / chromeos / dbus / fake_shill_device_client.cc
blob5475fd8a62dcf36612e8fa6d4bb6f624ad7e35e6
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.
5 #include "chromeos/dbus/fake_shill_device_client.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/stl_util.h"
12 #include "base/values.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_manager_client.h"
15 #include "chromeos/dbus/shill_property_changed_observer.h"
16 #include "dbus/bus.h"
17 #include "dbus/message.h"
18 #include "dbus/object_path.h"
19 #include "dbus/object_proxy.h"
20 #include "dbus/values_util.h"
21 #include "net/base/ip_endpoint.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
24 namespace chromeos {
26 namespace {
28 std::string kSimPin = "1111";
30 void ErrorFunction(const std::string& device_path,
31 const std::string& error_name,
32 const std::string& error_message) {
33 LOG(ERROR) << "Shill Error for: " << device_path
34 << ": " << error_name << " : " << error_message;
37 void PostNotFoundError(
38 const ShillDeviceClient::ErrorCallback& error_callback) {
39 std::string error_message("Failed");
40 base::MessageLoop::current()->PostTask(
41 FROM_HERE,
42 base::Bind(error_callback, shill::kErrorResultNotFound, error_message));
45 } // namespace
47 FakeShillDeviceClient::FakeShillDeviceClient()
48 : initial_tdls_busy_count_(0),
49 tdls_busy_count_(0),
50 weak_ptr_factory_(this) {
53 FakeShillDeviceClient::~FakeShillDeviceClient() {
54 STLDeleteContainerPairSecondPointers(
55 observer_list_.begin(), observer_list_.end());
58 // ShillDeviceClient overrides.
60 void FakeShillDeviceClient::Init(dbus::Bus* bus) {}
62 void FakeShillDeviceClient::AddPropertyChangedObserver(
63 const dbus::ObjectPath& device_path,
64 ShillPropertyChangedObserver* observer) {
65 GetObserverList(device_path).AddObserver(observer);
68 void FakeShillDeviceClient::RemovePropertyChangedObserver(
69 const dbus::ObjectPath& device_path,
70 ShillPropertyChangedObserver* observer) {
71 GetObserverList(device_path).RemoveObserver(observer);
74 void FakeShillDeviceClient::GetProperties(
75 const dbus::ObjectPath& device_path,
76 const DictionaryValueCallback& callback) {
77 base::MessageLoop::current()->PostTask(
78 FROM_HERE,
79 base::Bind(&FakeShillDeviceClient::PassStubDeviceProperties,
80 weak_ptr_factory_.GetWeakPtr(),
81 device_path, callback));
84 void FakeShillDeviceClient::ProposeScan(
85 const dbus::ObjectPath& device_path,
86 const VoidDBusMethodCallback& callback) {
87 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
90 void FakeShillDeviceClient::SetProperty(const dbus::ObjectPath& device_path,
91 const std::string& name,
92 const base::Value& value,
93 const base::Closure& callback,
94 const ErrorCallback& error_callback) {
95 base::DictionaryValue* device_properties = NULL;
96 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
97 &device_properties)) {
98 PostNotFoundError(error_callback);
99 return;
101 device_properties->SetWithoutPathExpansion(name, value.DeepCopy());
102 base::MessageLoop::current()->PostTask(
103 FROM_HERE,
104 base::Bind(&FakeShillDeviceClient::NotifyObserversPropertyChanged,
105 weak_ptr_factory_.GetWeakPtr(), device_path, name));
106 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
109 void FakeShillDeviceClient::ClearProperty(
110 const dbus::ObjectPath& device_path,
111 const std::string& name,
112 const VoidDBusMethodCallback& callback) {
113 base::DictionaryValue* device_properties = NULL;
114 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
115 &device_properties)) {
116 PostVoidCallback(callback, DBUS_METHOD_CALL_FAILURE);
117 return;
119 device_properties->RemoveWithoutPathExpansion(name, NULL);
120 PostVoidCallback(callback, DBUS_METHOD_CALL_SUCCESS);
123 void FakeShillDeviceClient::AddIPConfig(
124 const dbus::ObjectPath& device_path,
125 const std::string& method,
126 const ObjectPathDBusMethodCallback& callback) {
127 base::MessageLoop::current()->PostTask(FROM_HERE,
128 base::Bind(callback,
129 DBUS_METHOD_CALL_SUCCESS,
130 dbus::ObjectPath()));
133 void FakeShillDeviceClient::RequirePin(const dbus::ObjectPath& device_path,
134 const std::string& pin,
135 bool require,
136 const base::Closure& callback,
137 const ErrorCallback& error_callback) {
138 VLOG(1) << "RequirePin: " << device_path.value();
139 if (pin != kSimPin) {
140 base::MessageLoop::current()->PostTask(
141 FROM_HERE,
142 base::Bind(error_callback, shill::kErrorResultIncorrectPin, ""));
143 return;
145 base::DictionaryValue* device_properties = NULL;
146 if (!stub_devices_.GetDictionaryWithoutPathExpansion(device_path.value(),
147 &device_properties)) {
148 PostNotFoundError(error_callback);
149 return;
151 base::DictionaryValue* simlock_dict = NULL;
152 if (!device_properties->GetDictionaryWithoutPathExpansion(
153 shill::kSIMLockStatusProperty, &simlock_dict)) {
154 simlock_dict = new base::DictionaryValue;
155 device_properties->SetWithoutPathExpansion(
156 shill::kSIMLockStatusProperty, simlock_dict);
158 simlock_dict->Clear();
159 simlock_dict->SetBoolean(shill::kSIMLockEnabledProperty, require);
160 // TODO(stevenjb): Investigate why non-empty value breaks UI.
161 std::string lock_type = ""; // shill::kSIMLockPin
162 simlock_dict->SetString(shill::kSIMLockTypeProperty, lock_type);
163 simlock_dict->SetInteger(shill::kSIMLockRetriesLeftProperty, 5);
165 NotifyObserversPropertyChanged(device_path, shill::kSIMLockStatusProperty);
166 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
169 void FakeShillDeviceClient::EnterPin(const dbus::ObjectPath& device_path,
170 const std::string& pin,
171 const base::Closure& callback,
172 const ErrorCallback& error_callback) {
173 VLOG(1) << "EnterPin: " << device_path.value();
174 if (pin != kSimPin) {
175 base::MessageLoop::current()->PostTask(
176 FROM_HERE,
177 base::Bind(error_callback, shill::kErrorResultIncorrectPin, ""));
178 return;
180 if (!stub_devices_.HasKey(device_path.value())) {
181 PostNotFoundError(error_callback);
182 return;
184 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
187 void FakeShillDeviceClient::UnblockPin(const dbus::ObjectPath& device_path,
188 const std::string& puk,
189 const std::string& pin,
190 const base::Closure& callback,
191 const ErrorCallback& error_callback) {
192 VLOG(1) << "UnblockPin: " << device_path.value();
193 if (!stub_devices_.HasKey(device_path.value())) {
194 PostNotFoundError(error_callback);
195 return;
197 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
200 void FakeShillDeviceClient::ChangePin(const dbus::ObjectPath& device_path,
201 const std::string& old_pin,
202 const std::string& new_pin,
203 const base::Closure& callback,
204 const ErrorCallback& error_callback) {
205 VLOG(1) << "ChangePin: " << device_path.value();
206 if (!stub_devices_.HasKey(device_path.value())) {
207 PostNotFoundError(error_callback);
208 return;
210 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
213 void FakeShillDeviceClient::Register(const dbus::ObjectPath& device_path,
214 const std::string& network_id,
215 const base::Closure& callback,
216 const ErrorCallback& error_callback) {
217 if (!stub_devices_.HasKey(device_path.value())) {
218 PostNotFoundError(error_callback);
219 return;
221 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
224 void FakeShillDeviceClient::SetCarrier(const dbus::ObjectPath& device_path,
225 const std::string& carrier,
226 const base::Closure& callback,
227 const ErrorCallback& error_callback) {
228 if (!stub_devices_.HasKey(device_path.value())) {
229 PostNotFoundError(error_callback);
230 return;
232 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
235 void FakeShillDeviceClient::Reset(const dbus::ObjectPath& device_path,
236 const base::Closure& callback,
237 const ErrorCallback& error_callback) {
238 if (!stub_devices_.HasKey(device_path.value())) {
239 PostNotFoundError(error_callback);
240 return;
242 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
245 void FakeShillDeviceClient::PerformTDLSOperation(
246 const dbus::ObjectPath& device_path,
247 const std::string& operation,
248 const std::string& peer,
249 const StringCallback& callback,
250 const ErrorCallback& error_callback) {
251 if (!stub_devices_.HasKey(device_path.value())) {
252 PostNotFoundError(error_callback);
253 return;
255 // Use -1 to emulate a TDLS failure.
256 if (tdls_busy_count_ == -1) {
257 base::MessageLoop::current()->PostTask(
258 FROM_HERE,
259 base::Bind(error_callback, shill::kErrorDhcpFailed, "Failed"));
260 return;
262 if (operation != shill::kTDLSStatusOperation && tdls_busy_count_ > 0) {
263 --tdls_busy_count_;
264 base::MessageLoop::current()->PostTask(
265 FROM_HERE, base::Bind(error_callback, shill::kErrorResultInProgress,
266 "In-Progress"));
267 return;
270 tdls_busy_count_ = initial_tdls_busy_count_;
272 std::string result;
273 if (operation == shill::kTDLSDiscoverOperation) {
274 if (tdls_state_.empty())
275 tdls_state_ = shill::kTDLSDisconnectedState;
276 } else if (operation == shill::kTDLSSetupOperation) {
277 if (tdls_state_.empty())
278 tdls_state_ = shill::kTDLSConnectedState;
279 } else if (operation == shill::kTDLSTeardownOperation) {
280 if (tdls_state_.empty())
281 tdls_state_ = shill::kTDLSDisconnectedState;
282 } else if (operation == shill::kTDLSStatusOperation) {
283 result = tdls_state_;
286 base::MessageLoop::current()->PostTask(FROM_HERE,
287 base::Bind(callback, result));
290 void FakeShillDeviceClient::AddWakeOnPacketConnection(
291 const dbus::ObjectPath& device_path,
292 const net::IPEndPoint& ip_endpoint,
293 const base::Closure& callback,
294 const ErrorCallback& error_callback) {
295 if (!stub_devices_.HasKey(device_path.value())) {
296 PostNotFoundError(error_callback);
297 return;
300 wake_on_packet_connections_[device_path].insert(ip_endpoint);
302 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
305 void FakeShillDeviceClient::RemoveWakeOnPacketConnection(
306 const dbus::ObjectPath& device_path,
307 const net::IPEndPoint& ip_endpoint,
308 const base::Closure& callback,
309 const ErrorCallback& error_callback) {
310 const auto device_iter = wake_on_packet_connections_.find(device_path);
311 if (!stub_devices_.HasKey(device_path.value()) ||
312 device_iter == wake_on_packet_connections_.end()) {
313 PostNotFoundError(error_callback);
314 return;
317 const auto endpoint_iter = device_iter->second.find(ip_endpoint);
318 if (endpoint_iter == device_iter->second.end()) {
319 PostNotFoundError(error_callback);
320 return;
323 device_iter->second.erase(endpoint_iter);
325 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
328 void FakeShillDeviceClient::RemoveAllWakeOnPacketConnections(
329 const dbus::ObjectPath& device_path,
330 const base::Closure& callback,
331 const ErrorCallback& error_callback) {
332 const auto iter = wake_on_packet_connections_.find(device_path);
333 if (!stub_devices_.HasKey(device_path.value()) ||
334 iter == wake_on_packet_connections_.end()) {
335 PostNotFoundError(error_callback);
336 return;
339 wake_on_packet_connections_.erase(iter);
341 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
344 ShillDeviceClient::TestInterface* FakeShillDeviceClient::GetTestInterface() {
345 return this;
348 // ShillDeviceClient::TestInterface overrides.
350 void FakeShillDeviceClient::AddDevice(const std::string& device_path,
351 const std::string& type,
352 const std::string& name) {
353 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
354 AddDevice(device_path);
356 base::DictionaryValue* properties = GetDeviceProperties(device_path);
357 properties->SetStringWithoutPathExpansion(shill::kTypeProperty, type);
358 properties->SetStringWithoutPathExpansion(shill::kNameProperty, name);
359 properties->SetStringWithoutPathExpansion(shill::kDBusObjectProperty,
360 device_path);
361 properties->SetStringWithoutPathExpansion(
362 shill::kDBusServiceProperty, modemmanager::kModemManager1ServiceName);
363 if (type == shill::kTypeCellular) {
364 properties->SetBooleanWithoutPathExpansion(
365 shill::kCellularAllowRoamingProperty, false);
369 void FakeShillDeviceClient::RemoveDevice(const std::string& device_path) {
370 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
371 RemoveDevice(device_path);
373 stub_devices_.RemoveWithoutPathExpansion(device_path, NULL);
376 void FakeShillDeviceClient::ClearDevices() {
377 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
378 ClearDevices();
380 stub_devices_.Clear();
383 void FakeShillDeviceClient::SetDeviceProperty(const std::string& device_path,
384 const std::string& name,
385 const base::Value& value) {
386 VLOG(1) << "SetDeviceProperty: " << device_path
387 << ": " << name << " = " << value;
388 SetProperty(dbus::ObjectPath(device_path), name, value,
389 base::Bind(&base::DoNothing),
390 base::Bind(&ErrorFunction, device_path));
393 std::string FakeShillDeviceClient::GetDevicePathForType(
394 const std::string& type) {
395 for (base::DictionaryValue::Iterator iter(stub_devices_);
396 !iter.IsAtEnd(); iter.Advance()) {
397 const base::DictionaryValue* properties = NULL;
398 if (!iter.value().GetAsDictionary(&properties))
399 continue;
400 std::string prop_type;
401 if (!properties->GetStringWithoutPathExpansion(
402 shill::kTypeProperty, &prop_type) ||
403 prop_type != type)
404 continue;
405 return iter.key();
407 return std::string();
410 void FakeShillDeviceClient::SetTDLSBusyCount(int count) {
411 tdls_busy_count_ = std::max(count, -1);
414 void FakeShillDeviceClient::SetTDLSState(const std::string& state) {
415 tdls_state_ = state;
418 void FakeShillDeviceClient::PassStubDeviceProperties(
419 const dbus::ObjectPath& device_path,
420 const DictionaryValueCallback& callback) const {
421 const base::DictionaryValue* device_properties = NULL;
422 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
423 device_path.value(), &device_properties)) {
424 base::DictionaryValue empty_dictionary;
425 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
426 return;
428 callback.Run(DBUS_METHOD_CALL_SUCCESS, *device_properties);
431 // Posts a task to run a void callback with status code |status|.
432 void FakeShillDeviceClient::PostVoidCallback(
433 const VoidDBusMethodCallback& callback,
434 DBusMethodCallStatus status) {
435 base::MessageLoop::current()->PostTask(FROM_HERE,
436 base::Bind(callback, status));
439 void FakeShillDeviceClient::NotifyObserversPropertyChanged(
440 const dbus::ObjectPath& device_path,
441 const std::string& property) {
442 base::DictionaryValue* dict = NULL;
443 std::string path = device_path.value();
444 if (!stub_devices_.GetDictionaryWithoutPathExpansion(path, &dict)) {
445 LOG(ERROR) << "Notify for unknown service: " << path;
446 return;
448 base::Value* value = NULL;
449 if (!dict->GetWithoutPathExpansion(property, &value)) {
450 LOG(ERROR) << "Notify for unknown property: "
451 << path << " : " << property;
452 return;
454 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
455 GetObserverList(device_path),
456 OnPropertyChanged(property, *value));
459 base::DictionaryValue* FakeShillDeviceClient::GetDeviceProperties(
460 const std::string& device_path) {
461 base::DictionaryValue* properties = NULL;
462 if (!stub_devices_.GetDictionaryWithoutPathExpansion(
463 device_path, &properties)) {
464 properties = new base::DictionaryValue;
465 stub_devices_.SetWithoutPathExpansion(device_path, properties);
467 return properties;
470 FakeShillDeviceClient::PropertyObserverList&
471 FakeShillDeviceClient::GetObserverList(const dbus::ObjectPath& device_path) {
472 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
473 observer_list_.find(device_path);
474 if (iter != observer_list_.end())
475 return *(iter->second);
476 PropertyObserverList* observer_list = new PropertyObserverList();
477 observer_list_[device_path] = observer_list;
478 return *observer_list;
481 } // namespace chromeos