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