Turning in DCHECK to test for illegal visibility / opacity flag combination
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter.cc
blobd9a24f05ad8c696a5d3ba29aa5564ad0155e77b6
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"
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_discovery_session.h"
12 namespace device {
14 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
15 //static
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) {
40 AddDiscoverySession(
41 base::Bind(&BluetoothAdapter::OnStartDiscoverySession,
42 weak_ptr_factory_.GetWeakPtr(),
43 callback),
44 error_callback);
47 BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() {
48 ConstDeviceList const_devices =
49 const_cast<const BluetoothAdapter *>(this)->GetDevices();
51 DeviceList devices;
52 for (ConstDeviceList::const_iterator i = const_devices.begin();
53 i != const_devices.end(); ++i)
54 devices.push_back(const_cast<BluetoothDevice *>(*i));
56 return devices;
59 BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const {
60 ConstDeviceList devices;
61 for (DevicesMap::const_iterator iter = devices_.begin();
62 iter != devices_.end();
63 ++iter)
64 devices.push_back(iter->second);
66 return devices;
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 DevicesMap::const_iterator iter = devices_.find(address);
77 if (iter != devices_.end())
78 return iter->second;
80 return NULL;
83 void BluetoothAdapter::AddPairingDelegate(
84 BluetoothDevice::PairingDelegate* pairing_delegate,
85 PairingDelegatePriority priority) {
86 // Remove the delegate, if it already exists, before inserting to allow a
87 // change of priority.
88 RemovePairingDelegate(pairing_delegate);
90 // Find the first point with a lower priority, or the end of the list.
91 std::list<PairingDelegatePair>::iterator iter = pairing_delegates_.begin();
92 while (iter != pairing_delegates_.end() && iter->second >= priority)
93 ++iter;
95 pairing_delegates_.insert(iter, std::make_pair(pairing_delegate, priority));
98 void BluetoothAdapter::RemovePairingDelegate(
99 BluetoothDevice::PairingDelegate* pairing_delegate) {
100 for (std::list<PairingDelegatePair>::iterator iter =
101 pairing_delegates_.begin(); iter != pairing_delegates_.end(); ++iter) {
102 if (iter->first == pairing_delegate) {
103 RemovePairingDelegateInternal(pairing_delegate);
104 pairing_delegates_.erase(iter);
105 return;
110 BluetoothDevice::PairingDelegate* BluetoothAdapter::DefaultPairingDelegate() {
111 if (pairing_delegates_.empty())
112 return NULL;
114 return pairing_delegates_.front().first;
117 void BluetoothAdapter::OnStartDiscoverySession(
118 const DiscoverySessionCallback& callback) {
119 VLOG(1) << "Discovery session started!";
120 scoped_ptr<BluetoothDiscoverySession> discovery_session(
121 new BluetoothDiscoverySession(scoped_refptr<BluetoothAdapter>(this)));
122 discovery_sessions_.insert(discovery_session.get());
123 callback.Run(discovery_session.Pass());
126 void BluetoothAdapter::MarkDiscoverySessionsAsInactive() {
127 // As sessions are marked as inactive they will notify the adapter that they
128 // have become inactive, upon which the adapter will remove them from
129 // |discovery_sessions_|. To avoid invalidating the iterator, make a copy
130 // here.
131 std::set<BluetoothDiscoverySession*> temp(discovery_sessions_);
132 for (std::set<BluetoothDiscoverySession*>::iterator
133 iter = temp.begin();
134 iter != temp.end(); ++iter) {
135 (*iter)->MarkAsInactive();
139 void BluetoothAdapter::DiscoverySessionBecameInactive(
140 BluetoothDiscoverySession* discovery_session) {
141 DCHECK(!discovery_session->IsActive());
142 discovery_sessions_.erase(discovery_session);
145 } // namespace device