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::ServiceOptions::ServiceOptions() {
16 BluetoothAdapter::ServiceOptions::~ServiceOptions() {
19 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
21 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
22 const InitCallback
& init_callback
) {
23 return base::WeakPtr
<BluetoothAdapter
>();
25 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
27 #if defined(OS_CHROMEOS)
28 void BluetoothAdapter::Shutdown() {
33 BluetoothAdapter::BluetoothAdapter()
34 : weak_ptr_factory_(this) {
37 BluetoothAdapter::~BluetoothAdapter() {
38 STLDeleteValues(&devices_
);
41 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::GetWeakPtrForTesting() {
42 return weak_ptr_factory_
.GetWeakPtr();
45 void BluetoothAdapter::StartDiscoverySession(
46 const DiscoverySessionCallback
& callback
,
47 const ErrorCallback
& error_callback
) {
49 base::Bind(&BluetoothAdapter::OnStartDiscoverySession
,
50 weak_ptr_factory_
.GetWeakPtr(),
55 BluetoothAdapter::DeviceList
BluetoothAdapter::GetDevices() {
56 ConstDeviceList const_devices
=
57 const_cast<const BluetoothAdapter
*>(this)->GetDevices();
60 for (ConstDeviceList::const_iterator i
= const_devices
.begin();
61 i
!= const_devices
.end(); ++i
)
62 devices
.push_back(const_cast<BluetoothDevice
*>(*i
));
67 BluetoothAdapter::ConstDeviceList
BluetoothAdapter::GetDevices() const {
68 ConstDeviceList devices
;
69 for (DevicesMap::const_iterator iter
= devices_
.begin();
70 iter
!= devices_
.end();
72 devices
.push_back(iter
->second
);
77 BluetoothDevice
* BluetoothAdapter::GetDevice(const std::string
& address
) {
78 return const_cast<BluetoothDevice
*>(
79 const_cast<const BluetoothAdapter
*>(this)->GetDevice(address
));
82 const BluetoothDevice
* BluetoothAdapter::GetDevice(
83 const std::string
& address
) const {
84 std::string canonicalized_address
=
85 BluetoothDevice::CanonicalizeAddress(address
);
86 if (canonicalized_address
.empty())
89 DevicesMap::const_iterator iter
= devices_
.find(canonicalized_address
);
90 if (iter
!= devices_
.end())
96 void BluetoothAdapter::AddPairingDelegate(
97 BluetoothDevice::PairingDelegate
* pairing_delegate
,
98 PairingDelegatePriority priority
) {
99 // Remove the delegate, if it already exists, before inserting to allow a
100 // change of priority.
101 RemovePairingDelegate(pairing_delegate
);
103 // Find the first point with a lower priority, or the end of the list.
104 std::list
<PairingDelegatePair
>::iterator iter
= pairing_delegates_
.begin();
105 while (iter
!= pairing_delegates_
.end() && iter
->second
>= priority
)
108 pairing_delegates_
.insert(iter
, std::make_pair(pairing_delegate
, priority
));
111 void BluetoothAdapter::RemovePairingDelegate(
112 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
113 for (std::list
<PairingDelegatePair
>::iterator iter
=
114 pairing_delegates_
.begin(); iter
!= pairing_delegates_
.end(); ++iter
) {
115 if (iter
->first
== pairing_delegate
) {
116 RemovePairingDelegateInternal(pairing_delegate
);
117 pairing_delegates_
.erase(iter
);
123 BluetoothDevice::PairingDelegate
* BluetoothAdapter::DefaultPairingDelegate() {
124 if (pairing_delegates_
.empty())
127 return pairing_delegates_
.front().first
;
130 void BluetoothAdapter::OnStartDiscoverySession(
131 const DiscoverySessionCallback
& callback
) {
132 VLOG(1) << "Discovery session started!";
133 scoped_ptr
<BluetoothDiscoverySession
> discovery_session(
134 new BluetoothDiscoverySession(scoped_refptr
<BluetoothAdapter
>(this)));
135 discovery_sessions_
.insert(discovery_session
.get());
136 callback
.Run(discovery_session
.Pass());
139 void BluetoothAdapter::MarkDiscoverySessionsAsInactive() {
140 // As sessions are marked as inactive they will notify the adapter that they
141 // have become inactive, upon which the adapter will remove them from
142 // |discovery_sessions_|. To avoid invalidating the iterator, make a copy
144 std::set
<BluetoothDiscoverySession
*> temp(discovery_sessions_
);
145 for (std::set
<BluetoothDiscoverySession
*>::iterator
147 iter
!= temp
.end(); ++iter
) {
148 (*iter
)->MarkAsInactive();
152 void BluetoothAdapter::DiscoverySessionBecameInactive(
153 BluetoothDiscoverySession
* discovery_session
) {
154 DCHECK(!discovery_session
->IsActive());
155 discovery_sessions_
.erase(discovery_session
);
158 } // namespace device