blob: 977143982708e750916b12c3b2b34e4d73e599e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include <vector>
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
#if defined(OS_CHROMEOS) || defined(OS_LINUX)
#include "device/bluetooth/bluetooth_adapter_bluez.h"
#endif
#if defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#endif
namespace device {
namespace {
// Shared default adapter instance. We don't want to keep this class around
// if nobody is using it, so use a WeakPtr and create the object when needed.
// Since Google C++ Style (and clang's static analyzer) forbids us having
// exit-time destructors, we use a leaky lazy instance for it.
base::LazyInstance<base::WeakPtr<BluetoothAdapter> >::Leaky default_adapter =
LAZY_INSTANCE_INITIALIZER;
#if defined(OS_WIN)
typedef std::vector<BluetoothAdapterFactory::AdapterCallback>
AdapterCallbackList;
// List of adapter callbacks to be called once the adapter is initialized.
// Since Google C++ Style (and clang's static analyzer) forbids us having
// exit-time destructors we use a lazy instance for it.
base::LazyInstance<AdapterCallbackList> adapter_callbacks =
LAZY_INSTANCE_INITIALIZER;
void RunAdapterCallbacks() {
DCHECK(default_adapter.Get());
scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get().get());
for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator
iter = adapter_callbacks.Get().begin();
iter != adapter_callbacks.Get().end();
++iter) {
iter->Run(adapter);
}
adapter_callbacks.Get().clear();
}
#endif // defined(OS_WIN)
} // namespace
// static
bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
// SetAdapterForTesting() may be used to provide a test or mock adapter
// instance even on platforms that would otherwise not support it.
if (default_adapter.Get())
return true;
// Even though the adapter is available on Linux, we only want to use it for
// the Chrome API, which is why defines(OS_LINUX) is missing from here.
#if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_WIN)
return true;
#elif defined(OS_MACOSX)
return base::mac::IsOSLionOrLater();
#else
return false;
#endif
}
// static
void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) {
// TODO(rkc): This is a very slight hack to allow us to be able to create
// an adapter on Linux, 'without' exposing the adapter to all Bluetooth
// services within the browser.
#if !defined(OS_LINUX)
DCHECK(IsBluetoothAdapterAvailable());
#endif
#if defined(OS_WIN)
if (!default_adapter.Get()) {
default_adapter.Get() =
BluetoothAdapter::CreateAdapter(base::Bind(&RunAdapterCallbacks));
DCHECK(!default_adapter.Get()->IsInitialized());
}
if (!default_adapter.Get()->IsInitialized())
adapter_callbacks.Get().push_back(callback);
#else // !defined(OS_WIN)
if (!default_adapter.Get()) {
default_adapter.Get() =
BluetoothAdapter::CreateAdapter(BluetoothAdapter::InitCallback());
}
DCHECK(default_adapter.Get()->IsInitialized());
#endif // defined(OS_WIN)
if (default_adapter.Get()->IsInitialized())
callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get().get()));
}
#if defined(OS_CHROMEOS) || defined(OS_LINUX)
// static
void BluetoothAdapterFactory::Shutdown() {
if (default_adapter.Get())
default_adapter.Get().get()->Shutdown();
}
#endif
// static
void BluetoothAdapterFactory::SetAdapterForTesting(
scoped_refptr<BluetoothAdapter> adapter) {
default_adapter.Get() = adapter->GetWeakPtrForTesting();
}
// static
bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
return default_adapter.Get();
}
} // namespace device
|