Fix content_browsertests on Linux GN build.
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_adapter_client.cc
blob2ac0d9f160510415df35e6a8040beb90aaac3591
1 // Copyright (c) 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_bluetooth_adapter_client.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_bluetooth_device_client.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
14 namespace chromeos {
16 namespace {
18 // Default interval for delayed tasks.
19 const int kSimulationIntervalMs = 750;
21 } // namespace
23 const char FakeBluetoothAdapterClient::kAdapterPath[] =
24 "/fake/hci0";
25 const char FakeBluetoothAdapterClient::kAdapterName[] =
26 "Fake Adapter";
27 const char FakeBluetoothAdapterClient::kAdapterAddress[] =
28 "01:1A:2B:1A:2B:03";
30 const char FakeBluetoothAdapterClient::kSecondAdapterPath[] =
31 "/fake/hci1";
32 const char FakeBluetoothAdapterClient::kSecondAdapterName[] =
33 "Second Fake Adapter";
34 const char FakeBluetoothAdapterClient::kSecondAdapterAddress[] =
35 "00:DE:51:10:01:00";
37 FakeBluetoothAdapterClient::Properties::Properties(
38 const PropertyChangedCallback& callback)
39 : BluetoothAdapterClient::Properties(
40 NULL,
41 bluetooth_adapter::kBluetoothAdapterInterface,
42 callback) {
45 FakeBluetoothAdapterClient::Properties::~Properties() {
48 void FakeBluetoothAdapterClient::Properties::Get(
49 dbus::PropertyBase* property,
50 dbus::PropertySet::GetCallback callback) {
51 VLOG(1) << "Get " << property->name();
52 callback.Run(false);
55 void FakeBluetoothAdapterClient::Properties::GetAll() {
56 VLOG(1) << "GetAll";
59 void FakeBluetoothAdapterClient::Properties::Set(
60 dbus::PropertyBase *property,
61 dbus::PropertySet::SetCallback callback) {
62 VLOG(1) << "Set " << property->name();
63 if (property->name() == powered.name() ||
64 property->name() == alias.name() ||
65 property->name() == discoverable.name() ||
66 property->name() == discoverable_timeout.name()) {
67 callback.Run(true);
68 property->ReplaceValueWithSetValue();
69 } else {
70 callback.Run(false);
74 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient()
75 : visible_(true),
76 second_visible_(false),
77 discovering_count_(0),
78 simulation_interval_ms_(kSimulationIntervalMs) {
79 properties_.reset(new Properties(base::Bind(
80 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
82 properties_->address.ReplaceValue(kAdapterAddress);
83 properties_->name.ReplaceValue("Fake Adapter (Name)");
84 properties_->alias.ReplaceValue(kAdapterName);
85 properties_->pairable.ReplaceValue(true);
87 second_properties_.reset(new Properties(base::Bind(
88 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
90 second_properties_->address.ReplaceValue(kSecondAdapterAddress);
91 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)");
92 second_properties_->alias.ReplaceValue(kSecondAdapterName);
93 second_properties_->pairable.ReplaceValue(true);
96 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {
99 void FakeBluetoothAdapterClient::Init(dbus::Bus* bus) {
102 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) {
103 observers_.AddObserver(observer);
106 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) {
107 observers_.RemoveObserver(observer);
110 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() {
111 std::vector<dbus::ObjectPath> object_paths;
112 if (visible_)
113 object_paths.push_back(dbus::ObjectPath(kAdapterPath));
114 if (second_visible_)
115 object_paths.push_back(dbus::ObjectPath(kSecondAdapterPath));
116 return object_paths;
119 FakeBluetoothAdapterClient::Properties*
120 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath& object_path) {
121 if (object_path == dbus::ObjectPath(kAdapterPath))
122 return properties_.get();
123 else if (object_path == dbus::ObjectPath(kSecondAdapterPath))
124 return second_properties_.get();
125 else
126 return NULL;
129 void FakeBluetoothAdapterClient::StartDiscovery(
130 const dbus::ObjectPath& object_path,
131 const base::Closure& callback,
132 const ErrorCallback& error_callback) {
133 if (object_path != dbus::ObjectPath(kAdapterPath)) {
134 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
135 return;
138 ++discovering_count_;
139 VLOG(1) << "StartDiscovery: " << object_path.value() << ", "
140 << "count is now " << discovering_count_;
141 PostDelayedTask(callback);
143 if (discovering_count_ == 1) {
144 properties_->discovering.ReplaceValue(true);
146 FakeBluetoothDeviceClient* device_client =
147 static_cast<FakeBluetoothDeviceClient*>(
148 DBusThreadManager::Get()->GetBluetoothDeviceClient());
149 device_client->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
153 void FakeBluetoothAdapterClient::StopDiscovery(
154 const dbus::ObjectPath& object_path,
155 const base::Closure& callback,
156 const ErrorCallback& error_callback) {
157 if (object_path != dbus::ObjectPath(kAdapterPath)) {
158 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
159 return;
162 if (!discovering_count_) {
163 LOG(WARNING) << "StopDiscovery called when not discovering";
164 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
165 return;
168 --discovering_count_;
169 VLOG(1) << "StopDiscovery: " << object_path.value() << ", "
170 << "count is now " << discovering_count_;
171 PostDelayedTask(callback);
173 if (discovering_count_ == 0) {
174 FakeBluetoothDeviceClient* device_client =
175 static_cast<FakeBluetoothDeviceClient*>(
176 DBusThreadManager::Get()->GetBluetoothDeviceClient());
177 device_client->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
179 if (simulation_interval_ms_ > 100) {
180 device_client->BeginIncomingPairingSimulation(
181 dbus::ObjectPath(kAdapterPath));
184 properties_->discovering.ReplaceValue(false);
188 void FakeBluetoothAdapterClient::RemoveDevice(
189 const dbus::ObjectPath& object_path,
190 const dbus::ObjectPath& device_path,
191 const base::Closure& callback,
192 const ErrorCallback& error_callback) {
193 if (object_path != dbus::ObjectPath(kAdapterPath)) {
194 error_callback.Run(kNoResponseError, "");
195 return;
198 VLOG(1) << "RemoveDevice: " << object_path.value()
199 << " " << device_path.value();
200 callback.Run();
202 FakeBluetoothDeviceClient* device_client =
203 static_cast<FakeBluetoothDeviceClient*>(
204 DBusThreadManager::Get()->GetBluetoothDeviceClient());
205 device_client->RemoveDevice(dbus::ObjectPath(kAdapterPath), device_path);
208 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms) {
209 simulation_interval_ms_ = interval_ms;
212 void FakeBluetoothAdapterClient::SetVisible(
213 bool visible) {
214 if (visible && !visible_) {
215 // Adapter becoming visible
216 visible_ = visible;
218 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
219 AdapterAdded(dbus::ObjectPath(kAdapterPath)));
221 } else if (visible_ && !visible) {
222 // Adapter becoming invisible
223 visible_ = visible;
225 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
226 AdapterRemoved(dbus::ObjectPath(kAdapterPath)));
230 void FakeBluetoothAdapterClient::SetSecondVisible(
231 bool visible) {
232 if (visible && !second_visible_) {
233 // Second adapter becoming visible
234 second_visible_ = visible;
236 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
237 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath)));
239 } else if (second_visible_ && !visible) {
240 // Second adapter becoming invisible
241 second_visible_ = visible;
243 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
244 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath)));
248 void FakeBluetoothAdapterClient::OnPropertyChanged(
249 const std::string& property_name) {
250 if (property_name == properties_->powered.name() &&
251 !properties_->powered.value()) {
252 VLOG(1) << "Adapter powered off";
254 if (discovering_count_) {
255 discovering_count_ = 0;
256 properties_->discovering.ReplaceValue(false);
260 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
261 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath),
262 property_name));
265 void FakeBluetoothAdapterClient::PostDelayedTask(
266 const base::Closure& callback) {
267 base::MessageLoop::current()->PostDelayedTask(
268 FROM_HERE, callback,
269 base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
272 } // namespace chromeos