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"
8 #include "device/bluetooth/bluetooth_adapter.h"
9 #include "device/bluetooth/bluetooth_discovery_filter.h"
13 BluetoothDiscoverySession::BluetoothDiscoverySession(
14 scoped_refptr
<BluetoothAdapter
> adapter
,
15 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
)
18 discovery_filter_(discovery_filter
.release()),
19 weak_ptr_factory_(this) {
20 DCHECK(adapter_
.get());
23 BluetoothDiscoverySession::~BluetoothDiscoverySession() {
25 Stop(base::Bind(&base::DoNothing
), base::Bind(&base::DoNothing
));
30 bool BluetoothDiscoverySession::IsActive() const {
34 void BluetoothDiscoverySession::Stop(const base::Closure
& success_callback
,
35 const ErrorCallback
& error_callback
) {
37 LOG(WARNING
) << "Discovery session not active. Cannot stop.";
38 BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome(
39 UMABluetoothDiscoverySessionOutcome::NOT_ACTIVE
);
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
,
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();
71 void BluetoothDiscoverySession::OnDiscoverySessionRemovalFailed(
72 const base::Closure
& error_callback
,
73 UMABluetoothDiscoverySessionOutcome outcome
) {
74 BluetoothAdapter::RecordBluetoothDiscoverySessionStopOutcome(outcome
);
78 void BluetoothDiscoverySession::DeactivateDiscoverySession() {
80 discovery_filter_
.reset();
83 void BluetoothDiscoverySession::MarkAsInactive() {
87 adapter_
->DiscoverySessionBecameInactive(this);
90 static void IgnoreDiscoveryOutcome(
91 const base::Closure
& error_callback
,
92 UMABluetoothDiscoverySessionOutcome outcome
) {
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()
110 return discovery_filter_
.get();
113 } // namespace device