Browse Source

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
Charles Kerr 7 years ago
parent
commit
dd2c2660b9

+ 3 - 4
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<base::Environment> 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;

+ 3 - 4
brightray/browser/linux/libnotify_notification.cc

@@ -8,13 +8,13 @@
 #include <string>
 #include <vector>
 
-#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<base::Environment> 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)) {

+ 24 - 0
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 <string>
+
+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_

+ 30 - 0
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<base::Environment> 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

+ 2 - 0
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',
     ],
   },
 }