Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 | 6 |
| 7 #include <bluetooth/bluetooth.h> | 7 #include <bluetooth/bluetooth.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 bool IsGattOffsetValid(int offset) { | 172 bool IsGattOffsetValid(int offset) { |
| 173 return 0 <= offset && offset < kMaxGattAttributeLength; | 173 return 0 <= offset && offset < kMaxGattAttributeLength; |
| 174 } | 174 } |
| 175 | 175 |
| 176 } // namespace | 176 } // namespace |
| 177 | 177 |
| 178 namespace arc { | 178 namespace arc { |
| 179 | 179 |
| 180 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 180 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
| 181 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 181 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
| 182 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 182 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
|
rkc
2016/08/08 21:58:29
This is a useless check. This only checks if Bluet
puthik_chromium
2016/08/08 22:33:32
It's out of scope of this CL so I will leave it he
| |
| 183 VLOG(1) << "registering bluetooth adapter"; | 183 VLOG(1) << "Registering bluetooth adapter."; |
| 184 BluetoothAdapterFactory::GetAdapter(base::Bind( | 184 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 185 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 185 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| 186 } else { | 186 } else { |
| 187 VLOG(1) << "no bluetooth adapter available"; | 187 VLOG(1) << "No bluetooth adapter available."; |
| 188 } | 188 } |
| 189 | |
| 190 arc_bridge_service()->bluetooth()->AddObserver(this); | 189 arc_bridge_service()->bluetooth()->AddObserver(this); |
| 191 } | 190 } |
| 192 | 191 |
| 193 ArcBluetoothBridge::~ArcBluetoothBridge() { | 192 ArcBluetoothBridge::~ArcBluetoothBridge() { |
| 194 DCHECK(CalledOnValidThread()); | 193 DCHECK(CalledOnValidThread()); |
| 195 | 194 |
| 196 arc_bridge_service()->bluetooth()->RemoveObserver(this); | 195 arc_bridge_service()->bluetooth()->RemoveObserver(this); |
| 197 | 196 |
| 198 if (bluetooth_adapter_) | 197 if (bluetooth_adapter_) |
| 199 bluetooth_adapter_->RemoveObserver(this); | 198 bluetooth_adapter_->RemoveObserver(this); |
| 200 } | 199 } |
| 201 | 200 |
| 202 void ArcBluetoothBridge::OnAdapterInitialized( | 201 void ArcBluetoothBridge::OnAdapterInitialized( |
| 203 scoped_refptr<BluetoothAdapter> adapter) { | 202 scoped_refptr<BluetoothAdapter> adapter) { |
| 204 DCHECK(CalledOnValidThread()); | 203 DCHECK(CalledOnValidThread()); |
| 205 | 204 |
| 206 // We can downcast here because we are always running on Chrome OS, and | 205 // We can downcast here because we are always running on Chrome OS, and |
| 207 // so our adapter uses BlueZ. | 206 // so our adapter uses BlueZ. |
| 208 bluetooth_adapter_ = | 207 bluetooth_adapter_ = |
| 209 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); | 208 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); |
| 210 bluetooth_adapter_->AddObserver(this); | 209 |
| 210 // Arc instance is ready before Bluetooth adapter. | |
| 211 // We should register ourselves to bluetooth observer here. | |
| 212 if (!bluetooth_observer_flag_ && | |
|
Luis Héctor Chávez
2016/08/08 21:54:16
s/bluetooth_observer_flag_/bluetooth_adapter_->Has
puthik_chromium
2016/08/08 22:33:32
There is no HasObserver(this) for bluetooth_adapte
rkc
2016/08/08 22:36:32
Yes, it is trivial to add it though.
puthik_chromium
2016/08/08 22:46:51
Done.
| |
| 213 arc_bridge_service()->bluetooth()->instance()) { | |
| 214 bluetooth_observer_flag_ = true; | |
| 215 bluetooth_adapter_->AddObserver(this); | |
| 216 } | |
| 211 } | 217 } |
| 212 | 218 |
| 213 void ArcBluetoothBridge::OnInstanceReady() { | 219 void ArcBluetoothBridge::OnInstanceReady() { |
| 214 mojom::BluetoothInstance* bluetooth_instance = | 220 mojom::BluetoothInstance* bluetooth_instance = |
| 215 arc_bridge_service()->bluetooth()->instance(); | 221 arc_bridge_service()->bluetooth()->instance(); |
| 216 if (!bluetooth_instance) { | 222 if (!bluetooth_instance) { |
| 217 LOG(ERROR) << "OnBluetoothInstanceReady called, " | 223 LOG(ERROR) << "OnBluetoothInstanceReady called, " |
| 218 << "but no bluetooth instance found"; | 224 << "but no bluetooth instance found"; |
| 219 return; | 225 return; |
| 220 } | 226 } |
| 227 | |
| 221 bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind()); | 228 bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 229 | |
| 230 // Bluetooth adapter is ready before Arc instance. | |
| 231 // We should register ourselves to bluetooth observer here. | |
|
rkc
2016/08/08 21:58:29
This comment isn't clear. Instead, it should read
puthik_chromium
2016/08/08 22:33:32
Done.
| |
| 232 if (!bluetooth_observer_flag_ && bluetooth_adapter_) { | |
| 233 bluetooth_observer_flag_ = true; | |
| 234 bluetooth_adapter_->AddObserver(this); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 void ArcBluetoothBridge::OnInstanceClosed() { | |
| 239 if (bluetooth_observer_flag_ && bluetooth_adapter_) | |
| 240 bluetooth_adapter_->RemoveObserver(this); | |
| 241 bluetooth_observer_flag_ = false; | |
| 222 } | 242 } |
| 223 | 243 |
| 224 void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter, | 244 void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter, |
| 225 bool powered) { | 245 bool powered) { |
| 226 if (!HasBluetoothInstance()) | 246 if (!HasBluetoothInstance()) |
| 227 return; | 247 return; |
| 228 | 248 |
| 229 // TODO(smbarber): Invoke EnableAdapter or DisableAdapter via ARC bridge | 249 // TODO(smbarber): Invoke EnableAdapter or DisableAdapter via ARC bridge |
| 230 // service. | 250 // service. |
| 231 } | 251 } |
| (...skipping 1408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1640 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1660 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1641 << ") need version " << version_need; | 1661 << ") need version " << version_need; |
| 1642 return false; | 1662 return false; |
| 1643 } | 1663 } |
| 1644 | 1664 |
| 1645 bool ArcBluetoothBridge::CalledOnValidThread() { | 1665 bool ArcBluetoothBridge::CalledOnValidThread() { |
| 1646 return thread_checker_.CalledOnValidThread(); | 1666 return thread_checker_.CalledOnValidThread(); |
| 1647 } | 1667 } |
| 1648 | 1668 |
| 1649 } // namespace arc | 1669 } // namespace arc |
| OLD | NEW |