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"
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
2015-09-28 18:09:19 +00:00
|
|
|
#include "base/files/file_enumerator.h"
|
2015-10-01 09:30:59 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2013-12-18 21:02:49 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-04-20 20:22:18 +00:00
|
|
|
#include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
|
2014-07-27 10:27:41 +00:00
|
|
|
#include "content/public/browser/desktop_notification_delegate.h"
|
2015-03-14 18:34:53 +00:00
|
|
|
#include "content/public/common/platform_notification_data.h"
|
2013-12-18 21:02:49 +00:00
|
|
|
#include "common/application_info.h"
|
2015-09-30 16:14:31 +00:00
|
|
|
#include <stdlib.h>
|
2015-08-04 08:43:26 +00:00
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2015-04-20 20:22:18 +00:00
|
|
|
|
2013-12-18 21:02:49 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-10-01 09:30:59 +00:00
|
|
|
bool unity_has_result = false;
|
|
|
|
bool unity_result = false;
|
2015-04-20 22:31:24 +00:00
|
|
|
|
2015-10-01 09:30:59 +00:00
|
|
|
bool UnityIsRunning() {
|
|
|
|
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
|
|
|
|
return true;
|
2015-09-28 18:09:19 +00:00
|
|
|
|
2015-10-01 09:30:59 +00:00
|
|
|
if (unity_has_result)
|
2015-04-20 22:31:24 +00:00
|
|
|
return unity_result;
|
2015-09-30 16:14:31 +00:00
|
|
|
|
2015-10-01 09:30:59 +00:00
|
|
|
unity_has_result = true;
|
|
|
|
|
|
|
|
// Look for the presence of libunity as our hint that we're under Ubuntu.
|
|
|
|
base::FileEnumerator enumerator(base::FilePath("/usr/lib"),
|
|
|
|
false, base::FileEnumerator::FILES);
|
|
|
|
base::FilePath haystack;
|
|
|
|
while (!((haystack = enumerator.Next()).empty())) {
|
|
|
|
if (base::StartsWith(haystack.value(), "/usr/lib/libunity-", base::CompareCase::SENSITIVE)) {
|
2015-09-28 18:09:19 +00:00
|
|
|
unity_result = true;
|
2015-10-01 09:30:59 +00:00
|
|
|
break;
|
2015-09-28 18:09:19 +00:00
|
|
|
}
|
2015-04-20 22:31:24 +00:00
|
|
|
}
|
2015-10-01 09:30:59 +00:00
|
|
|
|
2015-04-20 22:31:24 +00:00
|
|
|
return unity_result;
|
|
|
|
}
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
void log_and_clear_error(GError* error, const char* context) {
|
2013-12-19 22:01:01 +00:00
|
|
|
LOG(ERROR) << context
|
|
|
|
<< ": domain=" << error->domain
|
|
|
|
<< " code=" << error->code
|
|
|
|
<< " message=\"" << error->message << '"';
|
|
|
|
g_error_free(error);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
content::DesktopNotificationDelegate* GetDelegateFromNotification(
|
|
|
|
NotifyNotification* notification) {
|
|
|
|
return static_cast<content::DesktopNotificationDelegate*>(
|
|
|
|
g_object_get_data(G_OBJECT(notification), "delegate"));
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 19:06:05 +00:00
|
|
|
} // namespace
|
2013-12-18 21:02:49 +00:00
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
// static
|
2013-12-18 21:02:49 +00:00
|
|
|
NotificationPresenter* NotificationPresenter::Create() {
|
|
|
|
if (!notify_is_initted()) {
|
|
|
|
notify_init(GetApplicationName().c_str());
|
|
|
|
}
|
|
|
|
return new NotificationPresenterLinux;
|
|
|
|
}
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
NotificationPresenterLinux::NotificationPresenterLinux()
|
|
|
|
: notifications_(nullptr) {
|
|
|
|
}
|
2013-12-19 05:05:19 +00:00
|
|
|
|
|
|
|
NotificationPresenterLinux::~NotificationPresenterLinux() {
|
2013-12-19 05:14:08 +00:00
|
|
|
// unref any outstanding notifications, and then free the list.
|
2014-07-27 10:27:41 +00:00
|
|
|
if (notifications_)
|
|
|
|
g_list_free_full(notifications_, g_object_unref);
|
2013-12-19 05:05:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 21:02:49 +00:00
|
|
|
void NotificationPresenterLinux::ShowNotification(
|
2015-03-14 18:34:53 +00:00
|
|
|
const content::PlatformNotificationData& data,
|
2015-04-20 20:20:50 +00:00
|
|
|
const SkBitmap& icon,
|
2014-10-13 03:02:56 +00:00
|
|
|
scoped_ptr<content::DesktopNotificationDelegate> delegate_ptr,
|
2014-07-27 10:27:41 +00:00
|
|
|
base::Closure* cancel_callback) {
|
2015-03-14 18:34:53 +00:00
|
|
|
std::string title = base::UTF16ToUTF8(data.title);
|
|
|
|
std::string body = base::UTF16ToUTF8(data.body);
|
2014-07-27 10:27:41 +00:00
|
|
|
NotifyNotification* notification = notify_notification_new(title.c_str(), body.c_str(), nullptr);
|
|
|
|
|
2014-10-13 03:02:56 +00:00
|
|
|
content::DesktopNotificationDelegate* delegate = delegate_ptr.release();
|
|
|
|
|
|
|
|
g_object_set_data_full(G_OBJECT(notification), "delegate", delegate, operator delete);
|
2014-07-27 10:27:41 +00:00
|
|
|
g_signal_connect(notification, "closed", G_CALLBACK(OnNotificationClosedThunk), this);
|
2015-04-20 20:22:18 +00:00
|
|
|
|
|
|
|
// NB: On Unity, adding a notification action will cause the notification
|
|
|
|
// to display as a modal dialog box. Testing for distros that have "Unity
|
2015-04-20 22:31:24 +00:00
|
|
|
// Zen Nature" is difficult, we will test for the presence of the indicate
|
|
|
|
// dbus service
|
|
|
|
if (!UnityIsRunning()) {
|
2015-06-05 04:07:27 +00:00
|
|
|
notify_notification_add_action(
|
|
|
|
notification, "default", "View", OnNotificationViewThunk, this, nullptr);
|
2015-04-20 20:22:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 08:43:26 +00:00
|
|
|
if (!icon.drawsNothing()) {
|
|
|
|
GdkPixbuf* pixbuf = libgtk2ui::GdkPixbufFromSkBitmap(icon);
|
|
|
|
notify_notification_set_image_from_pixbuf(notification, pixbuf);
|
|
|
|
notify_notification_set_timeout(notification, NOTIFY_EXPIRES_DEFAULT);
|
|
|
|
g_object_unref(pixbuf);
|
|
|
|
}
|
2014-07-27 10:27:41 +00:00
|
|
|
|
|
|
|
GError* error = nullptr;
|
|
|
|
notify_notification_show(notification, &error);
|
|
|
|
if (error) {
|
|
|
|
log_and_clear_error(error, "notify_notification_show");
|
2015-01-28 01:18:46 +00:00
|
|
|
g_object_unref(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
return;
|
2014-07-27 10:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
notifications_ = g_list_append(notifications_, notification);
|
|
|
|
delegate->NotificationDisplayed();
|
2013-12-18 21:02:49 +00:00
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
if (cancel_callback)
|
|
|
|
*cancel_callback = base::Bind(
|
|
|
|
&NotificationPresenterLinux::CancelNotification,
|
|
|
|
base::Unretained(this),
|
|
|
|
notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
void NotificationPresenterLinux::CancelNotification(NotifyNotification* notification) {
|
|
|
|
GError* error = nullptr;
|
|
|
|
notify_notification_close(notification, &error);
|
|
|
|
if (error)
|
|
|
|
log_and_clear_error(error, "notify_notification_close");
|
2013-12-18 21:02:49 +00:00
|
|
|
|
2015-05-22 06:51:16 +00:00
|
|
|
GetDelegateFromNotification(notification)->NotificationClosed();
|
2014-07-27 10:27:41 +00:00
|
|
|
DeleteNotification(notification);
|
|
|
|
}
|
2013-12-18 21:02:49 +00:00
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
void NotificationPresenterLinux::DeleteNotification(NotifyNotification* notification) {
|
|
|
|
notifications_ = g_list_remove(notifications_, notification);
|
|
|
|
g_object_unref(notification);
|
|
|
|
}
|
2013-12-18 21:02:49 +00:00
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
void NotificationPresenterLinux::OnNotificationClosed(NotifyNotification* notification) {
|
2015-08-04 08:44:32 +00:00
|
|
|
if (!notification)
|
|
|
|
return;
|
2015-05-22 06:51:16 +00:00
|
|
|
GetDelegateFromNotification(notification)->NotificationClosed();
|
2014-07-27 10:27:41 +00:00
|
|
|
DeleteNotification(notification);
|
2013-12-19 05:05:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 10:27:41 +00:00
|
|
|
void NotificationPresenterLinux::OnNotificationView(
|
|
|
|
NotifyNotification* notification, char* action) {
|
2015-08-04 08:44:32 +00:00
|
|
|
if (!notification)
|
|
|
|
return;
|
2014-07-27 10:27:41 +00:00
|
|
|
GetDelegateFromNotification(notification)->NotificationClick();
|
|
|
|
DeleteNotification(notification);
|
2013-12-18 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 19:06:05 +00:00
|
|
|
} // namespace brightray
|