OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/sys_info.h" | |
9 #include "base/threading/thread.h" | |
10 #include "chromeos/chromeos_switches.h" | |
stevenjb
2015/09/17 21:48:01
Not needed
rkc
2015/09/21 18:54:22
Done.
| |
11 #include "chromeos/dbus/dbus_thread_manager.h" | |
12 #include "dbus/bus.h" | |
13 #include "dbus/dbus_statistics.h" | |
14 #include "device/bluetooth/dbus/bluetooth_adapter_client.h" | |
15 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" | |
16 #include "device/bluetooth/dbus/bluetooth_device_client.h" | |
17 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h" | |
18 #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h" | |
19 #include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h" | |
20 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h" | |
21 #include "device/bluetooth/dbus/bluetooth_input_client.h" | |
22 #include "device/bluetooth/dbus/bluetooth_le_advertising_manager_client.h" | |
23 #include "device/bluetooth/dbus/bluetooth_media_client.h" | |
24 #include "device/bluetooth/dbus/bluetooth_media_transport_client.h" | |
25 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" | |
26 | |
27 namespace bluez { | |
28 | |
29 static BluezDBusManager* g_dbus_thread_manager = NULL; | |
30 static bool g_using_dbus_thread_manager_for_testing = false; | |
31 | |
32 BluezDBusManager::BluezDBusManager( | |
33 scoped_ptr<BluetoothDBusClientBundle> client_bundle) | |
34 : client_bundle_(client_bundle.Pass()) { | |
35 // Make sure DBusThreadManager was initialized before us. | |
36 DCHECK(chromeos::DBusThreadManager::Get()); | |
37 } | |
38 | |
39 BluezDBusManager::~BluezDBusManager() { | |
40 // Delete all D-Bus clients before shutting down the system bus. | |
41 client_bundle_.reset(); | |
42 } | |
43 | |
44 dbus::Bus* bluez::BluezDBusManager::GetSystemBus() { | |
45 DCHECK(chromeos::DBusThreadManager::Get()); | |
46 return chromeos::DBusThreadManager::Get()->GetSystemBus(); | |
stevenjb
2015/09/17 21:48:01
Rather than depending on DBusThreadManager (which
rkc
2015/09/21 18:54:22
Done.
| |
47 } | |
48 | |
49 BluetoothAdapterClient* bluez::BluezDBusManager::GetBluetoothAdapterClient() { | |
50 return client_bundle_->bluetooth_adapter_client(); | |
51 } | |
52 | |
53 BluetoothLEAdvertisingManagerClient* | |
54 bluez::BluezDBusManager::GetBluetoothLEAdvertisingManagerClient() { | |
55 return client_bundle_->bluetooth_le_advertising_manager_client(); | |
56 } | |
57 | |
58 BluetoothAgentManagerClient* | |
59 bluez::BluezDBusManager::GetBluetoothAgentManagerClient() { | |
60 return client_bundle_->bluetooth_agent_manager_client(); | |
61 } | |
62 | |
63 BluetoothDeviceClient* bluez::BluezDBusManager::GetBluetoothDeviceClient() { | |
64 return client_bundle_->bluetooth_device_client(); | |
65 } | |
66 | |
67 BluetoothGattCharacteristicClient* | |
68 bluez::BluezDBusManager::GetBluetoothGattCharacteristicClient() { | |
69 return client_bundle_->bluetooth_gatt_characteristic_client(); | |
70 } | |
71 | |
72 BluetoothGattDescriptorClient* | |
73 bluez::BluezDBusManager::GetBluetoothGattDescriptorClient() { | |
74 return client_bundle_->bluetooth_gatt_descriptor_client(); | |
75 } | |
76 | |
77 BluetoothGattManagerClient* | |
78 bluez::BluezDBusManager::GetBluetoothGattManagerClient() { | |
79 return client_bundle_->bluetooth_gatt_manager_client(); | |
80 } | |
81 | |
82 BluetoothGattServiceClient* | |
83 bluez::BluezDBusManager::GetBluetoothGattServiceClient() { | |
84 return client_bundle_->bluetooth_gatt_service_client(); | |
85 } | |
86 | |
87 BluetoothInputClient* bluez::BluezDBusManager::GetBluetoothInputClient() { | |
88 return client_bundle_->bluetooth_input_client(); | |
89 } | |
90 | |
91 BluetoothMediaClient* bluez::BluezDBusManager::GetBluetoothMediaClient() { | |
92 return client_bundle_->bluetooth_media_client(); | |
93 } | |
94 | |
95 BluetoothMediaTransportClient* | |
96 bluez::BluezDBusManager::GetBluetoothMediaTransportClient() { | |
97 return client_bundle_->bluetooth_media_transport_client(); | |
98 } | |
99 | |
100 BluetoothProfileManagerClient* | |
101 bluez::BluezDBusManager::GetBluetoothProfileManagerClient() { | |
102 return client_bundle_->bluetooth_profile_manager_client(); | |
103 } | |
104 | |
105 void BluezDBusManager::InitializeClients() { | |
106 GetBluetoothAdapterClient()->Init(GetSystemBus()); | |
107 GetBluetoothAgentManagerClient()->Init(GetSystemBus()); | |
108 GetBluetoothDeviceClient()->Init(GetSystemBus()); | |
109 GetBluetoothGattCharacteristicClient()->Init(GetSystemBus()); | |
110 GetBluetoothGattDescriptorClient()->Init(GetSystemBus()); | |
111 GetBluetoothGattManagerClient()->Init(GetSystemBus()); | |
112 GetBluetoothGattServiceClient()->Init(GetSystemBus()); | |
113 GetBluetoothInputClient()->Init(GetSystemBus()); | |
114 GetBluetoothLEAdvertisingManagerClient()->Init(GetSystemBus()); | |
115 GetBluetoothMediaClient()->Init(GetSystemBus()); | |
116 GetBluetoothMediaTransportClient()->Init(GetSystemBus()); | |
117 GetBluetoothProfileManagerClient()->Init(GetSystemBus()); | |
118 | |
119 // This must be called after the list of clients so they've each had a | |
120 // chance to register with their object g_dbus_thread_managers. | |
121 if (GetSystemBus()) | |
122 GetSystemBus()->GetManagedObjects(); | |
123 } | |
124 | |
125 // static | |
126 void BluezDBusManager::Initialize() { | |
127 // If we initialize BluezDBusManager twice we may also be shutting it down | |
128 // early; do not allow that. | |
129 if (g_using_dbus_thread_manager_for_testing) | |
130 return; | |
131 | |
132 CHECK(!g_dbus_thread_manager); | |
133 bool use_dbus_stub = chromeos::DBusThreadManager::Get()->IsUsingStub( | |
134 chromeos::DBusClientBundle::BLUETOOTH); | |
stevenjb
2015/09/17 21:48:01
Pass |use_stub| here explicitly instead.
rkc
2015/09/21 18:54:22
Done.
| |
135 if (use_dbus_stub) { | |
136 InitializeWithStubs(); | |
137 } else { | |
138 InitializeWithRealClients(); | |
139 } | |
140 } | |
141 | |
142 // static | |
143 scoped_ptr<BluezDBusManagerSetter> | |
144 bluez::BluezDBusManager::GetSetterForTesting() { | |
145 if (!g_using_dbus_thread_manager_for_testing) { | |
146 g_using_dbus_thread_manager_for_testing = true; | |
147 InitializeWithStubs(); | |
148 } | |
149 | |
150 return make_scoped_ptr(new BluezDBusManagerSetter()); | |
151 } | |
152 | |
153 // static | |
154 void BluezDBusManager::CreateGlobalInstance(bool use_stubs) { | |
155 CHECK(!g_dbus_thread_manager); | |
156 g_dbus_thread_manager = new BluezDBusManager( | |
157 make_scoped_ptr(new BluetoothDBusClientBundle(use_stubs))); | |
158 g_dbus_thread_manager->InitializeClients(); | |
159 } | |
160 | |
161 // static | |
162 void BluezDBusManager::InitializeWithRealClients() { | |
163 CreateGlobalInstance(false); | |
164 VLOG(1) << "BluezDBusManager initialized for Chrome OS"; | |
165 } | |
166 | |
167 // static | |
168 void BluezDBusManager::InitializeWithStubs() { | |
169 CreateGlobalInstance(true); | |
170 VLOG(1) << "BluezDBusManager created for testing"; | |
171 } | |
172 | |
173 // static | |
174 bool BluezDBusManager::IsInitialized() { | |
175 return g_dbus_thread_manager != NULL; | |
176 } | |
177 | |
178 // static | |
179 void BluezDBusManager::Shutdown() { | |
180 // Ensure that we only shutdown BluezDBusManager once. | |
181 CHECK(g_dbus_thread_manager); | |
182 BluezDBusManager* dbus_thread_manager = g_dbus_thread_manager; | |
183 g_dbus_thread_manager = NULL; | |
184 g_using_dbus_thread_manager_for_testing = false; | |
185 delete dbus_thread_manager; | |
186 VLOG(1) << "BluezDBusManager Shutdown completed"; | |
187 } | |
188 | |
189 // static | |
190 BluezDBusManager* bluez::BluezDBusManager::Get() { | |
191 CHECK(g_dbus_thread_manager) | |
192 << "bluez::BluezDBusManager::Get() called before Initialize()"; | |
193 return g_dbus_thread_manager; | |
194 } | |
195 | |
196 BluezDBusManagerSetter::BluezDBusManagerSetter() {} | |
197 | |
198 BluezDBusManagerSetter::~BluezDBusManagerSetter() {} | |
199 | |
200 void BluezDBusManagerSetter::SetBluetoothAdapterClient( | |
201 scoped_ptr<BluetoothAdapterClient> client) { | |
202 bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_adapter_client_ = | |
203 client.Pass(); | |
204 } | |
205 | |
206 void BluezDBusManagerSetter::SetBluetoothLEAdvertisingManagerClient( | |
207 scoped_ptr<BluetoothLEAdvertisingManagerClient> client) { | |
208 bluez::BluezDBusManager::Get() | |
209 ->client_bundle_->bluetooth_le_advertising_manager_client_ = | |
210 client.Pass(); | |
211 } | |
212 | |
213 void BluezDBusManagerSetter::SetBluetoothAgentManagerClient( | |
214 scoped_ptr<BluetoothAgentManagerClient> client) { | |
215 bluez::BluezDBusManager::Get() | |
216 ->client_bundle_->bluetooth_agent_manager_client_ = client.Pass(); | |
217 } | |
218 | |
219 void BluezDBusManagerSetter::SetBluetoothDeviceClient( | |
220 scoped_ptr<BluetoothDeviceClient> client) { | |
221 bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_device_client_ = | |
222 client.Pass(); | |
223 } | |
224 | |
225 void BluezDBusManagerSetter::SetBluetoothGattCharacteristicClient( | |
226 scoped_ptr<BluetoothGattCharacteristicClient> client) { | |
227 bluez::BluezDBusManager::Get() | |
228 ->client_bundle_->bluetooth_gatt_characteristic_client_ = client.Pass(); | |
229 } | |
230 | |
231 void BluezDBusManagerSetter::SetBluetoothGattDescriptorClient( | |
232 scoped_ptr<BluetoothGattDescriptorClient> client) { | |
233 bluez::BluezDBusManager::Get() | |
234 ->client_bundle_->bluetooth_gatt_descriptor_client_ = client.Pass(); | |
235 } | |
236 | |
237 void BluezDBusManagerSetter::SetBluetoothGattManagerClient( | |
238 scoped_ptr<BluetoothGattManagerClient> client) { | |
239 bluez::BluezDBusManager::Get() | |
240 ->client_bundle_->bluetooth_gatt_manager_client_ = client.Pass(); | |
241 } | |
242 | |
243 void BluezDBusManagerSetter::SetBluetoothGattServiceClient( | |
244 scoped_ptr<BluetoothGattServiceClient> client) { | |
245 bluez::BluezDBusManager::Get() | |
246 ->client_bundle_->bluetooth_gatt_service_client_ = client.Pass(); | |
247 } | |
248 | |
249 void BluezDBusManagerSetter::SetBluetoothInputClient( | |
250 scoped_ptr<BluetoothInputClient> client) { | |
251 bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_input_client_ = | |
252 client.Pass(); | |
253 } | |
254 | |
255 void BluezDBusManagerSetter::SetBluetoothMediaClient( | |
256 scoped_ptr<BluetoothMediaClient> client) { | |
257 bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_media_client_ = | |
258 client.Pass(); | |
259 } | |
260 | |
261 void BluezDBusManagerSetter::SetBluetoothMediaTransportClient( | |
262 scoped_ptr<BluetoothMediaTransportClient> client) { | |
263 bluez::BluezDBusManager::Get() | |
264 ->client_bundle_->bluetooth_media_transport_client_ = client.Pass(); | |
265 } | |
266 | |
267 void BluezDBusManagerSetter::SetBluetoothProfileManagerClient( | |
268 scoped_ptr<BluetoothProfileManagerClient> client) { | |
269 bluez::BluezDBusManager::Get() | |
270 ->client_bundle_->bluetooth_profile_manager_client_ = client.Pass(); | |
271 } | |
272 | |
273 } // namespace bluez | |
OLD | NEW |