electron/shell/browser/notifications/linux/libnotify_notification.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

188 lines
6.1 KiB
C++
Raw Normal View History

2015-12-25 03:52:19 +00:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/notifications/linux/libnotify_notification.h"
2015-12-25 03:52:19 +00:00
#include <string>
#include "base/containers/flat_set.h"
2015-12-25 03:52:19 +00:00
#include "base/files/file_enumerator.h"
#include "base/functional/bind.h"
#include "base/logging.h"
2015-12-25 03:52:19 +00:00
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/ui/gtk_util.h"
#include "shell/common/application_info.h"
#include "shell/common/platform_util.h"
2015-12-25 03:52:19 +00:00
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gtk/gtk_util.h" // nogncheck
2015-12-25 03:52:19 +00:00
namespace electron {
2015-12-25 03:52:19 +00:00
namespace {
2016-05-17 11:30:53 +00:00
LibNotifyLoader libnotify_loader_;
2015-12-25 03:52:19 +00:00
const base::flat_set<std::string>& GetServerCapabilities() {
static base::flat_set<std::string> 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<const char*>(l->data));
g_list_free_full(capabilities, g_free);
}
return caps;
}
bool HasCapability(const std::string& capability) {
return GetServerCapabilities().contains(capability);
}
bool NotifierSupportsActions() {
2015-12-25 03:52:19 +00:00
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
return false;
return HasCapability("actions");
2015-12-25 03:52:19 +00:00
}
void log_and_clear_error(GError* error, const char* context) {
2018-04-18 01:56:12 +00:00
LOG(ERROR) << context << ": domain=" << error->domain
<< " code=" << error->code << " message=\"" << error->message
<< '"';
2015-12-25 03:52:19 +00:00
g_error_free(error);
}
} // namespace
// static
bool LibnotifyNotification::Initialize() {
2016-05-17 11:30:53 +00:00
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
!libnotify_loader_.Load("libnotify.so.5") &&
2015-12-25 03:52:19 +00:00
!libnotify_loader_.Load("libnotify.so.1") &&
!libnotify_loader_.Load("libnotify.so")) {
LOG(WARNING) << "Unable to find libnotify; notifications disabled";
2015-12-25 03:52:19 +00:00
return false;
}
if (!libnotify_loader_.notify_is_initted() &&
2018-10-24 10:49:10 +00:00
!libnotify_loader_.notify_init(GetApplicationName().c_str())) {
LOG(WARNING) << "Unable to initialize libnotify; notifications disabled";
2015-12-25 03:52:19 +00:00
return false;
}
return true;
}
LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {}
2015-12-25 03:52:19 +00:00
LibnotifyNotification::~LibnotifyNotification() {
if (notification_) {
g_signal_handlers_disconnect_by_data(notification_, this);
g_object_unref(notification_);
}
2015-12-25 03:52:19 +00:00
}
2017-06-24 11:03:27 +00:00
void LibnotifyNotification::Show(const NotificationOptions& options) {
2015-12-25 03:52:19 +00:00
notification_ = libnotify_loader_.notify_notification_new(
2017-06-24 11:03:27 +00:00
base::UTF16ToUTF8(options.title).c_str(),
2018-04-18 01:56:12 +00:00
base::UTF16ToUTF8(options.msg).c_str(), nullptr);
2015-12-25 03:52:19 +00:00
signal_ = ScopedGSignal(
notification_, "closed",
base::BindRepeating(&LibnotifyNotification::OnNotificationClosed,
base::Unretained(this)));
2015-12-25 03:52:19 +00:00
// NB: On Unity and on any other DE using Notify-OSD, adding a notification
// action will cause the notification to display as a modal dialog box.
if (NotifierSupportsActions()) {
2015-12-25 03:52:19 +00:00
libnotify_loader_.notify_notification_add_action(
notification_, "default", "View", OnNotificationView, this, nullptr);
2015-12-25 03:52:19 +00:00
}
NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL;
chore: bump chromium to 92.0.4488.0 (master) (#28676) * chore: bump chromium in DEPS to 92.0.4478.0 * chore: update chromium patches * chore: update v8 patches * fix: add scale parameter to LookupIconFromFilepath Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2748317 Follow up: https://github.com/electron/electron/issues/28678 * build: depend on gtkprint config for gtk_util.h Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2824022 * build: add missing print_job_constants header Refs: unknown * chore: bump chromium in DEPS to 92.0.4479.0 * update patches * chore: bump chromium in DEPS to 92.0.4480.0 * chore: bump chromium in DEPS to 92.0.4481.0 * chore: bump chromium in DEPS to 92.0.4482.2 * chore: bump chromium in DEPS to 92.0.4483.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4484.0 * chore: bump chromium in DEPS to 92.0.4485.0 * fix patches * update patches * 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") https://chromium-review.googlesource.com/c/chromium/src/+/2810414 * 2781233: NotificationService: Plumb document_url for non-persistent notifications. https://chromium-review.googlesource.com/c/chromium/src/+/2781233 * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * 2836669: Refactor GTK build target and dependencies https://chromium-review.googlesource.com/c/chromium/src/+/2836669 * chore: bump chromium in DEPS to 92.0.4486.0 * update patches * fix DecrementCapturerCount patch * explicitly include badging.mojom.h * include ui/gtk/gtk_ui_factory.h for BuildGtkUi() * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * iwyu fix for base::size * iwyu for TRACE_EVENT0 * 2799631: Use structured interface for DevTools messages https://chromium-review.googlesource.com/c/chromium/src/+/2799631 * 2801573: Convert enum to enum class for Widget::InitParams::Activatable https://chromium-review.googlesource.com/c/chromium/src/+/2801573 * 2805764: Add ContentBrowserClient support for service worker-scoped binders https://chromium-review.googlesource.com/c/chromium/src/+/2805764 * fixup! 2799631: Use structured interface for DevTools messages * fixup! 2805764: Add ContentBrowserClient support for service worker-scoped binders * oops, use of linux_ui after std::move * fix devtools message handling for null params * disable node test parallel/test-debug-args https://chromium-review.googlesource.com/c/v8/v8/+/2843348 * fix gn check * chore: bump chromium in DEPS to 92.0.4487.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4488.0 * update patches * Remove vpython use from Chromium DEPS file https://chromium-review.googlesource.com/c/chromium/src/+/2810121 * Partial revert "workaround: disable CFG longjmp protection for Windows on Arm" https://chromium-review.googlesource.com/c/chromium/src/+/2788210 Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
2021-04-27 21:27:34 +00:00
if (options.urgency == u"critical") {
urgency = NOTIFY_URGENCY_CRITICAL;
chore: bump chromium to 92.0.4488.0 (master) (#28676) * chore: bump chromium in DEPS to 92.0.4478.0 * chore: update chromium patches * chore: update v8 patches * fix: add scale parameter to LookupIconFromFilepath Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2748317 Follow up: https://github.com/electron/electron/issues/28678 * build: depend on gtkprint config for gtk_util.h Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2824022 * build: add missing print_job_constants header Refs: unknown * chore: bump chromium in DEPS to 92.0.4479.0 * update patches * chore: bump chromium in DEPS to 92.0.4480.0 * chore: bump chromium in DEPS to 92.0.4481.0 * chore: bump chromium in DEPS to 92.0.4482.2 * chore: bump chromium in DEPS to 92.0.4483.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4484.0 * chore: bump chromium in DEPS to 92.0.4485.0 * fix patches * update patches * 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") https://chromium-review.googlesource.com/c/chromium/src/+/2810414 * 2781233: NotificationService: Plumb document_url for non-persistent notifications. https://chromium-review.googlesource.com/c/chromium/src/+/2781233 * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * 2836669: Refactor GTK build target and dependencies https://chromium-review.googlesource.com/c/chromium/src/+/2836669 * chore: bump chromium in DEPS to 92.0.4486.0 * update patches * fix DecrementCapturerCount patch * explicitly include badging.mojom.h * include ui/gtk/gtk_ui_factory.h for BuildGtkUi() * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * iwyu fix for base::size * iwyu for TRACE_EVENT0 * 2799631: Use structured interface for DevTools messages https://chromium-review.googlesource.com/c/chromium/src/+/2799631 * 2801573: Convert enum to enum class for Widget::InitParams::Activatable https://chromium-review.googlesource.com/c/chromium/src/+/2801573 * 2805764: Add ContentBrowserClient support for service worker-scoped binders https://chromium-review.googlesource.com/c/chromium/src/+/2805764 * fixup! 2799631: Use structured interface for DevTools messages * fixup! 2805764: Add ContentBrowserClient support for service worker-scoped binders * oops, use of linux_ui after std::move * fix devtools message handling for null params * disable node test parallel/test-debug-args https://chromium-review.googlesource.com/c/v8/v8/+/2843348 * fix gn check * chore: bump chromium in DEPS to 92.0.4487.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4488.0 * update patches * Remove vpython use from Chromium DEPS file https://chromium-review.googlesource.com/c/chromium/src/+/2810121 * Partial revert "workaround: disable CFG longjmp protection for Windows on Arm" https://chromium-review.googlesource.com/c/chromium/src/+/2788210 Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
2021-04-27 21:27:34 +00:00
} else if (options.urgency == u"low") {
urgency = NOTIFY_URGENCY_LOW;
}
// Set the urgency level of the notification.
libnotify_loader_.notify_notification_set_urgency(notification_, urgency);
2017-06-24 11:03:27 +00:00
if (!options.icon.drawsNothing()) {
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(options.icon);
2018-04-18 01:56:12 +00:00
libnotify_loader_.notify_notification_set_image_from_pixbuf(notification_,
pixbuf);
2015-12-25 03:52:19 +00:00
g_object_unref(pixbuf);
}
// Set the timeout duration for the notification
chore: bump chromium to 92.0.4488.0 (master) (#28676) * chore: bump chromium in DEPS to 92.0.4478.0 * chore: update chromium patches * chore: update v8 patches * fix: add scale parameter to LookupIconFromFilepath Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2748317 Follow up: https://github.com/electron/electron/issues/28678 * build: depend on gtkprint config for gtk_util.h Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2824022 * build: add missing print_job_constants header Refs: unknown * chore: bump chromium in DEPS to 92.0.4479.0 * update patches * chore: bump chromium in DEPS to 92.0.4480.0 * chore: bump chromium in DEPS to 92.0.4481.0 * chore: bump chromium in DEPS to 92.0.4482.2 * chore: bump chromium in DEPS to 92.0.4483.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4484.0 * chore: bump chromium in DEPS to 92.0.4485.0 * fix patches * update patches * 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") https://chromium-review.googlesource.com/c/chromium/src/+/2810414 * 2781233: NotificationService: Plumb document_url for non-persistent notifications. https://chromium-review.googlesource.com/c/chromium/src/+/2781233 * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * 2836669: Refactor GTK build target and dependencies https://chromium-review.googlesource.com/c/chromium/src/+/2836669 * chore: bump chromium in DEPS to 92.0.4486.0 * update patches * fix DecrementCapturerCount patch * explicitly include badging.mojom.h * include ui/gtk/gtk_ui_factory.h for BuildGtkUi() * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * iwyu fix for base::size * iwyu for TRACE_EVENT0 * 2799631: Use structured interface for DevTools messages https://chromium-review.googlesource.com/c/chromium/src/+/2799631 * 2801573: Convert enum to enum class for Widget::InitParams::Activatable https://chromium-review.googlesource.com/c/chromium/src/+/2801573 * 2805764: Add ContentBrowserClient support for service worker-scoped binders https://chromium-review.googlesource.com/c/chromium/src/+/2805764 * fixup! 2799631: Use structured interface for DevTools messages * fixup! 2805764: Add ContentBrowserClient support for service worker-scoped binders * oops, use of linux_ui after std::move * fix devtools message handling for null params * disable node test parallel/test-debug-args https://chromium-review.googlesource.com/c/v8/v8/+/2843348 * fix gn check * chore: bump chromium in DEPS to 92.0.4487.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4488.0 * update patches * Remove vpython use from Chromium DEPS file https://chromium-review.googlesource.com/c/chromium/src/+/2810121 * Partial revert "workaround: disable CFG longjmp protection for Windows on Arm" https://chromium-review.googlesource.com/c/chromium/src/+/2788210 Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
2021-04-27 21:27:34 +00:00
bool neverTimeout = options.timeout_type == u"never";
int timeout = (neverTimeout) ? NOTIFY_EXPIRES_NEVER : NOTIFY_EXPIRES_DEFAULT;
libnotify_loader_.notify_notification_set_timeout(notification_, timeout);
2017-06-24 11:03:27 +00:00
if (!options.tag.empty()) {
GQuark id = g_quark_from_string(options.tag.c_str());
g_object_set(G_OBJECT(notification_), "id", id, nullptr);
}
// Always try to append notifications.
// Unique tags can be used to prevent this.
if (HasCapability("append")) {
2018-04-18 01:56:12 +00:00
libnotify_loader_.notify_notification_set_hint_string(notification_,
"append", "true");
} else if (HasCapability("x-canonical-append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "x-canonical-append", "true");
}
// Send the desktop name to identify the application
// The desktop-entry is the part before the .desktop
std::string desktop_id = platform_util::GetXdgAppId();
if (!desktop_id.empty()) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "desktop-entry", desktop_id.c_str());
}
2015-12-25 03:52:19 +00:00
GError* error = nullptr;
libnotify_loader_.notify_notification_show(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_show");
NotificationFailed();
return;
}
2017-05-30 09:06:51 +00:00
if (delegate())
delegate()->NotificationDisplayed();
2015-12-25 03:52:19 +00:00
}
void LibnotifyNotification::Dismiss() {
if (!notification_) {
Destroy();
return;
}
2015-12-25 03:52:19 +00:00
GError* error = nullptr;
libnotify_loader_.notify_notification_close(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_close");
Destroy();
}
}
void LibnotifyNotification::OnNotificationClosed(
NotifyNotification* notification) {
NotificationDismissed();
2015-12-25 03:52:19 +00:00
}
2018-04-18 01:56:12 +00:00
void LibnotifyNotification::OnNotificationView(NotifyNotification* notification,
char* action,
gpointer user_data) {
LibnotifyNotification* that = static_cast<LibnotifyNotification*>(user_data);
DCHECK(that);
that->NotificationClicked();
2015-12-25 03:52:19 +00:00
}
} // namespace electron