clang: Compiler tweaks to make android build for x64
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_factory.cc
blobc3e6d5cffd975c00a0372a1451bf8702fc08cb8f
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 #endif
19 #if defined(OS_MACOSX)
20 #include "base/mac/mac_util.h"
21 #endif
23 namespace device {
25 namespace {
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;
34 #if defined(OS_WIN)
35 typedef std::vector<BluetoothAdapterFactory::AdapterCallback>
36 AdapterCallbackList;
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();
50 ++iter) {
51 iter->Run(adapter);
53 adapter_callbacks.Get().clear();
55 #endif // defined(OS_WIN)
57 } // namespace
59 // static
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())
64 return true;
65 #if defined(OS_ANDROID) || 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 DCHECK(IsBluetoothAdapterAvailable());
78 #if defined(OS_WIN)
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)
102 // static
103 void BluetoothAdapterFactory::Shutdown() {
104 if (default_adapter.Get())
105 default_adapter.Get().get()->Shutdown();
107 #endif
109 // static
110 void BluetoothAdapterFactory::SetAdapterForTesting(
111 scoped_refptr<BluetoothAdapter> adapter) {
112 default_adapter.Get() = adapter->GetWeakPtrForTesting();
115 // static
116 bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
117 return default_adapter.Get();
120 } // namespace device