Revert 269361 "Fix WebURLLoaderImpl::Context leak if a pending r..."
[chromium-blink-merge.git] / device / bluetooth / bluetooth_discovery_session.cc
blobba5f5fdba64d2047a908d69cadc3b7cda2e4eebb
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.
5 #include "device/bluetooth/bluetooth_discovery_session.h"
7 #include "base/bind.h"
8 #include "device/bluetooth/bluetooth_adapter.h"
10 namespace device {
12 BluetoothDiscoverySession::BluetoothDiscoverySession(
13 scoped_refptr<BluetoothAdapter> adapter)
14 : active_(true), adapter_(adapter), weak_ptr_factory_(this) {
15 DCHECK(adapter_.get());
18 BluetoothDiscoverySession::BluetoothDiscoverySession()
19 : active_(false), weak_ptr_factory_(this) {}
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;
27 Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing));
28 MarkAsInactive();
31 bool BluetoothDiscoverySession::IsActive() const {
32 return active_;
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;
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);
52 void BluetoothDiscoverySession::OnStop(const base::Closure& callback) {
53 MarkAsInactive();
54 callback.Run();
57 void BluetoothDiscoverySession::MarkAsInactive() {
58 if (!active_)
59 return;
60 active_ = false;
61 DCHECK(adapter_.get());
62 adapter_->DiscoverySessionBecameInactive(this);
65 } // namespace device