| Differences between
and this patch
- a/Source/WebCore/ChangeLog +22 lines
Lines 1-3 a/Source/WebCore/ChangeLog_sec1
1
2021-10-19  Patrick Griffis  <pgriffis@igalia.com>
2
3
        [GTK] Rewrite LowPowerModeNotifier to use GPowerProfileMonitor
4
        https://bugs.webkit.org/show_bug.cgi?id=231958
5
6
        Reviewed by NOBODY (OOPS!).
7
8
        This replaces the previous direct use of UPower with some advantages:
9
10
        - Fixes support while being sandboxed without UPower DBus access
11
        - Respects a system-wide low power mode rather than only being
12
          enabled when the battery is low
13
14
        I decided to remove the old behavior entirely as it is a very
15
        different behavior than the new one and subjectively worse.
16
17
        * platform/LowPowerModeNotifier.h:
18
        * platform/glib/LowPowerModeNotifierGLib.cpp:
19
        (WebCore::LowPowerModeNotifier::LowPowerModeNotifier):
20
        (WebCore::LowPowerModeNotifier::powerSaverEnabledNotifyCallback):
21
        (WebCore::LowPowerModeNotifier::~LowPowerModeNotifier):
22
1
2021-10-19  Tyler Wilcock  <tyler_w@apple.com>
23
2021-10-19  Tyler Wilcock  <tyler_w@apple.com>
2
24
3
        AX: Fix broken spec links in AccessibilityObject.cpp and AccessibilityTree.cpp
25
        AX: Fix broken spec links in AccessibilityObject.cpp and AccessibilityTree.cpp
