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 static void Initialize();
91 // Returns a DBusThreadManagerSetter instance that allows tests to
92 // replace individual D-Bus clients with their own implementations.
93 // Also initializes the main DBusThreadManager for testing if necessary.
94 static scoped_ptr
<DBusThreadManagerSetter
> GetSetterForTesting();
96 // Returns true if DBusThreadManager has been initialized. Call this to
97 // avoid initializing + shutting down DBusThreadManager more than once.
98 static bool IsInitialized();
100 // Destroys the global instance.
101 static void Shutdown();
103 // Gets the global instance. Initialize() must be called first.
104 static DBusThreadManager
* Get();
106 // Returns true if |client| is stubbed.
107 static bool IsUsingStub(DBusClientBundle::DBusClientType client
);
109 // Returns various D-Bus bus instances, owned by DBusThreadManager.
110 dbus::Bus
* GetSystemBus();
112 // All returned objects are owned by DBusThreadManager. Do not cache these
113 // pointers and use them after DBusThreadManager has been shut down.
114 BluetoothAdapterClient
* GetBluetoothAdapterClient();
115 BluetoothAgentManagerClient
* GetBluetoothAgentManagerClient();
116 BluetoothDeviceClient
* GetBluetoothDeviceClient();
117 BluetoothGattCharacteristicClient
* GetBluetoothGattCharacteristicClient();
118 BluetoothGattDescriptorClient
* GetBluetoothGattDescriptorClient();
119 BluetoothGattManagerClient
* GetBluetoothGattManagerClient();
120 BluetoothGattServiceClient
* GetBluetoothGattServiceClient();
121 BluetoothInputClient
* GetBluetoothInputClient();
122 BluetoothProfileManagerClient
* GetBluetoothProfileManagerClient();
123 CrasAudioClient
* GetCrasAudioClient();
124 CrosDisksClient
* GetCrosDisksClient();
125 CryptohomeClient
* GetCryptohomeClient();
126 DebugDaemonClient
* GetDebugDaemonClient();
127 EasyUnlockClient
* GetEasyUnlockClient();
128 GsmSMSClient
* GetGsmSMSClient();
129 ImageBurnerClient
* GetImageBurnerClient();
130 IntrospectableClient
* GetIntrospectableClient();
131 LorgnetteManagerClient
* GetLorgnetteManagerClient();
132 ModemMessagingClient
* GetModemMessagingClient();
133 NfcAdapterClient
* GetNfcAdapterClient();
134 NfcDeviceClient
* GetNfcDeviceClient();
135 NfcManagerClient
* GetNfcManagerClient();
136 NfcRecordClient
* GetNfcRecordClient();
137 NfcTagClient
* GetNfcTagClient();
138 PermissionBrokerClient
* GetPermissionBrokerClient();
139 PowerManagerClient
* GetPowerManagerClient();
140 PowerPolicyController
* GetPowerPolicyController();
141 SessionManagerClient
* GetSessionManagerClient();
142 ShillDeviceClient
* GetShillDeviceClient();
143 ShillIPConfigClient
* GetShillIPConfigClient();
144 ShillManagerClient
* GetShillManagerClient();
145 ShillServiceClient
* GetShillServiceClient();
146 ShillProfileClient
* GetShillProfileClient();
147 SMSClient
* GetSMSClient();
148 SystemClockClient
* GetSystemClockClient();
149 UpdateEngineClient
* GetUpdateEngineClient();
152 friend class DBusThreadManagerSetter
;
155 ~DBusThreadManager();
157 // Creates a global instance of DBusThreadManager. Can not be called more
159 static void CreateGlobalInstance();
161 // Initialize global thread manager instance.
162 static void InitializeRegular();
164 // Initialize global thread manager instance with stubbed-out dbus clients
166 static void InitializeWithStubs();
168 // Initialize with stub implementations for only certain clients that are
169 // not included in comma-separated |unstub_clients| list.
170 static void InitializeWithPartialStub(const std::string
& unstub_clients
);
172 // Constructs all clients and stores them in the respective *_client_ member
174 void CreateDefaultClients();
176 // Constructs all clients and stores them in the respective *_client_ member
178 void InitializeClients();
180 // Bitmask that defines which dbus clients are not stubbed out. Bitmap flags
181 // are defined within DBusClientBundle::DBusClientType enum.
182 static DBusClientBundle::DBusClientTypeMask unstub_client_mask_
;
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_