OLD | NEW |
| (Empty) |
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 | |
5 #include "chrome/browser/ui/libgtk2ui/unity_service.h" | |
6 | |
7 #include <dlfcn.h> | |
8 #include <gtk/gtk.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 | |
13 #include "base/environment.h" | |
14 #include "base/nix/xdg_util.h" | |
15 #include "chrome/browser/shell_integration_linux.h" | |
16 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" | |
17 | |
18 // Unity data typedefs. | |
19 typedef struct _UnityInspector UnityInspector; | |
20 typedef UnityInspector* (*unity_inspector_get_default_func)(void); | |
21 typedef gboolean (*unity_inspector_get_unity_running_func) | |
22 (UnityInspector* self); | |
23 | |
24 typedef struct _UnityLauncherEntry UnityLauncherEntry; | |
25 typedef UnityLauncherEntry* (*unity_launcher_entry_get_for_desktop_id_func) | |
26 (const gchar* desktop_id); | |
27 typedef void (*unity_launcher_entry_set_count_func)(UnityLauncherEntry* self, | |
28 gint64 value); | |
29 typedef void (*unity_launcher_entry_set_count_visible_func) | |
30 (UnityLauncherEntry* self, gboolean value); | |
31 typedef void (*unity_launcher_entry_set_progress_func)(UnityLauncherEntry* self, | |
32 gdouble value); | |
33 typedef void (*unity_launcher_entry_set_progress_visible_func) | |
34 (UnityLauncherEntry* self, gboolean value); | |
35 | |
36 | |
37 namespace { | |
38 | |
39 bool attempted_load = false; | |
40 | |
41 // Unity has a singleton object that we can ask whether the unity is running. | |
42 UnityInspector* inspector = NULL; | |
43 | |
44 // A link to the desktop entry in the panel. | |
45 UnityLauncherEntry* chrome_entry = NULL; | |
46 | |
47 // Retrieved functions from libunity. | |
48 unity_inspector_get_unity_running_func get_unity_running = NULL; | |
49 unity_launcher_entry_set_count_func entry_set_count = NULL; | |
50 unity_launcher_entry_set_count_visible_func entry_set_count_visible = NULL; | |
51 unity_launcher_entry_set_progress_func entry_set_progress = NULL; | |
52 unity_launcher_entry_set_progress_visible_func entry_set_progress_visible = | |
53 NULL; | |
54 | |
55 void EnsureMethodsLoaded() { | |
56 using base::nix::GetDesktopEnvironment; | |
57 | |
58 if (attempted_load) | |
59 return; | |
60 attempted_load = true; | |
61 | |
62 std::unique_ptr<base::Environment> env(base::Environment::Create()); | |
63 base::nix::DesktopEnvironment desktop_env = | |
64 GetDesktopEnvironment(env.get()); | |
65 | |
66 // The "icon-tasks" KDE task manager also honors Unity Launcher API. | |
67 if (desktop_env != base::nix::DESKTOP_ENVIRONMENT_UNITY && | |
68 desktop_env != base::nix::DESKTOP_ENVIRONMENT_KDE4 && | |
69 desktop_env != base::nix::DESKTOP_ENVIRONMENT_KDE5) | |
70 return; | |
71 | |
72 // Ubuntu still hasn't given us a nice libunity.so symlink. | |
73 void* unity_lib = dlopen("libunity.so.4", RTLD_LAZY); | |
74 if (!unity_lib) | |
75 unity_lib = dlopen("libunity.so.6", RTLD_LAZY); | |
76 if (!unity_lib) | |
77 unity_lib = dlopen("libunity.so.9", RTLD_LAZY); | |
78 if (!unity_lib) | |
79 return; | |
80 | |
81 unity_inspector_get_default_func inspector_get_default = | |
82 reinterpret_cast<unity_inspector_get_default_func>( | |
83 dlsym(unity_lib, "unity_inspector_get_default")); | |
84 if (inspector_get_default) { | |
85 inspector = inspector_get_default(); | |
86 | |
87 get_unity_running = | |
88 reinterpret_cast<unity_inspector_get_unity_running_func>( | |
89 dlsym(unity_lib, "unity_inspector_get_unity_running")); | |
90 } | |
91 | |
92 unity_launcher_entry_get_for_desktop_id_func entry_get_for_desktop_id = | |
93 reinterpret_cast<unity_launcher_entry_get_for_desktop_id_func>( | |
94 dlsym(unity_lib, "unity_launcher_entry_get_for_desktop_id")); | |
95 if (entry_get_for_desktop_id) { | |
96 std::string desktop_id = libgtk2ui::GetDesktopName(env.get()); | |
97 chrome_entry = entry_get_for_desktop_id(desktop_id.c_str()); | |
98 | |
99 entry_set_count = | |
100 reinterpret_cast<unity_launcher_entry_set_count_func>( | |
101 dlsym(unity_lib, "unity_launcher_entry_set_count")); | |
102 | |
103 entry_set_count_visible = | |
104 reinterpret_cast<unity_launcher_entry_set_count_visible_func>( | |
105 dlsym(unity_lib, "unity_launcher_entry_set_count_visible")); | |
106 | |
107 entry_set_progress = | |
108 reinterpret_cast<unity_launcher_entry_set_progress_func>( | |
109 dlsym(unity_lib, "unity_launcher_entry_set_progress")); | |
110 | |
111 entry_set_progress_visible = | |
112 reinterpret_cast<unity_launcher_entry_set_progress_visible_func>( | |
113 dlsym(unity_lib, "unity_launcher_entry_set_progress_visible")); | |
114 } | |
115 } | |
116 | |
117 } // namespace | |
118 | |
119 | |
120 namespace unity { | |
121 | |
122 bool IsRunning() { | |
123 EnsureMethodsLoaded(); | |
124 if (inspector && get_unity_running) | |
125 return get_unity_running(inspector); | |
126 | |
127 return false; | |
128 } | |
129 | |
130 void SetDownloadCount(int count) { | |
131 EnsureMethodsLoaded(); | |
132 if (chrome_entry && entry_set_count && entry_set_count_visible) { | |
133 entry_set_count(chrome_entry, count); | |
134 entry_set_count_visible(chrome_entry, count != 0); | |
135 } | |
136 } | |
137 | |
138 void SetProgressFraction(float percentage) { | |
139 EnsureMethodsLoaded(); | |
140 if (chrome_entry && entry_set_progress && entry_set_progress_visible) { | |
141 entry_set_progress(chrome_entry, percentage); | |
142 entry_set_progress_visible(chrome_entry, | |
143 percentage > 0.0 && percentage < 1.0); | |
144 } | |
145 } | |
146 | |
147 } // namespace unity | |
OLD | NEW |