Make DBus a singleton.
All the proxies need dbus to initialize, so we are passing dbus around,
to make it easier to compile dbus conditionally, this patch makes it a
singleton so that the proxies can get dbus on their own.
Test: mma
Bug: 28800946
Change-Id: Idf062c843aa34a431c2201bae5b895dc1d0ea787
diff --git a/Android.mk b/Android.mk
index 141b144..78cdbdf 100644
--- a/Android.mk
+++ b/Android.mk
@@ -310,6 +310,7 @@
connection_manager.cc \
connection_utils.cc \
daemon.cc \
+ dbus_connection.cc \
dbus_service.cc \
hardware_android.cc \
image_properties_android.cc \
diff --git a/daemon.cc b/daemon.cc
index 6b2d317..4155243 100644
--- a/daemon.cc
+++ b/daemon.cc
@@ -20,7 +20,6 @@
#include <base/bind.h>
#include <base/location.h>
-#include <base/time/time.h>
#if USE_WEAVE || USE_BINDER
#include <binderwrapper/binder_wrapper.h>
#endif // USE_WEAVE || USE_BINDER
@@ -31,12 +30,6 @@
#include "update_engine/daemon_state_android.h"
#endif // USE_OMAHA
-#if USE_DBUS
-namespace {
-const int kDBusSystemMaxWaitSeconds = 2 * 60;
-} // namespace
-#endif // USE_DBUS
-
namespace chromeos_update_engine {
int UpdateEngineDaemon::OnInit() {
@@ -53,28 +46,12 @@
binder_watcher_.Init();
#endif // USE_WEAVE || USE_BINDER
-#if USE_DBUS
- // We wait for the D-Bus connection for up two minutes to avoid re-spawning
- // the daemon too fast causing thrashing if dbus-daemon is not running.
- scoped_refptr<dbus::Bus> bus = dbus_connection_.ConnectWithTimeout(
- base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds));
-
- if (!bus) {
- // TODO(deymo): Make it possible to run update_engine even if dbus-daemon
- // is not running or constantly crashing.
- LOG(ERROR) << "Failed to initialize DBus, aborting.";
- return 1;
- }
-
- CHECK(bus->SetUpAsyncOperations());
-#endif // USE_DBUS
-
#if USE_OMAHA
// Initialize update engine global state but continue if something fails.
// TODO(deymo): Move the daemon_state_ initialization to a factory method
// avoiding the explicit re-usage of the |bus| instance, shared between
// D-Bus service and D-Bus client calls.
- RealSystemState* real_system_state = new RealSystemState(bus);
+ RealSystemState* real_system_state = new RealSystemState();
daemon_state_.reset(real_system_state);
LOG_IF(ERROR, !real_system_state->Initialize())
<< "Failed to initialize system state.";
@@ -104,7 +81,7 @@
#if USE_DBUS
// Create the DBus service.
- dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state, bus));
+ dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state));
daemon_state_->AddObserver(dbus_adaptor_.get());
dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
diff --git a/daemon.h b/daemon.h
index faf957a..5910783 100644
--- a/daemon.h
+++ b/daemon.h
@@ -24,9 +24,6 @@
#include <brillo/binder_watcher.h>
#endif // USE_WEAVE || USE_BINDER
#include <brillo/daemons/daemon.h>
-#if USE_DBUS
-#include <brillo/dbus/dbus_connection.h>
-#endif // USE_DBUS
#if USE_BINDER
#if USE_OMAHA
@@ -57,8 +54,7 @@
// initialization.
void OnDBusRegistered(bool succeeded);
- // Main D-Bus connection and service adaptor.
- brillo::DBusConnection dbus_connection_;
+ // Main D-Bus service adaptor.
std::unique_ptr<UpdateEngineAdaptor> dbus_adaptor_;
#endif // USE_DBUS
diff --git a/dbus_connection.cc b/dbus_connection.cc
new file mode 100644
index 0000000..cf17ec9
--- /dev/null
+++ b/dbus_connection.cc
@@ -0,0 +1,55 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/dbus_connection.h"
+
+#include <base/time/time.h>
+
+namespace chromeos_update_engine {
+
+namespace {
+const int kDBusSystemMaxWaitSeconds = 2 * 60;
+
+DBusConnection* dbus_connection_singleton = nullptr;
+} // namespace
+
+DBusConnection::DBusConnection() {
+ // We wait for the D-Bus connection for up two minutes to avoid re-spawning
+ // the daemon too fast causing thrashing if dbus-daemon is not running.
+ bus_ = dbus_connection_.ConnectWithTimeout(
+ base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds));
+
+ if (!bus_) {
+ // TODO(deymo): Make it possible to run update_engine even if dbus-daemon
+ // is not running or constantly crashing.
+ LOG(FATAL) << "Failed to initialize DBus, aborting.";
+ }
+
+ CHECK(bus_->SetUpAsyncOperations());
+}
+
+const scoped_refptr<dbus::Bus>& DBusConnection::GetDBus() {
+ CHECK(bus_);
+ return bus_;
+}
+
+DBusConnection* DBusConnection::Get() {
+ if (!dbus_connection_singleton)
+ dbus_connection_singleton = new DBusConnection();
+ return dbus_connection_singleton;
+}
+
+} // namespace chromeos_update_engine
diff --git a/dbus_connection.h b/dbus_connection.h
new file mode 100644
index 0000000..c3205ba
--- /dev/null
+++ b/dbus_connection.h
@@ -0,0 +1,44 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_DBUS_CONNECTION_H_
+#define UPDATE_ENGINE_DBUS_CONNECTION_H_
+
+#include <base/memory/ref_counted.h>
+#include <brillo/dbus/dbus_connection.h>
+#include <dbus/bus.h>
+
+namespace chromeos_update_engine {
+
+class DBusConnection {
+ public:
+ DBusConnection();
+
+ const scoped_refptr<dbus::Bus>& GetDBus();
+
+ static DBusConnection* Get();
+
+ private:
+ scoped_refptr<dbus::Bus> bus_;
+
+ brillo::DBusConnection dbus_connection_;
+
+ DISALLOW_COPY_AND_ASSIGN(DBusConnection);
+};
+
+} // namespace chromeos_update_engine
+
+#endif // UPDATE_ENGINE_DBUS_CONNECTION_H_
diff --git a/dbus_service.cc b/dbus_service.cc
index de1f9b5..d1e6d9e 100644
--- a/dbus_service.cc
+++ b/dbus_service.cc
@@ -17,6 +17,7 @@
#include "update_engine/dbus_service.h"
#include "update_engine/dbus-constants.h"
+#include "update_engine/dbus_connection.h"
#include "update_engine/update_status_utils.h"
namespace chromeos_update_engine {
@@ -142,14 +143,13 @@
return common_->GetEolStatus(error, out_eol_status);
}
-UpdateEngineAdaptor::UpdateEngineAdaptor(SystemState* system_state,
- const scoped_refptr<dbus::Bus>& bus)
+UpdateEngineAdaptor::UpdateEngineAdaptor(SystemState* system_state)
: org::chromium::UpdateEngineInterfaceAdaptor(&dbus_service_),
- bus_(bus),
- dbus_service_(system_state),
- dbus_object_(nullptr,
- bus,
- dbus::ObjectPath(update_engine::kUpdateEngineServicePath)) {}
+ bus_(DBusConnection::Get()->GetDBus()),
+ dbus_service_(system_state),
+ dbus_object_(nullptr,
+ bus_,
+ dbus::ObjectPath(update_engine::kUpdateEngineServicePath)) {}
void UpdateEngineAdaptor::RegisterAsync(
const base::Callback<void(bool)>& completion_callback) {
diff --git a/dbus_service.h b/dbus_service.h
index 12f8cf1..8b25d43 100644
--- a/dbus_service.h
+++ b/dbus_service.h
@@ -147,8 +147,7 @@
class UpdateEngineAdaptor : public org::chromium::UpdateEngineInterfaceAdaptor,
public ServiceObserverInterface {
public:
- UpdateEngineAdaptor(SystemState* system_state,
- const scoped_refptr<dbus::Bus>& bus);
+ UpdateEngineAdaptor(SystemState* system_state);
~UpdateEngineAdaptor() = default;
// Register the DBus object with the update engine service asynchronously.
diff --git a/libcros_proxy.cc b/libcros_proxy.cc
index 689ed39..3aa87cb 100644
--- a/libcros_proxy.cc
+++ b/libcros_proxy.cc
@@ -16,6 +16,8 @@
#include "update_engine/libcros_proxy.h"
+#include "update_engine/dbus_connection.h"
+
using org::chromium::LibCrosServiceInterfaceProxy;
using org::chromium::LibCrosServiceInterfaceProxyInterface;
using org::chromium::UpdateEngineLibcrosProxyResolvedInterfaceProxy;
@@ -36,13 +38,13 @@
ue_proxy_resolved_interface_(std::move(ue_proxy_resolved_interface)) {
}
-LibCrosProxy::LibCrosProxy(const scoped_refptr<dbus::Bus>& bus)
- : service_interface_proxy_(
- new LibCrosServiceInterfaceProxy(bus, kLibCrosServiceName)),
- ue_proxy_resolved_interface_(
- new UpdateEngineLibcrosProxyResolvedInterfaceProxy(
- bus,
- kLibCrosServiceName)) {
+LibCrosProxy::LibCrosProxy() {
+ const scoped_refptr<dbus::Bus>& bus = DBusConnection::Get()->GetDBus();
+ service_interface_proxy_.reset(
+ new LibCrosServiceInterfaceProxy(bus, kLibCrosServiceName));
+ ue_proxy_resolved_interface_.reset(
+ new UpdateEngineLibcrosProxyResolvedInterfaceProxy(bus,
+ kLibCrosServiceName));
}
LibCrosServiceInterfaceProxyInterface* LibCrosProxy::service_interface_proxy() {
diff --git a/libcros_proxy.h b/libcros_proxy.h
index afb5d54..03bf312 100644
--- a/libcros_proxy.h
+++ b/libcros_proxy.h
@@ -30,7 +30,7 @@
// is a thin class to just hold the generated proxies (real or mocked ones).
class LibCrosProxy final {
public:
- explicit LibCrosProxy(const scoped_refptr<dbus::Bus>& bus);
+ LibCrosProxy();
LibCrosProxy(
std::unique_ptr<org::chromium::LibCrosServiceInterfaceProxyInterface>
service_interface_proxy,
diff --git a/real_system_state.cc b/real_system_state.cc
index 8a5f389..a71f4cd 100644
--- a/real_system_state.cc
+++ b/real_system_state.cc
@@ -40,10 +40,7 @@
RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
: debugd_proxy_(bus),
power_manager_proxy_(bus),
- session_manager_proxy_(bus),
- shill_proxy_(bus),
- libcros_proxy_(bus) {
-}
+ session_manager_proxy_(bus) {}
RealSystemState::~RealSystemState() {
// Prevent any DBus communication from UpdateAttempter when shutting down the
diff --git a/real_system_state.h b/real_system_state.h
index 071e3e0..bd51cb0 100644
--- a/real_system_state.h
+++ b/real_system_state.h
@@ -34,6 +34,7 @@
#include "update_engine/common/prefs.h"
#include "update_engine/connection_manager.h"
#include "update_engine/daemon_state_interface.h"
+#include "update_engine/dbus_connection.h"
#include "update_engine/p2p_manager.h"
#include "update_engine/payload_state.h"
#include "update_engine/shill_proxy.h"
@@ -49,7 +50,9 @@
public:
// Constructs all system objects that do not require separate initialization;
// see Initialize() below for the remaining ones.
+ // TODO(senj): Remove this constructor once all proxies get DBus themselves.
explicit RealSystemState(const scoped_refptr<dbus::Bus>& bus);
+ RealSystemState() : RealSystemState(DBusConnection::Get()->GetDBus()){};
~RealSystemState() override;
// Initializes and sets systems objects that require an initialization
diff --git a/shill_proxy.cc b/shill_proxy.cc
index 1c050b4..6049ee2 100644
--- a/shill_proxy.cc
+++ b/shill_proxy.cc
@@ -16,6 +16,8 @@
#include "update_engine/shill_proxy.h"
+#include "update_engine/dbus_connection.h"
+
using org::chromium::flimflam::ManagerProxy;
using org::chromium::flimflam::ManagerProxyInterface;
using org::chromium::flimflam::ServiceProxy;
@@ -23,9 +25,8 @@
namespace chromeos_update_engine {
-ShillProxy::ShillProxy(const scoped_refptr<dbus::Bus>& bus) : bus_(bus) {}
-
bool ShillProxy::Init() {
+ bus_ = DBusConnection::Get()->GetDBus();
manager_proxy_.reset(new ManagerProxy(bus_));
return true;
}
diff --git a/shill_proxy.h b/shill_proxy.h
index 6d545f6..78f5b9b 100644
--- a/shill_proxy.h
+++ b/shill_proxy.h
@@ -32,7 +32,7 @@
// This class implements the connection to shill using real DBus calls.
class ShillProxy : public ShillProxyInterface {
public:
- explicit ShillProxy(const scoped_refptr<dbus::Bus>& bus);
+ ShillProxy() = default;
~ShillProxy() override = default;
// Initializes the ShillProxy instance creating the manager proxy from the
diff --git a/update_engine.gyp b/update_engine.gyp
index 880a03a..d437003 100644
--- a/update_engine.gyp
+++ b/update_engine.gyp
@@ -254,6 +254,7 @@
'connection_manager.cc',
'connection_utils.cc',
'daemon.cc',
+ 'dbus_connection.cc',
'dbus_service.cc',
'hardware_chromeos.cc',
'image_properties_chromeos.cc',