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 BluetoothGattCharacteristicClient;
33 class BluetoothGattDescriptorClient;
34 class BluetoothGattManagerClient;
35 class BluetoothGattServiceClient;
36 class BluetoothInputClient;
37 class BluetoothProfileManagerClient;
38 class CrasAudioClient;
39 class CrosDisksClient;
40 class CryptohomeClient;
42 class DebugDaemonClient;
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 // Sets an alternative DBusThreadManager such as MockDBusThreadManager
92 // to be used in |Initialize()| for testing. Tests that call
93 // DBusThreadManager::Initialize() (such as browser_tests and
94 // interactive_ui_tests) should use this instead of calling
95 // |InitiailzeForTesting|. The injected object will be owned by the
96 // internal pointer and deleted by Shutdown().
97 static void SetInstanceForTesting(DBusThreadManager* dbus_thread_manager);
99 // Similar to Initialize(), but injects an alternative
100 // DBusThreadManager using SetInstanceForTest first. The injected
101 // object will be owned by the internal pointer and deleted by
102 // Shutdown(). Does not create any Fake client implementations.
103 static void InitializeForTesting(DBusThreadManager* dbus_thread_manager);
105 // Initialize with stub implementations for tests, creating a complete set
106 // of fake/stub client implementations. Also initializes a default set of
107 // fake Shill devices and services, customizable with switches::kShillStub.
108 static void InitializeWithStub();
110 // Returns true if DBusThreadManager has been initialized. Call this to
111 // avoid initializing + shutting down DBusThreadManager more than once.
112 static bool IsInitialized();
114 // Destroys the global instance.
115 static void Shutdown();
117 // Gets the global instance. Initialize() must be called first.
118 static DBusThreadManager* Get();
120 // Adds or removes an observer.
121 virtual void AddObserver(DBusThreadManagerObserver* observer) = 0;
122 virtual void RemoveObserver(DBusThreadManagerObserver* observer) = 0;
124 // Returns various D-Bus bus instances, owned by DBusThreadManager.
125 virtual dbus::Bus* GetSystemBus() = 0;
127 // All returned objects are owned by DBusThreadManager. Do not cache these
128 // pointers and use them after DBusThreadManager has been shut down.
129 virtual BluetoothAdapterClient* GetBluetoothAdapterClient() = 0;
130 virtual BluetoothAgentManagerClient* GetBluetoothAgentManagerClient() = 0;
131 virtual BluetoothDeviceClient* GetBluetoothDeviceClient() = 0;
132 virtual BluetoothGattCharacteristicClient*
133 GetBluetoothGattCharacteristicClient() = 0;
134 virtual BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient() = 0;
135 virtual BluetoothGattManagerClient* GetBluetoothGattManagerClient() = 0;
136 virtual BluetoothGattServiceClient* GetBluetoothGattServiceClient() = 0;
137 virtual BluetoothInputClient* GetBluetoothInputClient() = 0;
138 virtual BluetoothProfileManagerClient* GetBluetoothProfileManagerClient() = 0;
139 virtual CrasAudioClient* GetCrasAudioClient() = 0;
140 virtual CrosDisksClient* GetCrosDisksClient() = 0;
141 virtual CryptohomeClient* GetCryptohomeClient() = 0;
142 virtual DebugDaemonClient* GetDebugDaemonClient() = 0;
143 virtual GsmSMSClient* GetGsmSMSClient() = 0;
144 virtual ImageBurnerClient* GetImageBurnerClient() = 0;
145 virtual IntrospectableClient* GetIntrospectableClient() = 0;
146 virtual LorgnetteManagerClient* GetLorgnetteManagerClient() = 0;
147 virtual ModemMessagingClient* GetModemMessagingClient() = 0;
148 virtual NfcAdapterClient* GetNfcAdapterClient() = 0;
149 virtual NfcDeviceClient* GetNfcDeviceClient() = 0;
150 virtual NfcManagerClient* GetNfcManagerClient() = 0;
151 virtual NfcRecordClient* GetNfcRecordClient() = 0;
152 virtual NfcTagClient* GetNfcTagClient() = 0;
153 virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0;
154 virtual PowerManagerClient* GetPowerManagerClient() = 0;
155 virtual PowerPolicyController* GetPowerPolicyController() = 0;
156 virtual SessionManagerClient* GetSessionManagerClient() = 0;
157 virtual ShillDeviceClient* GetShillDeviceClient() = 0;
158 virtual ShillIPConfigClient* GetShillIPConfigClient() = 0;
159 virtual ShillManagerClient* GetShillManagerClient() = 0;
160 virtual ShillServiceClient* GetShillServiceClient() = 0;
161 virtual ShillProfileClient* GetShillProfileClient() = 0;
162 virtual SMSClient* GetSMSClient() = 0;
163 virtual SystemClockClient* GetSystemClockClient() = 0;
164 virtual UpdateEngineClient* GetUpdateEngineClient() = 0;
166 virtual ~DBusThreadManager();
172 // InitializeClients is called after g_dbus_thread_manager is set.
173 // NOTE: Clients that access other clients in their Init() must be
174 // initialized in the correct order.
175 static void InitializeClients();
177 // Initializes |client| with the |system_bus_|.
178 static void InitClient(DBusClient* client);
180 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager);
183 } // namespace chromeos
185 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_