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 #ifndef CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_
6 #define CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/dbus_client_bundle.h"
27 // Style Note: Clients are sorted by names.
28 class AmplifierClient
;
29 class ApManagerClient
;
31 class BluetoothAdapterClient
;
32 class BluetoothLEAdvertisingManagerClient
;
33 class BluetoothAgentManagerClient
;
34 class BluetoothDeviceClient
;
35 class BluetoothGattCharacteristicClient
;
36 class BluetoothGattDescriptorClient
;
37 class BluetoothGattManagerClient
;
38 class BluetoothGattServiceClient
;
39 class BluetoothInputClient
;
40 class BluetoothMediaClient
;
41 class BluetoothMediaTransportClient
;
42 class BluetoothProfileManagerClient
;
43 class CrasAudioClient
;
44 class CrosDisksClient
;
45 class CryptohomeClient
;
46 class DBusThreadManagerSetter
;
47 class DebugDaemonClient
;
48 class EasyUnlockClient
;
50 class ImageBurnerClient
;
51 class IntrospectableClient
;
52 class LeadershipDaemonManagerClient
;
53 class LorgnetteManagerClient
;
54 class MetronomeClient
;
55 class ModemMessagingClient
;
56 class NfcAdapterClient
;
57 class NfcDeviceClient
;
58 class NfcManagerClient
;
59 class NfcRecordClient
;
61 class PeerDaemonManagerClient
;
62 class PermissionBrokerClient
;
63 class PowerManagerClient
;
64 class PrivetDaemonManagerClient
;
66 class SessionManagerClient
;
67 class ShillDeviceClient
;
68 class ShillIPConfigClient
;
69 class ShillManagerClient
;
70 class ShillProfileClient
;
71 class ShillServiceClient
;
72 class ShillThirdPartyVpnDriverClient
;
73 class SystemClockClient
;
74 class UpdateEngineClient
;
76 // DBusThreadManager manages the D-Bus thread, the thread dedicated to
77 // handling asynchronous D-Bus operations.
79 // This class also manages D-Bus connections and D-Bus clients, which
80 // depend on the D-Bus thread to ensure the right order of shutdowns for
81 // the D-Bus thread, the D-Bus connections, and the D-Bus clients.
83 // CALLBACKS IN D-BUS CLIENTS:
85 // D-Bus clients managed by DBusThreadManager are guaranteed to be deleted
86 // after the D-Bus thread so the clients don't need to worry if new
87 // incoming messages arrive from the D-Bus thread during shutdown of the
88 // clients. The UI message loop is not running during the shutdown hence
89 // the UI message loop won't post tasks to D-BUS clients during the
90 // shutdown. However, to be extra cautious, clients should use
91 // WeakPtrFactory when creating callbacks that run on UI thread. See
92 // session_manager_client.cc for examples.
94 class CHROMEOS_EXPORT DBusThreadManager
{
96 // Sets the global instance. Must be called before any calls to Get().
97 // We explicitly initialize and shut down the global object, rather than
98 // making it a Singleton, to ensure clean startup and shutdown.
99 // This will initialize real or stub DBusClients depending on command-line
100 // arguments and whether this process runs in a ChromeOS environment.
101 static void Initialize();
103 // Returns a DBusThreadManagerSetter instance that allows tests to
104 // replace individual D-Bus clients with their own implementations.
105 // Also initializes the main DBusThreadManager for testing if necessary.
106 static scoped_ptr
<DBusThreadManagerSetter
> GetSetterForTesting();
108 // Returns true if DBusThreadManager has been initialized. Call this to
109 // avoid initializing + shutting down DBusThreadManager more than once.
110 static bool IsInitialized();
112 // Destroys the global instance.
113 static void Shutdown();
115 // Gets the global instance. Initialize() must be called first.
116 static DBusThreadManager
* Get();
118 // Returns true if |client| is stubbed.
119 bool IsUsingStub(DBusClientBundle::DBusClientType client
);
121 // Returns various D-Bus bus instances, owned by DBusThreadManager.
122 dbus::Bus
* GetSystemBus();
124 // All returned objects are owned by DBusThreadManager. Do not use these
125 // pointers after DBusThreadManager has been shut down.
126 AmplifierClient
* GetAmplifierClient();
127 ApManagerClient
* GetApManagerClient();
128 AudioDspClient
* GetAudioDspClient();
129 BluetoothAdapterClient
* GetBluetoothAdapterClient();
130 BluetoothLEAdvertisingManagerClient
* GetBluetoothLEAdvertisingManagerClient();
131 BluetoothAgentManagerClient
* GetBluetoothAgentManagerClient();
132 BluetoothDeviceClient
* GetBluetoothDeviceClient();
133 BluetoothGattCharacteristicClient
* GetBluetoothGattCharacteristicClient();
134 BluetoothGattDescriptorClient
* GetBluetoothGattDescriptorClient();
135 BluetoothGattManagerClient
* GetBluetoothGattManagerClient();
136 BluetoothGattServiceClient
* GetBluetoothGattServiceClient();
137 BluetoothInputClient
* GetBluetoothInputClient();
138 BluetoothMediaClient
* GetBluetoothMediaClient();
139 BluetoothMediaTransportClient
* GetBluetoothMediaTransportClient();
140 BluetoothProfileManagerClient
* GetBluetoothProfileManagerClient();
141 CrasAudioClient
* GetCrasAudioClient();
142 CrosDisksClient
* GetCrosDisksClient();
143 CryptohomeClient
* GetCryptohomeClient();
144 DebugDaemonClient
* GetDebugDaemonClient();
145 EasyUnlockClient
* GetEasyUnlockClient();
146 GsmSMSClient
* GetGsmSMSClient();
147 ImageBurnerClient
* GetImageBurnerClient();
148 IntrospectableClient
* GetIntrospectableClient();
149 LeadershipDaemonManagerClient
* GetLeadershipDaemonManagerClient();
150 LorgnetteManagerClient
* GetLorgnetteManagerClient();
151 MetronomeClient
* GetMetronomeClient();
152 ModemMessagingClient
* GetModemMessagingClient();
153 NfcAdapterClient
* GetNfcAdapterClient();
154 NfcDeviceClient
* GetNfcDeviceClient();
155 NfcManagerClient
* GetNfcManagerClient();
156 NfcRecordClient
* GetNfcRecordClient();
157 NfcTagClient
* GetNfcTagClient();
158 PeerDaemonManagerClient
* GetPeerDaemonManagerClient();
159 PermissionBrokerClient
* GetPermissionBrokerClient();
160 PrivetDaemonManagerClient
* GetPrivetDaemonManagerClient();
161 PowerManagerClient
* GetPowerManagerClient();
162 SessionManagerClient
* GetSessionManagerClient();
163 ShillDeviceClient
* GetShillDeviceClient();
164 ShillIPConfigClient
* GetShillIPConfigClient();
165 ShillManagerClient
* GetShillManagerClient();
166 ShillServiceClient
* GetShillServiceClient();
167 ShillProfileClient
* GetShillProfileClient();
168 ShillThirdPartyVpnDriverClient
* GetShillThirdPartyVpnDriverClient();
169 SMSClient
* GetSMSClient();
170 SystemClockClient
* GetSystemClockClient();
171 UpdateEngineClient
* GetUpdateEngineClient();
174 friend class DBusThreadManagerSetter
;
176 // Creates a new DBusThreadManager using the DBusClients set in
178 explicit DBusThreadManager(scoped_ptr
<DBusClientBundle
> client_bundle
);
179 ~DBusThreadManager();
181 // Creates a global instance of DBusThreadManager with the real
182 // implementations for all clients that are listed in |unstub_client_mask| and
183 // stub implementations for all clients that are not included. Cannot be
184 // called more than once.
185 static void CreateGlobalInstance(
186 DBusClientBundle::DBusClientTypeMask unstub_client_mask
);
188 // Initialize global thread manager instance with all real dbus client
190 static void InitializeWithRealClients();
192 // Initialize global thread manager instance with stubbed-out dbus clients
194 static void InitializeWithStubs();
196 // Initialize with stub implementations for only certain clients that are
197 // not included in the comma-separated |unstub_clients| list.
198 static void InitializeWithPartialStub(const std::string
& unstub_clients
);
200 // Initializes all currently stored DBusClients with the system bus and
201 // performs additional setup.
202 void InitializeClients();
204 scoped_ptr
<base::Thread
> dbus_thread_
;
205 scoped_refptr
<dbus::Bus
> system_bus_
;
206 scoped_ptr
<DBusClientBundle
> client_bundle_
;
208 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager
);
211 class CHROMEOS_EXPORT DBusThreadManagerSetter
{
213 ~DBusThreadManagerSetter();
215 void SetAmplifierClient(scoped_ptr
<AmplifierClient
> client
);
216 void SetAudioDspClient(scoped_ptr
<AudioDspClient
> client
);
217 void SetBluetoothAdapterClient(scoped_ptr
<BluetoothAdapterClient
> client
);
218 void SetBluetoothLEAdvertisingManagerClient(
219 scoped_ptr
<BluetoothLEAdvertisingManagerClient
> client
);
220 void SetBluetoothAgentManagerClient(
221 scoped_ptr
<BluetoothAgentManagerClient
> client
);
222 void SetBluetoothDeviceClient(scoped_ptr
<BluetoothDeviceClient
> client
);
223 void SetBluetoothGattCharacteristicClient(
224 scoped_ptr
<BluetoothGattCharacteristicClient
> client
);
225 void SetBluetoothGattDescriptorClient(
226 scoped_ptr
<BluetoothGattDescriptorClient
> client
);
227 void SetBluetoothGattManagerClient(
228 scoped_ptr
<BluetoothGattManagerClient
> client
);
229 void SetBluetoothGattServiceClient(
230 scoped_ptr
<BluetoothGattServiceClient
> client
);
231 void SetBluetoothInputClient(scoped_ptr
<BluetoothInputClient
> client
);
232 void SetBluetoothMediaClient(scoped_ptr
<BluetoothMediaClient
> client
);
233 void SetBluetoothMediaTransportClient(
234 scoped_ptr
<BluetoothMediaTransportClient
> client
);
235 void SetBluetoothProfileManagerClient(
236 scoped_ptr
<BluetoothProfileManagerClient
> client
);
237 void SetCrasAudioClient(scoped_ptr
<CrasAudioClient
> client
);
238 void SetCrosDisksClient(scoped_ptr
<CrosDisksClient
> client
);
239 void SetCryptohomeClient(scoped_ptr
<CryptohomeClient
> client
);
240 void SetDebugDaemonClient(scoped_ptr
<DebugDaemonClient
> client
);
241 void SetEasyUnlockClient(scoped_ptr
<EasyUnlockClient
> client
);
242 void SetLeadershipDaemonManagerClient(
243 scoped_ptr
<LeadershipDaemonManagerClient
> client
);
244 void SetLorgnetteManagerClient(scoped_ptr
<LorgnetteManagerClient
> client
);
245 void SetMetronomeClient(scoped_ptr
<MetronomeClient
> client
);
246 void SetShillDeviceClient(scoped_ptr
<ShillDeviceClient
> client
);
247 void SetShillIPConfigClient(scoped_ptr
<ShillIPConfigClient
> client
);
248 void SetShillManagerClient(scoped_ptr
<ShillManagerClient
> client
);
249 void SetShillServiceClient(scoped_ptr
<ShillServiceClient
> client
);
250 void SetShillProfileClient(scoped_ptr
<ShillProfileClient
> client
);
251 void SetShillThirdPartyVpnDriverClient(
252 scoped_ptr
<ShillThirdPartyVpnDriverClient
> client
);
253 void SetGsmSMSClient(scoped_ptr
<GsmSMSClient
> client
);
254 void SetImageBurnerClient(scoped_ptr
<ImageBurnerClient
> client
);
255 void SetIntrospectableClient(scoped_ptr
<IntrospectableClient
> client
);
256 void SetModemMessagingClient(scoped_ptr
<ModemMessagingClient
> client
);
257 void SetNfcAdapterClient(scoped_ptr
<NfcAdapterClient
> client
);
258 void SetNfcDeviceClient(scoped_ptr
<NfcDeviceClient
> client
);
259 void SetNfcManagerClient(scoped_ptr
<NfcManagerClient
> client
);
260 void SetNfcRecordClient(scoped_ptr
<NfcRecordClient
> client
);
261 void SetNfcTagClient(scoped_ptr
<NfcTagClient
> client
);
262 void SetPeerDaemonManagerClient(scoped_ptr
<PeerDaemonManagerClient
> client
);
263 void SetPermissionBrokerClient(scoped_ptr
<PermissionBrokerClient
> client
);
264 void SetPrivetDaemonManagerClient(
265 scoped_ptr
<PrivetDaemonManagerClient
> client
);
266 void SetPowerManagerClient(scoped_ptr
<PowerManagerClient
> client
);
267 void SetSessionManagerClient(scoped_ptr
<SessionManagerClient
> client
);
268 void SetSMSClient(scoped_ptr
<SMSClient
> client
);
269 void SetSystemClockClient(scoped_ptr
<SystemClockClient
> client
);
270 void SetUpdateEngineClient(scoped_ptr
<UpdateEngineClient
> client
);
273 friend class DBusThreadManager
;
275 DBusThreadManagerSetter();
277 DISALLOW_COPY_AND_ASSIGN(DBusThreadManagerSetter
);
280 } // namespace chromeos
282 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_