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"
26 class DBusThreadManagerObserver
;
28 // Style Note: Clients are sorted by names.
29 class BluetoothAdapterClient
;
30 class BluetoothAgentManagerClient
;
31 class BluetoothDeviceClient
;
32 class BluetoothInputClient
;
33 class BluetoothProfileManagerClient
;
34 class CrasAudioClient
;
35 class CrosDisksClient
;
36 class CryptohomeClient
;
38 class DebugDaemonClient
;
40 class IBusEngineFactoryService
;
41 class IBusEngineService
;
42 class ImageBurnerClient
;
43 class IntrospectableClient
;
44 class ModemMessagingClient
;
45 class NfcAdapterClient
;
46 class NfcDeviceClient
;
47 class NfcManagerClient
;
48 class NfcRecordClient
;
50 class PermissionBrokerClient
;
51 class PowerManagerClient
;
52 class PowerPolicyController
;
53 class SessionManagerClient
;
54 class ShillDeviceClient
;
55 class ShillIPConfigClient
;
56 class ShillManagerClient
;
57 class ShillProfileClient
;
58 class ShillServiceClient
;
60 class SystemClockClient
;
61 class UpdateEngineClient
;
63 // DBusThreadManager manages the D-Bus thread, the thread dedicated to
64 // handling asynchronous D-Bus operations.
66 // This class also manages D-Bus connections and D-Bus clients, which
67 // depend on the D-Bus thread to ensure the right order of shutdowns for
68 // the D-Bus thread, the D-Bus connections, and the D-Bus clients.
70 // CALLBACKS IN D-BUS CLIENTS:
72 // D-Bus clients managed by DBusThreadManager are guaranteed to be deleted
73 // after the D-Bus thread so the clients don't need to worry if new
74 // incoming messages arrive from the D-Bus thread during shutdown of the
75 // clients. The UI message loop is not running during the shutdown hence
76 // the UI message loop won't post tasks to D-BUS clients during the
77 // shutdown. However, to be extra cautious, clients should use
78 // WeakPtrFactory when creating callbacks that run on UI thread. See
79 // session_manager_client.cc for examples.
81 class CHROMEOS_EXPORT DBusThreadManager
{
83 // Sets the global instance. Must be called before any calls to Get().
84 // We explicitly initialize and shut down the global object, rather than
85 // making it a Singleton, to ensure clean startup and shutdown.
86 static void Initialize();
88 // Similar to Initialize(), but can inject an alternative
89 // DBusThreadManager such as MockDBusThreadManager for testing.
90 // The injected object will be owned by the internal pointer and deleted
92 static void InitializeForTesting(DBusThreadManager
* dbus_thread_manager
);
94 // Initialize with stub implementations for tests based on stubs.
95 static void InitializeWithStub();
97 // Returns true if DBusThreadManager has been initialized. Call this to
98 // avoid initializing + shutting down DBusThreadManager more than once.
99 static bool IsInitialized();
101 // Destroys the global instance.
102 static void Shutdown();
104 // Gets the global instance. Initialize() must be called first.
105 static DBusThreadManager
* Get();
107 // Adds or removes an observer.
108 virtual void AddObserver(DBusThreadManagerObserver
* observer
) = 0;
109 virtual void RemoveObserver(DBusThreadManagerObserver
* observer
) = 0;
111 // Returns various D-Bus bus instances, owned by DBusThreadManager.
112 virtual dbus::Bus
* GetSystemBus() = 0;
114 // All returned objects are owned by DBusThreadManager. Do not cache these
115 // pointers and use them after DBusThreadManager has been shut down.
116 virtual BluetoothAdapterClient
* GetBluetoothAdapterClient() = 0;
117 virtual BluetoothAgentManagerClient
* GetBluetoothAgentManagerClient() = 0;
118 virtual BluetoothDeviceClient
* GetBluetoothDeviceClient() = 0;
119 virtual BluetoothInputClient
* GetBluetoothInputClient() = 0;
120 virtual BluetoothProfileManagerClient
* GetBluetoothProfileManagerClient() = 0;
121 virtual CrasAudioClient
* GetCrasAudioClient() = 0;
122 virtual CrosDisksClient
* GetCrosDisksClient() = 0;
123 virtual CryptohomeClient
* GetCryptohomeClient() = 0;
124 virtual DebugDaemonClient
* GetDebugDaemonClient() = 0;
125 virtual GsmSMSClient
* GetGsmSMSClient() = 0;
126 virtual ImageBurnerClient
* GetImageBurnerClient() = 0;
127 virtual IntrospectableClient
* GetIntrospectableClient() = 0;
128 virtual ModemMessagingClient
* GetModemMessagingClient() = 0;
129 virtual NfcAdapterClient
* GetNfcAdapterClient() = 0;
130 virtual NfcDeviceClient
* GetNfcDeviceClient() = 0;
131 virtual NfcManagerClient
* GetNfcManagerClient() = 0;
132 virtual NfcRecordClient
* GetNfcRecordClient() = 0;
133 virtual NfcTagClient
* GetNfcTagClient() = 0;
134 virtual PermissionBrokerClient
* GetPermissionBrokerClient() = 0;
135 virtual PowerManagerClient
* GetPowerManagerClient() = 0;
136 virtual PowerPolicyController
* GetPowerPolicyController() = 0;
137 virtual SessionManagerClient
* GetSessionManagerClient() = 0;
138 virtual ShillDeviceClient
* GetShillDeviceClient() = 0;
139 virtual ShillIPConfigClient
* GetShillIPConfigClient() = 0;
140 virtual ShillManagerClient
* GetShillManagerClient() = 0;
141 virtual ShillServiceClient
* GetShillServiceClient() = 0;
142 virtual ShillProfileClient
* GetShillProfileClient() = 0;
143 virtual SMSClient
* GetSMSClient() = 0;
144 virtual SystemClockClient
* GetSystemClockClient() = 0;
145 virtual UpdateEngineClient
* GetUpdateEngineClient() = 0;
147 virtual ~DBusThreadManager();
153 // InitializeClients is called after g_dbus_thread_manager is set.
154 // NOTE: Clients that access other clients in their Init() must be
155 // initialized in the correct order.
156 static void InitializeClients();
158 // Initializes |client| with the |system_bus_|.
159 static void InitClient(DBusClient
* client
);
161 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager
);
164 } // namespace chromeos
166 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_