|
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 |