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