1 // Copyright (c) 2012 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 "device/bluetooth/bluetooth_adapter_win.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/stl_util.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "device/bluetooth/bluetooth_device_win.h"
18 #include "device/bluetooth/bluetooth_socket_thread.h"
19 #include "device/bluetooth/bluetooth_socket_win.h"
20 #include "device/bluetooth/bluetooth_task_manager_win.h"
21 #include "device/bluetooth/bluetooth_uuid.h"
26 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
27 const InitCallback
& init_callback
) {
28 return BluetoothAdapterWin::CreateAdapter(init_callback
);
32 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapterWin::CreateAdapter(
33 const InitCallback
& init_callback
) {
34 BluetoothAdapterWin
* adapter
= new BluetoothAdapterWin(init_callback
);
36 return adapter
->weak_ptr_factory_
.GetWeakPtr();
39 BluetoothAdapterWin::BluetoothAdapterWin(const InitCallback
& init_callback
)
41 init_callback_(init_callback
),
44 discovery_status_(NOT_DISCOVERING
),
45 num_discovery_listeners_(0),
46 weak_ptr_factory_(this) {
49 BluetoothAdapterWin::~BluetoothAdapterWin() {
50 if (task_manager_
.get()) {
51 task_manager_
->RemoveObserver(this);
52 task_manager_
->Shutdown();
56 std::string
BluetoothAdapterWin::GetAddress() const {
60 std::string
BluetoothAdapterWin::GetName() const {
64 void BluetoothAdapterWin::SetName(const std::string
& name
,
65 const base::Closure
& callback
,
66 const ErrorCallback
& error_callback
) {
70 // TODO(youngki): Return true when |task_manager_| initializes the adapter
72 bool BluetoothAdapterWin::IsInitialized() const {
76 bool BluetoothAdapterWin::IsPresent() const {
77 return !address_
.empty();
80 bool BluetoothAdapterWin::IsPowered() const {
84 void BluetoothAdapterWin::SetPowered(
86 const base::Closure
& callback
,
87 const ErrorCallback
& error_callback
) {
88 task_manager_
->PostSetPoweredBluetoothTask(powered
, callback
, error_callback
);
91 bool BluetoothAdapterWin::IsDiscoverable() const {
96 void BluetoothAdapterWin::SetDiscoverable(
98 const base::Closure
& callback
,
99 const ErrorCallback
& error_callback
) {
103 bool BluetoothAdapterWin::IsDiscovering() const {
104 return discovery_status_
== DISCOVERING
||
105 discovery_status_
== DISCOVERY_STOPPING
;
108 void BluetoothAdapterWin::DiscoveryStarted(bool success
) {
109 discovery_status_
= success
? DISCOVERING
: NOT_DISCOVERING
;
110 for (std::vector
<std::pair
<base::Closure
, ErrorCallback
> >::const_iterator
111 iter
= on_start_discovery_callbacks_
.begin();
112 iter
!= on_start_discovery_callbacks_
.end();
115 ui_task_runner_
->PostTask(FROM_HERE
, iter
->first
);
117 ui_task_runner_
->PostTask(FROM_HERE
, iter
->second
);
119 num_discovery_listeners_
= on_start_discovery_callbacks_
.size();
120 on_start_discovery_callbacks_
.clear();
123 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
124 AdapterDiscoveringChanged(this, true));
126 // If there are stop discovery requests, post the stop discovery again.
127 MaybePostStopDiscoveryTask();
128 } else if (!on_stop_discovery_callbacks_
.empty()) {
129 // If there are stop discovery requests but start discovery has failed,
130 // notify that stop discovery has been complete.
135 void BluetoothAdapterWin::DiscoveryStopped() {
136 discovered_devices_
.clear();
137 bool was_discovering
= IsDiscovering();
138 discovery_status_
= NOT_DISCOVERING
;
139 for (std::vector
<base::Closure
>::const_iterator iter
=
140 on_stop_discovery_callbacks_
.begin();
141 iter
!= on_stop_discovery_callbacks_
.end();
143 ui_task_runner_
->PostTask(FROM_HERE
, *iter
);
145 num_discovery_listeners_
= 0;
146 on_stop_discovery_callbacks_
.clear();
148 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
149 AdapterDiscoveringChanged(this, false));
151 // If there are start discovery requests, post the start discovery again.
152 MaybePostStartDiscoveryTask();
155 void BluetoothAdapterWin::CreateRfcommService(
156 const BluetoothUUID
& uuid
,
157 const ServiceOptions
& options
,
158 const CreateServiceCallback
& callback
,
159 const CreateServiceErrorCallback
& error_callback
) {
160 scoped_refptr
<BluetoothSocketWin
> socket
=
161 BluetoothSocketWin::CreateBluetoothSocket(
162 ui_task_runner_
, socket_thread_
);
163 socket
->Listen(this, uuid
, options
,
164 base::Bind(callback
, socket
),
168 void BluetoothAdapterWin::CreateL2capService(
169 const BluetoothUUID
& uuid
,
170 const ServiceOptions
& options
,
171 const CreateServiceCallback
& callback
,
172 const CreateServiceErrorCallback
& error_callback
) {
173 // TODO(keybuk): implement.
177 void BluetoothAdapterWin::RegisterAudioSink(
178 const BluetoothAudioSink::Options
& options
,
179 const AcquiredCallback
& callback
,
180 const BluetoothAudioSink::ErrorCallback
& error_callback
) {
182 error_callback
.Run(BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM
);
185 void BluetoothAdapterWin::RemovePairingDelegateInternal(
186 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
189 void BluetoothAdapterWin::AdapterStateChanged(
190 const BluetoothTaskManagerWin::AdapterState
& state
) {
191 DCHECK(thread_checker_
.CalledOnValidThread());
193 bool was_present
= IsPresent();
194 bool is_present
= !state
.address
.empty();
195 address_
= BluetoothDevice::CanonicalizeAddress(state
.address
);
196 if (was_present
!= is_present
) {
197 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
198 AdapterPresentChanged(this, is_present
));
200 if (powered_
!= state
.powered
) {
201 powered_
= state
.powered
;
202 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
203 AdapterPoweredChanged(this, powered_
));
207 init_callback_
.Run();
211 void BluetoothAdapterWin::DevicesPolled(
212 const ScopedVector
<BluetoothTaskManagerWin::DeviceState
>& devices
) {
213 DCHECK(thread_checker_
.CalledOnValidThread());
215 // We are receiving a new list of all devices known to the system. Merge this
216 // new list with the list we know of (|devices_|) and raise corresponding
217 // DeviceAdded, DeviceRemoved and DeviceChanged events.
219 typedef std::set
<std::string
> DeviceAddressSet
;
220 DeviceAddressSet known_devices
;
221 for (DevicesMap::const_iterator iter
= devices_
.begin();
222 iter
!= devices_
.end();
224 known_devices
.insert((*iter
).first
);
227 DeviceAddressSet new_devices
;
228 for (ScopedVector
<BluetoothTaskManagerWin::DeviceState
>::const_iterator iter
=
230 iter
!= devices
.end();
232 new_devices
.insert((*iter
)->address
);
235 // Process device removal first
236 DeviceAddressSet removed_devices
=
237 base::STLSetDifference
<DeviceAddressSet
>(known_devices
, new_devices
);
238 for (DeviceAddressSet::const_iterator iter
= removed_devices
.begin();
239 iter
!= removed_devices
.end();
241 BluetoothDevice
* device_win
= devices_
[(*iter
)];
242 devices_
.erase(*iter
);
243 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
245 DeviceRemoved(this, device_win
));
249 // Process added and (maybe) changed devices in one pass
250 DeviceAddressSet added_devices
=
251 base::STLSetDifference
<DeviceAddressSet
>(new_devices
, known_devices
);
252 DeviceAddressSet changed_devices
=
253 base::STLSetIntersection
<DeviceAddressSet
>(known_devices
, new_devices
);
254 for (ScopedVector
<BluetoothTaskManagerWin::DeviceState
>::const_iterator iter
=
256 iter
!= devices
.end();
258 BluetoothTaskManagerWin::DeviceState
* device_state
= (*iter
);
259 if (added_devices
.find(device_state
->address
) != added_devices
.end()) {
260 BluetoothDeviceWin
* device_win
=
261 new BluetoothDeviceWin(*device_state
,
265 net::NetLog::Source());
266 devices_
[device_state
->address
] = device_win
;
267 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
269 DeviceAdded(this, device_win
));
270 } else if (changed_devices
.find(device_state
->address
) !=
271 changed_devices
.end()) {
272 BluetoothDeviceWin
* device_win
=
273 static_cast<BluetoothDeviceWin
*>(devices_
[device_state
->address
]);
274 if (!device_win
->IsEqual(*device_state
)) {
275 device_win
->Update(*device_state
);
276 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
278 DeviceChanged(this, device_win
));
284 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING,
285 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped().
286 void BluetoothAdapterWin::AddDiscoverySession(
287 BluetoothDiscoveryFilter
* discovery_filter
,
288 const base::Closure
& callback
,
289 const ErrorCallback
& error_callback
) {
290 if (discovery_status_
== DISCOVERING
) {
291 num_discovery_listeners_
++;
295 on_start_discovery_callbacks_
.push_back(
296 std::make_pair(callback
, error_callback
));
297 MaybePostStartDiscoveryTask();
300 void BluetoothAdapterWin::RemoveDiscoverySession(
301 BluetoothDiscoveryFilter
* discovery_filter
,
302 const base::Closure
& callback
,
303 const ErrorCallback
& error_callback
) {
304 if (discovery_status_
== NOT_DISCOVERING
) {
305 error_callback
.Run();
308 on_stop_discovery_callbacks_
.push_back(callback
);
309 MaybePostStopDiscoveryTask();
312 void BluetoothAdapterWin::SetDiscoveryFilter(
313 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
314 const base::Closure
& callback
,
315 const ErrorCallback
& error_callback
) {
317 error_callback
.Run();
320 void BluetoothAdapterWin::Init() {
321 ui_task_runner_
= base::ThreadTaskRunnerHandle::Get();
322 socket_thread_
= BluetoothSocketThread::Get();
324 new BluetoothTaskManagerWin(ui_task_runner_
);
325 task_manager_
->AddObserver(this);
326 task_manager_
->Initialize();
329 void BluetoothAdapterWin::InitForTest(
330 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
331 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
) {
332 ui_task_runner_
= ui_task_runner
;
334 new BluetoothTaskManagerWin(ui_task_runner_
);
335 task_manager_
->AddObserver(this);
336 task_manager_
->InitializeWithBluetoothTaskRunner(bluetooth_task_runner
);
339 void BluetoothAdapterWin::MaybePostStartDiscoveryTask() {
340 if (discovery_status_
== NOT_DISCOVERING
&&
341 !on_start_discovery_callbacks_
.empty()) {
342 discovery_status_
= DISCOVERY_STARTING
;
343 task_manager_
->PostStartDiscoveryTask();
347 void BluetoothAdapterWin::MaybePostStopDiscoveryTask() {
348 if (discovery_status_
!= DISCOVERING
)
351 if (on_stop_discovery_callbacks_
.size() < num_discovery_listeners_
) {
352 for (std::vector
<base::Closure
>::const_iterator iter
=
353 on_stop_discovery_callbacks_
.begin();
354 iter
!= on_stop_discovery_callbacks_
.end();
356 ui_task_runner_
->PostTask(FROM_HERE
, *iter
);
358 num_discovery_listeners_
-= on_stop_discovery_callbacks_
.size();
359 on_stop_discovery_callbacks_
.clear();
363 discovery_status_
= DISCOVERY_STOPPING
;
364 task_manager_
->PostStopDiscoveryTask();
367 } // namespace device