From dd2c2660b91647acd97d1ed109e540888a055328 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 12 Mar 2018 16:02:47 +0900 Subject: [PATCH] Fix desktop-id notification edge case (#12216) * Fix desktop-id notification edge case * Extract-method platform_util::GetDesktopName() This removes duplicated code from libnotify_notifications.cc and atom/common/linux/application_info.cc. * Check for empty case in GetDesktopName(). * Move GetDesktopName() to brightray::util * Remove unnecessary changes in platform_util * Add a brightray::platform_util namespace --- atom/common/linux/application_info.cc | 7 ++--- .../browser/linux/libnotify_notification.cc | 7 ++--- brightray/common/platform_util.h | 24 +++++++++++++++ brightray/common/platform_util_linux.cc | 30 +++++++++++++++++++ brightray/filenames.gypi | 2 ++ 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 brightray/common/platform_util.h create mode 100644 brightray/common/platform_util_linux.cc diff --git a/atom/common/linux/application_info.cc b/atom/common/linux/application_info.cc index 37661ed1750d..05bd7f6fc150 100644 --- a/atom/common/linux/application_info.cc +++ b/atom/common/linux/application_info.cc @@ -13,6 +13,7 @@ #include "atom/common/atom_version.h" #include "base/environment.h" #include "base/logging.h" +#include "brightray/common/platform_util.h" #include "chrome/browser/ui/libgtkui/gtk_util.h" namespace { @@ -20,10 +21,8 @@ namespace { GDesktopAppInfo* get_desktop_app_info() { GDesktopAppInfo * ret = nullptr; - std::unique_ptr env(base::Environment::Create()); - const std::string desktop_id = libgtkui::GetDesktopName(env.get()); - const char * libcc_default_id = "chromium-browser.desktop"; - if (!desktop_id.empty() && (desktop_id != libcc_default_id)) + std::string desktop_id; + if (brightray::platform_util::GetDesktopName(&desktop_id)) ret = g_desktop_app_info_new(desktop_id.c_str()); return ret; diff --git a/brightray/browser/linux/libnotify_notification.cc b/brightray/browser/linux/libnotify_notification.cc index c1a72b3cc0ec..7f9fb35e434f 100644 --- a/brightray/browser/linux/libnotify_notification.cc +++ b/brightray/browser/linux/libnotify_notification.cc @@ -8,13 +8,13 @@ #include #include -#include "base/environment.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/browser/notification_delegate.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" @@ -130,9 +130,8 @@ void LibnotifyNotification::Show(const NotificationOptions& options) { // Send the desktop name to identify the application // The desktop-entry is the part before the .desktop - std::unique_ptr env(base::Environment::Create()); - std::string desktop_id = libgtkui::GetDesktopName(env.get()); - if (!desktop_id.empty()) { + std::string desktop_id; + if (platform_util::GetDesktopName(&desktop_id)) { const std::string suffix{".desktop"}; if (base::EndsWith(desktop_id, suffix, base::CompareCase::INSENSITIVE_ASCII)) { diff --git a/brightray/common/platform_util.h b/brightray/common/platform_util.h new file mode 100644 index 000000000000..48b5b4f015ed --- /dev/null +++ b/brightray/common/platform_util.h @@ -0,0 +1,24 @@ +// Copyright (c) 2018 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef BRIGHTRAY_COMMON_PLATFORM_UTIL_H_ +#define BRIGHTRAY_COMMON_PLATFORM_UTIL_H_ + +#include + +namespace brightray { + +namespace platform_util { + +#if defined(OS_LINUX) +// Returns a success flag. +// Unlike libgtkui, does *not* use "chromium-browser.desktop" as a fallback. +bool GetDesktopName(std::string* setme); +#endif + +} // namespace platform_util + +} // namespace brightray + +#endif // BRIGHTRAY_COMMON_PLATFORM_UTIL_H_ diff --git a/brightray/common/platform_util_linux.cc b/brightray/common/platform_util_linux.cc new file mode 100644 index 000000000000..994f755ca47b --- /dev/null +++ b/brightray/common/platform_util_linux.cc @@ -0,0 +1,30 @@ +// Copyright (c) 2018 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "brightray/common/platform_util.h" + +#include "base/environment.h" +#include "chrome/browser/ui/libgtkui/gtk_util.h" + +namespace brightray { + +namespace platform_util { + +bool GetDesktopName(std::string* setme) { + bool found = false; + + std::unique_ptr env(base::Environment::Create()); + std::string desktop_id = libgtkui::GetDesktopName(env.get()); + constexpr char const* libcc_default_id = "chromium-browser.desktop"; + if (!desktop_id.empty() && (desktop_id != libcc_default_id)) { + *setme = desktop_id; + found = true; + } + + return found; +} + +} // namespace platform_util + +} // namespace brightray diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index 6a7d91f135c4..9ddd990cfa52 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -124,6 +124,8 @@ 'common/main_delegate_mac.mm', 'common/switches.cc', 'common/switches.h', + 'common/platform_util_linux.cc', + 'common/platform_util.h', ], }, }