| 1 |
/* |
| 2 |
* Copyright © 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License version 3, as published |
| 6 |
* by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
* PURPOSE. See the GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Ted Gould <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "registry-impl.h" |
| 21 |
#include "application-icon-finder.h" |
| 22 |
#include "application-impl-base.h" |
| 23 |
#include "helper-impl.h" |
| 24 |
#include <regex> |
| 25 |
#include <lomiri/util/GObjectMemory.h> |
| 26 |
#include <lomiri/util/GlibMemory.h> |
| 27 |
|
| 28 |
using namespace lomiri::util; |
| 29 |
|
| 30 |
namespace lomiri |
| 31 |
{ |
| 32 |
namespace app_launch |
| 33 |
{ |
| 34 |
|
| 35 |
Registry::Impl::Impl() |
| 36 |
: thread([]() {}, |
| 37 |
[this]() { |
| 38 |
_clickUser.reset(); |
| 39 |
_clickDB.reset(); |
| 40 |
zgLog_.reset(); |
| 41 |
jobs_.reset(); |
| 42 |
|
| 43 |
if (_dbus) |
| 44 |
g_dbus_connection_flush_sync(_dbus.get(), nullptr, nullptr); |
| 45 |
_dbus.reset(); |
| 46 |
}) |
| 47 |
, jobs_{} |
| 48 |
, _iconFinders{} |
| 49 |
, _appStores{} |
| 50 |
{ |
| 51 |
auto cancel = thread.getCancellable(); |
| 52 |
_dbus = thread.executeOnThread<std::shared_ptr<GDBusConnection>>( |
| 53 |
[cancel]() { return share_gobject(g_bus_get_sync(G_BUS_TYPE_SESSION, cancel.get(), nullptr)); }); |
| 54 |
|
| 55 |
/* Determine where we're getting the helper from */ |
| 56 |
auto goomHelper = g_getenv("LOMIRI_APP_LAUNCH_OOM_HELPER"); |
| 57 |
if (goomHelper != nullptr) |
| 58 |
{ |
| 59 |
oomHelper_ = goomHelper; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
oomHelper_ = OOM_HELPER; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void Registry::Impl::initClick() |
| 68 |
{ |
| 69 |
if (_clickDB && _clickUser) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
auto init = thread.executeOnThread<bool>([this]() { |
| 75 |
GError* error = nullptr; |
| 76 |
|
| 77 |
if (!_clickDB) |
| 78 |
{ |
| 79 |
_clickDB = std::shared_ptr<ClickDB>(click_db_new(), [](ClickDB* db) { g_clear_object(&db); }); |
| 80 |
/* If TEST_CLICK_DB is unset, this reads the system database. */ |
| 81 |
click_db_read(_clickDB.get(), g_getenv("TEST_CLICK_DB"), &error); |
| 82 |
|
| 83 |
if (error != nullptr) |
| 84 |
{ |
| 85 |
auto perror = std::shared_ptr<GError>(error, [](GError* error) { g_error_free(error); }); |
| 86 |
throw std::runtime_error(perror->message); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (!_clickUser) |
| 91 |
{ |
| 92 |
_clickUser = |
| 93 |
std::shared_ptr<ClickUser>(click_user_new_for_user(_clickDB.get(), g_getenv("TEST_CLICK_USER"), &error), |
| 94 |
[](ClickUser* user) { g_clear_object(&user); }); |
| 95 |
|
| 96 |
if (error != nullptr) |
| 97 |
{ |
| 98 |
auto perror = std::shared_ptr<GError>(error, [](GError* error) { g_error_free(error); }); |
| 99 |
throw std::runtime_error(perror->message); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
g_debug("Initialized Click DB"); |
| 104 |
return true; |
| 105 |
}); |
| 106 |
|
| 107 |
if (!init) |
| 108 |
{ |
| 109 |
throw std::runtime_error("Unable to initialize the Click Database"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** Helper function for printing JSON objects to debug output */ |
| 114 |
std::string Registry::Impl::printJson(std::shared_ptr<JsonObject> jsonobj) |
| 115 |
{ |
| 116 |
auto node = json_node_alloc(); |
| 117 |
json_node_init_object(node, jsonobj.get()); |
| 118 |
|
| 119 |
auto snode = std::shared_ptr<JsonNode>(node, json_node_unref); |
| 120 |
return printJson(snode); |
| 121 |
} |
| 122 |
|
| 123 |
/** Helper function for printing JSON nodes to debug output */ |
| 124 |
std::string Registry::Impl::printJson(std::shared_ptr<JsonNode> jsonnode) |
| 125 |
{ |
| 126 |
std::string retval; |
| 127 |
auto gstr = unique_gchar(json_to_string(jsonnode.get(), TRUE)); |
| 128 |
|
| 129 |
if (gstr) |
| 130 |
{ |
| 131 |
retval = gstr.get(); |
| 132 |
} |
| 133 |
|
| 134 |
return retval; |
| 135 |
} |
| 136 |
|
| 137 |
std::shared_ptr<JsonObject> Registry::Impl::getClickManifest(const std::string& package) |
| 138 |
{ |
| 139 |
initClick(); |
| 140 |
|
| 141 |
auto retval = thread.executeOnThread<std::shared_ptr<JsonObject>>([this, package]() { |
| 142 |
GError* error = nullptr; |
| 143 |
auto mani = click_user_get_manifest(_clickUser.get(), package.c_str(), &error); |
| 144 |
|
| 145 |
if (error != nullptr) |
| 146 |
{ |
| 147 |
auto perror = std::shared_ptr<GError>(error, [](GError* error) { g_error_free(error); }); |
| 148 |
g_debug("Error parsing manifest for package '%s': %s", package.c_str(), perror->message); |
| 149 |
return std::shared_ptr<JsonObject>(); |
| 150 |
} |
| 151 |
|
| 152 |
auto node = json_node_alloc(); |
| 153 |
json_node_init_object(node, mani); |
| 154 |
json_object_unref(mani); |
| 155 |
|
| 156 |
auto retval = std::shared_ptr<JsonObject>(json_node_dup_object(node), json_object_unref); |
| 157 |
|
| 158 |
json_node_free(node); |
| 159 |
|
| 160 |
return retval; |
| 161 |
}); |
| 162 |
|
| 163 |
if (!retval) |
| 164 |
throw std::runtime_error("Unable to get Click manifest for package: " + package); |
| 165 |
|
| 166 |
return retval; |
| 167 |
} |
| 168 |
|
| 169 |
std::list<AppID::Package> Registry::Impl::getClickPackages() |
| 170 |
{ |
| 171 |
initClick(); |
| 172 |
|
| 173 |
return thread.executeOnThread<std::list<AppID::Package>>([this]() { |
| 174 |
GError* error = nullptr; |
| 175 |
GList* pkgs = click_user_get_package_names(_clickUser.get(), &error); |
| 176 |
|
| 177 |
if (error != nullptr) |
| 178 |
{ |
| 179 |
auto perror = std::shared_ptr<GError>(error, [](GError* error) { g_error_free(error); }); |
| 180 |
throw std::runtime_error(perror->message); |
| 181 |
} |
| 182 |
|
| 183 |
std::list<AppID::Package> list; |
| 184 |
for (GList* item = pkgs; item != nullptr; item = g_list_next(item)) |
| 185 |
{ |
| 186 |
auto pkgobj = static_cast<gchar*>(item->data); |
| 187 |
if (pkgobj) |
| 188 |
{ |
| 189 |
list.emplace_back(AppID::Package::from_raw(pkgobj)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
g_list_free_full(pkgs, g_free); |
| 194 |
return list; |
| 195 |
}); |
| 196 |
} |
| 197 |
|
| 198 |
std::string Registry::Impl::getClickDir(const std::string& package) |
| 199 |
{ |
| 200 |
initClick(); |
| 201 |
|
| 202 |
return thread.executeOnThread<std::string>([this, package]() { |
| 203 |
GError* error = nullptr; |
| 204 |
auto dir = click_user_get_path(_clickUser.get(), package.c_str(), &error); |
| 205 |
|
| 206 |
if (error != nullptr) |
| 207 |
{ |
| 208 |
auto perror = std::shared_ptr<GError>(error, [](GError* error) { g_error_free(error); }); |
| 209 |
throw std::runtime_error(perror->message); |
| 210 |
} |
| 211 |
|
| 212 |
std::string cppdir(dir); |
| 213 |
g_free(dir); |
| 214 |
return cppdir; |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
/** Send an event to Zeitgeist using the registry thread so that |
| 219 |
the callback comes back in the right place. */ |
| 220 |
void Registry::Impl::zgSendEvent(AppID appid, const std::string& eventtype) |
| 221 |
{ |
| 222 |
thread.executeOnThread([this, appid, eventtype] { |
| 223 |
std::string uri; |
| 224 |
|
| 225 |
if (appid.package.value().empty()) |
| 226 |
{ |
| 227 |
uri = "application://" + appid.appname.value() + ".desktop"; |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
uri = "application://" + appid.package.value() + "_" + appid.appname.value() + ".desktop"; |
| 232 |
} |
| 233 |
|
| 234 |
g_debug("Sending ZG event for '%s': %s", uri.c_str(), eventtype.c_str()); |
| 235 |
|
| 236 |
if (!zgLog_) |
| 237 |
{ |
| 238 |
zgLog_ = share_gobject(zeitgeist_log_new()); /* create a new log for us */ |
| 239 |
} |
| 240 |
|
| 241 |
auto event = unique_gobject(zeitgeist_event_new()); |
| 242 |
zeitgeist_event_set_actor(event.get(), "application://lomiri-app-launch.desktop"); |
| 243 |
zeitgeist_event_set_interpretation(event.get(), eventtype.c_str()); |
| 244 |
zeitgeist_event_set_manifestation(event.get(), ZEITGEIST_ZG_USER_ACTIVITY); |
| 245 |
|
| 246 |
auto subject = unique_gobject(zeitgeist_subject_new()); |
| 247 |
zeitgeist_subject_set_interpretation(subject.get(), ZEITGEIST_NFO_SOFTWARE); |
| 248 |
zeitgeist_subject_set_manifestation(subject.get(), ZEITGEIST_NFO_SOFTWARE_ITEM); |
| 249 |
zeitgeist_subject_set_mimetype(subject.get(), "application/x-desktop"); |
| 250 |
zeitgeist_subject_set_uri(subject.get(), uri.c_str()); |
| 251 |
|
| 252 |
zeitgeist_event_add_subject(event.get(), subject.get()); |
| 253 |
|
| 254 |
zeitgeist_log_insert_event(zgLog_.get(), /* log */ |
| 255 |
event.get(), /* event */ |
| 256 |
nullptr, /* cancellable */ |
| 257 |
[](GObject* obj, GAsyncResult* res, gpointer user_data) { |
| 258 |
GError* error = nullptr; |
| 259 |
|
| 260 |
unique_glib(zeitgeist_log_insert_event_finish(ZEITGEIST_LOG(obj), res, &error)); |
| 261 |
|
| 262 |
if (error != nullptr) |
| 263 |
{ |
| 264 |
g_warning("Unable to submit Zeitgeist Event: %s", error->message); |
| 265 |
g_error_free(error); |
| 266 |
} |
| 267 |
}, /* callback */ |
| 268 |
nullptr); /* userdata */ |
| 269 |
|
| 270 |
}); |
| 271 |
} |
| 272 |
|
| 273 |
std::shared_ptr<IconFinder>& Registry::Impl::getIconFinder(std::string basePath) |
| 274 |
{ |
| 275 |
const std::lock_guard<std::mutex> lock(_iconFindersMutex); |
| 276 |
if (_iconFinders.find(basePath) == _iconFinders.end()) |
| 277 |
{ |
| 278 |
_iconFinders[basePath] = std::make_shared<IconFinder>(basePath); |
| 279 |
} |
| 280 |
return _iconFinders[basePath]; |
| 281 |
} |
| 282 |
|
| 283 |
/** App start watching, if we're registered for the signal we |
| 284 |
can't wait on it. We are making this static right now because |
| 285 |
we need it to go across the C and C++ APIs smoothly, and those |
| 286 |
can end up with different registry objects. Long term, this |
| 287 |
should become a member variable. */ |
| 288 |
static bool watchingAppStarting_ = false; |
| 289 |
|
| 290 |
/** Variable to track if this program is watching app startup |
| 291 |
so that we can know to not wait on the response to that. */ |
| 292 |
void Registry::Impl::watchingAppStarting(bool rWatching) |
| 293 |
{ |
| 294 |
watchingAppStarting_ = rWatching; |
| 295 |
} |
| 296 |
|
| 297 |
/** Accessor for the internal variable to know whether an app |
| 298 |
is watching for app startup */ |
| 299 |
bool Registry::Impl::isWatchingAppStarting() |
| 300 |
{ |
| 301 |
return watchingAppStarting_; |
| 302 |
} |
| 303 |
|
| 304 |
/** Sets up the signals down to the info watchers and we aggregate |
| 305 |
them up to users of UAL. We connect to all their signals and |
| 306 |
pass them up. */ |
| 307 |
void Registry::Impl::infoWatchersSetup() |
| 308 |
{ |
| 309 |
std::call_once(flag_infoWatchersSetup, [this] { |
| 310 |
g_debug("Info watchers signals setup"); |
| 311 |
|
| 312 |
/* Grab all the app stores and the ZG info watcher */ |
| 313 |
std::list<std::shared_ptr<info_watcher::Base>> watchers{_appStores.begin(), _appStores.end()}; |
| 314 |
watchers.push_back(getZgWatcher()); |
| 315 |
|
| 316 |
/* Connect each of their signals to us, and track that connection */ |
| 317 |
for (const auto& watcher : watchers) |
| 318 |
{ |
| 319 |
infoWatchers_.emplace_back(std::make_pair( |
| 320 |
watcher, |
| 321 |
infoWatcherConnections{ |
| 322 |
watcher->infoChanged().connect( |
| 323 |
[this](const std::shared_ptr<Application>& app) { sig_appInfoUpdated(app); }), |
| 324 |
watcher->appAdded().connect([this](const std::shared_ptr<Application>& app) { sig_appAdded(app); }), |
| 325 |
watcher->appRemoved().connect([this](const AppID& appid) { sig_appRemoved(appid); }), |
| 326 |
})); |
| 327 |
} |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
core::Signal<const std::shared_ptr<Application>&>& Registry::Impl::appInfoUpdated() |
| 332 |
{ |
| 333 |
infoWatchersSetup(); |
| 334 |
return sig_appInfoUpdated; |
| 335 |
} |
| 336 |
|
| 337 |
core::Signal<const std::shared_ptr<Application>&>& Registry::Impl::appAdded() |
| 338 |
{ |
| 339 |
infoWatchersSetup(); |
| 340 |
return sig_appAdded; |
| 341 |
} |
| 342 |
|
| 343 |
core::Signal<const AppID&>& Registry::Impl::appRemoved() |
| 344 |
{ |
| 345 |
infoWatchersSetup(); |
| 346 |
return sig_appRemoved; |
| 347 |
} |
| 348 |
|
| 349 |
std::shared_ptr<Application> Registry::Impl::createApp(const AppID& appid) |
| 350 |
{ |
| 351 |
for (const auto& appStore : appStores()) |
| 352 |
{ |
| 353 |
if (appStore->hasAppId(appid)) |
| 354 |
{ |
| 355 |
return appStore->create(appid); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
throw std::runtime_error("Invalid app ID: " + std::string(appid)); |
| 360 |
} |
| 361 |
|
| 362 |
std::shared_ptr<Helper> Registry::Impl::createHelper(const Helper::Type& type, |
| 363 |
const AppID& appid, |
| 364 |
const std::shared_ptr<Registry::Impl>& sharedimpl) |
| 365 |
{ |
| 366 |
/* Only one type today */ |
| 367 |
return std::make_shared<helper_impls::Base>(type, appid, sharedimpl); |
| 368 |
} |
| 369 |
|
| 370 |
} // namespace app_launch |
| 371 |
} // namespace lomiri |