2013-12-18 21:02:49 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
2013-12-19 05:20:00 +00:00
|
|
|
// Copyright (c) 2013 Patrick Reynolds <piki@github.com>. All rights reserved.
|
2013-12-18 21:02:49 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
|
|
|
#include "browser/linux/notification_presenter_linux.h"
|
|
|
|
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/common/show_desktop_notification_params.h"
|
|
|
|
#include "common/application_info.h"
|
|
|
|
|
|
|
|
#include <libnotify/notify.h>
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
const char *kRenderProcessIDKey = "RenderProcessID";
|
|
|
|
const char *kRenderViewIDKey = "RenderViewID";
|
|
|
|
const char *kNotificationIDKey = "NotificationID";
|
2013-12-18 21:02:49 +00:00
|
|
|
|
|
|
|
void log_and_clear_error(GError *error, const char *context) {
|
|
|
|
if (error) {
|
|
|
|
LOG(ERROR) << context << ": domain=" << error->domain << " code=" << error->code << " message=\"" << error->message << "\"";
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:14:08 +00:00
|
|
|
void NotificationClosedCallback(NotifyNotification *notification, NotificationPresenterLinux *obj) {
|
2013-12-19 05:05:19 +00:00
|
|
|
int render_process_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kRenderProcessIDKey));
|
|
|
|
int render_view_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kRenderViewIDKey));
|
|
|
|
int notification_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kNotificationIDKey));
|
2013-12-19 05:14:08 +00:00
|
|
|
|
2013-12-19 05:13:33 +00:00
|
|
|
auto host = content::RenderViewHost::FromID(render_process_id, render_view_id);
|
|
|
|
if (host) host->DesktopNotificationPostClose(notification_id, false);
|
2013-12-19 05:05:19 +00:00
|
|
|
obj->RemoveNotification(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:14:08 +00:00
|
|
|
void NotificationViewCallback(NotifyNotification *notification, const char *action, NotificationPresenterLinux *obj) {
|
2013-12-19 05:05:19 +00:00
|
|
|
int render_process_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kRenderProcessIDKey));
|
|
|
|
int render_view_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kRenderViewIDKey));
|
|
|
|
int notification_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(notification), kNotificationIDKey));
|
2013-12-19 05:14:08 +00:00
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
auto host = content::RenderViewHost::FromID(render_process_id, render_view_id);
|
|
|
|
if (host) host->DesktopNotificationPostClick(notification_id);
|
|
|
|
obj->RemoveNotification(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPresenter* NotificationPresenter::Create() {
|
|
|
|
if (!notify_is_initted()) {
|
|
|
|
notify_init(GetApplicationName().c_str());
|
|
|
|
}
|
|
|
|
return new NotificationPresenterLinux;
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
NotificationPresenterLinux::NotificationPresenterLinux() : notifications_(NULL) { }
|
|
|
|
|
|
|
|
NotificationPresenterLinux::~NotificationPresenterLinux() {
|
2013-12-19 05:14:08 +00:00
|
|
|
// unref any outstanding notifications, and then free the list.
|
2013-12-19 05:05:19 +00:00
|
|
|
if (notifications_) {
|
|
|
|
for (GList *p = notifications_; p != NULL; p = p->next) {
|
|
|
|
g_object_unref(G_OBJECT(p->data));
|
|
|
|
}
|
|
|
|
g_list_free(notifications_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 21:02:49 +00:00
|
|
|
void NotificationPresenterLinux::ShowNotification(
|
|
|
|
const content::ShowDesktopNotificationHostMsgParams& params,
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id) {
|
2013-12-19 05:05:19 +00:00
|
|
|
std::string title = base::UTF16ToUTF8(params.title);
|
|
|
|
std::string body = base::UTF16ToUTF8(params.body);
|
|
|
|
NotifyNotification *notification = notify_notification_new(title.c_str(), body.c_str(), NULL);
|
|
|
|
g_object_set_data(G_OBJECT(notification), kRenderProcessIDKey, GINT_TO_POINTER(render_process_id));
|
|
|
|
g_object_set_data(G_OBJECT(notification), kRenderViewIDKey, GINT_TO_POINTER(render_view_id));
|
|
|
|
g_object_set_data(G_OBJECT(notification), kNotificationIDKey, GINT_TO_POINTER(params.notification_id));
|
2013-12-19 05:14:08 +00:00
|
|
|
g_signal_connect(notification, "closed",
|
|
|
|
G_CALLBACK(NotificationClosedCallback), this);
|
|
|
|
notify_notification_add_action(notification, "default", "View",
|
|
|
|
(NotifyActionCallback)NotificationViewCallback, this, NULL);
|
2013-12-19 05:05:19 +00:00
|
|
|
|
|
|
|
notifications_ = g_list_append(notifications_, notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
notify_notification_show(notification, &error);
|
|
|
|
log_and_clear_error(error, "notify_notification_show");
|
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
auto host = content::RenderViewHost::FromID(render_process_id, render_view_id);
|
2013-12-18 21:02:49 +00:00
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
host->DesktopNotificationPostDisplay(params.notification_id);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationPresenterLinux::CancelNotification(
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id,
|
|
|
|
int notification_id) {
|
2013-12-19 05:05:19 +00:00
|
|
|
NotifyNotification *notification = NULL;
|
|
|
|
for (GList *p = notifications_; p != NULL; p = p->next) {
|
|
|
|
if (render_process_id == GPOINTER_TO_INT(g_object_get_data(G_OBJECT(p->data), kRenderProcessIDKey))
|
|
|
|
&& render_view_id == GPOINTER_TO_INT(g_object_get_data(G_OBJECT(p->data), kRenderViewIDKey))
|
|
|
|
&& notification_id == GPOINTER_TO_INT(g_object_get_data(G_OBJECT(p->data), kNotificationIDKey))) {
|
|
|
|
notification = (NotifyNotification*)p->data;
|
|
|
|
notifications_ = g_list_delete_link(notifications_, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!notification)
|
2013-12-18 21:02:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
notify_notification_close(notification, &error);
|
|
|
|
log_and_clear_error(error, "notify_notification_close");
|
2013-12-19 05:05:19 +00:00
|
|
|
g_object_unref(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
|
|
|
|
auto host = content::RenderViewHost::FromID(render_process_id, render_view_id);
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
2013-12-19 05:05:19 +00:00
|
|
|
host->DesktopNotificationPostClose(notification_id, false);
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:14:08 +00:00
|
|
|
void NotificationPresenterLinux::RemoveNotification(NotifyNotification *notification) {
|
|
|
|
notifications_ = g_list_remove(notifications_, notification);
|
|
|
|
g_object_unref(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|