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.h"
8 #include "base/stl_util.h"
9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_discovery_session.h"
14 BluetoothAdapter::BluetoothAdapter()
15 : weak_ptr_factory_(this) {
18 BluetoothAdapter::~BluetoothAdapter() {
19 STLDeleteValues(&devices_
);
22 void BluetoothAdapter::StartDiscoverySession(
23 const DiscoverySessionCallback
& callback
,
24 const ErrorCallback
& error_callback
) {
26 base::Bind(&BluetoothAdapter::OnStartDiscoverySession
,
27 weak_ptr_factory_
.GetWeakPtr(),
32 BluetoothAdapter::DeviceList
BluetoothAdapter::GetDevices() {
33 ConstDeviceList const_devices
=
34 const_cast<const BluetoothAdapter
*>(this)->GetDevices();
37 for (ConstDeviceList::const_iterator i
= const_devices
.begin();
38 i
!= const_devices
.end(); ++i
)
39 devices
.push_back(const_cast<BluetoothDevice
*>(*i
));
44 BluetoothAdapter::ConstDeviceList
BluetoothAdapter::GetDevices() const {
45 ConstDeviceList devices
;
46 for (DevicesMap::const_iterator iter
= devices_
.begin();
47 iter
!= devices_
.end();
49 devices
.push_back(iter
->second
);
54 BluetoothDevice
* BluetoothAdapter::GetDevice(const std::string
& address
) {
55 return const_cast<BluetoothDevice
*>(
56 const_cast<const BluetoothAdapter
*>(this)->GetDevice(address
));
59 const BluetoothDevice
* BluetoothAdapter::GetDevice(
60 const std::string
& address
) const {
61 DevicesMap::const_iterator iter
= devices_
.find(address
);
62 if (iter
!= devices_
.end())
68 void BluetoothAdapter::AddPairingDelegate(
69 BluetoothDevice::PairingDelegate
* pairing_delegate
,
70 PairingDelegatePriority priority
) {
71 // Remove the delegate, if it already exists, before inserting to allow a
72 // change of priority.
73 RemovePairingDelegate(pairing_delegate
);
75 // Find the first point with a lower priority, or the end of the list.
76 std::list
<PairingDelegatePair
>::iterator iter
= pairing_delegates_
.begin();
77 while (iter
!= pairing_delegates_
.end() && iter
->second
>= priority
)
80 pairing_delegates_
.insert(iter
, std::make_pair(pairing_delegate
, priority
));
83 void BluetoothAdapter::RemovePairingDelegate(
84 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
85 for (std::list
<PairingDelegatePair
>::iterator iter
=
86 pairing_delegates_
.begin(); iter
!= pairing_delegates_
.end(); ++iter
) {
87 if (iter
->first
== pairing_delegate
) {
88 RemovePairingDelegateInternal(pairing_delegate
);
89 pairing_delegates_
.erase(iter
);
95 BluetoothDevice::PairingDelegate
* BluetoothAdapter::DefaultPairingDelegate() {
96 if (pairing_delegates_
.empty())
99 return pairing_delegates_
.front().first
;
102 void BluetoothAdapter::OnStartDiscoverySession(
103 const DiscoverySessionCallback
& callback
) {
104 VLOG(1) << "Discovery session started!";
105 scoped_ptr
<BluetoothDiscoverySession
> discovery_session(
106 new BluetoothDiscoverySession(scoped_refptr
<BluetoothAdapter
>(this)));
107 discovery_sessions_
.insert(discovery_session
.get());
108 callback
.Run(discovery_session
.Pass());
111 void BluetoothAdapter::MarkDiscoverySessionsAsInactive() {
112 // As sessions are marked as inactive they will notify the adapter that they
113 // have become inactive, upon which the adapter will remove them from
114 // |discovery_sessions_|. To avoid invalidating the iterator, make a copy
116 std::set
<BluetoothDiscoverySession
*> temp(discovery_sessions_
);
117 for (std::set
<BluetoothDiscoverySession
*>::iterator
119 iter
!= temp
.end(); ++iter
) {
120 (*iter
)->MarkAsInactive();
124 void BluetoothAdapter::DiscoverySessionBecameInactive(
125 BluetoothDiscoverySession
* discovery_session
) {
126 DCHECK(!discovery_session
->IsActive());
127 discovery_sessions_
.erase(discovery_session
);
130 } // namespace device