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