Roll breakpad a513e85:7caf028 (svn 1384:1385)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_factory.cc
blob33950531312af20a9c055c817a7e4c031c2a37e7
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_MACOSX)
16 #include "base/mac/mac_util.h"
17 #endif
19 namespace device {
21 namespace {
23 // Shared default adapter instance. We don't want to keep this class around
24 // if nobody is using it, so use a WeakPtr and create the object when needed.
25 // Since Google C++ Style (and clang's static analyzer) forbids us having
26 // exit-time destructors, we use a leaky lazy instance for it.
27 base::LazyInstance<base::WeakPtr<BluetoothAdapter> >::Leaky default_adapter =
28 LAZY_INSTANCE_INITIALIZER;
30 #if defined(OS_WIN)
31 typedef std::vector<BluetoothAdapterFactory::AdapterCallback>
32 AdapterCallbackList;
34 // List of adapter callbacks to be called once the adapter is initialized.
35 // Since Google C++ Style (and clang's static analyzer) forbids us having
36 // exit-time destructors we use a lazy instance for it.
37 base::LazyInstance<AdapterCallbackList> adapter_callbacks =
38 LAZY_INSTANCE_INITIALIZER;
40 void RunAdapterCallbacks() {
41 DCHECK(default_adapter.Get());
42 scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get().get());
43 for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator
44 iter = adapter_callbacks.Get().begin();
45 iter != adapter_callbacks.Get().end();
46 ++iter) {
47 iter->Run(adapter);
49 adapter_callbacks.Get().clear();
51 #endif // defined(OS_WIN)
53 } // namespace
55 // static
56 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
57 // SetAdapterForTesting() may be used to provide a test or mock adapter
58 // instance even on platforms that would otherwise not support it.
59 if (default_adapter.Get())
60 return true;
61 #if defined(OS_CHROMEOS) || defined(OS_WIN)
62 return true;
63 #elif defined(OS_MACOSX)
64 return base::mac::IsOSLionOrLater();
65 #else
66 return false;
67 #endif
70 // static
71 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) {
72 DCHECK(IsBluetoothAdapterAvailable());
74 #if defined(OS_WIN)
75 if (!default_adapter.Get()) {
76 default_adapter.Get() =
77 BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks));
78 DCHECK(!default_adapter.Get()->IsInitialized());
81 if (!default_adapter.Get()->IsInitialized())
82 adapter_callbacks.Get().push_back(callback);
83 #else // !defined(OS_WIN)
84 if (!default_adapter.Get()) {
85 default_adapter.Get() =
86 BluetoothAdapter::CreateAdapter(BluetoothAdapter::InitCallback());
89 DCHECK(default_adapter.Get()->IsInitialized());
90 #endif // defined(OS_WIN)
92 if (default_adapter.Get()->IsInitialized())
93 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get().get()));
97 // static
98 void BluetoothAdapterFactory::SetAdapterForTesting(
99 scoped_refptr<BluetoothAdapter> adapter) {
100 default_adapter.Get() = adapter->GetWeakPtrForTesting();
103 // static
104 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
105 return default_adapter.Get();
108 } // namespace device