1 // Copyright (c) 2012 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_adapter_factory.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
15 #if defined(OS_CHROMEOS)
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_win.h"
19 #elif defined(OS_MACOSX)
20 #include "base/mac/mac_util.h"
21 #include "device/bluetooth/bluetooth_adapter_mac.h"
26 using device::BluetoothAdapter
;
27 using device::BluetoothAdapterFactory
;
29 // Shared default adapter instance, we don't want to keep this class around
30 // if nobody is using it so use a WeakPtr and create the object when needed;
31 // since Google C++ Style (and clang's static analyzer) forbids us having
32 // exit-time destructors we use a leaky lazy instance for it.
33 base::LazyInstance
<base::WeakPtr
<device::BluetoothAdapter
> >::Leaky
34 default_adapter
= LAZY_INSTANCE_INITIALIZER
;
36 typedef std::vector
<BluetoothAdapterFactory::AdapterCallback
>
39 // List of adapter callbacks to be called once the adapter is initialized.
40 // Since Google C++ Style (and clang's static analyzer) forbids us having
41 // exit-time destructors we use a lazy instance for it.
42 base::LazyInstance
<AdapterCallbackList
> adapter_callbacks
=
43 LAZY_INSTANCE_INITIALIZER
;
46 void RunAdapterCallbacks() {
47 CHECK(default_adapter
.Get().get());
48 scoped_refptr
<BluetoothAdapter
> adapter(default_adapter
.Get().get());
49 for (std::vector
<BluetoothAdapterFactory::AdapterCallback
>::const_iterator
50 iter
= adapter_callbacks
.Get().begin();
51 iter
!= adapter_callbacks
.Get().end();
55 adapter_callbacks
.Get().clear();
57 #endif // defined(OS_WIN)
64 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
65 #if defined(OS_CHROMEOS) || defined(OS_WIN)
67 #elif defined(OS_MACOSX)
68 return base::mac::IsOSLionOrLater();
75 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback
& callback
) {
76 if (!default_adapter
.Get().get()) {
77 #if defined(OS_CHROMEOS)
78 chromeos::BluetoothAdapterChromeOS
* new_adapter
=
79 new chromeos::BluetoothAdapterChromeOS();
80 default_adapter
.Get() = new_adapter
->weak_ptr_factory_
.GetWeakPtr();
82 BluetoothAdapterWin
* new_adapter
= new BluetoothAdapterWin(
83 base::Bind(&RunAdapterCallbacks
));
85 default_adapter
.Get() = new_adapter
->weak_ptr_factory_
.GetWeakPtr();
86 #elif defined(OS_MACOSX)
87 BluetoothAdapterMac
* new_adapter
= new BluetoothAdapterMac();
89 default_adapter
.Get() = new_adapter
->weak_ptr_factory_
.GetWeakPtr();
93 if (default_adapter
.Get()->IsInitialized()) {
94 callback
.Run(scoped_refptr
<BluetoothAdapter
>(default_adapter
.Get().get()));
96 adapter_callbacks
.Get().push_back(callback
);
101 scoped_refptr
<BluetoothAdapter
> BluetoothAdapterFactory::MaybeGetAdapter() {
102 return scoped_refptr
<BluetoothAdapter
>(default_adapter
.Get().get());
105 } // namespace device