electron/shell/browser/linux/unity_service.cc
Charles Kerr aa23198ad8
chore: remove more unused #include calls (#43000)
* chore: in shell/renderer/renderer_client_base.h, remove include media/base/key_systems_support_registration.h

last use removed in c670e38b (##41610)

* chore: iwyu electron/fuses.h

* chore: iwyu media/base/video_frame.h

* chore: iwyu base/functional/callback.h

* chore: iwyu base/task/cancelable_task_tracker.h

* chore: iwyu shell/browser/draggable_region_provider.h

* chore: iwyu shell/browser/ui/inspectable_web_contents_view.h

* chore: iwyu ui/aura/window.h

* chore: iwyu ui/base/win/shell.h

* chore: iwyu ui/display/win/screen_win.h

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

* chore: iwyu ui/display/display.h

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

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

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

* chore: iwyu ui/gfx/scoped_canvas.h

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

* chore: iwyu ui/accessibility/ax_node_data.h

* chore: iwyu ui/views/animation/ink_drop_highlight.h

* chore: iwyu ui/gfx/font_list.h

* chore: iwyu ui/linux/nav_button_provider.h

* chore: iwyu shell/browser/ui/views/frameless_view.h

* chore: iwyu services/metrics/public/cpp/ukm_source_id.h

* chore: iwyu net/http/http_util.h

* chore: iwyu net/base/mime_util.h

* chore: iwyu content/public/common/content_client.h

* chore: iwyu <list>

* chore: iwyu <optional>

* chore: iwyu <memory>

* chore: iwyu base/files/file_path.h

* chore: iwyu ui/base/cursor/cursor.h

* chore: iwyu build/build_config.h

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

* chore: iwyu shell/browser/hid/hid_chooser_context.h

* chore: iwyu shell/common/platform_util.h

* chore: iwyu base/task/single_thread_task_runner.h

* chore: iwyu content/browser/renderer_host/render_widget_host_impl.h

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

* chore: iwyu shell/browser/electron_browser_context.h

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

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

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

* chore: iwyu chrome/common/chrome_paths.h

* chore: iwyu chrome/browser/icon_manager.h

* chore: iwyu printing/print_settings.h

* chore: iwyu renderer/pepper_helper.h

* chore: iwyu shell/browser/api/process_metric.h

* chore: iwyu shell/browser/electron_browser_client.h

* chore: iwyu shell/browser/electron_browser_context.h

* chore: iwyu shell/browser/api/electron_api_session.h

* chore: iwyu shell/browser/api/electron_api_app.h

* chore: iwyu shell/browser/ui/views/client_frame_view_linux.h

* chore: iwyu shell/browser/native_window_views.h

* chore: iwyu base/win/windows_version.h

* chore: iwyu shell/common/electron_paths.h

* chore: iwyu content/public/common/content_switches.h

* chore: iwyu third_party/skia/include/core/SkRRect.h

* chore: iwyu third_party/skia/include/core/SkBitmap.h

* chore: iwyu third_party/skia

* chore: iwyu shell/browser/osr/osr_host_display_client.h

* chore: iwyu shell/browser/login_handler.h

* chore: iwyu shell/browser/javascript_environment.h

* chore: iwyu shell/browser/event_emitter_mixin.h

* fix: mac

* fix: mac

* chore: iwyu base/nix/xdg_util.h

* fix: win

* fix: win

* fix: win

* fix: win
2024-07-25 11:25:45 +02:00

128 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>
// 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