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