Relanding this change. The bug 332430 which caused this to be reverted will be addres...
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_factory.cc
blobb48d4f94efe10a9ccbe00438af63e634e76b442c
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"
7 #include <vector>
9 #include "base/bind.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"
17 #elif defined(OS_WIN)
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"
22 #endif
24 namespace {
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>
37 AdapterCallbackList;
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;
45 #if defined(OS_WIN)
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();
52 ++iter) {
53 iter->Run(adapter);
55 adapter_callbacks.Get().clear();
57 #endif // defined(OS_WIN)
59 } // namespace
61 namespace device {
63 // static
64 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
65 #if defined(OS_CHROMEOS) || defined(OS_WIN)
66 return true;
67 #elif defined(OS_MACOSX)
68 return base::mac::IsOSLionOrLater();
69 #else
70 return false;
71 #endif
74 // static
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();
81 #elif defined(OS_WIN)
82 BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin(
83 base::Bind(&RunAdapterCallbacks));
84 new_adapter->Init();
85 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
86 #elif defined(OS_MACOSX)
87 BluetoothAdapterMac* new_adapter = new BluetoothAdapterMac();
88 new_adapter->Init();
89 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
90 #endif
93 if (default_adapter.Get()->IsInitialized()) {
94 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get().get()));
95 } else {
96 adapter_callbacks.Get().push_back(callback);
100 // static
101 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::MaybeGetAdapter() {
102 return scoped_refptr<BluetoothAdapter>(default_adapter.Get().get());
105 } // namespace device