Home | History | Annotate | Download | only in dbus
      1 //
      2 // Copyright (C) 2015 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "apmanager/dbus/dbus_control.h"
     18 
     19 #include "apmanager/dbus/config_dbus_adaptor.h"
     20 #include "apmanager/dbus/device_dbus_adaptor.h"
     21 #include "apmanager/dbus/manager_dbus_adaptor.h"
     22 #include "apmanager/dbus/service_dbus_adaptor.h"
     23 #include "apmanager/dbus/shill_dbus_proxy.h"
     24 #include "apmanager/manager.h"
     25 
     26 #if !defined(__ANDROID__)
     27 #include "apmanager/dbus/permission_broker_dbus_proxy.h"
     28 #else
     29 #include "apmanager/dbus/firewalld_dbus_proxy.h"
     30 #endif  //__ANDROID__
     31 
     32 using brillo::dbus_utils::AsyncEventSequencer;
     33 using brillo::dbus_utils::ExportedObjectManager;
     34 
     35 namespace apmanager {
     36 
     37 namespace {
     38 const char kServiceName[] = "org.chromium.apmanager";
     39 const char kServicePath[] = "/org/chromium/apmanager";
     40 }  // namespace
     41 
     42 DBusControl::DBusControl() {}
     43 
     44 DBusControl::~DBusControl() {}
     45 
     46 void DBusControl::Init() {
     47   // Setup bus connection.
     48   dbus::Bus::Options options;
     49   options.bus_type = dbus::Bus::SYSTEM;
     50   bus_ = new dbus::Bus(options);
     51   CHECK(bus_->Connect());
     52 
     53   // Create and register ObjectManager.
     54   scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer());
     55   object_manager_.reset(
     56       new ExportedObjectManager(bus_, dbus::ObjectPath(kServicePath)));
     57   object_manager_->RegisterAsync(
     58       sequencer->GetHandler("ObjectManager.RegisterAsync() failed.", true));
     59 
     60   // Create and register Manager.
     61   manager_.reset(new Manager(this));
     62   manager_->RegisterAsync(
     63       sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
     64 
     65   // Take over the service ownership once the objects registration is completed.
     66   sequencer->OnAllTasksCompletedCall({
     67     base::Bind(&DBusControl::OnObjectRegistrationCompleted,
     68                base::Unretained(this))
     69   });
     70 }
     71 
     72 void DBusControl::Shutdown() {
     73   manager_.reset();
     74   object_manager_.reset();
     75   if (bus_) {
     76     bus_->ShutdownAndBlock();
     77   }
     78 }
     79 
     80 void DBusControl::OnObjectRegistrationCompleted(bool registration_success) {
     81   // Success should always be true since we've said that failures are fatal.
     82   CHECK(registration_success) << "Init of one or more objects has failed.";
     83   CHECK(bus_->RequestOwnershipAndBlock(kServiceName,
     84                                        dbus::Bus::REQUIRE_PRIMARY))
     85       << "Unable to take ownership of " << kServiceName;
     86 
     87   // D-Bus service is ready, now we can start the Manager.
     88   manager_->Start();
     89 }
     90 
     91 std::unique_ptr<ConfigAdaptorInterface> DBusControl::CreateConfigAdaptor(
     92     Config* config, int service_identifier) {
     93   return std::unique_ptr<ConfigAdaptorInterface>(
     94       new ConfigDBusAdaptor(
     95           bus_, object_manager_.get(), config, service_identifier));
     96 }
     97 
     98 std::unique_ptr<DeviceAdaptorInterface> DBusControl::CreateDeviceAdaptor(
     99     Device* device) {
    100   return std::unique_ptr<DeviceAdaptorInterface>(
    101       new DeviceDBusAdaptor(bus_, object_manager_.get(), device));
    102 }
    103 
    104 std::unique_ptr<ManagerAdaptorInterface> DBusControl::CreateManagerAdaptor(
    105     Manager* manager) {
    106   return std::unique_ptr<ManagerAdaptorInterface>(
    107       new ManagerDBusAdaptor(bus_, object_manager_.get(), manager));
    108 }
    109 
    110 std::unique_ptr<ServiceAdaptorInterface> DBusControl::CreateServiceAdaptor(
    111     Service* service) {
    112   return std::unique_ptr<ServiceAdaptorInterface>(
    113       new ServiceDBusAdaptor(bus_, object_manager_.get(), service));
    114 }
    115 
    116 std::unique_ptr<FirewallProxyInterface> DBusControl::CreateFirewallProxy(
    117     const base::Closure& service_appeared_callback,
    118     const base::Closure& service_vanished_callback) {
    119 #if !defined(__ANDROID__)
    120   return std::unique_ptr<FirewallProxyInterface>(
    121       new PermissionBrokerDBusProxy(
    122           bus_, service_appeared_callback, service_vanished_callback));
    123 #else
    124   return std::unique_ptr<FirewallProxyInterface>(
    125       new FirewalldDBusProxy(
    126           bus_, service_appeared_callback, service_vanished_callback));
    127 #endif  // __ANDROID__
    128 }
    129 
    130 std::unique_ptr<ShillProxyInterface> DBusControl::CreateShillProxy(
    131     const base::Closure& service_appeared_callback,
    132     const base::Closure& service_vanished_callback) {
    133   return std::unique_ptr<ShillProxyInterface>(
    134       new ShillDBusProxy(
    135           bus_, service_appeared_callback, service_vanished_callback));
    136 }
    137 
    138 }  // namespace apmanager
    139