libnotify_notification.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "brightray/browser/linux/libnotify_notification.h"
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8. #include "base/files/file_enumerator.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "brightray/browser/notification_delegate.h"
  13. #include "brightray/common/application_info.h"
  14. #include "brightray/common/platform_util.h"
  15. #include "chrome/browser/ui/libgtkui/gtk_util.h"
  16. #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
  17. #include "third_party/skia/include/core/SkBitmap.h"
  18. namespace brightray {
  19. namespace {
  20. LibNotifyLoader libnotify_loader_;
  21. const std::set<std::string>& GetServerCapabilities() {
  22. static std::set<std::string> caps;
  23. if (caps.empty()) {
  24. auto* capabilities = libnotify_loader_.notify_get_server_caps();
  25. for (auto* l = capabilities; l != nullptr; l = l->next)
  26. caps.insert(static_cast<const char*>(l->data));
  27. g_list_free_full(capabilities, g_free);
  28. }
  29. return caps;
  30. }
  31. bool HasCapability(const std::string& capability) {
  32. return GetServerCapabilities().count(capability) != 0;
  33. }
  34. bool NotifierSupportsActions() {
  35. if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
  36. return false;
  37. return HasCapability("actions");
  38. }
  39. void log_and_clear_error(GError* error, const char* context) {
  40. LOG(ERROR) << context << ": domain=" << error->domain
  41. << " code=" << error->code << " message=\"" << error->message
  42. << '"';
  43. g_error_free(error);
  44. }
  45. } // namespace
  46. // static
  47. bool LibnotifyNotification::Initialize() {
  48. if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
  49. !libnotify_loader_.Load("libnotify.so.5") &&
  50. !libnotify_loader_.Load("libnotify.so.1") &&
  51. !libnotify_loader_.Load("libnotify.so")) {
  52. LOG(WARNING) << "Unable to find libnotify; notifications disabled";
  53. return false;
  54. }
  55. if (!libnotify_loader_.notify_is_initted() &&
  56. !libnotify_loader_.notify_init(GetApplicationName().c_str())) {
  57. LOG(WARNING) << "Unable to initialize libnotify; notifications disabled";
  58. return false;
  59. }
  60. return true;
  61. }
  62. LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
  63. NotificationPresenter* presenter)
  64. : Notification(delegate, presenter), notification_(nullptr) {}
  65. LibnotifyNotification::~LibnotifyNotification() {
  66. if (notification_) {
  67. g_signal_handlers_disconnect_by_data(notification_, this);
  68. g_object_unref(notification_);
  69. }
  70. }
  71. void LibnotifyNotification::Show(const NotificationOptions& options) {
  72. notification_ = libnotify_loader_.notify_notification_new(
  73. base::UTF16ToUTF8(options.title).c_str(),
  74. base::UTF16ToUTF8(options.msg).c_str(), nullptr);
  75. g_signal_connect(notification_, "closed",
  76. G_CALLBACK(OnNotificationClosedThunk), this);
  77. // NB: On Unity and on any other DE using Notify-OSD, adding a notification
  78. // action will cause the notification to display as a modal dialog box.
  79. if (NotifierSupportsActions()) {
  80. libnotify_loader_.notify_notification_add_action(
  81. notification_, "default", "View", OnNotificationViewThunk, this,
  82. nullptr);
  83. }
  84. if (!options.icon.drawsNothing()) {
  85. GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
  86. libnotify_loader_.notify_notification_set_image_from_pixbuf(notification_,
  87. pixbuf);
  88. libnotify_loader_.notify_notification_set_timeout(notification_,
  89. NOTIFY_EXPIRES_DEFAULT);
  90. g_object_unref(pixbuf);
  91. }
  92. if (!options.tag.empty()) {
  93. GQuark id = g_quark_from_string(options.tag.c_str());
  94. g_object_set(G_OBJECT(notification_), "id", id, NULL);
  95. }
  96. // Always try to append notifications.
  97. // Unique tags can be used to prevent this.
  98. if (HasCapability("append")) {
  99. libnotify_loader_.notify_notification_set_hint_string(notification_,
  100. "append", "true");
  101. } else if (HasCapability("x-canonical-append")) {
  102. libnotify_loader_.notify_notification_set_hint_string(
  103. notification_, "x-canonical-append", "true");
  104. }
  105. // Send the desktop name to identify the application
  106. // The desktop-entry is the part before the .desktop
  107. std::string desktop_id;
  108. if (platform_util::GetDesktopName(&desktop_id)) {
  109. const std::string suffix{".desktop"};
  110. if (base::EndsWith(desktop_id, suffix,
  111. base::CompareCase::INSENSITIVE_ASCII)) {
  112. desktop_id.resize(desktop_id.size() - suffix.size());
  113. }
  114. libnotify_loader_.notify_notification_set_hint_string(
  115. notification_, "desktop-entry", desktop_id.c_str());
  116. }
  117. GError* error = nullptr;
  118. libnotify_loader_.notify_notification_show(notification_, &error);
  119. if (error) {
  120. log_and_clear_error(error, "notify_notification_show");
  121. NotificationFailed();
  122. return;
  123. }
  124. if (delegate())
  125. delegate()->NotificationDisplayed();
  126. }
  127. void LibnotifyNotification::Dismiss() {
  128. if (!notification_) {
  129. Destroy();
  130. return;
  131. }
  132. GError* error = nullptr;
  133. libnotify_loader_.notify_notification_close(notification_, &error);
  134. if (error) {
  135. log_and_clear_error(error, "notify_notification_close");
  136. Destroy();
  137. }
  138. }
  139. void LibnotifyNotification::OnNotificationClosed(
  140. NotifyNotification* notification) {
  141. NotificationDismissed();
  142. }
  143. void LibnotifyNotification::OnNotificationView(NotifyNotification* notification,
  144. char* action) {
  145. NotificationClicked();
  146. }
  147. } // namespace brightray