summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_discovery_session.cc
blob: e07df34dd28eb102c4e0c9a8098584e331c11c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/bluetooth_discovery_session.h"

#include "base/bind.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace device {

BluetoothDiscoverySession::BluetoothDiscoverySession(BluetoothAdapter* adapter)
    : active_(true),
      adapter_(adapter),
      weak_ptr_factory_(this) {
}

BluetoothDiscoverySession::~BluetoothDiscoverySession() {
  Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing));
  adapter_->DiscoverySessionDestroyed(this);
}

bool BluetoothDiscoverySession::IsActive() const {
  return active_;
}

void BluetoothDiscoverySession::Stop(
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  if (!active_) {
    LOG(ERROR) << "Discovery session not active. Cannot stop.";
    error_callback.Run();
    return;
  }
  VLOG(1) << "Stopping device discovery session.";
  adapter_->RemoveDiscoverySession(
      base::Bind(&BluetoothDiscoverySession::OnStop,
                 weak_ptr_factory_.GetWeakPtr(),
                 callback),
      error_callback);
}

void BluetoothDiscoverySession::OnStop(const base::Closure& callback) {
  active_ = false;
  callback.Run();
}

void BluetoothDiscoverySession::MarkAsInactive() {
  active_ = false;
}

}  // namespace device