Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 5 | #include "shill/dbus_control.h" |
| 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 9 | #include <dbus-c++/glib-integration.h> |
| 10 | #include <dbus-c++/util.h> |
| 11 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 12 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 13 | #include "shill/ipconfig_dbus_adaptor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 14 | #include "shill/logging.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 15 | #include "shill/manager_dbus_adaptor.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 16 | #include "shill/profile_dbus_adaptor.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 17 | #include "shill/rpc_task_dbus_adaptor.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 18 | #include "shill/service_dbus_adaptor.h" |
Prabhu Kaliamoorthi | 127b341 | 2014-10-16 13:00:25 +0200 | [diff] [blame^] | 19 | #include "shill/third_party_vpn_dbus_adaptor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 22 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 23 | DBusControl::DBusControl() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 25 | DBusControl::~DBusControl() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 27 | template <typename Object, typename AdaptorInterface, typename Adaptor> |
| 28 | AdaptorInterface *DBusControl::CreateAdaptor(Object *object) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 29 | AdaptorInterface *adaptor = nullptr; |
Wade Guthrie | e2e2924 | 2012-05-08 08:58:56 -0700 | [diff] [blame] | 30 | try { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 31 | adaptor = new Adaptor(connection_.get(), object); |
| 32 | } catch(const DBus::ErrorObjectPathInUse &error) { |
| 33 | LOG(FATAL) << error.message() << " (object path in use)"; |
| 34 | } catch(const DBus::ErrorNoMemory &error) { |
| 35 | LOG(FATAL) << error.message() << " (no memory)"; |
Wade Guthrie | e2e2924 | 2012-05-08 08:58:56 -0700 | [diff] [blame] | 36 | } catch(const DBus::Error &error) { |
Ben Chan | 7fe43b8 | 2014-03-06 15:36:33 -0800 | [diff] [blame] | 37 | LOG(FATAL) << error.message(); |
Wade Guthrie | e2e2924 | 2012-05-08 08:58:56 -0700 | [diff] [blame] | 38 | } |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 39 | return adaptor; |
| 40 | } |
| 41 | |
| 42 | DeviceAdaptorInterface *DBusControl::CreateDeviceAdaptor(Device *device) { |
| 43 | return CreateAdaptor<Device, DeviceAdaptorInterface, DeviceDBusAdaptor>( |
| 44 | device); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 47 | IPConfigAdaptorInterface *DBusControl::CreateIPConfigAdaptor(IPConfig *config) { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 48 | return CreateAdaptor<IPConfig, IPConfigAdaptorInterface, IPConfigDBusAdaptor>( |
| 49 | config); |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | ManagerAdaptorInterface *DBusControl::CreateManagerAdaptor(Manager *manager) { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 53 | return CreateAdaptor<Manager, ManagerAdaptorInterface, ManagerDBusAdaptor>( |
| 54 | manager); |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 57 | ProfileAdaptorInterface *DBusControl::CreateProfileAdaptor(Profile *profile) { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 58 | return CreateAdaptor<Profile, ProfileAdaptorInterface, ProfileDBusAdaptor>( |
| 59 | profile); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 62 | RPCTaskAdaptorInterface *DBusControl::CreateRPCTaskAdaptor(RPCTask *task) { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 63 | return CreateAdaptor<RPCTask, RPCTaskAdaptorInterface, RPCTaskDBusAdaptor>( |
| 64 | task); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 67 | ServiceAdaptorInterface *DBusControl::CreateServiceAdaptor(Service *service) { |
Ben Chan | 3d0e8d2 | 2014-07-10 23:40:47 -0700 | [diff] [blame] | 68 | return CreateAdaptor<Service, ServiceAdaptorInterface, ServiceDBusAdaptor>( |
| 69 | service); |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Prabhu Kaliamoorthi | 127b341 | 2014-10-16 13:00:25 +0200 | [diff] [blame^] | 72 | #ifndef DISABLE_VPN |
| 73 | ThirdPartyVpnAdaptorInterface *DBusControl::CreateThirdPartyVpnAdaptor( |
| 74 | ThirdPartyVpnDriver *driver) { |
| 75 | return CreateAdaptor<ThirdPartyVpnDriver, ThirdPartyVpnAdaptorInterface, |
| 76 | ThirdPartyVpnAdaptor>(driver); |
| 77 | } |
| 78 | #endif |
| 79 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 80 | void DBusControl::Init() { |
| 81 | CHECK(!connection_.get()); |
| 82 | dispatcher_.reset(new(std::nothrow) DBus::Glib::BusDispatcher()); |
| 83 | CHECK(dispatcher_.get()) << "Failed to create a dbus-dispatcher"; |
| 84 | DBus::default_dispatcher = dispatcher_.get(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 85 | dispatcher_->attach(nullptr); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 86 | connection_.reset(new DBus::Connection(DBus::Connection::SystemBus())); |
mukesh agrawal | 4041a1c | 2014-04-15 14:49:59 -0700 | [diff] [blame] | 87 | if (!connection_->acquire_name(SHILL_INTERFACE)) { |
| 88 | LOG(FATAL) << "Failed to acquire D-Bus name " << SHILL_INTERFACE << ". " |
| 89 | << "Is another shill running?"; |
| 90 | } |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 91 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 92 | |
| 93 | } // namespace shill |