From 83bab8c1db0a1cad2c1367b8b9ab6161958a0104 Mon Sep 17 00:00:00 2001 From: Stanca Serban Date: Wed, 8 Nov 2023 14:40:08 +0200 Subject: [PATCH] Backed out changeset 7c1e05f847ba (bug 1862159) for causing WakeLock related failures. CLOSED TREE --- widget/gtk/WakeLockListener.cpp | 167 +++++++++++++--------------------------- widget/gtk/WakeLockListener.h | 2 +- 2 files changed, 56 insertions(+), 113 deletions(-) diff --git a/widget/gtk/WakeLockListener.cpp b/widget/gtk/WakeLockListener.cpp index d8e647a16298..aed6e318b615 100644 --- a/widget/gtk/WakeLockListener.cpp +++ b/widget/gtk/WakeLockListener.cpp @@ -50,9 +50,8 @@ NS_IMPL_ISUPPORTS(WakeLockListener, nsIDOMMozWakeLockListener) StaticRefPtr WakeLockListener::sSingleton; -#define WAKE_LOCK_LOG(str, ...) \ - MOZ_LOG(gLinuxWakeLockLog, mozilla::LogLevel::Debug, \ - ("[%p] " str, this, ##__VA_ARGS__)) +#define WAKE_LOCK_LOG(...) \ + MOZ_LOG(gLinuxWakeLockLog, mozilla::LogLevel::Debug, (__VA_ARGS__)) static mozilla::LazyLogModule gLinuxWakeLockLog("LinuxWakeLock"); enum WakeLockType { @@ -71,20 +70,6 @@ enum WakeLockType { Unsupported = 6, }; -bool IsDBusWakeLock(int aWakeLockType) { -#if defined(MOZ_ENABLE_DBUS) - switch (aWakeLockType) { - case FreeDesktopScreensaver: - case FreeDesktopPower: - case GNOME: - return true; - default: - return false; - } -#endif - return false; -} - #ifdef MOZ_LOGGING const char* WakeLockTypeNames[7] = { "Initial", "FreeDesktopScreensaver", "FreeDesktopPower", "GNOME", @@ -98,13 +83,9 @@ class WakeLockTopic { explicit WakeLockTopic(const nsAString& aTopic) { CopyUTF16toUTF8(aTopic, mTopic); - WAKE_LOCK_LOG("WakeLockTopic::WakeLockTopic() created %s", mTopic.get()); if (sWakeLockType == Initial) { SwitchToNextWakeLockType(); } -#ifdef MOZ_ENABLE_DBUS - mCancellable = dont_AddRef(g_cancellable_new()); -#endif } nsresult InhibitScreensaver(void); @@ -115,7 +96,7 @@ class WakeLockTopic { bool SendUninhibit(); #if defined(MOZ_X11) - bool CheckXScreenSaverSupport(); + static bool CheckXScreenSaverSupport(); bool InhibitXScreenSaver(bool inhibit); #endif @@ -130,6 +111,7 @@ class WakeLockTopic { bool SwitchToNextWakeLockType(); #ifdef MOZ_ENABLE_DBUS + void DBusPrepareCancellable(); void DBusInhibitScreensaver(const char* aName, const char* aPath, const char* aCall, const char* aMethod, RefPtr aArgs); @@ -149,11 +131,9 @@ class WakeLockTopic { void DBusUninhibitSucceeded(); void DBusUninhibitFailed(); #endif + ~WakeLockTopic() { WAKE_LOCK_LOG("WakeLockTopic::~WakeLockTopic() state %d", mInhibited); -#ifdef MOZ_ENABLE_DBUS - g_cancellable_cancel(mCancellable); -#endif if (mInhibited) { UninhibitScreensaver(); } @@ -169,15 +149,12 @@ class WakeLockTopic { bool mInhibited = false; #ifdef MOZ_ENABLE_DBUS - // We're waiting for DBus reply (inhibit/uninhibit calls). - bool mWaitingForDBusInhibit = false; - bool mWaitingForDBusUninhibit = false; + // We're waiting for DBus reply (inhibit/uninhibit call). + bool mWaitingForDBusReply = false; // mInhibitRequestID is received from success screen saver inhibit call // and it's needed for screen saver enablement. uint32_t mInhibitRequestID = 0; - - RefPtr mCancellable; #endif static int sWakeLockType; @@ -187,14 +164,12 @@ int WakeLockTopic::sWakeLockType = Initial; #ifdef MOZ_ENABLE_DBUS void WakeLockTopic::DBusInhibitSucceeded(uint32_t aInhibitRequestID) { - mWaitingForDBusInhibit = false; + mWaitingForDBusReply = false; mInhibitRequestID = aInhibitRequestID; mInhibited = true; - WAKE_LOCK_LOG( - "WakeLockTopic::DBusInhibitSucceeded(), mInhibitRequestID %u " - "mShouldInhibit %d", - mInhibitRequestID, mShouldInhibit); + WAKE_LOCK_LOG("WakeLockTopic::DBusInhibitSucceeded(), mInhibitRequestID %u", + mInhibitRequestID); // Uninhibit was requested before inhibit request was finished. // So ask for it now. @@ -206,7 +181,7 @@ void WakeLockTopic::DBusInhibitSucceeded(uint32_t aInhibitRequestID) { void WakeLockTopic::DBusInhibitFailed(bool aFatal) { WAKE_LOCK_LOG("WakeLockTopic::DBusInhibitFailed(%d)", aFatal); - mWaitingForDBusInhibit = false; + mWaitingForDBusReply = false; mInhibitRequestID = 0; // Non-recoverable DBus error. Switch to another wake lock type. @@ -216,10 +191,9 @@ void WakeLockTopic::DBusInhibitFailed(bool aFatal) { } void WakeLockTopic::DBusUninhibitSucceeded() { - WAKE_LOCK_LOG("WakeLockTopic::DBusUninhibitSucceeded() mShouldInhibit %d", - mShouldInhibit); + WAKE_LOCK_LOG("WakeLockTopic::DBusInhibitSucceeded()"); - mWaitingForDBusUninhibit = false; + mWaitingForDBusReply = false; mInhibitRequestID = 0; mInhibited = false; @@ -232,45 +206,36 @@ void WakeLockTopic::DBusUninhibitSucceeded() { void WakeLockTopic::DBusUninhibitFailed() { WAKE_LOCK_LOG("WakeLockTopic::DBusUninhibitFailed()"); - mWaitingForDBusUninhibit = false; - mInhibitRequestID = 0; + mWaitingForDBusReply = false; } void WakeLockTopic::DBusInhibitScreensaver(const char* aName, const char* aPath, const char* aCall, const char* aMethod, RefPtr aArgs) { - WAKE_LOCK_LOG( - "WakeLockTopic::DBusInhibitScreensaver() mWaitingForDBusInhibit %d " - "mWaitingForDBusUninhibit %d", - mWaitingForDBusInhibit, mWaitingForDBusUninhibit); - if (mWaitingForDBusInhibit) { - WAKE_LOCK_LOG(" already waiting to inihibit, return"); - return; - } - if (mWaitingForDBusUninhibit) { - WAKE_LOCK_LOG(" cancel un-inihibit request"); - g_cancellable_cancel(mCancellable); - mWaitingForDBusUninhibit = false; + WAKE_LOCK_LOG("WakeLockTopic::DBusInhibitScreensaver() waiting %d", + mWaitingForDBusReply); + if (mWaitingForDBusReply) { + // g_cancellable_cancel(mCancellable); } - mWaitingForDBusInhibit = true; + mWaitingForDBusReply = true; widget::CreateDBusProxyForBus( G_BUS_TYPE_SESSION, GDBusProxyFlags(G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES), - /* aInterfaceInfo = */ nullptr, aName, aPath, aCall, mCancellable) + /* aInterfaceInfo = */ nullptr, aName, aPath, aCall) ->Then( GetCurrentSerialEventTarget(), __func__, - [self = RefPtr{this}, this, args = RefPtr{aArgs}, + [self = RefPtr{this}, args = RefPtr{aArgs}, aMethod](RefPtr&& aProxy) { WAKE_LOCK_LOG( "WakeLockTopic::DBusInhibitScreensaver() proxy created"); DBusProxyCall(aProxy.get(), aMethod, args.get(), - G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, mCancellable) + G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT) ->Then( GetCurrentSerialEventTarget(), __func__, - [s = RefPtr{this}, this](RefPtr&& aResult) { + [s = RefPtr{self}](RefPtr&& aResult) { if (!g_variant_is_of_type(aResult.get(), G_VARIANT_TYPE_TUPLE) || g_variant_n_children(aResult.get()) != 1) { @@ -278,7 +243,7 @@ void WakeLockTopic::DBusInhibitScreensaver(const char* aName, const char* aPath, "WakeLockTopic::DBusInhibitScreensaver() wrong " "reply type %s\n", g_variant_get_type_string(aResult.get())); - DBusInhibitFailed(/* aFatal */ true); + s->DBusInhibitFailed(/* aFatal */ true); return; } RefPtr variant = dont_AddRef( @@ -289,31 +254,29 @@ void WakeLockTopic::DBusInhibitScreensaver(const char* aName, const char* aPath, "WakeLockTopic::DBusInhibitScreensaver() wrong " "reply type %s\n", g_variant_get_type_string(aResult.get())); - DBusInhibitFailed(/* aFatal */ true); + s->DBusInhibitFailed(/* aFatal */ true); return; } - DBusInhibitSucceeded(g_variant_get_uint32(variant)); + s->DBusInhibitSucceeded(g_variant_get_uint32(variant)); }, - [s = RefPtr{this}, this, - aMethod](GUniquePtr&& aError) { + [s = RefPtr{self}, aMethod](GUniquePtr&& aError) { // Failed to send inhibit request over proxy. // Switch to another wake lock type. WAKE_LOCK_LOG( "WakeLockTopic::DBusInhibitFailed() %s call failed : " "%s\n", aMethod, aError->message); - DBusInhibitFailed( - /* aFatal */ !IsCancelledGError(aError.get())); + s->DBusInhibitFailed(/* aFatal */ true); }); }, - [self = RefPtr{this}, this](GUniquePtr&& aError) { + [self = RefPtr{this}](GUniquePtr&& aError) { // We failed to create DBus proxy. Switch to another // wake lock type. WAKE_LOCK_LOG( "WakeLockTopic::DBusInhibitScreensaver() Proxy creation " "failed: %s\n", aError->message); - DBusInhibitFailed(/* aFatal */ !IsCancelledGError(aError.get())); + self->DBusInhibitFailed(/* aFatal */ true); }); } @@ -322,27 +285,17 @@ void WakeLockTopic::DBusUninhibitScreensaver(const char* aName, const char* aCall, const char* aMethod) { WAKE_LOCK_LOG( - "WakeLockTopic::DBusUninhibitScreensaver() mWaitingForDBusInhibit %d " - "mWaitingForDBusUninhibit %d request id %u", - mWaitingForDBusInhibit, mWaitingForDBusUninhibit, mInhibitRequestID); + "WakeLockTopic::DBusUninhibitScreensaver() waiting %d request id %u", + mWaitingForDBusReply, mInhibitRequestID); - if (mWaitingForDBusUninhibit) { - WAKE_LOCK_LOG(" already waiting to uninihibit, return"); + if (mWaitingForDBusReply) { return; } - - if (mWaitingForDBusInhibit) { - WAKE_LOCK_LOG(" cancel inihibit request"); - g_cancellable_cancel(mCancellable); - mWaitingForDBusInhibit = false; - } - if (!mInhibitRequestID) { - WAKE_LOCK_LOG(" missing inihibit token, quit."); // missing uninhibit token, just quit. return; } - mWaitingForDBusUninhibit = true; + mWaitingForDBusReply = true; RefPtr variant = dont_AddRef(g_variant_ref_sink(g_variant_new("(u)", mInhibitRequestID))); @@ -350,35 +303,34 @@ void WakeLockTopic::DBusUninhibitScreensaver(const char* aName, G_BUS_TYPE_SESSION, GDBusProxyFlags(G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES), - /* aInterfaceInfo = */ nullptr, aName, aPath, aCall, mCancellable) + /* aInterfaceInfo = */ nullptr, aName, aPath, aCall) ->Then( GetCurrentSerialEventTarget(), __func__, - [self = RefPtr{this}, this, args = RefPtr{variant}, + [self = RefPtr{this}, args = RefPtr{variant}, aMethod](RefPtr&& aProxy) { WAKE_LOCK_LOG( "WakeLockTopic::DBusUninhibitScreensaver() proxy created"); DBusProxyCall(aProxy.get(), aMethod, args.get(), - G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, mCancellable) + G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT) ->Then( GetCurrentSerialEventTarget(), __func__, - [s = RefPtr{this}, this](RefPtr&& aResult) { - DBusUninhibitSucceeded(); + [s = RefPtr{self}](RefPtr&& aResult) { + s->DBusUninhibitSucceeded(); }, - [s = RefPtr{this}, this, - aMethod](GUniquePtr&& aError) { + [s = RefPtr{self}, aMethod](GUniquePtr&& aError) { WAKE_LOCK_LOG( "WakeLockTopic::DBusUninhibitFailed() %s call failed " ": %s\n", aMethod, aError->message); - DBusUninhibitFailed(); + s->DBusUninhibitFailed(); }); }, - [self = RefPtr{this}, this](GUniquePtr&& aError) { + [self = RefPtr{this}](GUniquePtr&& aError) { WAKE_LOCK_LOG( "WakeLockTopic::DBusUninhibitFailed() Proxy creation failed: " "%s\n", aError->message); - DBusUninhibitFailed(); + self->DBusUninhibitFailed(); }); } @@ -614,7 +566,7 @@ bool WakeLockTopic::SendUninhibit() { } nsresult WakeLockTopic::InhibitScreensaver() { - WAKE_LOCK_LOG("WakeLockTopic::InhibitScreensaver() Inhibited %d", mInhibited); + WAKE_LOCK_LOG("WakeLockTopic::InhibitScreensaver() state %d", mInhibited); if (mInhibited) { // Screensaver is inhibited. Nothing to do here. @@ -631,8 +583,7 @@ nsresult WakeLockTopic::InhibitScreensaver() { } nsresult WakeLockTopic::UninhibitScreensaver() { - WAKE_LOCK_LOG("WakeLockTopic::UninhibitScreensaver() Inhibited %d", - mInhibited); + WAKE_LOCK_LOG("WakeLockTopic::UninhibitScreensaver() state %d", mInhibited); if (!mInhibited) { // Screensaver isn't inhibited. Nothing to do here. @@ -695,15 +646,6 @@ bool WakeLockTopic::SwitchToNextWakeLockType() { }); #endif - if (IsDBusWakeLock(sWakeLockType)) { - // We're switching out of DBus wakelock - clear our recent DBus states. - MOZ_DIAGNOSTIC_ASSERT(g_cancellable_is_cancelled(mCancellable)); - mWaitingForDBusInhibit = false; - mWaitingForDBusUninhibit = false; - mInhibitRequestID = 0; - mInhibited = false; - } - while (sWakeLockType != Unsupported) { sWakeLockType++; if (IsWakeLockTypeAvailable(sWakeLockType)) { @@ -723,8 +665,7 @@ WakeLockListener* WakeLockListener::GetSingleton(bool aCreate) { /* static */ void WakeLockListener::Shutdown() { - MOZ_LOG(gLinuxWakeLockLog, mozilla::LogLevel::Debug, - ("WakeLockListener::Shutdown()")); + WAKE_LOCK_LOG("WakeLockListener::Shutdown()"); sSingleton = nullptr; } @@ -735,15 +676,17 @@ nsresult WakeLockListener::Callback(const nsAString& topic, return NS_OK; } - RefPtr topicLock = mTopics.LookupOrInsertWith( - topic, [&] { return MakeRefPtr(topic); }); + WAKE_LOCK_LOG("WakeLockListener %s state %s", + NS_ConvertUTF16toUTF8(topic).get(), + NS_ConvertUTF16toUTF8(state).get()); + + RefPtr* topicLock = + mTopics.GetOrInsertNew(topic, MakeRefPtr(topic)); // Treat "locked-background" the same as "unlocked" on desktop linux. bool shouldLock = state.EqualsLiteral("locked-foreground"); - WAKE_LOCK_LOG("WakeLockListener topic %s state %s request lock %d", - NS_ConvertUTF16toUTF8(topic).get(), - NS_ConvertUTF16toUTF8(state).get(), shouldLock); + WAKE_LOCK_LOG("shouldLock %d", shouldLock); - return shouldLock ? topicLock->InhibitScreensaver() - : topicLock->UninhibitScreensaver(); + return shouldLock ? topicLock->get()->InhibitScreensaver() + : topicLock->get()->UninhibitScreensaver(); } diff --git a/widget/gtk/WakeLockListener.h b/widget/gtk/WakeLockListener.h index 5050dd0dfed9..6ae47828529e 100644 --- a/widget/gtk/WakeLockListener.h +++ b/widget/gtk/WakeLockListener.h @@ -43,7 +43,7 @@ class WakeLockListener final : public nsIDOMMozWakeLockListener { // Map of topic names to |WakeLockTopic|s. // We assume a small, finite-sized set of topics. - nsRefPtrHashtable mTopics; + nsClassHashtable> mTopics; }; #endif // __WakeLockListener_h__ -- 2.11.4.GIT