| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | |
| 6 | |
| 7 #include "base/threading/thread.h" | |
| 8 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" | |
| 9 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h" | |
| 10 #include "chrome/browser/chromeos/dbus/bluetooth_input_client.h" | |
| 11 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" | |
| 12 #include "chrome/browser/chromeos/dbus/bluetooth_node_client.h" | |
| 13 #include "chrome/browser/chromeos/dbus/cashew_client.h" | |
| 14 #include "chrome/browser/chromeos/dbus/cros_disks_client.h" | |
| 15 #include "chrome/browser/chromeos/dbus/cryptohome_client.h" | |
| 16 #include "chrome/browser/chromeos/dbus/image_burner_client.h" | |
| 17 #include "chrome/browser/chromeos/dbus/introspectable_client.h" | |
| 18 #include "chrome/browser/chromeos/dbus/power_manager_client.h" | |
| 19 #include "chrome/browser/chromeos/dbus/session_manager_client.h" | |
| 20 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" | |
| 21 #include "chrome/browser/chromeos/dbus/update_engine_client.h" | |
| 22 #include "dbus/bus.h" | |
| 23 | |
| 24 namespace chromeos { | |
| 25 | |
| 26 static DBusThreadManager* g_dbus_thread_manager = NULL; | |
| 27 | |
| 28 // The DBusThreadManager implementation used in production. | |
| 29 class DBusThreadManagerImpl : public DBusThreadManager { | |
| 30 public: | |
| 31 DBusThreadManagerImpl() { | |
| 32 // Create the D-Bus thread. | |
| 33 base::Thread::Options thread_options; | |
| 34 thread_options.message_loop_type = MessageLoop::TYPE_IO; | |
| 35 dbus_thread_.reset(new base::Thread("D-Bus thread")); | |
| 36 dbus_thread_->StartWithOptions(thread_options); | |
| 37 | |
| 38 // Create the connection to the system bus. | |
| 39 dbus::Bus::Options system_bus_options; | |
| 40 system_bus_options.bus_type = dbus::Bus::SYSTEM; | |
| 41 system_bus_options.connection_type = dbus::Bus::PRIVATE; | |
| 42 system_bus_options.dbus_thread_message_loop_proxy = | |
| 43 dbus_thread_->message_loop_proxy(); | |
| 44 system_bus_ = new dbus::Bus(system_bus_options); | |
| 45 | |
| 46 // Create the bluetooth clients. | |
| 47 bluetooth_manager_client_.reset(BluetoothManagerClient::Create( | |
| 48 system_bus_.get())); | |
| 49 bluetooth_adapter_client_.reset(BluetoothAdapterClient::Create( | |
| 50 system_bus_.get(), bluetooth_manager_client_.get())); | |
| 51 bluetooth_device_client_.reset(BluetoothDeviceClient::Create( | |
| 52 system_bus_.get(), bluetooth_adapter_client_.get())); | |
| 53 bluetooth_input_client_.reset(BluetoothInputClient::Create( | |
| 54 system_bus_.get(), bluetooth_adapter_client_.get())); | |
| 55 bluetooth_node_client_.reset(BluetoothNodeClient::Create( | |
| 56 system_bus_.get(), bluetooth_device_client_.get())); | |
| 57 // Create the Cashew client. | |
| 58 cashew_client_.reset(CashewClient::Create(system_bus_.get())); | |
| 59 // Create the cros-disks client. | |
| 60 cros_disks_client_.reset( | |
| 61 CrosDisksClient::Create(system_bus_.get())); | |
| 62 // Create the Cryptohome client. | |
| 63 cryptohome_client_.reset( | |
| 64 CryptohomeClient::Create(system_bus_.get())); | |
| 65 // Create the image burner client. | |
| 66 image_burner_client_.reset(ImageBurnerClient::Create(system_bus_.get())); | |
| 67 // Create the introspectable object client. | |
| 68 introspectable_client_.reset( | |
| 69 IntrospectableClient::Create(system_bus_.get())); | |
| 70 // Create the power manager client. | |
| 71 power_manager_client_.reset(PowerManagerClient::Create(system_bus_.get())); | |
| 72 // Create the session manager client. | |
| 73 session_manager_client_.reset( | |
| 74 SessionManagerClient::Create(system_bus_.get())); | |
| 75 // Create the speech synthesizer client. | |
| 76 speech_synthesizer_client_.reset( | |
| 77 SpeechSynthesizerClient::Create(system_bus_.get())); | |
| 78 // Create the update engine client. | |
| 79 update_engine_client_.reset( | |
| 80 UpdateEngineClient::Create(system_bus_.get())); | |
| 81 } | |
| 82 | |
| 83 virtual ~DBusThreadManagerImpl() { | |
| 84 // Shut down the bus. During the browser shutdown, it's ok to shut down | |
| 85 // the bus synchronously. | |
| 86 system_bus_->ShutdownOnDBusThreadAndBlock(); | |
| 87 | |
| 88 // Stop the D-Bus thread. | |
| 89 dbus_thread_->Stop(); | |
| 90 } | |
| 91 | |
| 92 // DBusThreadManager override. | |
| 93 virtual dbus::Bus* GetSystemBus() OVERRIDE { | |
| 94 return system_bus_.get(); | |
| 95 } | |
| 96 | |
| 97 // DBusThreadManager override. | |
| 98 virtual BluetoothAdapterClient* GetBluetoothAdapterClient() OVERRIDE { | |
| 99 return bluetooth_adapter_client_.get(); | |
| 100 } | |
| 101 | |
| 102 // DBusThreadManager override. | |
| 103 virtual BluetoothDeviceClient* GetBluetoothDeviceClient() OVERRIDE { | |
| 104 return bluetooth_device_client_.get(); | |
| 105 } | |
| 106 | |
| 107 // DBusThreadManager override. | |
| 108 virtual BluetoothInputClient* GetBluetoothInputClient() OVERRIDE { | |
| 109 return bluetooth_input_client_.get(); | |
| 110 } | |
| 111 | |
| 112 // DBusThreadManager override. | |
| 113 virtual BluetoothManagerClient* GetBluetoothManagerClient() OVERRIDE { | |
| 114 return bluetooth_manager_client_.get(); | |
| 115 } | |
| 116 | |
| 117 // DBusThreadManager override. | |
| 118 virtual BluetoothNodeClient* GetBluetoothNodeClient() OVERRIDE { | |
| 119 return bluetooth_node_client_.get(); | |
| 120 } | |
| 121 | |
| 122 // DBusThreadManager override. | |
| 123 virtual CashewClient* GetCashewClient() OVERRIDE { | |
| 124 return cashew_client_.get(); | |
| 125 } | |
| 126 | |
| 127 // DBusThreadManager override. | |
| 128 virtual CrosDisksClient* GetCrosDisksClient() OVERRIDE { | |
| 129 return cros_disks_client_.get(); | |
| 130 } | |
| 131 | |
| 132 // DBusThreadManager override. | |
| 133 virtual CryptohomeClient* GetCryptohomeClient() OVERRIDE { | |
| 134 return cryptohome_client_.get(); | |
| 135 } | |
| 136 | |
| 137 // DBusThreadManager override. | |
| 138 virtual ImageBurnerClient* GetImageBurnerClient() OVERRIDE { | |
| 139 return image_burner_client_.get(); | |
| 140 } | |
| 141 | |
| 142 // DBusThreadManager override. | |
| 143 virtual IntrospectableClient* GetIntrospectableClient() OVERRIDE { | |
| 144 return introspectable_client_.get(); | |
| 145 } | |
| 146 | |
| 147 // DBusThreadManager override. | |
| 148 virtual PowerManagerClient* GetPowerManagerClient() OVERRIDE { | |
| 149 return power_manager_client_.get(); | |
| 150 } | |
| 151 | |
| 152 // DBusThreadManager override. | |
| 153 virtual SessionManagerClient* GetSessionManagerClient() OVERRIDE { | |
| 154 return session_manager_client_.get(); | |
| 155 } | |
| 156 | |
| 157 // DBusThreadManager override. | |
| 158 virtual SpeechSynthesizerClient* GetSpeechSynthesizerClient() OVERRIDE { | |
| 159 return speech_synthesizer_client_.get(); | |
| 160 } | |
| 161 | |
| 162 // DBusThreadManager override. | |
| 163 virtual UpdateEngineClient* GetUpdateEngineClient() OVERRIDE { | |
| 164 return update_engine_client_.get(); | |
| 165 } | |
| 166 | |
| 167 scoped_ptr<base::Thread> dbus_thread_; | |
| 168 scoped_refptr<dbus::Bus> system_bus_; | |
| 169 scoped_ptr<BluetoothAdapterClient> bluetooth_adapter_client_; | |
| 170 scoped_ptr<BluetoothDeviceClient> bluetooth_device_client_; | |
| 171 scoped_ptr<BluetoothInputClient> bluetooth_input_client_; | |
| 172 scoped_ptr<BluetoothManagerClient> bluetooth_manager_client_; | |
| 173 scoped_ptr<BluetoothNodeClient> bluetooth_node_client_; | |
| 174 scoped_ptr<CashewClient> cashew_client_; | |
| 175 scoped_ptr<CrosDisksClient> cros_disks_client_; | |
| 176 scoped_ptr<CryptohomeClient> cryptohome_client_; | |
| 177 scoped_ptr<ImageBurnerClient> image_burner_client_; | |
| 178 scoped_ptr<IntrospectableClient> introspectable_client_; | |
| 179 scoped_ptr<PowerManagerClient> power_manager_client_; | |
| 180 scoped_ptr<SessionManagerClient> session_manager_client_; | |
| 181 scoped_ptr<SpeechSynthesizerClient> speech_synthesizer_client_; | |
| 182 scoped_ptr<UpdateEngineClient> update_engine_client_; | |
| 183 }; | |
| 184 | |
| 185 // static | |
| 186 void DBusThreadManager::Initialize() { | |
| 187 if (g_dbus_thread_manager) { | |
| 188 LOG(WARNING) << "DBusThreadManager was already initialized"; | |
| 189 return; | |
| 190 } | |
| 191 g_dbus_thread_manager = new DBusThreadManagerImpl; | |
| 192 VLOG(1) << "DBusThreadManager initialized"; | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 void DBusThreadManager::InitializeForTesting( | |
| 197 DBusThreadManager* dbus_thread_manager) { | |
| 198 if (g_dbus_thread_manager) { | |
| 199 LOG(WARNING) << "DBusThreadManager was already initialized"; | |
| 200 return; | |
| 201 } | |
| 202 g_dbus_thread_manager = dbus_thread_manager; | |
| 203 VLOG(1) << "DBusThreadManager initialized"; | |
| 204 } | |
| 205 | |
| 206 // static | |
| 207 void DBusThreadManager::Shutdown() { | |
| 208 if (!g_dbus_thread_manager) { | |
| 209 // TODO(satorux): Make it a DCHECK() once it's ready. | |
| 210 LOG(WARNING) << "DBusThreadManager::Shutdown() called with NULL manager"; | |
| 211 return; | |
| 212 } | |
| 213 delete g_dbus_thread_manager; | |
| 214 g_dbus_thread_manager = NULL; | |
| 215 VLOG(1) << "DBusThreadManager Shutdown completed"; | |
| 216 } | |
| 217 | |
| 218 DBusThreadManager::DBusThreadManager() { | |
| 219 } | |
| 220 | |
| 221 DBusThreadManager::~DBusThreadManager() { | |
| 222 } | |
| 223 | |
| 224 // static | |
| 225 DBusThreadManager* DBusThreadManager::Get() { | |
| 226 CHECK(g_dbus_thread_manager) | |
| 227 << "DBusThreadManager::Get() called before Initialize()"; | |
| 228 return g_dbus_thread_manager; | |
| 229 } | |
| 230 | |
| 231 } // namespace chromeos | |
| OLD | NEW |