From 2a16b28be43db7d78c8263d01c4ea51813af21ad Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 20 Feb 2018 07:53:10 -0600 Subject: [PATCH] Cache libnotify server caps (#11965) * cache libnotify server capabilities * fix broken production cache in NotifierSupportsActions() * log a warning if LibnotifyNotification::Initialize() fails --- .../browser/linux/libnotify_notification.cc | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/brightray/browser/linux/libnotify_notification.cc b/brightray/browser/linux/libnotify_notification.cc index db59e451388..143a5fdea3e 100644 --- a/brightray/browser/linux/libnotify_notification.cc +++ b/brightray/browser/linux/libnotify_notification.cc @@ -4,9 +4,12 @@ #include "brightray/browser/linux/libnotify_notification.h" +#include +#include #include #include "base/files/file_enumerator.h" +#include "base/logging.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" #include "brightray/browser/notification_delegate.h" @@ -20,31 +23,26 @@ namespace { LibNotifyLoader libnotify_loader_; +const std::set& GetServerCapabilities() { + static std::set caps; + if (caps.empty()) { + auto capabilities = libnotify_loader_.notify_get_server_caps(); + for (auto l=capabilities; l != nullptr; l=l->next) + caps.insert(static_cast(l->data)); + g_list_free_full(capabilities, g_free); + } + return caps; +} + bool HasCapability(const std::string& capability) { - bool result = false; - GList* capabilities = libnotify_loader_.notify_get_server_caps(); - - if (g_list_find_custom(capabilities, capability.c_str(), - (GCompareFunc)g_strcmp0) != NULL) - result = true; - - g_list_free_full(capabilities, g_free); - - return result; + return GetServerCapabilities().count(capability) != 0; } bool NotifierSupportsActions() { if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER")) return false; - static bool notify_has_result = false; - static bool notify_result = false; - - if (notify_has_result) - return notify_result; - - notify_result = HasCapability("actions"); - return notify_result; + return HasCapability("actions"); } void log_and_clear_error(GError* error, const char* context) { @@ -63,10 +61,12 @@ bool LibnotifyNotification::Initialize() { !libnotify_loader_.Load("libnotify.so.5") && !libnotify_loader_.Load("libnotify.so.1") && !libnotify_loader_.Load("libnotify.so")) { + LOG(WARNING) << "Unable to find libnotify; notifications disabled"; return false; } if (!libnotify_loader_.notify_is_initted() && !libnotify_loader_.notify_init(GetApplicationName().c_str())) { + LOG(WARNING) << "Unable to initialize libnotify; notifications disabled"; return false; } return true;