]> khamidou — GitHub mirrors - electron.git/commitdiff
linux: Fix detect global menu bar
authorCheng Zhao <zcbenz@gmail.com>
Tue, 21 Oct 2014 04:07:34 +0000 (12:07 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 21 Oct 2014 04:10:31 +0000 (12:10 +0800)
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

index ec5a9a666a9adea008c037d6c706886704d781c7..2ac747e334779165b9be719d861457be20aa1d3c 100644 (file)
@@ -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<base::Environment> 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<dbus::Bus> 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<dbus::Response> 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