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 "device/bluetooth/dbus/dbus_thread_manager_linux.h" |
| 6 |
| 7 #include "base/threading/thread.h" |
| 8 #include "dbus/bus.h" |
| 9 |
| 10 namespace bluez { |
| 11 |
| 12 static DBusThreadManagerLinux* g_linux_dbus_manager = NULL; |
| 13 |
| 14 DBusThreadManagerLinux::DBusThreadManagerLinux() { |
| 15 base::Thread::Options thread_options; |
| 16 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 17 dbus_thread_.reset(new base::Thread("D-Bus thread")); |
| 18 dbus_thread_->StartWithOptions(thread_options); |
| 19 |
| 20 // Create the connection to the system bus. |
| 21 dbus::Bus::Options system_bus_options; |
| 22 system_bus_options.bus_type = dbus::Bus::SYSTEM; |
| 23 system_bus_options.connection_type = dbus::Bus::PRIVATE; |
| 24 system_bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
| 25 system_bus_ = new dbus::Bus(system_bus_options); |
| 26 } |
| 27 |
| 28 DBusThreadManagerLinux::~DBusThreadManagerLinux() { |
| 29 // Shut down the bus. During the browser shutdown, it's ok to shut down |
| 30 // the bus synchronously. |
| 31 if (system_bus_.get()) |
| 32 system_bus_->ShutdownOnDBusThreadAndBlock(); |
| 33 |
| 34 // Stop the D-Bus thread. |
| 35 if (dbus_thread_) |
| 36 dbus_thread_->Stop(); |
| 37 |
| 38 if (!g_linux_dbus_manager) |
| 39 return; // Called form Shutdown() or local test instance. |
| 40 |
| 41 // There should never be both a global instance and a local instance. |
| 42 CHECK(this == g_linux_dbus_manager); |
| 43 } |
| 44 |
| 45 dbus::Bus* DBusThreadManagerLinux::GetSystemBus() { |
| 46 return system_bus_.get(); |
| 47 } |
| 48 |
| 49 // static |
| 50 void DBusThreadManagerLinux::Initialize() { |
| 51 CHECK(!g_linux_dbus_manager); |
| 52 g_linux_dbus_manager = new DBusThreadManagerLinux(); |
| 53 } |
| 54 |
| 55 // static |
| 56 void DBusThreadManagerLinux::Shutdown() { |
| 57 // Ensure that we only shutdown LinuxDBusManager once. |
| 58 CHECK(g_linux_dbus_manager); |
| 59 DBusThreadManagerLinux* dbus_thread_manager = g_linux_dbus_manager; |
| 60 g_linux_dbus_manager = NULL; |
| 61 delete dbus_thread_manager; |
| 62 VLOG(1) << "LinuxDBusManager Shutdown completed"; |
| 63 } |
| 64 |
| 65 // static |
| 66 DBusThreadManagerLinux* DBusThreadManagerLinux::Get() { |
| 67 CHECK(g_linux_dbus_manager) |
| 68 << "LinuxDBusManager::Get() called before Initialize()"; |
| 69 return g_linux_dbus_manager; |
| 70 } |
| 71 |
| 72 } // namespace bluez |
OLD | NEW |