Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2223203002: arc: bluetooth: Add/Remove BT observer only when ARC is ready (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix nit in last patch Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | device/bluetooth/bluetooth_adapter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
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()) {
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 // The ARC instance was ready before the Bluetooth adapter, hence we didn't
211 // register ourselves as an observer with it then. Since our adapter is
212 // ready, we should register it now.
213 if (!bluetooth_adapter_->HasObserver(this) &&
214 arc_bridge_service()->bluetooth()->instance()) {
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 // The Bluetooth adapter was ready before the ARC instance, hence we didn't
231 // register ourselves as an observer with it then. Since our instance is
232 // ready, we should register it now.
233 if (bluetooth_adapter_ && !bluetooth_adapter_->HasObserver(this))
234 bluetooth_adapter_->AddObserver(this);
235 }
236
237 void ArcBluetoothBridge::OnInstanceClosed() {
238 if (bluetooth_adapter_)
239 bluetooth_adapter_->RemoveObserver(this);
222 } 240 }
223 241
224 void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter, 242 void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter,
225 bool powered) { 243 bool powered) {
226 if (!HasBluetoothInstance()) 244 if (!HasBluetoothInstance())
227 return; 245 return;
228 246
229 // TODO(smbarber): Invoke EnableAdapter or DisableAdapter via ARC bridge 247 // TODO(smbarber): Invoke EnableAdapter or DisableAdapter via ARC bridge
230 // service. 248 // service.
231 } 249 }
(...skipping 1408 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1658 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1641 << ") need version " << version_need; 1659 << ") need version " << version_need;
1642 return false; 1660 return false;
1643 } 1661 }
1644 1662
1645 bool ArcBluetoothBridge::CalledOnValidThread() { 1663 bool ArcBluetoothBridge::CalledOnValidThread() {
1646 return thread_checker_.CalledOnValidThread(); 1664 return thread_checker_.CalledOnValidThread();
1647 } 1665 }
1648 1666
1649 } // namespace arc 1667 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | device/bluetooth/bluetooth_adapter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698