| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_adapter.h" | 5 #include "device/bluetooth/bluetooth_adapter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "device/bluetooth/bluetooth_device.h" | 9 #include "device/bluetooth/bluetooth_device.h" |
| 10 #include "device/bluetooth/bluetooth_discovery_session.h" | 10 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 11 | 11 |
| 12 namespace device { | 12 namespace device { |
| 13 | 13 |
| 14 BluetoothAdapter::ServiceOptions::ServiceOptions() { | 14 BluetoothAdapter::ServiceOptions::ServiceOptions() { |
| 15 } | 15 } |
| 16 BluetoothAdapter::ServiceOptions::~ServiceOptions() { | 16 BluetoothAdapter::ServiceOptions::~ServiceOptions() { |
| 17 } | 17 } |
| 18 | 18 |
| 19 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX) | 19 #if !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX) |
| 20 //static | 20 //static |
| 21 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | 21 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( |
| 22 const InitCallback& init_callback) { | 22 const InitCallback& init_callback) { |
| 23 return base::WeakPtr<BluetoothAdapter>(); | 23 return base::WeakPtr<BluetoothAdapter>(); |
| 24 } | 24 } |
| 25 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX) | 25 #endif // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX) |
| 26 | 26 |
| 27 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::GetWeakPtrForTesting() { |
| 28 return weak_ptr_factory_.GetWeakPtr(); |
| 29 } |
| 30 |
| 27 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
| 28 void BluetoothAdapter::Shutdown() { | 32 void BluetoothAdapter::Shutdown() { |
| 29 NOTIMPLEMENTED(); | 33 NOTIMPLEMENTED(); |
| 30 } | 34 } |
| 31 #endif | 35 #endif |
| 32 | 36 |
| 33 BluetoothAdapter::BluetoothAdapter() | 37 void BluetoothAdapter::StartDiscoverySession( |
| 34 : weak_ptr_factory_(this) { | 38 const DiscoverySessionCallback& callback, |
| 35 } | 39 const ErrorCallback& error_callback) { |
| 36 | 40 StartDiscoverySessionWithFilter(nullptr, callback, error_callback); |
| 37 BluetoothAdapter::~BluetoothAdapter() { | |
| 38 STLDeleteValues(&devices_); | |
| 39 } | |
| 40 | |
| 41 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::GetWeakPtrForTesting() { | |
| 42 return weak_ptr_factory_.GetWeakPtr(); | |
| 43 } | 41 } |
| 44 | 42 |
| 45 void BluetoothAdapter::StartDiscoverySessionWithFilter( | 43 void BluetoothAdapter::StartDiscoverySessionWithFilter( |
| 46 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, | 44 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, |
| 47 const DiscoverySessionCallback& callback, | 45 const DiscoverySessionCallback& callback, |
| 48 const ErrorCallback& error_callback) { | 46 const ErrorCallback& error_callback) { |
| 49 AddDiscoverySession(discovery_filter.get(), | 47 AddDiscoverySession(discovery_filter.get(), |
| 50 base::Bind(&BluetoothAdapter::OnStartDiscoverySession, | 48 base::Bind(&BluetoothAdapter::OnStartDiscoverySession, |
| 51 weak_ptr_factory_.GetWeakPtr(), | 49 weak_ptr_factory_.GetWeakPtr(), |
| 52 base::Passed(&discovery_filter), callback), | 50 base::Passed(&discovery_filter), callback), |
| 53 error_callback); | 51 error_callback); |
| 54 } | 52 } |
| 55 | 53 |
| 56 void BluetoothAdapter::StartDiscoverySession( | |
| 57 const DiscoverySessionCallback& callback, | |
| 58 const ErrorCallback& error_callback) { | |
| 59 StartDiscoverySessionWithFilter(nullptr, callback, error_callback); | |
| 60 } | |
| 61 | |
| 62 scoped_ptr<BluetoothDiscoveryFilter> | |
| 63 BluetoothAdapter::GetMergedDiscoveryFilterHelper( | |
| 64 const BluetoothDiscoveryFilter* masked_filter, | |
| 65 bool omit) const { | |
| 66 scoped_ptr<BluetoothDiscoveryFilter> result; | |
| 67 bool first_merge = true; | |
| 68 | |
| 69 std::set<BluetoothDiscoverySession*> temp(discovery_sessions_); | |
| 70 for (const auto& iter : temp) { | |
| 71 const BluetoothDiscoveryFilter* curr_filter = iter->GetDiscoveryFilter(); | |
| 72 | |
| 73 if (!iter->IsActive()) | |
| 74 continue; | |
| 75 | |
| 76 if (omit && curr_filter == masked_filter) { | |
| 77 // if masked_filter is pointing to empty filter, and there are | |
| 78 // multiple empty filters in discovery_sessions_, make sure we'll | |
| 79 // process next empty sessions. | |
| 80 omit = false; | |
| 81 continue; | |
| 82 } | |
| 83 | |
| 84 if (first_merge) { | |
| 85 first_merge = false; | |
| 86 if (curr_filter) { | |
| 87 result.reset(new BluetoothDiscoveryFilter( | |
| 88 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); | |
| 89 result->CopyFrom(*curr_filter); | |
| 90 } | |
| 91 continue; | |
| 92 } | |
| 93 | |
| 94 result = BluetoothDiscoveryFilter::Merge(result.get(), curr_filter); | |
| 95 } | |
| 96 | |
| 97 return result.Pass(); | |
| 98 } | |
| 99 | |
| 100 scoped_ptr<BluetoothDiscoveryFilter> | 54 scoped_ptr<BluetoothDiscoveryFilter> |
| 101 BluetoothAdapter::GetMergedDiscoveryFilter() const { | 55 BluetoothAdapter::GetMergedDiscoveryFilter() const { |
| 102 return GetMergedDiscoveryFilterHelper(nullptr, false); | 56 return GetMergedDiscoveryFilterHelper(nullptr, false); |
| 103 } | 57 } |
| 104 | 58 |
| 105 scoped_ptr<BluetoothDiscoveryFilter> | 59 scoped_ptr<BluetoothDiscoveryFilter> |
| 106 BluetoothAdapter::GetMergedDiscoveryFilterMasked( | 60 BluetoothAdapter::GetMergedDiscoveryFilterMasked( |
| 107 BluetoothDiscoveryFilter* masked_filter) const { | 61 BluetoothDiscoveryFilter* masked_filter) const { |
| 108 return GetMergedDiscoveryFilterHelper(masked_filter, true); | 62 return GetMergedDiscoveryFilterHelper(masked_filter, true); |
| 109 } | 63 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 130 } |
| 177 } | 131 } |
| 178 | 132 |
| 179 BluetoothDevice::PairingDelegate* BluetoothAdapter::DefaultPairingDelegate() { | 133 BluetoothDevice::PairingDelegate* BluetoothAdapter::DefaultPairingDelegate() { |
| 180 if (pairing_delegates_.empty()) | 134 if (pairing_delegates_.empty()) |
| 181 return NULL; | 135 return NULL; |
| 182 | 136 |
| 183 return pairing_delegates_.front().first; | 137 return pairing_delegates_.front().first; |
| 184 } | 138 } |
| 185 | 139 |
| 140 BluetoothAdapter::BluetoothAdapter() : weak_ptr_factory_(this) { |
| 141 } |
| 142 |
| 143 BluetoothAdapter::~BluetoothAdapter() { |
| 144 STLDeleteValues(&devices_); |
| 145 } |
| 146 |
| 186 void BluetoothAdapter::OnStartDiscoverySession( | 147 void BluetoothAdapter::OnStartDiscoverySession( |
| 187 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, | 148 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, |
| 188 const DiscoverySessionCallback& callback) { | 149 const DiscoverySessionCallback& callback) { |
| 189 VLOG(1) << "Discovery session started!"; | 150 VLOG(1) << "Discovery session started!"; |
| 190 scoped_ptr<BluetoothDiscoverySession> discovery_session( | 151 scoped_ptr<BluetoothDiscoverySession> discovery_session( |
| 191 new BluetoothDiscoverySession(scoped_refptr<BluetoothAdapter>(this), | 152 new BluetoothDiscoverySession(scoped_refptr<BluetoothAdapter>(this), |
| 192 discovery_filter.Pass())); | 153 discovery_filter.Pass())); |
| 193 discovery_sessions_.insert(discovery_session.get()); | 154 discovery_sessions_.insert(discovery_session.get()); |
| 194 callback.Run(discovery_session.Pass()); | 155 callback.Run(discovery_session.Pass()); |
| 195 } | 156 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 206 (*iter)->MarkAsInactive(); | 167 (*iter)->MarkAsInactive(); |
| 207 } | 168 } |
| 208 } | 169 } |
| 209 | 170 |
| 210 void BluetoothAdapter::DiscoverySessionBecameInactive( | 171 void BluetoothAdapter::DiscoverySessionBecameInactive( |
| 211 BluetoothDiscoverySession* discovery_session) { | 172 BluetoothDiscoverySession* discovery_session) { |
| 212 DCHECK(!discovery_session->IsActive()); | 173 DCHECK(!discovery_session->IsActive()); |
| 213 discovery_sessions_.erase(discovery_session); | 174 discovery_sessions_.erase(discovery_session); |
| 214 } | 175 } |
| 215 | 176 |
| 177 scoped_ptr<BluetoothDiscoveryFilter> |
| 178 BluetoothAdapter::GetMergedDiscoveryFilterHelper( |
| 179 const BluetoothDiscoveryFilter* masked_filter, |
| 180 bool omit) const { |
| 181 scoped_ptr<BluetoothDiscoveryFilter> result; |
| 182 bool first_merge = true; |
| 183 |
| 184 std::set<BluetoothDiscoverySession*> temp(discovery_sessions_); |
| 185 for (const auto& iter : temp) { |
| 186 const BluetoothDiscoveryFilter* curr_filter = iter->GetDiscoveryFilter(); |
| 187 |
| 188 if (!iter->IsActive()) |
| 189 continue; |
| 190 |
| 191 if (omit && curr_filter == masked_filter) { |
| 192 // if masked_filter is pointing to empty filter, and there are |
| 193 // multiple empty filters in discovery_sessions_, make sure we'll |
| 194 // process next empty sessions. |
| 195 omit = false; |
| 196 continue; |
| 197 } |
| 198 |
| 199 if (first_merge) { |
| 200 first_merge = false; |
| 201 if (curr_filter) { |
| 202 result.reset(new BluetoothDiscoveryFilter( |
| 203 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); |
| 204 result->CopyFrom(*curr_filter); |
| 205 } |
| 206 continue; |
| 207 } |
| 208 |
| 209 result = BluetoothDiscoveryFilter::Merge(result.get(), curr_filter); |
| 210 } |
| 211 |
| 212 return result.Pass(); |
| 213 } |
| 214 |
| 216 } // namespace device | 215 } // namespace device |
| OLD | NEW |