blob: adffe9b746a97bc3da48503b256e0bf45cb3941a [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/dbus/dbus_thread_manager_linux.h"
#include "base/threading/thread.h"
#include "dbus/bus.h"
namespace bluez {
static DBusThreadManagerLinux* g_linux_dbus_manager = NULL;
DBusThreadManagerLinux::DBusThreadManagerLinux() {
base::Thread::Options thread_options;
thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
dbus_thread_.reset(new base::Thread("D-Bus thread"));
// Create the connection to the system bus.
dbus::Bus::Options system_bus_options;
system_bus_options.bus_type = dbus::Bus::SYSTEM;
system_bus_options.connection_type = dbus::Bus::PRIVATE;
system_bus_options.dbus_task_runner = dbus_thread_->task_runner();
system_bus_ = new dbus::Bus(system_bus_options);
DBusThreadManagerLinux::~DBusThreadManagerLinux() {
// Shut down the bus. During the browser shutdown, it's ok to shut down
// the bus synchronously.
if (system_bus_.get())
// Stop the D-Bus thread.
if (dbus_thread_)
if (!g_linux_dbus_manager)
return; // Called form Shutdown() or local test instance.
// There should never be both a global instance and a local instance.
CHECK(this == g_linux_dbus_manager);
dbus::Bus* DBusThreadManagerLinux::GetSystemBus() {
return system_bus_.get();
// static
void DBusThreadManagerLinux::Initialize() {
g_linux_dbus_manager = new DBusThreadManagerLinux();
// static
void DBusThreadManagerLinux::Shutdown() {
// Ensure that we only shutdown LinuxDBusManager once.
DBusThreadManagerLinux* dbus_thread_manager = g_linux_dbus_manager;
g_linux_dbus_manager = NULL;
delete dbus_thread_manager;
VLOG(1) << "LinuxDBusManager Shutdown completed";
// static
DBusThreadManagerLinux* DBusThreadManagerLinux::Get() {
<< "LinuxDBusManager::Get() called before Initialize()";
return g_linux_dbus_manager;
} // namespace bluez