Simplify About Chrome settings page.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_discovery_session.cc
blobaa348a2b5393c6bfb409457b8995ba1129bb0f3a
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"
9 #include "device/bluetooth/bluetooth_discovery_filter.h"
11 namespace device {
13 BluetoothDiscoverySession::BluetoothDiscoverySession(
14 scoped_refptr<BluetoothAdapter> adapter,
15 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter)
16 : active_(true),
17 adapter_(adapter),
18 discovery_filter_(discovery_filter.release()),
19 weak_ptr_factory_(this) {
20 DCHECK(adapter_.get());
23 BluetoothDiscoverySession::~BluetoothDiscoverySession() {
24 if (active_) {
25 Stop(base::Bind(&base::DoNothing), base::Bind(&base::DoNothing));
26 MarkAsInactive();
30 bool BluetoothDiscoverySession::IsActive() const {
31 return active_;
34 void BluetoothDiscoverySession::Stop(const base::Closure& success_callback,
35 const ErrorCallback& error_callback) {
36 if (!active_) {
37 LOG(WARNING) << "Discovery session not active. Cannot stop.";
38 error_callback.Run();
39 return;
41 VLOG(1) << "Stopping device discovery session.";
42 base::Closure deactive_discovery_session =
43 base::Bind(&BluetoothDiscoverySession::DeactivateDiscoverySession,
44 weak_ptr_factory_.GetWeakPtr());
46 // Create a callback that runs
47 // BluetoothDiscoverySession::DeactivateDiscoverySession if the session still
48 // exists, but always runs success_callback.
49 base::Closure discovery_session_removed_callback =
50 base::Bind(&BluetoothDiscoverySession::OnDiscoverySessionRemoved,
51 deactive_discovery_session, success_callback);
52 adapter_->RemoveDiscoverySession(discovery_filter_.get(),
53 discovery_session_removed_callback,
54 error_callback);
57 // static
58 void BluetoothDiscoverySession::OnDiscoverySessionRemoved(
59 const base::Closure& deactivate_discovery_session,
60 const base::Closure& success_callback) {
61 deactivate_discovery_session.Run();
62 success_callback.Run();
65 void BluetoothDiscoverySession::DeactivateDiscoverySession() {
66 MarkAsInactive();
67 discovery_filter_.reset();
70 void BluetoothDiscoverySession::MarkAsInactive() {
71 if (!active_)
72 return;
73 active_ = false;
74 adapter_->DiscoverySessionBecameInactive(this);
77 void BluetoothDiscoverySession::SetDiscoveryFilter(
78 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
79 const base::Closure& callback,
80 const ErrorCallback& error_callback) {
81 discovery_filter_.reset(discovery_filter.release());
82 adapter_->SetDiscoveryFilter(adapter_->GetMergedDiscoveryFilter().Pass(),
83 callback, error_callback);
86 const BluetoothDiscoveryFilter* BluetoothDiscoverySession::GetDiscoveryFilter()
87 const {
88 return discovery_filter_.get();
91 } // namespace device