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.
This commit is contained in:
Cheng Zhao 2014-10-21 12:07:34 +08:00
parent 593dce23a5
commit f82de6528b

View 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