unity_service.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/linux/unity_service.h"
  5. #include <dlfcn.h>
  6. #include <gtk/gtk.h>
  7. #include <string>
  8. // Unity data typedefs.
  9. using UnityInspector = struct _UnityInspector;
  10. using unity_inspector_get_default_func = UnityInspector* (*)();
  11. using unity_inspector_get_unity_running_func =
  12. gboolean (*)(UnityInspector* self);
  13. using UnityLauncherEntry = struct _UnityLauncherEntry;
  14. using unity_launcher_entry_get_for_desktop_id_func =
  15. UnityLauncherEntry* (*)(const gchar* desktop_id);
  16. using unity_launcher_entry_set_count_func = void (*)(UnityLauncherEntry* self,
  17. gint64 value);
  18. using unity_launcher_entry_set_count_visible_func =
  19. void (*)(UnityLauncherEntry* self, gboolean value);
  20. using unity_launcher_entry_set_progress_func =
  21. void (*)(UnityLauncherEntry* self, gdouble value);
  22. using unity_launcher_entry_set_progress_visible_func =
  23. void (*)(UnityLauncherEntry* self, gboolean value);
  24. namespace {
  25. bool attempted_load = false;
  26. // Unity has a singleton object that we can ask whether the unity is running.
  27. UnityInspector* inspector = nullptr;
  28. // A link to the desktop entry in the panel.
  29. UnityLauncherEntry* chrome_entry = nullptr;
  30. // Retrieved functions from libunity.
  31. unity_inspector_get_unity_running_func get_unity_running = nullptr;
  32. unity_launcher_entry_set_count_func entry_set_count = nullptr;
  33. unity_launcher_entry_set_count_visible_func entry_set_count_visible = nullptr;
  34. unity_launcher_entry_set_progress_func entry_set_progress = nullptr;
  35. unity_launcher_entry_set_progress_visible_func entry_set_progress_visible =
  36. nullptr;
  37. void EnsureLibUnityLoaded() {
  38. if (attempted_load)
  39. return;
  40. attempted_load = true;
  41. // Ubuntu still hasn't given us a nice libunity.so symlink.
  42. void* unity_lib = dlopen("libunity.so.4", RTLD_LAZY);
  43. if (!unity_lib)
  44. unity_lib = dlopen("libunity.so.6", RTLD_LAZY);
  45. if (!unity_lib)
  46. unity_lib = dlopen("libunity.so.9", RTLD_LAZY);
  47. if (!unity_lib)
  48. return;
  49. unity_inspector_get_default_func inspector_get_default =
  50. reinterpret_cast<unity_inspector_get_default_func>(
  51. dlsym(unity_lib, "unity_inspector_get_default"));
  52. if (inspector_get_default) {
  53. inspector = inspector_get_default();
  54. get_unity_running =
  55. reinterpret_cast<unity_inspector_get_unity_running_func>(
  56. dlsym(unity_lib, "unity_inspector_get_unity_running"));
  57. }
  58. unity_launcher_entry_get_for_desktop_id_func entry_get_for_desktop_id =
  59. reinterpret_cast<unity_launcher_entry_get_for_desktop_id_func>(
  60. dlsym(unity_lib, "unity_launcher_entry_get_for_desktop_id"));
  61. if (entry_get_for_desktop_id) {
  62. std::string desktop_id = getenv("CHROME_DESKTOP");
  63. chrome_entry = entry_get_for_desktop_id(desktop_id.c_str());
  64. entry_set_count = reinterpret_cast<unity_launcher_entry_set_count_func>(
  65. dlsym(unity_lib, "unity_launcher_entry_set_count"));
  66. entry_set_count_visible =
  67. reinterpret_cast<unity_launcher_entry_set_count_visible_func>(
  68. dlsym(unity_lib, "unity_launcher_entry_set_count_visible"));
  69. entry_set_progress =
  70. reinterpret_cast<unity_launcher_entry_set_progress_func>(
  71. dlsym(unity_lib, "unity_launcher_entry_set_progress"));
  72. entry_set_progress_visible =
  73. reinterpret_cast<unity_launcher_entry_set_progress_visible_func>(
  74. dlsym(unity_lib, "unity_launcher_entry_set_progress_visible"));
  75. }
  76. }
  77. } // namespace
  78. namespace unity {
  79. bool IsRunning() {
  80. EnsureLibUnityLoaded();
  81. if (inspector && get_unity_running)
  82. return get_unity_running(inspector);
  83. return false;
  84. }
  85. void SetDownloadCount(int count) {
  86. EnsureLibUnityLoaded();
  87. if (chrome_entry && entry_set_count && entry_set_count_visible) {
  88. entry_set_count(chrome_entry, count);
  89. entry_set_count_visible(chrome_entry, count != 0);
  90. }
  91. }
  92. void SetProgressFraction(float percentage) {
  93. EnsureLibUnityLoaded();
  94. if (chrome_entry && entry_set_progress && entry_set_progress_visible) {
  95. entry_set_progress(chrome_entry, percentage);
  96. entry_set_progress_visible(chrome_entry,
  97. percentage > 0.0 && percentage < 1.0);
  98. }
  99. }
  100. } // namespace unity