refactor: move notifications from brightray to atom (#15209)
This commit is contained in:
parent
4d085c4aae
commit
a369a4172b
46 changed files with 423 additions and 363 deletions
130
atom/browser/notifications/linux/libnotify_loader.cc
Normal file
130
atom/browser/notifications/linux/libnotify_loader.cc
Normal file
|
@ -0,0 +1,130 @@
|
|||
// 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 "atom/browser/notifications/linux/libnotify_loader.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
LibNotifyLoader::LibNotifyLoader() : loaded_(false) {}
|
||||
|
||||
LibNotifyLoader::~LibNotifyLoader() {
|
||||
CleanUp(loaded_);
|
||||
}
|
||||
|
||||
bool LibNotifyLoader::Load(const std::string& library_name) {
|
||||
if (loaded_)
|
||||
return false;
|
||||
|
||||
library_ = dlopen(library_name.c_str(), RTLD_LAZY);
|
||||
if (!library_)
|
||||
return false;
|
||||
|
||||
notify_is_initted = reinterpret_cast<decltype(this->notify_is_initted)>(
|
||||
dlsym(library_, "notify_is_initted"));
|
||||
if (!notify_is_initted) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_init = reinterpret_cast<decltype(this->notify_init)>(
|
||||
dlsym(library_, "notify_init"));
|
||||
if (!notify_init) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_get_server_info =
|
||||
reinterpret_cast<decltype(this->notify_get_server_info)>(
|
||||
dlsym(library_, "notify_get_server_info"));
|
||||
if (!notify_get_server_info) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_get_server_caps =
|
||||
reinterpret_cast<decltype(this->notify_get_server_caps)>(
|
||||
dlsym(library_, "notify_get_server_caps"));
|
||||
if (!notify_get_server_caps) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_new =
|
||||
reinterpret_cast<decltype(this->notify_notification_new)>(
|
||||
dlsym(library_, "notify_notification_new"));
|
||||
if (!notify_notification_new) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_add_action =
|
||||
reinterpret_cast<decltype(this->notify_notification_add_action)>(
|
||||
dlsym(library_, "notify_notification_add_action"));
|
||||
if (!notify_notification_add_action) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_set_image_from_pixbuf = reinterpret_cast<decltype(
|
||||
this->notify_notification_set_image_from_pixbuf)>(
|
||||
dlsym(library_, "notify_notification_set_image_from_pixbuf"));
|
||||
if (!notify_notification_set_image_from_pixbuf) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_set_timeout =
|
||||
reinterpret_cast<decltype(this->notify_notification_set_timeout)>(
|
||||
dlsym(library_, "notify_notification_set_timeout"));
|
||||
if (!notify_notification_set_timeout) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_set_hint_string =
|
||||
reinterpret_cast<decltype(this->notify_notification_set_hint_string)>(
|
||||
dlsym(library_, "notify_notification_set_hint_string"));
|
||||
if (!notify_notification_set_hint_string) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_show =
|
||||
reinterpret_cast<decltype(this->notify_notification_show)>(
|
||||
dlsym(library_, "notify_notification_show"));
|
||||
if (!notify_notification_show) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
notify_notification_close =
|
||||
reinterpret_cast<decltype(this->notify_notification_close)>(
|
||||
dlsym(library_, "notify_notification_close"));
|
||||
if (!notify_notification_close) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
loaded_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void LibNotifyLoader::CleanUp(bool unload) {
|
||||
if (unload) {
|
||||
dlclose(library_);
|
||||
library_ = NULL;
|
||||
}
|
||||
loaded_ = false;
|
||||
notify_is_initted = NULL;
|
||||
notify_init = NULL;
|
||||
notify_get_server_info = NULL;
|
||||
notify_get_server_caps = NULL;
|
||||
notify_notification_new = NULL;
|
||||
notify_notification_add_action = NULL;
|
||||
notify_notification_set_image_from_pixbuf = NULL;
|
||||
notify_notification_set_timeout = NULL;
|
||||
notify_notification_set_hint_string = NULL;
|
||||
notify_notification_show = NULL;
|
||||
notify_notification_close = NULL;
|
||||
}
|
49
atom/browser/notifications/linux/libnotify_loader.h
Normal file
49
atom/browser/notifications/linux/libnotify_loader.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_LOADER_H_
|
||||
#define ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_LOADER_H_
|
||||
|
||||
// FIXME Generate during build using
|
||||
// //tools/generate_library_loader/generate_library_loader.gni
|
||||
|
||||
#include <libnotify/notify.h>
|
||||
#include <string>
|
||||
|
||||
class LibNotifyLoader {
|
||||
public:
|
||||
LibNotifyLoader();
|
||||
~LibNotifyLoader();
|
||||
|
||||
bool Load(const std::string& library_name)
|
||||
__attribute__((warn_unused_result));
|
||||
|
||||
bool loaded() const { return loaded_; }
|
||||
|
||||
decltype(&::notify_is_initted) notify_is_initted;
|
||||
decltype(&::notify_init) notify_init;
|
||||
decltype(&::notify_get_server_caps) notify_get_server_caps;
|
||||
decltype(&::notify_get_server_info) notify_get_server_info;
|
||||
decltype(&::notify_notification_new) notify_notification_new;
|
||||
decltype(&::notify_notification_add_action) notify_notification_add_action;
|
||||
decltype(&::notify_notification_set_image_from_pixbuf)
|
||||
notify_notification_set_image_from_pixbuf;
|
||||
decltype(&::notify_notification_set_timeout) notify_notification_set_timeout;
|
||||
decltype(&::notify_notification_set_hint_string)
|
||||
notify_notification_set_hint_string;
|
||||
decltype(&::notify_notification_show) notify_notification_show;
|
||||
decltype(&::notify_notification_close) notify_notification_close;
|
||||
|
||||
private:
|
||||
void CleanUp(bool unload);
|
||||
|
||||
void* library_;
|
||||
bool loaded_;
|
||||
|
||||
// Disallow copy constructor and assignment operator.
|
||||
LibNotifyLoader(const LibNotifyLoader&);
|
||||
void operator=(const LibNotifyLoader&);
|
||||
};
|
||||
|
||||
#endif // ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_LOADER_H_
|
176
atom/browser/notifications/linux/libnotify_notification.cc
Normal file
176
atom/browser/notifications/linux/libnotify_notification.cc
Normal file
|
@ -0,0 +1,176 @@
|
|||
// 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 "atom/browser/notifications/linux/libnotify_notification.h"
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/notifications/notification_delegate.h"
|
||||
#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/common/application_info.h"
|
||||
#include "brightray/common/platform_util.h"
|
||||
#include "chrome/browser/ui/libgtkui/gtk_util.h"
|
||||
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
LibNotifyLoader libnotify_loader_;
|
||||
|
||||
const std::set<std::string>& GetServerCapabilities() {
|
||||
static std::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().count(capability) != 0;
|
||||
}
|
||||
|
||||
bool NotifierSupportsActions() {
|
||||
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
|
||||
return false;
|
||||
|
||||
return HasCapability("actions");
|
||||
}
|
||||
|
||||
void log_and_clear_error(GError* error, const char* context) {
|
||||
LOG(ERROR) << context << ": domain=" << error->domain
|
||||
<< " code=" << error->code << " message=\"" << error->message
|
||||
<< '"';
|
||||
g_error_free(error);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
bool LibnotifyNotification::Initialize() {
|
||||
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
|
||||
!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(brightray::GetApplicationName().c_str())) {
|
||||
LOG(WARNING) << "Unable to initialize libnotify; notifications disabled";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
|
||||
NotificationPresenter* presenter)
|
||||
: Notification(delegate, presenter), notification_(nullptr) {}
|
||||
|
||||
LibnotifyNotification::~LibnotifyNotification() {
|
||||
if (notification_) {
|
||||
g_signal_handlers_disconnect_by_data(notification_, this);
|
||||
g_object_unref(notification_);
|
||||
}
|
||||
}
|
||||
|
||||
void LibnotifyNotification::Show(const NotificationOptions& options) {
|
||||
notification_ = libnotify_loader_.notify_notification_new(
|
||||
base::UTF16ToUTF8(options.title).c_str(),
|
||||
base::UTF16ToUTF8(options.msg).c_str(), nullptr);
|
||||
|
||||
g_signal_connect(notification_, "closed",
|
||||
G_CALLBACK(OnNotificationClosedThunk), this);
|
||||
|
||||
// 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()) {
|
||||
libnotify_loader_.notify_notification_add_action(
|
||||
notification_, "default", "View", OnNotificationViewThunk, this,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
if (!options.icon.drawsNothing()) {
|
||||
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
|
||||
libnotify_loader_.notify_notification_set_image_from_pixbuf(notification_,
|
||||
pixbuf);
|
||||
libnotify_loader_.notify_notification_set_timeout(notification_,
|
||||
NOTIFY_EXPIRES_DEFAULT);
|
||||
g_object_unref(pixbuf);
|
||||
}
|
||||
|
||||
if (!options.tag.empty()) {
|
||||
GQuark id = g_quark_from_string(options.tag.c_str());
|
||||
g_object_set(G_OBJECT(notification_), "id", id, NULL);
|
||||
}
|
||||
|
||||
// Always try to append notifications.
|
||||
// Unique tags can be used to prevent this.
|
||||
if (HasCapability("append")) {
|
||||
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;
|
||||
if (brightray::platform_util::GetDesktopName(&desktop_id)) {
|
||||
const std::string suffix{".desktop"};
|
||||
if (base::EndsWith(desktop_id, suffix,
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
desktop_id.resize(desktop_id.size() - suffix.size());
|
||||
}
|
||||
libnotify_loader_.notify_notification_set_hint_string(
|
||||
notification_, "desktop-entry", desktop_id.c_str());
|
||||
}
|
||||
|
||||
GError* error = nullptr;
|
||||
libnotify_loader_.notify_notification_show(notification_, &error);
|
||||
if (error) {
|
||||
log_and_clear_error(error, "notify_notification_show");
|
||||
NotificationFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (delegate())
|
||||
delegate()->NotificationDisplayed();
|
||||
}
|
||||
|
||||
void LibnotifyNotification::Dismiss() {
|
||||
if (!notification_) {
|
||||
Destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void LibnotifyNotification::OnNotificationView(NotifyNotification* notification,
|
||||
char* action) {
|
||||
NotificationClicked();
|
||||
}
|
||||
|
||||
} // namespace atom
|
47
atom/browser/notifications/linux/libnotify_notification.h
Normal file
47
atom/browser/notifications/linux/libnotify_notification.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_NOTIFICATION_H_
|
||||
#define ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_NOTIFICATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/notifications/linux/libnotify_loader.h"
|
||||
#include "atom/browser/notifications/notification.h"
|
||||
#include "ui/base/glib/glib_signal.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class LibnotifyNotification : public Notification {
|
||||
public:
|
||||
LibnotifyNotification(NotificationDelegate* delegate,
|
||||
NotificationPresenter* presenter);
|
||||
~LibnotifyNotification() override;
|
||||
|
||||
static bool Initialize();
|
||||
|
||||
// Notification:
|
||||
void Show(const NotificationOptions& options) override;
|
||||
void Dismiss() override;
|
||||
|
||||
private:
|
||||
CHROMEG_CALLBACK_0(LibnotifyNotification,
|
||||
void,
|
||||
OnNotificationClosed,
|
||||
NotifyNotification*);
|
||||
CHROMEG_CALLBACK_1(LibnotifyNotification,
|
||||
void,
|
||||
OnNotificationView,
|
||||
NotifyNotification*,
|
||||
char*);
|
||||
|
||||
NotifyNotification* notification_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(LibnotifyNotification);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_NOTIFICATIONS_LINUX_LIBNOTIFY_NOTIFICATION_H_
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Copyright (c) 2013 Patrick Reynolds <piki@github.com>. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE-CHROMIUM file.
|
||||
|
||||
#include "atom/browser/notifications/linux/notification_presenter_linux.h"
|
||||
|
||||
#include "atom/browser/notifications/linux/libnotify_notification.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
// static
|
||||
NotificationPresenter* NotificationPresenter::Create() {
|
||||
if (!LibnotifyNotification::Initialize())
|
||||
return nullptr;
|
||||
return new NotificationPresenterLinux;
|
||||
}
|
||||
|
||||
NotificationPresenterLinux::NotificationPresenterLinux() {}
|
||||
|
||||
NotificationPresenterLinux::~NotificationPresenterLinux() {}
|
||||
|
||||
Notification* NotificationPresenterLinux::CreateNotificationObject(
|
||||
NotificationDelegate* delegate) {
|
||||
return new LibnotifyNotification(delegate, this);
|
||||
}
|
||||
|
||||
} // namespace atom
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Copyright (c) 2013 Patrick Reynolds <piki@github.com>. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE-CHROMIUM file.
|
||||
|
||||
#ifndef ATOM_BROWSER_NOTIFICATIONS_LINUX_NOTIFICATION_PRESENTER_LINUX_H_
|
||||
#define ATOM_BROWSER_NOTIFICATIONS_LINUX_NOTIFICATION_PRESENTER_LINUX_H_
|
||||
|
||||
#include "atom/browser/notifications/notification_presenter.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class NotificationPresenterLinux : public NotificationPresenter {
|
||||
public:
|
||||
NotificationPresenterLinux();
|
||||
~NotificationPresenterLinux() override;
|
||||
|
||||
private:
|
||||
Notification* CreateNotificationObject(
|
||||
NotificationDelegate* delegate) override;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(NotificationPresenterLinux);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_NOTIFICATIONS_LINUX_NOTIFICATION_PRESENTER_LINUX_H_
|
Loading…
Add table
Add a link
Reference in a new issue