Remove ExtensionPrefs::SetDidExtensionEscalatePermissions.
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_adapter_client.cc
blob51a5cf876b1f33886a114d78d982f2177bbba479
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 set_discovery_filter_should_fail_(false),
79 simulation_interval_ms_(kSimulationIntervalMs) {
80 properties_.reset(new Properties(base::Bind(
81 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
83 properties_->address.ReplaceValue(kAdapterAddress);
84 properties_->name.ReplaceValue("Fake Adapter (Name)");
85 properties_->alias.ReplaceValue(kAdapterName);
86 properties_->pairable.ReplaceValue(true);
88 second_properties_.reset(new Properties(base::Bind(
89 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
91 second_properties_->address.ReplaceValue(kSecondAdapterAddress);
92 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)");
93 second_properties_->alias.ReplaceValue(kSecondAdapterName);
94 second_properties_->pairable.ReplaceValue(true);
97 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {
100 void FakeBluetoothAdapterClient::Init(dbus::Bus* bus) {
103 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) {
104 observers_.AddObserver(observer);
107 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) {
108 observers_.RemoveObserver(observer);
111 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() {
112 std::vector<dbus::ObjectPath> object_paths;
113 if (visible_)
114 object_paths.push_back(dbus::ObjectPath(kAdapterPath));
115 if (second_visible_)
116 object_paths.push_back(dbus::ObjectPath(kSecondAdapterPath));
117 return object_paths;
120 FakeBluetoothAdapterClient::Properties*
121 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath& object_path) {
122 if (object_path == dbus::ObjectPath(kAdapterPath))
123 return properties_.get();
124 else if (object_path == dbus::ObjectPath(kSecondAdapterPath))
125 return second_properties_.get();
126 else
127 return NULL;
130 void FakeBluetoothAdapterClient::StartDiscovery(
131 const dbus::ObjectPath& object_path,
132 const base::Closure& callback,
133 const ErrorCallback& error_callback) {
134 if (object_path != dbus::ObjectPath(kAdapterPath)) {
135 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
136 return;
139 ++discovering_count_;
140 VLOG(1) << "StartDiscovery: " << object_path.value() << ", "
141 << "count is now " << discovering_count_;
142 PostDelayedTask(callback);
144 if (discovering_count_ == 1) {
145 properties_->discovering.ReplaceValue(true);
147 FakeBluetoothDeviceClient* device_client =
148 static_cast<FakeBluetoothDeviceClient*>(
149 DBusThreadManager::Get()->GetBluetoothDeviceClient());
150 device_client->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
154 void FakeBluetoothAdapterClient::StopDiscovery(
155 const dbus::ObjectPath& object_path,
156 const base::Closure& callback,
157 const ErrorCallback& error_callback) {
158 if (object_path != dbus::ObjectPath(kAdapterPath)) {
159 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
160 return;
163 if (!discovering_count_) {
164 LOG(WARNING) << "StopDiscovery called when not discovering";
165 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
166 return;
169 --discovering_count_;
170 VLOG(1) << "StopDiscovery: " << object_path.value() << ", "
171 << "count is now " << discovering_count_;
172 PostDelayedTask(callback);
174 if (discovering_count_ == 0) {
175 FakeBluetoothDeviceClient* device_client =
176 static_cast<FakeBluetoothDeviceClient*>(
177 DBusThreadManager::Get()->GetBluetoothDeviceClient());
178 device_client->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
180 if (simulation_interval_ms_ > 100) {
181 device_client->BeginIncomingPairingSimulation(
182 dbus::ObjectPath(kAdapterPath));
185 discovery_filter_.reset();
186 properties_->discovering.ReplaceValue(false);
190 void FakeBluetoothAdapterClient::RemoveDevice(
191 const dbus::ObjectPath& object_path,
192 const dbus::ObjectPath& device_path,
193 const base::Closure& callback,
194 const ErrorCallback& error_callback) {
195 if (object_path != dbus::ObjectPath(kAdapterPath)) {
196 error_callback.Run(kNoResponseError, "");
197 return;
200 VLOG(1) << "RemoveDevice: " << object_path.value()
201 << " " << device_path.value();
202 callback.Run();
204 FakeBluetoothDeviceClient* device_client =
205 static_cast<FakeBluetoothDeviceClient*>(
206 DBusThreadManager::Get()->GetBluetoothDeviceClient());
207 device_client->RemoveDevice(dbus::ObjectPath(kAdapterPath), device_path);
210 void FakeBluetoothAdapterClient::MakeSetDiscoveryFilterFail() {
211 set_discovery_filter_should_fail_ = true;
214 void FakeBluetoothAdapterClient::SetDiscoveryFilter(
215 const dbus::ObjectPath& object_path,
216 const DiscoveryFilter& discovery_filter,
217 const base::Closure& callback,
218 const ErrorCallback& error_callback) {
219 if (object_path != dbus::ObjectPath(kAdapterPath)) {
220 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
221 return;
223 VLOG(1) << "SetDiscoveryFilter: " << object_path.value();
225 if (set_discovery_filter_should_fail_) {
226 PostDelayedTask(base::Bind(error_callback, kNoResponseError, ""));
227 set_discovery_filter_should_fail_ = false;
228 return;
231 discovery_filter_.reset(new DiscoveryFilter());
232 discovery_filter_->CopyFrom(discovery_filter);
233 PostDelayedTask(callback);
236 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms) {
237 simulation_interval_ms_ = interval_ms;
240 BluetoothAdapterClient::DiscoveryFilter*
241 FakeBluetoothAdapterClient::GetDiscoveryFilter() {
242 return discovery_filter_.get();
245 void FakeBluetoothAdapterClient::SetVisible(
246 bool visible) {
247 if (visible && !visible_) {
248 // Adapter becoming visible
249 visible_ = visible;
251 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
252 AdapterAdded(dbus::ObjectPath(kAdapterPath)));
254 } else if (visible_ && !visible) {
255 // Adapter becoming invisible
256 visible_ = visible;
258 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
259 AdapterRemoved(dbus::ObjectPath(kAdapterPath)));
263 void FakeBluetoothAdapterClient::SetSecondVisible(
264 bool visible) {
265 if (visible && !second_visible_) {
266 // Second adapter becoming visible
267 second_visible_ = visible;
269 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
270 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath)));
272 } else if (second_visible_ && !visible) {
273 // Second adapter becoming invisible
274 second_visible_ = visible;
276 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
277 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath)));
281 void FakeBluetoothAdapterClient::OnPropertyChanged(
282 const std::string& property_name) {
283 if (property_name == properties_->powered.name() &&
284 !properties_->powered.value()) {
285 VLOG(1) << "Adapter powered off";
287 if (discovering_count_) {
288 discovering_count_ = 0;
289 properties_->discovering.ReplaceValue(false);
293 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
294 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath),
295 property_name));
298 void FakeBluetoothAdapterClient::PostDelayedTask(
299 const base::Closure& callback) {
300 base::MessageLoop::current()->PostDelayedTask(
301 FROM_HERE, callback,
302 base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
305 } // namespace chromeos