- a/Source/WebCore/platform/LowPowerModeNotifier.h -9 / +3 lines
Lines 33-40 OBJC_CLASS WebLowPowerModeObserver; a/Source/WebCore/platform/LowPowerModeNotifier.h_sec1
33
#endif
33
#endif
34
34
35
#if USE(GLIB)
35
#if USE(GLIB)
36
#include <gio/gio.h>
36
#include <wtf/glib/GRefPtr.h>
37
#include <wtf/glib/GRefPtr.h>
37
typedef struct _GDBusProxy GDBusProxy;
38
#endif
38
#endif
39
39
40
namespace WebCore {
40
namespace WebCore {
Lines 55-69 private: a/Source/WebCore/platform/LowPowerModeNotifier.h_sec2
55
55
56
    RetainPtr<WebLowPowerModeObserver> m_observer;
56
    RetainPtr<WebLowPowerModeObserver> m_observer;
57
    LowPowerModeChangeCallback m_callback;
57
    LowPowerModeChangeCallback m_callback;
58
#elif USE(GLIB)
58
#elif USE(GLIB) && GLIB_CHECK_VERSION(2, 69, 1)
59
    void updateWarningLevel();
60
    void warningLevelChanged();
61
    static void gPropertiesChangedCallback(LowPowerModeNotifier*, GVariant* changedProperties);
62
63
    GRefPtr<GDBusProxy> m_displayDeviceProxy;
64
    GRefPtr<GCancellable> m_cancellable;
65
    LowPowerModeChangeCallback m_callback;
59
    LowPowerModeChangeCallback m_callback;
66
    bool m_lowPowerModeEnabled { false };
60
    GRefPtr<GPowerProfileMonitor> m_powerProfileMonitor;
67
#endif
61
#endif
68
};
62
};
69
63
- a/Source/WebCore/platform/glib/LowPowerModeNotifierGLib.cpp -65 / +18 lines
Lines 26-108 a/Source/WebCore/platform/glib/LowPowerModeNotifierGLib.cpp_sec1
26
26
27
namespace WebCore {
27
namespace WebCore {
28
28
29
static const char kWarningLevel[] = "WarningLevel";
30
29
31
LowPowerModeNotifier::LowPowerModeNotifier(LowPowerModeChangeCallback&& callback)
30
LowPowerModeNotifier::LowPowerModeNotifier(LowPowerModeChangeCallback&& callback)
32
    : m_cancellable(adoptGRef(g_cancellable_new()))
31
    : m_callback(WTFMove(callback))
33
    , m_callback(WTFMove(callback))
32
#if GLIB_CHECK_VERSION(2, 69, 1)
33
    , m_powerProfileMonitor(adoptGRef(g_power_profile_monitor_dup_default()))
34
#endif
34
{
35
{
35
    g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES),
36
    m_callback(isLowPowerModeEnabled());
36
        nullptr, "org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.UPower.Device", m_cancellable.get(),
37
        [](GObject*, GAsyncResult* result, gpointer userData) {
38
            GUniqueOutPtr<GError> error;
39
            GRefPtr<GDBusProxy> proxy = adoptGRef(g_dbus_proxy_new_for_bus_finish(result, &error.outPtr()));
40
            if (g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED))
41
                return;
42
37
43
            auto* self = static_cast<LowPowerModeNotifier*>(userData);
38
#if GLIB_CHECK_VERSION(2, 69, 1)
44
            if (proxy) {
39
    g_signal_connect_swapped(m_powerProfileMonitor.get(), "notify::power-saver-enabled", G_CALLBACK(+[] (LowPowerModeNotifier* self, GParamSpec*, GPowerProfileMonitor*) {
45
                GUniquePtr<char> nameOwner(g_dbus_proxy_get_name_owner(proxy.get()));
40
        self->m_callback(self->isLowPowerModeEnabled());
46
                if (nameOwner) {
41
    }), this);
47
                    self->m_displayDeviceProxy = WTFMove(proxy);
42
#endif
48
                    self->updateWarningLevel();
49
                    g_signal_connect_swapped(self->m_displayDeviceProxy.get(), "g-properties-changed", G_CALLBACK(gPropertiesChangedCallback), self);
50
                    return;
51
                }
52
            }
53
54
            // Now, if there is no name owner, it would be good to try to
55
            // connect to a Flatpak battery status portal instead.
56
            // Unfortunately, no such portal currently exists.
57
            self->m_cancellable = nullptr;
58
    }, this);
59
}
60
61
void LowPowerModeNotifier::updateWarningLevel()
62
{
63
    GRefPtr<GVariant> variant = adoptGRef(g_dbus_proxy_get_cached_property(m_displayDeviceProxy.get(), kWarningLevel));
64
    if (!variant) {
65
        m_lowPowerModeEnabled = false;
66
        return;
67
    }
68
69
    // 0: Unknown
70
    // 1: None
71
    // 2: Discharging (only for universal power supplies)
72
    // 3: Low
73
    // 4: Critical
74
    // 5: Action
75
    m_lowPowerModeEnabled = g_variant_get_uint32(variant.get()) > 1;
76
}
77
78
void LowPowerModeNotifier::warningLevelChanged()
79
{
80
    updateWarningLevel();
81
    m_callback(m_lowPowerModeEnabled);
82
}
83
84
void LowPowerModeNotifier::gPropertiesChangedCallback(LowPowerModeNotifier* self, GVariant* changedProperties)
85
{
86
    GUniqueOutPtr<GVariantIter> iter;
87
    g_variant_get(changedProperties, "a{sv}", &iter.outPtr());
88
89
    const char* propertyName;
90
    while (g_variant_iter_next(iter.get(), "{&sv}", &propertyName, nullptr)) {
91
        if (!strcmp(propertyName, kWarningLevel)) {
92
            self->warningLevelChanged();
93
            break;
94
        }
95
    }
96
}
43
}
97
44
98
LowPowerModeNotifier::~LowPowerModeNotifier()
45
LowPowerModeNotifier::~LowPowerModeNotifier()
99
{
46
{
100
    g_cancellable_cancel(m_cancellable.get());
47
#if GLIB_CHECK_VERSION(2, 69, 1)
48
    g_signal_handlers_disconnect_by_data(m_powerProfileMonitor.get(), this);
49
#endif
101
}
50
}
102
51
103
bool LowPowerModeNotifier::isLowPowerModeEnabled() const
52
bool LowPowerModeNotifier::isLowPowerModeEnabled() const
104
{
53
{
105
    return m_lowPowerModeEnabled;
54
#if GLIB_CHECK_VERSION(2, 69, 1)
55
    return g_power_profile_monitor_get_power_saver_enabled(m_powerProfileMonitor.get());
56
#else
57
    return false;
58
#endif
106
}
59
}
107
60
108
} // namespace WebCore
61
} // namespace WebCore

Return to Bug 231958