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 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
16 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
17 const InitCallback
& init_callback
) {
18 return base::WeakPtr
<BluetoothAdapter
>();
20 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
22 const int BluetoothAdapter::kChannelAuto
= 0;
23 const int BluetoothAdapter::kPsmAuto
= 0;
25 BluetoothAdapter::BluetoothAdapter()
26 : weak_ptr_factory_(this) {
29 BluetoothAdapter::~BluetoothAdapter() {
30 STLDeleteValues(&devices_
);
33 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::GetWeakPtrForTesting() {
34 return weak_ptr_factory_
.GetWeakPtr();
37 void BluetoothAdapter::StartDiscoverySession(
38 const DiscoverySessionCallback
& callback
,
39 const ErrorCallback
& error_callback
) {
41 base::Bind(&BluetoothAdapter::OnStartDiscoverySession
,
42 weak_ptr_factory_
.GetWeakPtr(),
47 BluetoothAdapter::DeviceList
BluetoothAdapter::GetDevices() {
48 ConstDeviceList const_devices
=
49 const_cast<const BluetoothAdapter
*>(this)->GetDevices();
52 for (ConstDeviceList::const_iterator i
= const_devices
.begin();
53 i
!= const_devices
.end(); ++i
)
54 devices
.push_back(const_cast<BluetoothDevice
*>(*i
));
59 BluetoothAdapter::ConstDeviceList
BluetoothAdapter::GetDevices() const {
60 ConstDeviceList devices
;
61 for (DevicesMap::const_iterator iter
= devices_
.begin();
62 iter
!= devices_
.end();
64 devices
.push_back(iter
->second
);
69 BluetoothDevice
* BluetoothAdapter::GetDevice(const std::string
& address
) {
70 return const_cast<BluetoothDevice
*>(
71 const_cast<const BluetoothAdapter
*>(this)->GetDevice(address
));
74 const BluetoothDevice
* BluetoothAdapter::GetDevice(
75 const std::string
& address
) const {
76 std::string canonicalized_address
=
77 BluetoothDevice::CanonicalizeAddress(address
);
78 if (canonicalized_address
.empty())
81 DevicesMap::const_iterator iter
= devices_
.find(canonicalized_address
);
82 if (iter
!= devices_
.end())
88 void BluetoothAdapter::AddPairingDelegate(
89 BluetoothDevice::PairingDelegate
* pairing_delegate
,
90 PairingDelegatePriority priority
) {
91 // Remove the delegate, if it already exists, before inserting to allow a
92 // change of priority.
93 RemovePairingDelegate(pairing_delegate
);
95 // Find the first point with a lower priority, or the end of the list.
96 std::list
<PairingDelegatePair
>::iterator iter
= pairing_delegates_
.begin();
97 while (iter
!= pairing_delegates_
.end() && iter
->second
>= priority
)
100 pairing_delegates_
.insert(iter
, std::make_pair(pairing_delegate
, priority
));
103 void BluetoothAdapter::RemovePairingDelegate(
104 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
105 for (std::list
<PairingDelegatePair
>::iterator iter
=
106 pairing_delegates_
.begin(); iter
!= pairing_delegates_
.end(); ++iter
) {
107 if (iter
->first
== pairing_delegate
) {
108 RemovePairingDelegateInternal(pairing_delegate
);
109 pairing_delegates_
.erase(iter
);
115 BluetoothDevice::PairingDelegate
* BluetoothAdapter::DefaultPairingDelegate() {
116 if (pairing_delegates_
.empty())
119 return pairing_delegates_
.front().first
;
122 void BluetoothAdapter::OnStartDiscoverySession(
123 const DiscoverySessionCallback
& callback
) {
124 VLOG(1) << "Discovery session started!";
125 scoped_ptr
<BluetoothDiscoverySession
> discovery_session(
126 new BluetoothDiscoverySession(scoped_refptr
<BluetoothAdapter
>(this)));
127 discovery_sessions_
.insert(discovery_session
.get());
128 callback
.Run(discovery_session
.Pass());
131 void BluetoothAdapter::MarkDiscoverySessionsAsInactive() {
132 // As sessions are marked as inactive they will notify the adapter that they
133 // have become inactive, upon which the adapter will remove them from
134 // |discovery_sessions_|. To avoid invalidating the iterator, make a copy
136 std::set
<BluetoothDiscoverySession
*> temp(discovery_sessions_
);
137 for (std::set
<BluetoothDiscoverySession
*>::iterator
139 iter
!= temp
.end(); ++iter
) {
140 (*iter
)->MarkAsInactive();
144 void BluetoothAdapter::DiscoverySessionBecameInactive(
145 BluetoothDiscoverySession
* discovery_session
) {
146 DCHECK(!discovery_session
->IsActive());
147 discovery_sessions_
.erase(discovery_session
);
150 } // namespace device