From f82de6528b4a25ec4d04749a653223c791669ed3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 21 Oct 2014 12:07:34 +0800 Subject: [PATCH] linux: Fix detect global menu bar It seems that detecting the bus name "com.canonical.AppMenu.Registrar" is the most reliable way to detect whether the appmenu is available, see http://git.io/JmP7Yg for the discussion. --- atom/browser/native_window_views.cc | 43 +++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index ec5a9a666a9a..2ac747e33477 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -37,6 +37,9 @@ #include "base/nix/xdg_util.h" #include "base/strings/stringprintf.h" #include "chrome/browser/ui/libgtk2ui/unity_service.h" +#include "dbus/bus.h" +#include "dbus/object_proxy.h" +#include "dbus/message.h" #include "ui/base/x/x11_util.h" #include "ui/gfx/x/x11_types.h" #include "ui/views/window/native_frame_view.h" @@ -64,16 +67,38 @@ const int kMenuBarHeight = 25; // window role for X11. int kWindowsCreated = 0; +// Returns true if the bus name "com.canonical.AppMenu.Registrar" is available. bool ShouldUseGlobalMenuBar() { - // Some DE would pretend to be Unity but don't have global application menu, - // so we can not trust unity::IsRunning(). - scoped_ptr env(base::Environment::Create()); - bool is_unity = unity::IsRunning() && - base::nix::GetDesktopEnvironment(env.get()) == - base::nix::DESKTOP_ENVIRONMENT_UNITY; - std::string menu_proxy; - return is_unity && env->GetVar("UBUNTU_MENUPROXY", &menu_proxy) && - menu_proxy.length() > 1; + dbus::Bus::Options options; + scoped_refptr bus(new dbus::Bus(options)); + + dbus::ObjectProxy* object_proxy = + bus->GetObjectProxy(DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS)); + dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, "ListNames"); + scoped_ptr response(object_proxy->CallMethodAndBlock( + &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); + if (!response) { + bus->ShutdownAndBlock(); + return false; + } + + dbus::MessageReader reader(response.get()); + dbus::MessageReader array_reader(NULL); + if (!reader.PopArray(&array_reader)) { + bus->ShutdownAndBlock(); + return false; + } + while (array_reader.HasMoreData()) { + std::string name; + if (array_reader.PopString(&name) && + name == "com.canonical.AppMenu.Registrar") { + bus->ShutdownAndBlock(); + return true; + } + } + + bus->ShutdownAndBlock(); + return false; } #endif