Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 | |
| 9 #include "base/environment.h" | |
| 10 #include "base/scoped_ptr.h" | |
| 11 #include "chrome/browser/shell_integration.h" | |
| 12 | |
| 13 // Unity data typedefs. | |
| 14 typedef struct _UnityInspector UnityInspector; | |
| 15 typedef UnityInspector* (*unity_inspector_get_default_func)(void); | |
| 16 typedef gboolean (*unity_inspector_get_unity_running_func) | |
| 17 (UnityInspector* self); | |
| 18 | |
| 19 typedef struct _UnityLauncherEntry UnityLauncherEntry; | |
| 20 typedef UnityLauncherEntry* (*unity_launcher_entry_get_for_desktop_id_func) | |
| 21 (const gchar* desktop_id); | |
| 22 typedef void (*unity_launcher_entry_set_count_func)(UnityLauncherEntry* self, | |
| 23 gint64 value); | |
| 24 typedef void (*unity_launcher_entry_set_count_visible_func) | |
| 25 (UnityLauncherEntry* self, gboolean value); | |
| 26 typedef void (*unity_launcher_entry_set_progress_func)(UnityLauncherEntry* self, | |
| 27 gdouble value); | |
| 28 typedef void (*unity_launcher_entry_set_progress_visible_func) | |
| 29 (UnityLauncherEntry* self, gboolean value); | |
| 30 | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 bool attempted_load = false; | |
| 35 | |
| 36 // Unity has a singleton object that we can ask whether the unity is running. | |
| 37 UnityInspector* inspector = NULL; | |
| 38 | |
| 39 // A link to the desktop entry in the panel. | |
| 40 UnityLauncherEntry* chrome_entry = NULL; | |
| 41 | |
| 42 // Retrieved functions from libunity. | |
| 43 unity_inspector_get_unity_running_func get_unity_running = NULL; | |
| 44 unity_launcher_entry_set_count_func entry_set_count = NULL; | |
| 45 unity_launcher_entry_set_count_visible_func entry_set_count_visible = NULL; | |
| 46 unity_launcher_entry_set_progress_func entry_set_progress = NULL; | |
| 47 unity_launcher_entry_set_progress_visible_func entry_set_progress_visible = | |
| 48 NULL; | |
| 49 | |
| 50 void EnsureMethodsLoaded() { | |
| 51 if (!attempted_load) { | |
|
Evan Martin
2011/04/26 21:33:04
Can early-exit this function rather than doing the
| |
| 52 void* unity_lib = dlopen("libunity.so.4", RTLD_LAZY); | |
| 53 if (unity_lib) { | |
|
Evan Martin
2011/04/26 21:33:04
Here too (if you set attempted_load=true) earlier.
| |
| 54 unity_inspector_get_default_func inspector_get_default = | |
| 55 reinterpret_cast<unity_inspector_get_default_func>( | |
| 56 dlsym(unity_lib, "unity_inspector_get_default")); | |
| 57 if (inspector_get_default) { | |
| 58 inspector = inspector_get_default(); | |
| 59 | |
| 60 get_unity_running = | |
| 61 reinterpret_cast<unity_inspector_get_unity_running_func>( | |
| 62 dlsym(unity_lib, "unity_inspector_get_unity_running")); | |
| 63 } | |
| 64 | |
| 65 unity_launcher_entry_get_for_desktop_id_func entry_get_for_desktop_id = | |
| 66 reinterpret_cast<unity_launcher_entry_get_for_desktop_id_func>( | |
| 67 dlsym(unity_lib, "unity_launcher_entry_get_for_desktop_id")); | |
| 68 | |
| 69 if (entry_get_for_desktop_id) { | |
| 70 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 71 std::string desktop_id = ShellIntegration::GetDesktopName(env.get()); | |
| 72 chrome_entry = entry_get_for_desktop_id(desktop_id.c_str()); | |
| 73 | |
| 74 entry_set_count = | |
| 75 reinterpret_cast<unity_launcher_entry_set_count_func>( | |
| 76 dlsym(unity_lib, "unity_launcher_entry_set_count")); | |
| 77 | |
| 78 entry_set_count_visible = | |
| 79 reinterpret_cast<unity_launcher_entry_set_count_visible_func>( | |
| 80 dlsym(unity_lib, "unity_launcher_entry_set_count_visible")); | |
| 81 | |
| 82 entry_set_progress = | |
| 83 reinterpret_cast<unity_launcher_entry_set_progress_func>( | |
| 84 dlsym(unity_lib, "unity_launcher_entry_set_progress")); | |
| 85 | |
| 86 entry_set_progress_visible = | |
| 87 reinterpret_cast<unity_launcher_entry_set_progress_visible_func>( | |
| 88 dlsym(unity_lib, "unity_launcher_entry_set_progress_visible")); | |
| 89 } | |
| 90 } | |
| 91 attempted_load = true; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 | |
| 98 namespace unity { | |
| 99 | |
| 100 bool IsRunning() { | |
| 101 EnsureMethodsLoaded(); | |
| 102 if (inspector && get_unity_running) | |
| 103 return get_unity_running(inspector); | |
|
Evan Martin
2011/04/26 21:33:04
I'm concerned this is going to take a while, like
Elliot Glaysher
2011/04/26 21:44:40
libunity reads a boolean. It modifies the boolean
| |
| 104 | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 void SetDownloadCount(int count) { | |
| 109 EnsureMethodsLoaded(); | |
| 110 if (chrome_entry && entry_set_count && entry_set_count_visible) { | |
| 111 entry_set_count(chrome_entry, count); | |
| 112 entry_set_count_visible(chrome_entry, count != 0); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void SetProgressPercent(float percentage) { | |
| 117 EnsureMethodsLoaded(); | |
| 118 if (chrome_entry && entry_set_progress && entry_set_progress_visible) { | |
| 119 entry_set_progress(chrome_entry, percentage); | |
| 120 entry_set_progress_visible(chrome_entry, | |
| 121 percentage > 0.0 && percentage < 1.0); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 } // namespace unity | |
| OLD | NEW |