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