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