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 10namespace device { 11 12BluetoothDiscoverySession::BluetoothDiscoverySession( 13 scoped_refptr<BluetoothAdapter> adapter) 14 : active_(true), adapter_(adapter), weak_ptr_factory_(this) { 15 DCHECK(adapter_.get()); 16} 17 18BluetoothDiscoverySession::~BluetoothDiscoverySession() { 19 if (active_) { 20 Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); 21 MarkAsInactive(); 22 } 23} 24 25bool BluetoothDiscoverySession::IsActive() const { 26 return active_; 27} 28 29void BluetoothDiscoverySession::Stop( 30 const base::Closure& callback, 31 const ErrorCallback& error_callback) { 32 if (!active_) { 33 LOG(WARNING) << "Discovery session not active. Cannot stop."; 34 error_callback.Run(); 35 return; 36 } 37 VLOG(1) << "Stopping device discovery session."; 38 adapter_->RemoveDiscoverySession( 39 base::Bind(&BluetoothDiscoverySession::OnStop, 40 weak_ptr_factory_.GetWeakPtr(), 41 callback), 42 error_callback); 43} 44 45void BluetoothDiscoverySession::OnStop(const base::Closure& callback) { 46 MarkAsInactive(); 47 callback.Run(); 48} 49 50void BluetoothDiscoverySession::MarkAsInactive() { 51 if (!active_) 52 return; 53 active_ = false; 54 adapter_->DiscoverySessionBecameInactive(this); 55} 56 57} // namespace device 58