QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_discovery_session.cc
blob964b8c5c7392999e82fc6ee3a1b10f79a705f434
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 BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome(
39 UMABluetoothDiscoverySessionOutcome::NOT_ACTIVE);
40 error_callback.Run();
41 return;
43 VLOG(1) << "Stopping device discovery session.";
44 base::Closure deactive_discovery_session =
45 base::Bind(&BluetoothDiscoverySession::DeactivateDiscoverySession,
46 weak_ptr_factory_.GetWeakPtr());
48 // Create a callback that runs
49 // BluetoothDiscoverySession::DeactivateDiscoverySession if the session still
50 // exists, but always runs success_callback.
51 base::Closure discovery_session_removed_callback =
52 base::Bind(&BluetoothDiscoverySession::OnDiscoverySessionRemoved,
53 deactive_discovery_session, success_callback);
54 adapter_->RemoveDiscoverySession(
55 discovery_filter_.get(), discovery_session_removed_callback,
56 base::Bind(&BluetoothDiscoverySession::OnDiscoverySessionRemovalFailed,
57 error_callback));
60 // static
61 void BluetoothDiscoverySession::OnDiscoverySessionRemoved(
62 const base::Closure& deactivate_discovery_session,
63 const base::Closure& success_callback) {
64 BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome(
65 UMABluetoothDiscoverySessionOutcome::SUCCESS);
66 deactivate_discovery_session.Run();
67 success_callback.Run();
70 // static
71 void BluetoothDiscoverySession::OnDiscoverySessionRemovalFailed(
72 const base::Closure& error_callback,
73 UMABluetoothDiscoverySessionOutcome outcome) {
74 BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome(outcome);
75 error_callback.Run();
78 void BluetoothDiscoverySession::DeactivateDiscoverySession() {
79 MarkAsInactive();
80 discovery_filter_.reset();
83 void BluetoothDiscoverySession::MarkAsInactive() {
84 if (!active_)
85 return;
86 active_ = false;
87 adapter_->DiscoverySessionBecameInactive(this);
90 static void IgnoreDiscoveryOutcome(
91 const base::Closure& error_callback,
92 UMABluetoothDiscoverySessionOutcome outcome) {
93 error_callback.Run();
96 void BluetoothDiscoverySession::SetDiscoveryFilter(
97 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
98 const base::Closure& callback,
99 const ErrorCallback& error_callback) {
100 discovery_filter_.reset(discovery_filter.release());
101 // BluetoothDiscoverySession::SetDiscoveryFilter is only used from a private
102 // extension API, so we don't bother histogramming its failures.
103 adapter_->SetDiscoveryFilter(
104 adapter_->GetMergedDiscoveryFilter().Pass(), callback,
105 base::Bind(&IgnoreDiscoveryOutcome, error_callback));
108 const BluetoothDiscoveryFilter* BluetoothDiscoverySession::GetDiscoveryFilter()
109 const {
110 return discovery_filter_.get();
113 } // namespace device