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. 4 5 #ifndef CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 6 #define CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 7 8 #include <string> 9 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" 14 15 namespace base { 16 class Thread; 17 }; 18 19 namespace dbus { 20 class Bus; 21 class ObjectPath; 22 }; 23 24 namespace chromeos { 25 26 class DBusThreadManagerObserver; 27 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; 37 class DebugDaemonClient; 38 class GsmSMSClient; 39 class IBusClient; 40 class IBusConfigClient; 41 class IBusEngineFactoryService; 42 class IBusEngineService; 43 class IBusInputContextClient; 44 class IBusPanelService; 45 class ImageBurnerClient; 46 class IntrospectableClient; 47 class ModemMessagingClient; 48 class PermissionBrokerClient; 49 class PowerManagerClient; 50 class PowerPolicyController; 51 class SMSClient; 52 class SessionManagerClient; 53 class ShillDeviceClient; 54 class ShillIPConfigClient; 55 class ShillManagerClient; 56 class ShillProfileClient; 57 class ShillServiceClient; 58 class SystemClockClient; 59 class UpdateEngineClient; 60 61 // DBusThreadManager manages the D-Bus thread, the thread dedicated to 62 // handling asynchronous D-Bus operations. 63 // 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. 67 // 68 // CALLBACKS IN D-BUS CLIENTS: 69 // 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. 78 // 79 class CHROMEOS_EXPORT DBusThreadManager { 80 public: 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(); 85 86 // Similar to Initialize(), but can inject an alternative 87 // DBusThreadManager such as MockDBusThreadManager for testing. 88 // The injected object will be owned by the internal pointer and deleted 89 // by Shutdown(). 90 static void InitializeForTesting(DBusThreadManager* dbus_thread_manager); 91 92 // Initialize with stub implementations for tests based on stubs. 93 static void InitializeWithStub(); 94 95 // Returns true if DBusThreadManager has been initialized. Call this to 96 // avoid initializing + shutting down DBusThreadManager more than once. 97 static bool IsInitialized(); 98 99 // Destroys the global instance. 100 static void Shutdown(); 101 102 // Gets the global instance. Initialize() must be called first. 103 static DBusThreadManager* Get(); 104 105 // Adds or removes an observer. 106 virtual void AddObserver(DBusThreadManagerObserver* observer) = 0; 107 virtual void RemoveObserver(DBusThreadManagerObserver* observer) = 0; 108 109 // Creates new IBusBus instance to communicate with ibus-daemon with specified 110 // ibus address. |on_disconnected_callback| will be called when the connection 111 // with ibus-daemon is disconnected. Must be called before using ibus related 112 // clients. 113 // TODO(nona): Support shutdown to enable dynamical ibus-daemon shutdown. 114 virtual void InitIBusBus(const std::string& ibus_address, 115 const base::Closure& on_disconnected_callback) = 0; 116 117 // Returns various D-Bus bus instances, owned by DBusThreadManager. 118 virtual dbus::Bus* GetSystemBus() = 0; 119 virtual dbus::Bus* GetIBusBus() = 0; 120 121 // All returned objects are owned by DBusThreadManager. Do not cache these 122 // pointers and use them after DBusThreadManager has been shut down. 123 virtual BluetoothAdapterClient* GetBluetoothAdapterClient() = 0; 124 virtual BluetoothAgentManagerClient* GetBluetoothAgentManagerClient() = 0; 125 virtual BluetoothDeviceClient* GetBluetoothDeviceClient() = 0; 126 virtual BluetoothInputClient* GetBluetoothInputClient() = 0; 127 virtual BluetoothProfileManagerClient* GetBluetoothProfileManagerClient() = 0; 128 virtual CrasAudioClient* GetCrasAudioClient() = 0; 129 virtual CrosDisksClient* GetCrosDisksClient() = 0; 130 virtual CryptohomeClient* GetCryptohomeClient() = 0; 131 virtual DebugDaemonClient* GetDebugDaemonClient() = 0; 132 virtual GsmSMSClient* GetGsmSMSClient() = 0; 133 virtual IBusClient* GetIBusClient() = 0; 134 virtual IBusConfigClient* GetIBusConfigClient() = 0; 135 virtual IBusEngineFactoryService* GetIBusEngineFactoryService() = 0; 136 virtual IBusEngineService* GetIBusEngineService( 137 const dbus::ObjectPath& object_path) = 0; 138 virtual IBusInputContextClient* GetIBusInputContextClient() = 0; 139 virtual IBusPanelService* GetIBusPanelService() = 0; 140 virtual ImageBurnerClient* GetImageBurnerClient() = 0; 141 virtual IntrospectableClient* GetIntrospectableClient() = 0; 142 virtual ModemMessagingClient* GetModemMessagingClient() = 0; 143 virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0; 144 virtual PowerManagerClient* GetPowerManagerClient() = 0; 145 virtual PowerPolicyController* GetPowerPolicyController() = 0; 146 virtual SessionManagerClient* GetSessionManagerClient() = 0; 147 virtual ShillDeviceClient* GetShillDeviceClient() = 0; 148 virtual ShillIPConfigClient* GetShillIPConfigClient() = 0; 149 virtual ShillManagerClient* GetShillManagerClient() = 0; 150 virtual ShillServiceClient* GetShillServiceClient() = 0; 151 virtual ShillProfileClient* GetShillProfileClient() = 0; 152 virtual SMSClient* GetSMSClient() = 0; 153 virtual SystemClockClient* GetSystemClockClient() = 0; 154 virtual UpdateEngineClient* GetUpdateEngineClient() = 0; 155 156 // Removes the ibus engine services for |object_path|. 157 virtual void RemoveIBusEngineService(const dbus::ObjectPath& object_path) = 0; 158 159 virtual ~DBusThreadManager(); 160 161 protected: 162 DBusThreadManager(); 163 164 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager); 165 }; 166 167 } // namespace chromeos 168 169 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 170