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.";
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
,
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() {
67 discovery_filter_
.reset();
70 void BluetoothDiscoverySession::MarkAsInactive() {
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()
88 return discovery_filter_
.get();