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"
19 #if defined(OS_MACOSX)
20 #include "base/mac/mac_util.h"
27 // Shared default adapter instance. We don't want to keep this class around
28 // if nobody is using it, so use a WeakPtr and create the object when needed.
29 // Since Google C++ Style (and clang's static analyzer) forbids us having
30 // exit-time destructors, we use a leaky lazy instance for it.
31 base::LazyInstance
<base::WeakPtr
<BluetoothAdapter
> >::Leaky default_adapter
=
32 LAZY_INSTANCE_INITIALIZER
;
35 typedef std::vector
<BluetoothAdapterFactory::AdapterCallback
>
38 // List of adapter callbacks to be called once the adapter is initialized.
39 // Since Google C++ Style (and clang's static analyzer) forbids us having
40 // exit-time destructors we use a lazy instance for it.
41 base::LazyInstance
<AdapterCallbackList
> adapter_callbacks
=
42 LAZY_INSTANCE_INITIALIZER
;
44 void RunAdapterCallbacks() {
45 DCHECK(default_adapter
.Get());
46 scoped_refptr
<BluetoothAdapter
> adapter(default_adapter
.Get().get());
47 for (std::vector
<BluetoothAdapterFactory::AdapterCallback
>::const_iterator
48 iter
= adapter_callbacks
.Get().begin();
49 iter
!= adapter_callbacks
.Get().end();
53 adapter_callbacks
.Get().clear();
55 #endif // defined(OS_WIN)
60 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
61 // SetAdapterForTesting() may be used to provide a test or mock adapter
62 // instance even on platforms that would otherwise not support it.
63 if (default_adapter
.Get())
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 DCHECK(IsBluetoothAdapterAvailable());
79 if (!default_adapter
.Get()) {
80 default_adapter
.Get() =
81 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks
));
82 DCHECK(!default_adapter
.Get()->IsInitialized());
85 if (!default_adapter
.Get()->IsInitialized())
86 adapter_callbacks
.Get().push_back(callback
);
87 #else // !defined(OS_WIN)
88 if (!default_adapter
.Get()) {
89 default_adapter
.Get() =
90 BluetoothAdapter::CreateAdapter(BluetoothAdapter::InitCallback());
93 DCHECK(default_adapter
.Get()->IsInitialized());
94 #endif // defined(OS_WIN)
96 if (default_adapter
.Get()->IsInitialized())
97 callback
.Run(scoped_refptr
<BluetoothAdapter
>(default_adapter
.Get().get()));
101 #if defined(OS_CHROMEOS)
103 void BluetoothAdapterFactory::Shutdown() {
104 if (default_adapter
.Get())
105 default_adapter
.Get().get()->Shutdown();
110 void BluetoothAdapterFactory::SetAdapterForTesting(
111 scoped_refptr
<BluetoothAdapter
> adapter
) {
112 default_adapter
.Get() = adapter
->GetWeakPtrForTesting();
116 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
117 return default_adapter
.Get();
120 } // namespace device