dbus_thread_manager.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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
15namespace base {
16class Thread;
17};
18
19namespace dbus {
20class Bus;
21class ObjectPath;
22};
23
24namespace chromeos {
25
26class DBusThreadManagerObserver;
27
28// Style Note: Clients are sorted by names.
29class BluetoothAdapterClient;
30class BluetoothAgentManagerClient;
31class BluetoothDeviceClient;
32class BluetoothInputClient;
33class BluetoothProfileManagerClient;
34class CrasAudioClient;
35class CrosDisksClient;
36class CryptohomeClient;
37class DebugDaemonClient;
38class GsmSMSClient;
39class IBusClient;
40class IBusConfigClient;
41class IBusEngineFactoryService;
42class IBusEngineService;
43class IBusInputContextClient;
44class IBusPanelService;
45class ImageBurnerClient;
46class IntrospectableClient;
47class ModemMessagingClient;
48class PermissionBrokerClient;
49class PowerManagerClient;
50class PowerPolicyController;
51class SMSClient;
52class SessionManagerClient;
53class ShillDeviceClient;
54class ShillIPConfigClient;
55class ShillManagerClient;
56class ShillProfileClient;
57class ShillServiceClient;
58class SystemClockClient;
59class 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//
79class 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