electron/shell/browser/linux/unity_service.cc
Charles Kerr 60c4c9fec6
chore: remove unused #includes (#42971)
* chore: iwyu buildflags.h

* chore: iwyu dictionary.h

* chore: iwyu arguments.h

* chore: iwyu values.h

* chore: iwyu compiler_specific.h

* chore: iwyu binder_map.h

* chore: iwyu <vector>

* chore: iwyu <set>

* chore: iwyu raw_ptr

* chore: iwyu gfx/canvas.h

* chore: iwyu gfx/color_utils.h

* chore: iwyu base/strings/stringprintf.h

* chore: iwyu base/task/thread_pool.h

* chore: iwyu base/no_destructor.h

* chore: iwyu base/path_service.h

* chore: iwyu base/files/file_pathh

* chore: iwyu base/strings/sys_string_conversions.h

* chore: iwyu base/logging.h

* chore: iwyu base/command_line.h

* chore: iwyu base/files/file_util.h

* chore: iwyu base/files/scoped_file.h

* chore: iwyu base/strings/utf_string_conversions.h

* chore: iwyu base/environment.h

* chore: iwyu base/scoped_observation.h

* chore: iwyu base/strings/string_split.h

* chore: iwyu base/strings/pattern.h

* chore: iwyu base/json/string_escape.h

* chore: iwyu base/json/json_reader.h

* chore: iwyu base/memory/singleton.h

* chore: iwyu base/observer_list.h

* chore: iwyu base/timer/timer.h

* fixup! chore: iwyu values.h

* chore: iwyu shell/browser/browser.h

* chore: iwyu base/stl_util.h

* chore: iwyu base/strings/string_util.h

* chore: iwyu shell/browser/javascript_environment.h

* chore: iwyu base/memory/ref_counted.h

* chore: iwyu base/environment.h

* chore: iwyu content/public/browser/browser_thread.h

* chore: remove unused typedef gin_helper::EventEmitter::ValueArray

* chore: iwyu gin/wrappable.h

* chore: iwyu shell/common/gin_helper/function_template_extensions.h

* chore: iwyu shell/common/gin_converters/login_item_settings_converter.h

* chore: iwyu shell/common/gin_helper/arguments.h

* chore: iwyu ui/gfx/skia_util.h

* chore: iwyu ui/gfx/geometry/rect.h

* chore: iwyu ui/gfx/image/image.h

* chore: iwyu base/strings/strcat.h

* chore: iwyu ui/native_theme/native_theme.h

* fixup! chore: iwyu shell/browser/javascript_environment.h

* fixup! chore: iwyu gfx/canvas.h

* fixup! chore: iwyu content/public/browser/browser_thread.h

* fixup! chore: iwyu ui/native_theme/native_theme.h

* fixup! chore: iwyu ui/native_theme/native_theme.h
2024-07-22 11:31:32 +02:00

130 lines
4.3 KiB
C++

// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shell/browser/linux/unity_service.h"
#include <dlfcn.h>
#include <gtk/gtk.h>
#include <string>
#include "base/nix/xdg_util.h"
// Unity data typedefs.
typedef struct _UnityInspector UnityInspector;
typedef UnityInspector* (*unity_inspector_get_default_func)();
typedef gboolean (*unity_inspector_get_unity_running_func)(
UnityInspector* self);
typedef struct _UnityLauncherEntry UnityLauncherEntry;
typedef UnityLauncherEntry* (*unity_launcher_entry_get_for_desktop_id_func)(
const gchar* desktop_id);
typedef void (*unity_launcher_entry_set_count_func)(UnityLauncherEntry* self,
gint64 value);
typedef void (*unity_launcher_entry_set_count_visible_func)(
UnityLauncherEntry* self,
gboolean value);
typedef void (*unity_launcher_entry_set_progress_func)(UnityLauncherEntry* self,
gdouble value);
typedef void (*unity_launcher_entry_set_progress_visible_func)(
UnityLauncherEntry* self,
gboolean value);
namespace {
bool attempted_load = false;
// Unity has a singleton object that we can ask whether the unity is running.
UnityInspector* inspector = nullptr;
// A link to the desktop entry in the panel.
UnityLauncherEntry* chrome_entry = nullptr;
// Retrieved functions from libunity.
unity_inspector_get_unity_running_func get_unity_running = nullptr;
unity_launcher_entry_set_count_func entry_set_count = nullptr;
unity_launcher_entry_set_count_visible_func entry_set_count_visible = nullptr;
unity_launcher_entry_set_progress_func entry_set_progress = nullptr;
unity_launcher_entry_set_progress_visible_func entry_set_progress_visible =
nullptr;
void EnsureLibUnityLoaded() {
if (attempted_load)
return;
attempted_load = true;
// Ubuntu still hasn't given us a nice libunity.so symlink.
void* unity_lib = dlopen("libunity.so.4", RTLD_LAZY);
if (!unity_lib)
unity_lib = dlopen("libunity.so.6", RTLD_LAZY);
if (!unity_lib)
unity_lib = dlopen("libunity.so.9", RTLD_LAZY);
if (!unity_lib)
return;
unity_inspector_get_default_func inspector_get_default =
reinterpret_cast<unity_inspector_get_default_func>(
dlsym(unity_lib, "unity_inspector_get_default"));
if (inspector_get_default) {
inspector = inspector_get_default();
get_unity_running =
reinterpret_cast<unity_inspector_get_unity_running_func>(
dlsym(unity_lib, "unity_inspector_get_unity_running"));
}
unity_launcher_entry_get_for_desktop_id_func entry_get_for_desktop_id =
reinterpret_cast<unity_launcher_entry_get_for_desktop_id_func>(
dlsym(unity_lib, "unity_launcher_entry_get_for_desktop_id"));
if (entry_get_for_desktop_id) {
std::string desktop_id = getenv("CHROME_DESKTOP");
chrome_entry = entry_get_for_desktop_id(desktop_id.c_str());
entry_set_count = reinterpret_cast<unity_launcher_entry_set_count_func>(
dlsym(unity_lib, "unity_launcher_entry_set_count"));
entry_set_count_visible =
reinterpret_cast<unity_launcher_entry_set_count_visible_func>(
dlsym(unity_lib, "unity_launcher_entry_set_count_visible"));
entry_set_progress =
reinterpret_cast<unity_launcher_entry_set_progress_func>(
dlsym(unity_lib, "unity_launcher_entry_set_progress"));
entry_set_progress_visible =
reinterpret_cast<unity_launcher_entry_set_progress_visible_func>(
dlsym(unity_lib, "unity_launcher_entry_set_progress_visible"));
}
}
} // namespace
namespace unity {
bool IsRunning() {
EnsureLibUnityLoaded();
if (inspector && get_unity_running)
return get_unity_running(inspector);
return false;
}
void SetDownloadCount(int count) {
EnsureLibUnityLoaded();
if (chrome_entry && entry_set_count && entry_set_count_visible) {
entry_set_count(chrome_entry, count);
entry_set_count_visible(chrome_entry, count != 0);
}
}
void SetProgressFraction(float percentage) {
EnsureLibUnityLoaded();
if (chrome_entry && entry_set_progress && entry_set_progress_visible) {
entry_set_progress(chrome_entry, percentage);
entry_set_progress_visible(chrome_entry,
percentage > 0.0 && percentage < 1.0);
}
}
} // namespace unity