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