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 DBusThreadManagerSetter instance that allows tests to
92 // replace individual dbus clients with their own implementations.
93 static scoped_ptr
<DBusThreadManagerSetter
> GetSetterForTesting();
95 // Returns true if DBusThreadManager has been initialized. Call this to
96 // avoid initializing + shutting down DBusThreadManager more than once.
97 static bool IsInitialized();
99 // Destroys the global instance.
100 static void Shutdown();
102 // Gets the global instance. Initialize() must be called first.
103 static DBusThreadManager
* Get();
105 // Returns true if |client| is stubbed.
106 static bool IsUsingStub(DBusClientBundle::DBusClientType client
);
108 // Returns various D-Bus bus instances, owned by DBusThreadManager.
109 dbus::Bus
* GetSystemBus();
111 // All returned objects are owned by DBusThreadManager. Do not cache these
112 // pointers and use them after DBusThreadManager has been shut down.
113 BluetoothAdapterClient
* GetBluetoothAdapterClient();
114 BluetoothAgentManagerClient
* GetBluetoothAgentManagerClient();
115 BluetoothDeviceClient
* GetBluetoothDeviceClient();
116 BluetoothGattCharacteristicClient
* GetBluetoothGattCharacteristicClient();
117 BluetoothGattDescriptorClient
* GetBluetoothGattDescriptorClient();
118 BluetoothGattManagerClient
* GetBluetoothGattManagerClient();
119 BluetoothGattServiceClient
* GetBluetoothGattServiceClient();
120 BluetoothInputClient
* GetBluetoothInputClient();
121 BluetoothProfileManagerClient
* GetBluetoothProfileManagerClient();
122 CrasAudioClient
* GetCrasAudioClient();
123 CrosDisksClient
* GetCrosDisksClient();
124 CryptohomeClient
* GetCryptohomeClient();
125 DebugDaemonClient
* GetDebugDaemonClient();
126 EasyUnlockClient
* GetEasyUnlockClient();
127 GsmSMSClient
* GetGsmSMSClient();
128 ImageBurnerClient
* GetImageBurnerClient();
129 IntrospectableClient
* GetIntrospectableClient();
130 LorgnetteManagerClient
* GetLorgnetteManagerClient();
131 ModemMessagingClient
* GetModemMessagingClient();
132 NfcAdapterClient
* GetNfcAdapterClient();
133 NfcDeviceClient
* GetNfcDeviceClient();
134 NfcManagerClient
* GetNfcManagerClient();
135 NfcRecordClient
* GetNfcRecordClient();
136 NfcTagClient
* GetNfcTagClient();
137 PermissionBrokerClient
* GetPermissionBrokerClient();
138 PowerManagerClient
* GetPowerManagerClient();
139 PowerPolicyController
* GetPowerPolicyController();
140 SessionManagerClient
* GetSessionManagerClient();
141 ShillDeviceClient
* GetShillDeviceClient();
142 ShillIPConfigClient
* GetShillIPConfigClient();
143 ShillManagerClient
* GetShillManagerClient();
144 ShillServiceClient
* GetShillServiceClient();
145 ShillProfileClient
* GetShillProfileClient();
146 SMSClient
* GetSMSClient();
147 SystemClockClient
* GetSystemClockClient();
148 UpdateEngineClient
* GetUpdateEngineClient();
151 friend class DBusThreadManagerSetter
;
154 ~DBusThreadManager();
156 // Creates a global instance of DBusThreadManager. Can not be called more
158 static void CreateGlobalInstance();
160 // Initialize global thread manager instance.
161 static void InitializeRegular();
163 // Initialize global thread manager instance with stubbed-out dbus clients
165 static void InitializeWithStubs();
167 // Initialize with stub implementations for only certain clients that are
168 // not included in comma-separated |unstub_clients| list.
169 static void InitializeWithPartialStub(const std::string
& unstub_clients
);
171 // Constructs all clients and stores them in the respective *_client_ member
173 void CreateDefaultClients();
175 // Constructs all clients and stores them in the respective *_client_ member
177 void InitializeClients();
179 // Bitmask that defines which dbus clients are not stubbed out. Bitmap flags
180 // are defined within DBusClientBundle::DBusClientType enum.
181 static DBusClientBundle::DBusClientTypeMask unstub_client_mask_
;
183 scoped_ptr
<base::Thread
> dbus_thread_
;
184 scoped_refptr
<dbus::Bus
> system_bus_
;
185 scoped_ptr
<DBusClientBundle
> client_bundle_
;
186 scoped_ptr
<PowerPolicyController
> power_policy_controller_
;
188 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager
);
191 class CHROMEOS_EXPORT DBusThreadManagerSetter
{
193 ~DBusThreadManagerSetter();
195 void SetBluetoothAdapterClient(scoped_ptr
<BluetoothAdapterClient
> client
);
196 void SetBluetoothAgentManagerClient(
197 scoped_ptr
<BluetoothAgentManagerClient
> client
);
198 void SetBluetoothDeviceClient(scoped_ptr
<BluetoothDeviceClient
> client
);
199 void SetBluetoothGattCharacteristicClient(
200 scoped_ptr
<BluetoothGattCharacteristicClient
> client
);
201 void SetBluetoothGattDescriptorClient(
202 scoped_ptr
<BluetoothGattDescriptorClient
> client
);
203 void SetBluetoothGattManagerClient(
204 scoped_ptr
<BluetoothGattManagerClient
> client
);
205 void SetBluetoothGattServiceClient(
206 scoped_ptr
<BluetoothGattServiceClient
> client
);
207 void SetBluetoothInputClient(scoped_ptr
<BluetoothInputClient
> client
);
208 void SetBluetoothProfileManagerClient(
209 scoped_ptr
<BluetoothProfileManagerClient
> client
);
210 void SetCrasAudioClient(scoped_ptr
<CrasAudioClient
> client
);
211 void SetCrosDisksClient(scoped_ptr
<CrosDisksClient
> client
);
212 void SetCryptohomeClient(scoped_ptr
<CryptohomeClient
> client
);
213 void SetDebugDaemonClient(scoped_ptr
<DebugDaemonClient
> client
);
214 void SetEasyUnlockClient(scoped_ptr
<EasyUnlockClient
> client
);
215 void SetLorgnetteManagerClient(scoped_ptr
<LorgnetteManagerClient
> client
);
216 void SetShillDeviceClient(scoped_ptr
<ShillDeviceClient
> client
);
217 void SetShillIPConfigClient(scoped_ptr
<ShillIPConfigClient
> client
);
218 void SetShillManagerClient(scoped_ptr
<ShillManagerClient
> client
);
219 void SetShillServiceClient(scoped_ptr
<ShillServiceClient
> client
);
220 void SetShillProfileClient(scoped_ptr
<ShillProfileClient
> client
);
221 void SetGsmSMSClient(scoped_ptr
<GsmSMSClient
> client
);
222 void SetImageBurnerClient(scoped_ptr
<ImageBurnerClient
> client
);
223 void SetIntrospectableClient(scoped_ptr
<IntrospectableClient
> client
);
224 void SetModemMessagingClient(scoped_ptr
<ModemMessagingClient
> client
);
225 void SetNfcAdapterClient(scoped_ptr
<NfcAdapterClient
> client
);
226 void SetNfcDeviceClient(scoped_ptr
<NfcDeviceClient
> client
);
227 void SetNfcManagerClient(scoped_ptr
<NfcManagerClient
> client
);
228 void SetNfcRecordClient(scoped_ptr
<NfcRecordClient
> client
);
229 void SetNfcTagClient(scoped_ptr
<NfcTagClient
> client
);
230 void SetPermissionBrokerClient(scoped_ptr
<PermissionBrokerClient
> client
);
231 void SetPowerManagerClient(scoped_ptr
<PowerManagerClient
> client
);
232 void SetSessionManagerClient(scoped_ptr
<SessionManagerClient
> client
);
233 void SetSMSClient(scoped_ptr
<SMSClient
> client
);
234 void SetSystemClockClient(scoped_ptr
<SystemClockClient
> client
);
235 void SetUpdateEngineClient(scoped_ptr
<UpdateEngineClient
> client
);
238 friend class DBusThreadManager
;
240 DBusThreadManagerSetter();
242 DISALLOW_COPY_AND_ASSIGN(DBusThreadManagerSetter
);
245 } // namespace chromeos
247 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_