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/logging.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "device/bluetooth/bluetooth_device_win.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "device/bluetooth/bluetooth_socket_win.h"
19 #include "device/bluetooth/bluetooth_task_manager_win.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
25 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
26 const InitCallback
& init_callback
) {
27 return BluetoothAdapterWin::CreateAdapter(init_callback
);
31 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapterWin::CreateAdapter(
32 const InitCallback
& init_callback
) {
33 BluetoothAdapterWin
* adapter
= new BluetoothAdapterWin(init_callback
);
35 return adapter
->weak_ptr_factory_
.GetWeakPtr();
38 BluetoothAdapterWin::BluetoothAdapterWin(const InitCallback
& init_callback
)
40 init_callback_(init_callback
),
43 discovery_status_(NOT_DISCOVERING
),
44 num_discovery_listeners_(0),
45 weak_ptr_factory_(this) {
48 BluetoothAdapterWin::~BluetoothAdapterWin() {
50 task_manager_
->RemoveObserver(this);
51 task_manager_
->Shutdown();
55 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer
* observer
) {
57 observers_
.AddObserver(observer
);
60 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer
* observer
) {
62 observers_
.RemoveObserver(observer
);
65 std::string
BluetoothAdapterWin::GetAddress() const {
69 std::string
BluetoothAdapterWin::GetName() const {
73 void BluetoothAdapterWin::SetName(const std::string
& name
,
74 const base::Closure
& callback
,
75 const ErrorCallback
& error_callback
) {
79 // TODO(youngki): Return true when |task_manager_| initializes the adapter
81 bool BluetoothAdapterWin::IsInitialized() const {
85 bool BluetoothAdapterWin::IsPresent() const {
86 return !address_
.empty();
89 bool BluetoothAdapterWin::IsPowered() const {
93 void BluetoothAdapterWin::SetPowered(
95 const base::Closure
& callback
,
96 const ErrorCallback
& error_callback
) {
97 task_manager_
->PostSetPoweredBluetoothTask(powered
, callback
, error_callback
);
100 bool BluetoothAdapterWin::IsDiscoverable() const {
105 void BluetoothAdapterWin::SetDiscoverable(
107 const base::Closure
& callback
,
108 const ErrorCallback
& error_callback
) {
112 bool BluetoothAdapterWin::IsDiscovering() const {
113 return discovery_status_
== DISCOVERING
||
114 discovery_status_
== DISCOVERY_STOPPING
;
117 void BluetoothAdapterWin::DiscoveryStarted(bool success
) {
118 discovery_status_
= success
? DISCOVERING
: NOT_DISCOVERING
;
119 for (std::vector
<std::pair
<base::Closure
, ErrorCallback
> >::const_iterator
120 iter
= on_start_discovery_callbacks_
.begin();
121 iter
!= on_start_discovery_callbacks_
.end();
124 ui_task_runner_
->PostTask(FROM_HERE
, iter
->first
);
126 ui_task_runner_
->PostTask(FROM_HERE
, iter
->second
);
128 num_discovery_listeners_
= on_start_discovery_callbacks_
.size();
129 on_start_discovery_callbacks_
.clear();
132 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
133 AdapterDiscoveringChanged(this, true));
135 // If there are stop discovery requests, post the stop discovery again.
136 MaybePostStopDiscoveryTask();
137 } else if (!on_stop_discovery_callbacks_
.empty()) {
138 // If there are stop discovery requests but start discovery has failed,
139 // notify that stop discovery has been complete.
144 void BluetoothAdapterWin::DiscoveryStopped() {
145 discovered_devices_
.clear();
146 bool was_discovering
= IsDiscovering();
147 discovery_status_
= NOT_DISCOVERING
;
148 for (std::vector
<base::Closure
>::const_iterator iter
=
149 on_stop_discovery_callbacks_
.begin();
150 iter
!= on_stop_discovery_callbacks_
.end();
152 ui_task_runner_
->PostTask(FROM_HERE
, *iter
);
154 num_discovery_listeners_
= 0;
155 on_stop_discovery_callbacks_
.clear();
157 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
158 AdapterDiscoveringChanged(this, false));
160 // If there are start discovery requests, post the start discovery again.
161 MaybePostStartDiscoveryTask();
164 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData(
165 const BluetoothOutOfBandPairingDataCallback
& callback
,
166 const ErrorCallback
& error_callback
) {
170 void BluetoothAdapterWin::CreateRfcommService(
171 const BluetoothUUID
& uuid
,
174 const CreateServiceCallback
& callback
,
175 const CreateServiceErrorCallback
& error_callback
) {
176 // TODO(keybuk): implement.
180 void BluetoothAdapterWin::CreateL2capService(
181 const BluetoothUUID
& uuid
,
183 const CreateServiceCallback
& callback
,
184 const CreateServiceErrorCallback
& error_callback
) {
185 // TODO(keybuk): implement.
189 void BluetoothAdapterWin::RemovePairingDelegateInternal(
190 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
193 void BluetoothAdapterWin::AdapterStateChanged(
194 const BluetoothTaskManagerWin::AdapterState
& state
) {
195 DCHECK(thread_checker_
.CalledOnValidThread());
197 bool was_present
= IsPresent();
198 bool is_present
= !state
.address
.empty();
199 address_
= BluetoothDevice::CanonicalizeAddress(state
.address
);
200 if (was_present
!= is_present
) {
201 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
202 AdapterPresentChanged(this, is_present
));
204 if (powered_
!= state
.powered
) {
205 powered_
= state
.powered
;
206 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
207 AdapterPoweredChanged(this, powered_
));
211 init_callback_
.Run();
215 void BluetoothAdapterWin::DevicesDiscovered(
216 const ScopedVector
<BluetoothTaskManagerWin::DeviceState
>& devices
) {
217 DCHECK(thread_checker_
.CalledOnValidThread());
218 for (ScopedVector
<BluetoothTaskManagerWin::DeviceState
>::const_iterator iter
=
220 iter
!= devices
.end();
222 if (discovered_devices_
.find((*iter
)->address
) ==
223 discovered_devices_
.end()) {
224 BluetoothDeviceWin
device_win(
225 **iter
, ui_task_runner_
, socket_thread_
, NULL
, net::NetLog::Source());
226 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
227 DeviceAdded(this, &device_win
));
228 discovered_devices_
.insert((*iter
)->address
);
233 void BluetoothAdapterWin::DevicesUpdated(
234 const ScopedVector
<BluetoothTaskManagerWin::DeviceState
>& devices
) {
235 STLDeleteValues(&devices_
);
236 for (ScopedVector
<BluetoothTaskManagerWin::DeviceState
>::const_iterator iter
=
238 iter
!= devices
.end();
240 devices_
[(*iter
)->address
] = new BluetoothDeviceWin(
241 **iter
, ui_task_runner_
, socket_thread_
, NULL
, net::NetLog::Source());
245 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING,
246 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped().
247 void BluetoothAdapterWin::AddDiscoverySession(
248 const base::Closure
& callback
,
249 const ErrorCallback
& error_callback
) {
250 if (discovery_status_
== DISCOVERING
) {
251 num_discovery_listeners_
++;
255 on_start_discovery_callbacks_
.push_back(
256 std::make_pair(callback
, error_callback
));
257 MaybePostStartDiscoveryTask();
260 void BluetoothAdapterWin::RemoveDiscoverySession(
261 const base::Closure
& callback
,
262 const ErrorCallback
& error_callback
) {
263 if (discovery_status_
== NOT_DISCOVERING
) {
264 error_callback
.Run();
267 on_stop_discovery_callbacks_
.push_back(callback
);
268 MaybePostStopDiscoveryTask();
271 void BluetoothAdapterWin::Init() {
272 ui_task_runner_
= base::ThreadTaskRunnerHandle::Get();
273 socket_thread_
= BluetoothSocketThread::Get();
275 new BluetoothTaskManagerWin(ui_task_runner_
);
276 task_manager_
->AddObserver(this);
277 task_manager_
->Initialize();
280 void BluetoothAdapterWin::InitForTest(
281 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
282 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
) {
283 ui_task_runner_
= ui_task_runner
;
285 new BluetoothTaskManagerWin(ui_task_runner_
);
286 task_manager_
->AddObserver(this);
287 task_manager_
->InitializeWithBluetoothTaskRunner(bluetooth_task_runner
);
290 void BluetoothAdapterWin::MaybePostStartDiscoveryTask() {
291 if (discovery_status_
== NOT_DISCOVERING
&&
292 !on_start_discovery_callbacks_
.empty()) {
293 discovery_status_
= DISCOVERY_STARTING
;
294 task_manager_
->PostStartDiscoveryTask();
298 void BluetoothAdapterWin::MaybePostStopDiscoveryTask() {
299 if (discovery_status_
!= DISCOVERING
)
302 if (on_stop_discovery_callbacks_
.size() < num_discovery_listeners_
) {
303 for (std::vector
<base::Closure
>::const_iterator iter
=
304 on_stop_discovery_callbacks_
.begin();
305 iter
!= on_stop_discovery_callbacks_
.end();
307 ui_task_runner_
->PostTask(FROM_HERE
, *iter
);
309 num_discovery_listeners_
-= on_stop_discovery_callbacks_
.size();
310 on_stop_discovery_callbacks_
.clear();
314 discovery_status_
= DISCOVERY_STOPPING
;
315 task_manager_
->PostStopDiscoveryTask();
318 } // namespace device