blob: 034f2512b1f861904744c1e776da2536c8853dad [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS 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
Paul Stewart75897df2011-04-27 09:05:53 -07005#include <string>
6
Chris Masoned7732e42011-05-20 11:08:56 -07007#include <base/logging.h>
8#include <dbus-c++/glib-integration.h>
9#include <dbus-c++/util.h>
10
Paul Stewart75897df2011-04-27 09:05:53 -070011#include "shill/dbus_control.h"
Chris Masoned7732e42011-05-20 11:08:56 -070012#include "shill/device_dbus_adaptor.h"
13#include "shill/manager_dbus_adaptor.h"
14#include "shill/service_dbus_adaptor.h"
Paul Stewart75897df2011-04-27 09:05:53 -070015
16namespace shill {
Chris Masoned7732e42011-05-20 11:08:56 -070017DBusControl::DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070018
Chris Masoned7732e42011-05-20 11:08:56 -070019DBusControl::~DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070020
Chris Masone413a3192011-05-09 17:10:05 -070021ManagerAdaptorInterface *DBusControl::CreateManagerAdaptor(Manager *manager) {
Paul Stewart28fbfdf2011-05-31 09:37:38 -070022 EnsureConnection();
23 connection_->request_name(ManagerDBusAdaptor::kInterfaceName);
24 return new(std::nothrow) ManagerDBusAdaptor(*(connection_.get()), manager);
Paul Stewart75897df2011-04-27 09:05:53 -070025}
26
Chris Masone413a3192011-05-09 17:10:05 -070027ServiceAdaptorInterface *DBusControl::CreateServiceAdaptor(Service *service) {
Paul Stewart28fbfdf2011-05-31 09:37:38 -070028 EnsureConnection();
29 connection_->request_name(ServiceDBusAdaptor::kInterfaceName);
30 return new(std::nothrow) ServiceDBusAdaptor(*(connection_.get()), service);
Paul Stewart75897df2011-04-27 09:05:53 -070031}
32
Chris Masone413a3192011-05-09 17:10:05 -070033DeviceAdaptorInterface *DBusControl::CreateDeviceAdaptor(Device *device) {
Paul Stewart28fbfdf2011-05-31 09:37:38 -070034 EnsureConnection();
35 connection_->request_name(DeviceDBusAdaptor::kInterfaceName);
36 return new(std::nothrow) DeviceDBusAdaptor(*(connection_.get()), device);
Paul Stewart75897df2011-04-27 09:05:53 -070037}
38
Paul Stewart28fbfdf2011-05-31 09:37:38 -070039void DBusControl::EnsureConnection() {
40 if (!connection_.get()) {
Chris Masoned7732e42011-05-20 11:08:56 -070041 dispatcher_.reset(new(std::nothrow) DBus::Glib::BusDispatcher());
42 CHECK(dispatcher_.get()) << "Failed to create a dbus-dispatcher";
43 DBus::default_dispatcher = dispatcher_.get();
44 dispatcher_->attach(NULL);
Paul Stewart28fbfdf2011-05-31 09:37:38 -070045 connection_.reset(new DBus::Connection(DBus::Connection::SystemBus()));
Chris Masoned7732e42011-05-20 11:08:56 -070046 }
47}
Paul Stewart75897df2011-04-27 09:05:53 -070048
49} // namespace shill