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