From a302870b4955b0f20573fc4ddb12f4fa3aded911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 03:52:50 +0200 Subject: Actions: add Unity session proxy, and use to lock the screen if available --- src/backend-dbus/CMakeLists.txt | 6 ++- src/backend-dbus/actions.c | 55 ++++++++++++++++++++++-- src/backend-dbus/com.canonical.Unity.Session.xml | 12 ++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/backend-dbus/com.canonical.Unity.Session.xml diff --git a/src/backend-dbus/CMakeLists.txt b/src/backend-dbus/CMakeLists.txt index fa41534..1fb9d7c 100644 --- a/src/backend-dbus/CMakeLists.txt +++ b/src/backend-dbus/CMakeLists.txt @@ -14,7 +14,7 @@ add_gdbus_codegen (BACKEND_GENERATED_SOURCES dbus-webcredentials add_gdbus_codegen (BACKEND_GENERATED_SOURCES dbus-accounts org.freedesktop ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.Accounts.xml) - + add_gdbus_codegen (BACKEND_GENERATED_SOURCES dbus-user org.freedesktop ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.Accounts.User.xml) @@ -43,6 +43,10 @@ add_gdbus_codegen (BACKEND_GENERATED_SOURCES dbus-end-session-dialog org.gnome.SessionManager ${CMAKE_CURRENT_SOURCE_DIR}/org.gnome.SessionManager.EndSessionDialog.xml) +add_gdbus_codegen (BACKEND_GENERATED_SOURCES unity-session + com.canonical + ${CMAKE_CURRENT_SOURCE_DIR}/com.canonical.Unity.Session.xml) + set (SOURCES actions.c guest.c users.c backend-dbus.c utils.c) # add warnings/coverage info on handwritten files diff --git a/src/backend-dbus/actions.c b/src/backend-dbus/actions.c index b1fa8ac..d99f81f 100644 --- a/src/backend-dbus/actions.c +++ b/src/backend-dbus/actions.c @@ -25,6 +25,7 @@ #include "dbus-webcredentials.h" #include "gnome-screen-saver.h" #include "gnome-session-manager.h" +#include "unity-session.h" #include "actions.h" @@ -43,6 +44,7 @@ struct _IndicatorSessionActionsDbusPriv GSettings * indicator_settings; GnomeScreenSaver * screen_saver; GnomeSessionManager * session_manager; + UnitySession * unity_session; Login1Manager * login1_manager; GCancellable * login1_manager_cancellable; Login1Seat * login1_seat; @@ -196,6 +198,23 @@ on_screensaver_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpoin log_and_clear_error (&err, G_STRLOC, G_STRFUNC); } +static void +on_unity_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself) +{ + GError * err; + UnitySession * us; + + err = NULL; + us = unity_session_proxy_new_for_bus_finish (res, &err); + if (err == NULL) + { + INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->unity_session = us; + + } + + log_and_clear_error (&err, G_STRLOC, G_STRFUNC); +} + static void on_can_suspend_ready (GObject * o, GAsyncResult * res, gpointer gself) { @@ -759,9 +778,30 @@ my_about (IndicatorSessionActions * self G_GNUC_UNUSED) ***/ static void -lock_current_session (IndicatorSessionActions * self) +lock_current_session (IndicatorSessionActions * self, gboolean immediate) { priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv; + gchar * name_owner; + + if (G_IS_DBUS_PROXY (p->unity_session)) + { + name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (p->unity_session)); + + if (name_owner) + { + if (immediate) + { + unity_session_call_prompt_lock (p->unity_session, p->cancellable, NULL, NULL); + } + else + { + unity_session_call_lock (p->unity_session, p->cancellable, NULL, NULL); + } + + g_free (name_owner); + return; + } + } g_return_if_fail (p->screen_saver != NULL); @@ -771,7 +811,7 @@ lock_current_session (IndicatorSessionActions * self) static void my_switch_to_screensaver (IndicatorSessionActions * self) { - lock_current_session (self); + lock_current_session (self, FALSE); } static void @@ -793,7 +833,7 @@ my_switch_to_guest (IndicatorSessionActions * self) g_return_if_fail (p->dm_seat != NULL); - lock_current_session (self); + lock_current_session (self, TRUE); display_manager_seat_call_switch_to_guest (p->dm_seat, "", p->dm_seat_cancellable, @@ -850,6 +890,7 @@ my_dispose (GObject * o) g_clear_object (&p->screen_saver); g_clear_object (&p->session_manager); + g_clear_object (&p->unity_session); set_dm_seat (self, NULL); set_login1_manager (self, NULL); set_login1_seat (self, NULL); @@ -952,6 +993,14 @@ indicator_session_actions_dbus_init (IndicatorSessionActionsDbus * self) on_screensaver_proxy_ready, self); + unity_session_proxy_new_for_bus (G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + "com.canonical.Unity", + "/com/canonical/Unity/Session", + p->cancellable, + on_unity_proxy_ready, + self); + gnome_session_manager_proxy_new_for_bus (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, "org.gnome.SessionManager", diff --git a/src/backend-dbus/com.canonical.Unity.Session.xml b/src/backend-dbus/com.canonical.Unity.Session.xml new file mode 100644 index 0000000..1dce43e --- /dev/null +++ b/src/backend-dbus/com.canonical.Unity.Session.xml @@ -0,0 +1,12 @@ + + + + + + + + + + -- cgit v1.2.3 From 4ad55d6c928cff40ed58a6a12971355a6757bc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 03:55:12 +0200 Subject: Actions: lock current session also when switching to greeter and another user --- src/backend-dbus/actions.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend-dbus/actions.c b/src/backend-dbus/actions.c index d99f81f..b5810a7 100644 --- a/src/backend-dbus/actions.c +++ b/src/backend-dbus/actions.c @@ -821,6 +821,8 @@ my_switch_to_greeter (IndicatorSessionActions * self) g_return_if_fail (p->dm_seat != NULL); + lock_current_session (self, TRUE); + display_manager_seat_call_switch_to_greeter (p->dm_seat, p->dm_seat_cancellable, NULL, NULL); @@ -847,6 +849,8 @@ my_switch_to_username (IndicatorSessionActions * self, const char * username) g_return_if_fail (p->dm_seat != NULL); + lock_current_session (self, TRUE); + display_manager_seat_call_switch_to_user (p->dm_seat, username, "", p->dm_seat_cancellable, NULL, NULL); -- cgit v1.2.3 From 78112284ec71ea481625408455835e1dc4e0d7f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 06:58:04 +0200 Subject: actions: add have_unity_session to check if unity is currently running and exporting his session bus --- src/backend-dbus/actions.c | 37 ++++++++++++++++++++---------- src/backend-dbus/org.gnome.ScreenSaver.xml | 3 --- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/backend-dbus/actions.c b/src/backend-dbus/actions.c index b5810a7..f395e12 100644 --- a/src/backend-dbus/actions.c +++ b/src/backend-dbus/actions.c @@ -777,8 +777,8 @@ my_about (IndicatorSessionActions * self G_GNUC_UNUSED) **** ***/ -static void -lock_current_session (IndicatorSessionActions * self, gboolean immediate) +static gboolean +have_unity_session (IndicatorSessionActions * self) { priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv; gchar * name_owner; @@ -789,20 +789,33 @@ lock_current_session (IndicatorSessionActions * self, gboolean immediate) if (name_owner) { - if (immediate) - { - unity_session_call_prompt_lock (p->unity_session, p->cancellable, NULL, NULL); - } - else - { - unity_session_call_lock (p->unity_session, p->cancellable, NULL, NULL); - } - g_free (name_owner); - return; + return TRUE; } } + return FALSE; +} + +static void +lock_current_session (IndicatorSessionActions * self, gboolean immediate) +{ + priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv; + + if (have_unity_session (self)) + { + if (immediate) + { + unity_session_call_prompt_lock (p->unity_session, p->cancellable, NULL, NULL); + } + else + { + unity_session_call_lock (p->unity_session, p->cancellable, NULL, NULL); + } + + return; + } + g_return_if_fail (p->screen_saver != NULL); gnome_screen_saver_call_lock (p->screen_saver, p->cancellable, NULL, NULL); diff --git a/src/backend-dbus/org.gnome.ScreenSaver.xml b/src/backend-dbus/org.gnome.ScreenSaver.xml index c21fdc5..a1da786 100644 --- a/src/backend-dbus/org.gnome.ScreenSaver.xml +++ b/src/backend-dbus/org.gnome.ScreenSaver.xml @@ -8,9 +8,6 @@ - - - -- cgit v1.2.3 From 9fd91d7e3d07219922cc4ac0c749262bc36ae024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 07:31:32 +0200 Subject: actions: cleanup spacing --- src/backend-dbus/actions.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend-dbus/actions.c b/src/backend-dbus/actions.c index f395e12..3d4b6f0 100644 --- a/src/backend-dbus/actions.c +++ b/src/backend-dbus/actions.c @@ -209,7 +209,6 @@ on_unity_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gs if (err == NULL) { INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->unity_session = us; - } log_and_clear_error (&err, G_STRLOC, G_STRFUNC); -- cgit v1.2.3 From 99418d9780518e2bcda8034ad9cb950b899f8cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 14:16:40 +0200 Subject: ScreenSaver API: add SimulateUserActivity back --- src/backend-dbus/org.gnome.ScreenSaver.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/backend-dbus/org.gnome.ScreenSaver.xml b/src/backend-dbus/org.gnome.ScreenSaver.xml index a1da786..c21fdc5 100644 --- a/src/backend-dbus/org.gnome.ScreenSaver.xml +++ b/src/backend-dbus/org.gnome.ScreenSaver.xml @@ -8,6 +8,9 @@ + + + -- cgit v1.2.3 From 700dbeacd2c0377e4f1c9c48565f61e437c40534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 14:39:14 +0200 Subject: DBusActions: use if/else instead of returning in lock_current_session --- src/backend-dbus/actions.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/backend-dbus/actions.c b/src/backend-dbus/actions.c index 3d4b6f0..cac7c40 100644 --- a/src/backend-dbus/actions.c +++ b/src/backend-dbus/actions.c @@ -811,13 +811,13 @@ lock_current_session (IndicatorSessionActions * self, gboolean immediate) { unity_session_call_lock (p->unity_session, p->cancellable, NULL, NULL); } - - return; } + else + { + g_return_if_fail (p->screen_saver != NULL); - g_return_if_fail (p->screen_saver != NULL); - - gnome_screen_saver_call_lock (p->screen_saver, p->cancellable, NULL, NULL); + gnome_screen_saver_call_lock (p->screen_saver, p->cancellable, NULL, NULL); + } } static void -- cgit v1.2.3 From e4b9f042a74b471f0b5992b99701231c3052e3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 17:11:06 +0200 Subject: MockUnitySession: add new mock for unity session, and use it in tests --- tests/backend-dbus/CMakeLists.txt | 2 + tests/backend-dbus/gtest-mock-dbus-fixture.h | 4 ++ tests/backend-dbus/mock-unity-session.cc | 71 ++++++++++++++++++++++++++++ tests/backend-dbus/mock-unity-session.h | 53 +++++++++++++++++++++ tests/backend-dbus/test-actions.cc | 8 +++- 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/backend-dbus/mock-unity-session.cc create mode 100644 tests/backend-dbus/mock-unity-session.h diff --git a/tests/backend-dbus/CMakeLists.txt b/tests/backend-dbus/CMakeLists.txt index e28d8e6..6ef68bd 100644 --- a/tests/backend-dbus/CMakeLists.txt +++ b/tests/backend-dbus/CMakeLists.txt @@ -23,6 +23,8 @@ add_library (desktopmock STATIC mock-object.h mock-screen-saver.cc mock-screen-saver.h + mock-unity-session.cc + mock-unity-session.h mock-session-manager.cc mock-session-manager.h mock-user.cc diff --git a/tests/backend-dbus/gtest-mock-dbus-fixture.h b/tests/backend-dbus/gtest-mock-dbus-fixture.h index bab82bf..d4f598c 100644 --- a/tests/backend-dbus/gtest-mock-dbus-fixture.h +++ b/tests/backend-dbus/gtest-mock-dbus-fixture.h @@ -25,6 +25,7 @@ #include "mock-display-manager-seat.h" #include "mock-end-session-dialog.h" #include "mock-screen-saver.h" +#include "mock-unity-session.h" #include "mock-session-manager.h" #include "mock-user.h" #include "mock-webcredentials.h" @@ -42,6 +43,7 @@ class GTestMockDBusFixture: public GTestDBusFixture protected: MockScreenSaver * screen_saver; + MockUnitySession * unity_session; MockSessionManager * session_manager; MockDisplayManagerSeat * dm_seat; MockAccounts * accounts; @@ -60,6 +62,7 @@ class GTestMockDBusFixture: public GTestDBusFixture end_session_dialog = new MockEndSessionDialog (loop, conn); session_manager = new MockSessionManager (loop, conn); screen_saver = new MockScreenSaver (loop, conn); + unity_session = new MockUnitySession (loop, conn); dm_seat = new MockDisplayManagerSeat (loop, conn); g_setenv ("XDG_SEAT_PATH", dm_seat->path(), TRUE); dm_seat->set_guest_allowed (false); @@ -83,6 +86,7 @@ class GTestMockDBusFixture: public GTestDBusFixture delete login1_manager; delete dm_seat; delete screen_saver; + delete unity_session; delete session_manager; delete end_session_dialog; delete webcredentials; diff --git a/tests/backend-dbus/mock-unity-session.cc b/tests/backend-dbus/mock-unity-session.cc new file mode 100644 index 0000000..c996310 --- /dev/null +++ b/tests/backend-dbus/mock-unity-session.cc @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * Authors: + * Marco Trevisan + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "mock-unity-session.h" + + +gboolean +MockUnitySession :: handle_lock (UnitySession * us, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Lock; + unity_session_complete_lock (us, inv); + return true; +} + +gboolean +MockUnitySession :: handle_prompt_lock (UnitySession * us, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = PromptLock; + unity_session_complete_prompt_lock (us, inv); + return true; +} + +/*** +**** +***/ + +namespace +{ + const char * const UNITY_SESSION_NAME = "com.canonical.Unity"; + const char * const UNITY_SESSION_PATH = "/com/canonical/Unity/Session"; + +} + +MockUnitySession :: MockUnitySession (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, UNITY_SESSION_NAME, UNITY_SESSION_PATH), + my_skeleton (unity_session_skeleton_new ()), + my_last_action (None) +{ + g_signal_connect (my_skeleton, "handle-lock", + G_CALLBACK(handle_lock), this); + g_signal_connect (my_skeleton, "handle-prompt-lock", + G_CALLBACK(handle_prompt_lock), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockUnitySession :: ~MockUnitySession () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-unity-session.h b/tests/backend-dbus/mock-unity-session.h new file mode 100644 index 0000000..ded246a --- /dev/null +++ b/tests/backend-dbus/mock-unity-session.h @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * Authors: + * Marco Trevisan + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MOCK_UNITY_SESSION_H +#define MOCK_UNITY_SESSION_H + +#include "mock-object.h" // parent class +#include "backend-dbus/unity-session.h" // Unity Session + +class MockUnitySession: public MockObject +{ + public: + + MockUnitySession (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockUnitySession (); + + public: + + enum Action { None, Lock, PromptLock, RequestLogout, RequestShutdown, RequestReboot }; + Action last_action () { return my_last_action; } + + private: + + UnitySession * my_skeleton; + Action my_last_action; + + static gboolean handle_lock (UnitySession *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_prompt_lock (UnitySession *, + GDBusMethodInvocation *, + gpointer); + +}; + +#endif diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc index c0f8517..717509d 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -316,17 +316,19 @@ TEST_F (Actions, Hibernate) TEST_F (Actions, SwitchToScreensaver) { - ASSERT_EQ (MockScreenSaver::None, screen_saver->last_action()); + ASSERT_EQ (MockUnitySession::None, unity_session->last_action()); indicator_session_actions_switch_to_screensaver (actions); wait_msec (50); - ASSERT_EQ (MockScreenSaver::Lock, screen_saver->last_action()); + ASSERT_EQ (MockUnitySession::Lock, unity_session->last_action()); } TEST_F (Actions, SwitchToGreeter) { ASSERT_NE (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); + ASSERT_EQ (MockUnitySession::None, unity_session->last_action()); indicator_session_actions_switch_to_greeter (actions); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); ASSERT_EQ (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); } @@ -346,6 +348,7 @@ TEST_F (Actions, SwitchToGuest) wait_for_signal (login1_seat->skeleton(), "notify::active-session"); ASSERT_EQ (guest_session_tag, login1_seat->active_session()); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); } TEST_F (Actions, SwitchToUsername) @@ -367,6 +370,7 @@ TEST_F (Actions, SwitchToUsername) wait_for_signal (login1_seat->skeleton(), "notify::active-session"); ASSERT_EQ (dr1_session, login1_seat->active_session()); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); indicator_session_actions_switch_to_username (actions, dr2_username); wait_for_signal (login1_seat->skeleton(), "notify::active-session"); -- cgit v1.2.3