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