From 3b84a37b2caae0c7c971b3c7cc5df9be1319a5ac Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 21 Apr 2016 09:19:25 -0500 Subject: don't pass a GDBusConnection to the Greeter ctor --- src/greeter.cpp | 109 +++++++++++++++++++++++++++----------------- src/greeter.h | 4 +- src/main.cpp | 12 +---- tests/unit/greeter-test.cpp | 10 ++-- 4 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/greeter.cpp b/src/greeter.cpp index 903bd2a..7f79bf9 100644 --- a/src/greeter.cpp +++ b/src/greeter.cpp @@ -26,40 +26,20 @@ class UnityGreeter::Impl { public: - explicit Impl(GDBusConnection* connection): - m_bus(G_DBUS_CONNECTION(g_object_ref(connection))), - m_cancellable{g_cancellable_new()} + explicit Impl() { - m_watcher_id = g_bus_watch_name_on_connection( - m_bus, - DBusNames::UnityGreeter::NAME, - G_BUS_NAME_WATCHER_FLAGS_AUTO_START, - on_greeter_appeared, - on_greeter_vanished, - this, - nullptr); - - m_subscription_id = g_dbus_connection_signal_subscribe( - m_bus, - DBusNames::UnityGreeter::NAME, - DBusNames::Properties::INTERFACE, - DBusNames::Properties::PropertiesChanged::NAME, - DBusNames::UnityGreeter::PATH, - DBusNames::UnityGreeter::INTERFACE, - G_DBUS_SIGNAL_FLAGS_NONE, - on_properties_changed_signal, - this, - nullptr); + m_cancellable.reset( + g_cancellable_new(), + [](GCancellable* c){ + g_cancellable_cancel(c); + g_clear_object(&c); + } + ); + + g_bus_get(G_BUS_TYPE_SESSION, m_cancellable.get(), on_bus_ready, this); } - ~Impl() - { - g_cancellable_cancel(m_cancellable); - g_clear_object(&m_cancellable); - g_bus_unwatch_name(m_watcher_id); - g_dbus_connection_signal_unsubscribe(m_bus, m_subscription_id); - g_clear_object(&m_bus); - } + ~Impl() =default; core::Property& is_active() { @@ -68,8 +48,57 @@ public: private: + static void on_bus_ready( + GObject* /*source*/, + GAsyncResult* res, + gpointer gself) + { + GError* error {}; + auto bus = g_bus_get_finish(res, &error); + if (error != nullptr) + { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("Greeter: Error getting bus: %s", error->message); + g_clear_error(&error); + } + else + { + auto self = static_cast(gself); + + const auto watcher_id = g_bus_watch_name_on_connection( + bus, + DBusNames::UnityGreeter::NAME, + G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + on_greeter_appeared, + on_greeter_vanished, + gself, + nullptr); + + const auto subscription_id = g_dbus_connection_signal_subscribe( + bus, + DBusNames::UnityGreeter::NAME, + DBusNames::Properties::INTERFACE, + DBusNames::Properties::PropertiesChanged::NAME, + DBusNames::UnityGreeter::PATH, + DBusNames::UnityGreeter::INTERFACE, + G_DBUS_SIGNAL_FLAGS_NONE, + on_properties_changed_signal, + gself, + nullptr); + + self->m_bus.reset( + bus, + [watcher_id, subscription_id](GDBusConnection* o){ + g_bus_unwatch_name(watcher_id); + g_dbus_connection_signal_unsubscribe(o, subscription_id); + g_clear_object(&o); + } + ); + } + } + static void on_greeter_appeared( - GDBusConnection* /*session_bus*/, + GDBusConnection* bus, const char* /*name*/, const char* name_owner, gpointer gself) @@ -79,7 +108,7 @@ private: self->m_owner = name_owner; g_dbus_connection_call( - self->m_bus, + bus, DBusNames::UnityGreeter::NAME, DBusNames::UnityGreeter::PATH, DBusNames::Properties::INTERFACE, @@ -88,13 +117,13 @@ private: G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, -1, - self->m_cancellable, + self->m_cancellable.get(), on_get_is_active_ready, gself); } static void on_greeter_vanished( - GDBusConnection* /*session_bus*/, + GDBusConnection* /*bus*/, const char* /*name*/, gpointer gself) { @@ -155,10 +184,8 @@ private: core::Property m_is_active {false}; - GDBusConnection* m_bus {}; - GCancellable* m_cancellable {}; - guint m_watcher_id {}; - unsigned int m_subscription_id {}; + std::shared_ptr m_bus; + std::shared_ptr m_cancellable; std::string m_owner; }; @@ -170,8 +197,8 @@ Greeter::Greeter() =default; Greeter::~Greeter() =default; -UnityGreeter::UnityGreeter(GDBusConnection* connection): - impl{new Impl{connection}} +UnityGreeter::UnityGreeter(): + impl{new Impl{}} { } diff --git a/src/greeter.h b/src/greeter.h index 6707c4f..9508220 100644 --- a/src/greeter.h +++ b/src/greeter.h @@ -21,8 +21,6 @@ #include -#include - #include #include @@ -38,7 +36,7 @@ public: class UnityGreeter: public Greeter { public: - explicit UnityGreeter(GDBusConnection* connection); + explicit UnityGreeter(); virtual ~UnityGreeter(); core::Property& is_active() override; diff --git a/src/main.cpp b/src/main.cpp index ee05f52..52cdd58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,15 +47,6 @@ main(int /*argc*/, char** /*argv*/) g_main_loop_quit(loop); }; - // get the session bus - GError* error {}; - auto session_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error); - if (error != nullptr) { - g_critical("Unable to get session bus: %s", error->message); - g_clear_error(&error); - return 0; - } - // build all our indicators. // Right now we've only got one -- rotation lock -- but hey, we can dream. std::vector> indicators; @@ -72,7 +63,7 @@ main(int /*argc*/, char** /*argv*/) static constexpr char const * ADB_SOCKET_PATH {"/dev/socket/adbd"}; static constexpr char const * PUBLIC_KEYS_FILENAME {"/data/misc/adb/adb_keys"}; auto usb_monitor = std::make_shared(); - auto greeter = std::make_shared(session_bus); + auto greeter = std::make_shared(); UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME, usb_monitor, greeter}; // let's go! @@ -80,6 +71,5 @@ main(int /*argc*/, char** /*argv*/) // cleanup g_main_loop_unref(loop); - g_clear_object(&session_bus); return 0; } diff --git a/tests/unit/greeter-test.cpp b/tests/unit/greeter-test.cpp index 5d29023..874efed 100644 --- a/tests/unit/greeter-test.cpp +++ b/tests/unit/greeter-test.cpp @@ -41,7 +41,7 @@ protected: std::shared_ptr m_dbus_runner; std::shared_ptr m_dbus_mock; - GDBusConnection* m_session_bus; + GDBusConnection* m_session_bus {}; void SetUp() override { @@ -111,7 +111,7 @@ TEST_F(GreeterFixture, ActiveServiceStartsBeforeWatcher) start_greeter_service(expected); - UnityGreeter greeter(m_session_bus); + UnityGreeter greeter; ASSERT_PROPERTY_EVENTUALLY(expected, greeter.is_active()); } @@ -120,7 +120,7 @@ TEST_F(GreeterFixture, WatcherStartsBeforeActiveService) { constexpr bool expected {true}; - UnityGreeter greeter(m_session_bus); + UnityGreeter greeter; start_greeter_service(expected); @@ -133,7 +133,7 @@ TEST_F(GreeterFixture, InactiveServiceStartsBeforeWatcher) start_greeter_service(expected); - UnityGreeter greeter(m_session_bus); + UnityGreeter greeter; ASSERT_PROPERTY_EVENTUALLY(expected, greeter.is_active()); } @@ -142,7 +142,7 @@ TEST_F(GreeterFixture, WatcherStartsBeforeInactiveService) { constexpr bool expected {false}; - UnityGreeter greeter(m_session_bus); + UnityGreeter greeter; start_greeter_service(expected); -- cgit v1.2.3