1 // Copyright 2014 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. 4 5 #include "device/bluetooth/bluetooth_discovery_session.h" 6 7 #include "base/bind.h" 8 #include "device/bluetooth/bluetooth_adapter.h" 9 10 namespace device { 11 12 BluetoothDiscoverySession::BluetoothDiscoverySession( 13 scoped_refptr<BluetoothAdapter> adapter) 14 : active_(true), adapter_(adapter), weak_ptr_factory_(this) { 15 DCHECK(adapter_.get()); 16 } 17 18 BluetoothDiscoverySession::BluetoothDiscoverySession() 19 : active_(false), weak_ptr_factory_(this) {} 20 21 BluetoothDiscoverySession::~BluetoothDiscoverySession() { 22 // |adapter_| may be NULL if this instance was initialized as a mock. 23 if (!adapter_.get()) { 24 DCHECK(!active_); 25 return; 26 } 27 Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); 28 MarkAsInactive(); 29 } 30 31 bool BluetoothDiscoverySession::IsActive() const { 32 return active_; 33 } 34 35 void BluetoothDiscoverySession::Stop( 36 const base::Closure& callback, 37 const ErrorCallback& error_callback) { 38 if (!active_) { 39 LOG(WARNING) << "Discovery session not active. Cannot stop."; 40 error_callback.Run(); 41 return; 42 } 43 VLOG(1) << "Stopping device discovery session."; 44 DCHECK(adapter_.get()); 45 adapter_->RemoveDiscoverySession( 46 base::Bind(&BluetoothDiscoverySession::OnStop, 47 weak_ptr_factory_.GetWeakPtr(), 48 callback), 49 error_callback); 50 } 51 52 void BluetoothDiscoverySession::OnStop(const base::Closure& callback) { 53 MarkAsInactive(); 54 callback.Run(); 55 } 56 57 void BluetoothDiscoverySession::MarkAsInactive() { 58 if (!active_) 59 return; 60 active_ = false; 61 DCHECK(adapter_.get()); 62 adapter_->DiscoverySessionBecameInactive(this); 63 } 64 65 } // namespace device 66