| 1 |
/* |
| 2 |
* Copyright 2013 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 "lomiri-app-launch.h" |
| 21 |
#include "lomiri-app-launch-trace.h" |
| 22 |
|
| 23 |
extern "C" { |
| 24 |
#include <gio/gio.h> |
| 25 |
#include <gio/gunixfdlist.h> |
| 26 |
#include <string.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <errno.h> |
| 29 |
#pragma GCC diagnostic push |
| 30 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 31 |
#include <zeitgeist.h> |
| 32 |
#pragma GCC diagnostic pop |
| 33 |
|
| 34 |
#include "utils.h" |
| 35 |
#include "ual-tracepoint.h" |
| 36 |
#include "proxy-socket-demangler.h" |
| 37 |
} |
| 38 |
|
| 39 |
/* C++ Interface */ |
| 40 |
#include "application.h" |
| 41 |
#include "appid.h" |
| 42 |
#include "helper-impl.h" |
| 43 |
#include "registry.h" |
| 44 |
#include "registry-impl.h" |
| 45 |
#include <algorithm> |
| 46 |
#include <memory> |
| 47 |
#include <mutex> |
| 48 |
#include <lomiri/util/GlibMemory.h> |
| 49 |
|
| 50 |
using namespace lomiri::util; |
| 51 |
|
| 52 |
/* Make a typed string vector out of a GStrv */ |
| 53 |
template <typename T> |
| 54 |
static std::vector<T> uriVector (const gchar * const * uris) |
| 55 |
{ |
| 56 |
std::vector<T> urivect; |
| 57 |
|
| 58 |
for (auto i = 0; uris != nullptr && uris[i] != nullptr; i++) { |
| 59 |
urivect.emplace_back(T::from_raw(uris[i])); |
| 60 |
} |
| 61 |
|
| 62 |
return urivect; |
| 63 |
} |
| 64 |
|
| 65 |
gboolean |
| 66 |
lomiri_app_launch_start_application (const gchar * appid, const gchar * const * uris) |
| 67 |
{ |
| 68 |
try { |
| 69 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 70 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 71 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 72 |
|
| 73 |
auto uriv = uriVector<lomiri::app_launch::Application::URL>(uris); |
| 74 |
|
| 75 |
auto instance = app->launch(uriv); |
| 76 |
|
| 77 |
if (instance) { |
| 78 |
return TRUE; |
| 79 |
} else { |
| 80 |
return FALSE; |
| 81 |
} |
| 82 |
} catch (std::runtime_error &e) { |
| 83 |
g_warning("Unable to start app '%s': %s", appid, e.what()); |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
gboolean |
| 89 |
lomiri_app_launch_start_application_test (const gchar * appid, const gchar * const * uris) |
| 90 |
{ |
| 91 |
try { |
| 92 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 93 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 94 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 95 |
|
| 96 |
auto uriv = uriVector<lomiri::app_launch::Application::URL>(uris); |
| 97 |
|
| 98 |
auto instance = app->launchTest(uriv); |
| 99 |
|
| 100 |
if (instance) { |
| 101 |
return TRUE; |
| 102 |
} else { |
| 103 |
return FALSE; |
| 104 |
} |
| 105 |
} catch (std::runtime_error &e) { |
| 106 |
g_warning("Unable to start app '%s': %s", appid, e.what()); |
| 107 |
return FALSE; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
gboolean |
| 112 |
lomiri_app_launch_stop_application (const gchar * appid) |
| 113 |
{ |
| 114 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 115 |
|
| 116 |
try { |
| 117 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 118 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 119 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 120 |
|
| 121 |
auto instances = app->instances(); |
| 122 |
for (auto instance : instances) { |
| 123 |
instance->stop(); |
| 124 |
} |
| 125 |
|
| 126 |
return TRUE; |
| 127 |
} catch (std::runtime_error &e) { |
| 128 |
g_warning("Unable to stop app '%s': %s", appid, e.what()); |
| 129 |
return FALSE; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
gboolean |
| 134 |
lomiri_app_launch_pause_application (const gchar * appid) |
| 135 |
{ |
| 136 |
try { |
| 137 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 138 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 139 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 140 |
app->instances().at(0)->pause(); |
| 141 |
return TRUE; |
| 142 |
} catch (...) { |
| 143 |
return FALSE; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
gboolean |
| 148 |
lomiri_app_launch_resume_application (const gchar * appid) |
| 149 |
{ |
| 150 |
try { |
| 151 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 152 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 153 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 154 |
app->instances().at(0)->resume(); |
| 155 |
return TRUE; |
| 156 |
} catch (...) { |
| 157 |
return FALSE; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/* Function to take a work function and have it execute on a given |
| 162 |
GMainContext */ |
| 163 |
static GSourceUPtr executeOnContext (const std::shared_ptr<GMainContext>& context, std::function<void()> work) |
| 164 |
{ |
| 165 |
if (!context) { |
| 166 |
work(); |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
auto heapWork = new std::function<void()>(work); |
| 171 |
|
| 172 |
auto source = unique_glib(g_idle_source_new()); |
| 173 |
g_source_set_callback(source.get(), |
| 174 |
[](gpointer data) { |
| 175 |
auto heapWork = static_cast<std::function<void()>*>(data); |
| 176 |
(*heapWork)(); |
| 177 |
return G_SOURCE_REMOVE; |
| 178 |
}, |
| 179 |
heapWork, |
| 180 |
[](gpointer data) { |
| 181 |
auto heapWork = static_cast<std::function<void()>*>(data); |
| 182 |
delete heapWork; |
| 183 |
}); |
| 184 |
|
| 185 |
g_source_attach(source.get(), context.get()); |
| 186 |
|
| 187 |
return source; |
| 188 |
} |
| 189 |
|
| 190 |
/** A handy helper function that is based of a function to get |
| 191 |
a signal and put it into a map. */ |
| 192 |
template <core::Signal<const std::shared_ptr<lomiri::app_launch::Application>&, const std::shared_ptr<lomiri::app_launch::Application::Instance>&>& (*getSignal)(const std::shared_ptr<lomiri::app_launch::Registry>&)> |
| 193 |
static gboolean |
| 194 |
observer_add (LomiriAppLaunchAppObserver observer, gpointer user_data, std::map<std::pair<LomiriAppLaunchAppObserver, gpointer>, core::ScopedConnection> &observers) |
| 195 |
{ |
| 196 |
auto context = share_glib(g_main_context_ref_thread_default()); |
| 197 |
|
| 198 |
observers.emplace(std::make_pair( |
| 199 |
std::make_pair(observer, user_data), |
| 200 |
core::ScopedConnection( |
| 201 |
getSignal(lomiri::app_launch::Registry::getDefault()) |
| 202 |
.connect([context, observer, user_data](std::shared_ptr<lomiri::app_launch::Application> app, std::shared_ptr<lomiri::app_launch::Application::Instance> instance) { |
| 203 |
std::string appid = app->appId(); |
| 204 |
executeOnContext(context, [appid, observer, user_data]() { |
| 205 |
observer(appid.c_str(), user_data); |
| 206 |
}); |
| 207 |
}) |
| 208 |
) |
| 209 |
)); |
| 210 |
|
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
/** A handy helper to delete items from an observer map */ |
| 215 |
template<typename observertype> |
| 216 |
static gboolean |
| 217 |
observer_delete (observertype observer, gpointer user_data, std::map<std::pair<observertype, gpointer>, core::ScopedConnection> &observers) |
| 218 |
{ |
| 219 |
auto iter = observers.find(std::make_pair(observer, user_data)); |
| 220 |
|
| 221 |
if (iter == observers.end()) { |
| 222 |
return FALSE; |
| 223 |
} |
| 224 |
|
| 225 |
observers.erase(iter); |
| 226 |
return TRUE; |
| 227 |
} |
| 228 |
|
| 229 |
/** Map of all the observers listening for app started */ |
| 230 |
static std::map<std::pair<LomiriAppLaunchAppObserver, gpointer>, core::ScopedConnection> appStartedObservers; |
| 231 |
|
| 232 |
gboolean |
| 233 |
lomiri_app_launch_observer_add_app_started (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 234 |
{ |
| 235 |
return observer_add<&lomiri::app_launch::Registry::appStarted>(observer, user_data, appStartedObservers); |
| 236 |
} |
| 237 |
|
| 238 |
gboolean |
| 239 |
lomiri_app_launch_observer_delete_app_started (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 240 |
{ |
| 241 |
return observer_delete<LomiriAppLaunchAppObserver>(observer, user_data, appStartedObservers); |
| 242 |
} |
| 243 |
|
| 244 |
/* Map of all the observers listening for app stopped */ |
| 245 |
static std::map<std::pair<LomiriAppLaunchAppObserver, gpointer>, core::ScopedConnection> appStoppedObservers; |
| 246 |
|
| 247 |
gboolean |
| 248 |
lomiri_app_launch_observer_add_app_stop (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 249 |
{ |
| 250 |
return observer_add<&lomiri::app_launch::Registry::appStopped>(observer, user_data, appStoppedObservers); |
| 251 |
} |
| 252 |
|
| 253 |
gboolean |
| 254 |
lomiri_app_launch_observer_delete_app_stop (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 255 |
{ |
| 256 |
return observer_delete<LomiriAppLaunchAppObserver>(observer, user_data, appStoppedObservers); |
| 257 |
} |
| 258 |
|
| 259 |
/** Class to implement the Registry::Manager interface for the C code |
| 260 |
using a GLib mainloop. */ |
| 261 |
class CManager : public lomiri::app_launch::Registry::Manager |
| 262 |
{ |
| 263 |
public: |
| 264 |
CManager () { |
| 265 |
g_debug("Creating the CManager object"); |
| 266 |
} |
| 267 |
virtual ~CManager() { |
| 268 |
g_debug("Removing the shared the CManager object"); |
| 269 |
} |
| 270 |
|
| 271 |
void startingRequest(const std::shared_ptr<lomiri::app_launch::Application>& app, |
| 272 |
const std::shared_ptr<lomiri::app_launch::Application::Instance>& instance, |
| 273 |
std::function<void(bool)> reply) override { |
| 274 |
requestImpl(app, instance, reply, "starting", startingList); |
| 275 |
} |
| 276 |
|
| 277 |
void focusRequest(const std::shared_ptr<lomiri::app_launch::Application>& app, |
| 278 |
const std::shared_ptr<lomiri::app_launch::Application::Instance>& instance, |
| 279 |
std::function<void(bool)> reply) override { |
| 280 |
requestImpl(app, instance, reply, "focus", focusList); |
| 281 |
} |
| 282 |
|
| 283 |
void resumeRequest(const std::shared_ptr<lomiri::app_launch::Application> &app, |
| 284 |
const std::shared_ptr<lomiri::app_launch::Application::Instance> &instance, |
| 285 |
std::function<void(bool)> reply) override { |
| 286 |
requestImpl(app, instance, reply, "resume", resumeList); |
| 287 |
} |
| 288 |
|
| 289 |
private: |
| 290 |
/** The Data that we track on an observer. It is the functions to |
| 291 |
call, the user data and the context to call it on. */ |
| 292 |
struct ObserverData { |
| 293 |
LomiriAppLaunchAppObserver observer; |
| 294 |
gpointer user_data; |
| 295 |
GMainContextSPtr context; |
| 296 |
std::list<GSourceUPtr> sources; |
| 297 |
|
| 298 |
/** Handy constructor to get the context in one place */ |
| 299 |
ObserverData(LomiriAppLaunchAppObserver obs, gpointer ud) |
| 300 |
: observer(obs) |
| 301 |
, user_data(ud) { |
| 302 |
context = share_glib(g_main_context_ref_thread_default()); |
| 303 |
} |
| 304 |
|
| 305 |
~ObserverData() { |
| 306 |
/* |
| 307 |
* Cancel any work we've scheduled. This doesn't guarantee that |
| 308 |
* there won't be any work already running, if this is called from |
| 309 |
* a different thread than the context's. |
| 310 |
*/ |
| 311 |
for (auto const &s: sources) { |
| 312 |
g_source_destroy(s.get()); |
| 313 |
} |
| 314 |
} |
| 315 |
}; |
| 316 |
|
| 317 |
std::mutex mutex; /**< Protect observer lists */ |
| 318 |
std::list<ObserverData> focusList; /**< List of observers on the focus signal */ |
| 319 |
std::list<ObserverData> resumeList; /**< List of observers on the resume signal */ |
| 320 |
std::list<ObserverData> startingList; /**< List of observers on the starting signal */ |
| 321 |
|
| 322 |
/** Removes an observer from a specified list */ |
| 323 |
bool removeObserver (std::list<ObserverData> &list, LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 324 |
std::lock_guard<std::mutex> l(mutex); |
| 325 |
|
| 326 |
auto iter = std::find_if(list.begin(), list.end(), [observer, user_data](const ObserverData &data) { |
| 327 |
return data.observer == observer && data.user_data == user_data; |
| 328 |
}); |
| 329 |
|
| 330 |
if (iter == list.end()) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
list.erase(iter); |
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
/** Implements a request for a specified list by calling each observer and then the reply */ |
| 339 |
inline void requestImpl ( const std::shared_ptr<lomiri::app_launch::Application> &app, |
| 340 |
const std::shared_ptr<lomiri::app_launch::Application::Instance> &instance, |
| 341 |
std::function<void(bool)> reply, |
| 342 |
const std::string& name, |
| 343 |
std::list<ObserverData>& list) { |
| 344 |
std::string sappid = app->appId(); |
| 345 |
g_debug("CManager %s: %s", name.c_str(), sappid.c_str()); |
| 346 |
|
| 347 |
{ |
| 348 |
std::lock_guard<std::mutex> l(mutex); |
| 349 |
|
| 350 |
for (auto &data : list) { |
| 351 |
auto source = executeOnContext(data.context, [sappid, &data]() { |
| 352 |
/* |
| 353 |
* Note: access list's member without a lock. We can't hold |
| 354 |
* it here because observer could call g_main_loop_run() |
| 355 |
* causing us to recurse to this function again. |
| 356 |
* |
| 357 |
* It should still be safe, as the registry thread only reads |
| 358 |
* the list, not modify. That is, until some prick calls |
| 359 |
* _observer_delete_*() from a different thread it's added. |
| 360 |
* In that case, we can't do anything as we didn't request |
| 361 |
* a GDestroyNotify function, and thus can't guarantee the |
| 362 |
* validity of data.user_data even if we keep data itself |
| 363 |
* alive. |
| 364 |
* |
| 365 |
* And even if some other thread deletes their observer, |
| 366 |
* std::list guarantee that it won't affect the validity |
| 367 |
* of not-removed element's reference. |
| 368 |
*/ |
| 369 |
|
| 370 |
data.observer(sappid.c_str(), data.user_data); |
| 371 |
|
| 372 |
/* Remove the current GSource from the source list. */ |
| 373 |
auto cur_source = g_main_current_source(); |
| 374 |
auto source_iter = std::find_if(data.sources.begin(), data.sources.end(), |
| 375 |
[cur_source](const GSourceUPtr & list_source) { |
| 376 |
return list_source.get() == cur_source; |
| 377 |
}); |
| 378 |
|
| 379 |
if (source_iter != data.sources.end()) |
| 380 |
data.sources.erase(source_iter); |
| 381 |
}); |
| 382 |
|
| 383 |
if (source) |
| 384 |
data.sources.push_back(std::move(source)); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
reply(true); |
| 389 |
} |
| 390 |
|
| 391 |
public: |
| 392 |
void addFocus (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 393 |
std::lock_guard<std::mutex> l(mutex); |
| 394 |
focusList.emplace_back(observer, user_data); |
| 395 |
} |
| 396 |
void addResume (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 397 |
std::lock_guard<std::mutex> l(mutex); |
| 398 |
resumeList.emplace_back(observer, user_data); |
| 399 |
} |
| 400 |
void addStarting (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 401 |
std::lock_guard<std::mutex> l(mutex); |
| 402 |
startingList.emplace_back(observer, user_data); |
| 403 |
} |
| 404 |
bool deleteFocus (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 405 |
return removeObserver(focusList, observer, user_data); |
| 406 |
} |
| 407 |
bool deleteResume (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 408 |
return removeObserver(resumeList, observer, user_data); |
| 409 |
} |
| 410 |
bool deleteStarting (LomiriAppLaunchAppObserver observer, gpointer user_data) { |
| 411 |
return removeObserver(startingList, observer, user_data); |
| 412 |
} |
| 413 |
}; |
| 414 |
|
| 415 |
/** Weak pointer to the CManager if it is still in use. If it gets free'd by |
| 416 |
the registry we're okay with that. */ |
| 417 |
static std::weak_ptr<CManager> cmanager; |
| 418 |
|
| 419 |
/** Function to create the CManager if it doesn't currently exist. Otherwise |
| 420 |
just return a lock to it */ |
| 421 |
static std::shared_ptr<CManager> |
| 422 |
ensure_cmanager () |
| 423 |
{ |
| 424 |
auto retval = cmanager.lock(); |
| 425 |
|
| 426 |
if (!retval) { |
| 427 |
retval = std::make_shared<CManager>(); |
| 428 |
lomiri::app_launch::Registry::setManager(retval, lomiri::app_launch::Registry::getDefault()); |
| 429 |
cmanager = retval; |
| 430 |
} |
| 431 |
|
| 432 |
return retval; |
| 433 |
} |
| 434 |
|
| 435 |
gboolean |
| 436 |
lomiri_app_launch_observer_add_app_focus (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 437 |
{ |
| 438 |
auto manager = ensure_cmanager(); |
| 439 |
manager->addFocus(observer, user_data); |
| 440 |
return TRUE; |
| 441 |
} |
| 442 |
|
| 443 |
gboolean |
| 444 |
lomiri_app_launch_observer_delete_app_focus (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 445 |
{ |
| 446 |
auto manager = ensure_cmanager(); |
| 447 |
return manager->deleteFocus(observer, user_data) ? TRUE : FALSE; |
| 448 |
} |
| 449 |
|
| 450 |
gboolean |
| 451 |
lomiri_app_launch_observer_add_app_resume (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 452 |
{ |
| 453 |
auto manager = ensure_cmanager(); |
| 454 |
manager->addResume(observer, user_data); |
| 455 |
return TRUE; |
| 456 |
} |
| 457 |
|
| 458 |
gboolean |
| 459 |
lomiri_app_launch_observer_delete_app_resume (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 460 |
{ |
| 461 |
auto manager = ensure_cmanager(); |
| 462 |
return manager->deleteResume(observer, user_data) ? TRUE : FALSE; |
| 463 |
} |
| 464 |
|
| 465 |
gboolean |
| 466 |
lomiri_app_launch_observer_add_app_starting (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 467 |
{ |
| 468 |
auto manager = ensure_cmanager(); |
| 469 |
lomiri::app_launch::Registry::Impl::watchingAppStarting(true); |
| 470 |
manager->addStarting(observer, user_data); |
| 471 |
return TRUE; |
| 472 |
} |
| 473 |
|
| 474 |
gboolean |
| 475 |
lomiri_app_launch_observer_delete_app_starting (LomiriAppLaunchAppObserver observer, gpointer user_data) |
| 476 |
{ |
| 477 |
auto manager = ensure_cmanager(); |
| 478 |
lomiri::app_launch::Registry::Impl::watchingAppStarting(false); |
| 479 |
return manager->deleteStarting(observer, user_data) ? TRUE : FALSE; |
| 480 |
} |
| 481 |
|
| 482 |
/* Map of all the observers listening for app stopped */ |
| 483 |
static std::map<std::pair<LomiriAppLaunchAppFailedObserver, gpointer>, core::ScopedConnection> appFailedObservers; |
| 484 |
|
| 485 |
gboolean |
| 486 |
lomiri_app_launch_observer_add_app_failed (LomiriAppLaunchAppFailedObserver observer, gpointer user_data) |
| 487 |
{ |
| 488 |
auto context = share_glib(g_main_context_ref_thread_default()); |
| 489 |
auto reg = lomiri::app_launch::Registry::getDefault(); |
| 490 |
|
| 491 |
appFailedObservers.emplace(std::make_pair( |
| 492 |
std::make_pair(observer, user_data), |
| 493 |
core::ScopedConnection( |
| 494 |
lomiri::app_launch::Registry::appFailed(reg).connect([context, observer, user_data](std::shared_ptr<lomiri::app_launch::Application> app, std::shared_ptr<lomiri::app_launch::Application::Instance> instance, lomiri::app_launch::Registry::FailureType type) { |
| 495 |
std::string appid = app->appId(); |
| 496 |
executeOnContext(context, [appid, type, observer, user_data]() { |
| 497 |
LomiriAppLaunchAppFailed ctype{LOMIRI_APP_LAUNCH_APP_FAILED_CRASH}; |
| 498 |
|
| 499 |
switch (type) { |
| 500 |
case lomiri::app_launch::Registry::FailureType::CRASH: |
| 501 |
ctype = LOMIRI_APP_LAUNCH_APP_FAILED_CRASH; |
| 502 |
break; |
| 503 |
case lomiri::app_launch::Registry::FailureType::START_FAILURE: |
| 504 |
ctype = LOMIRI_APP_LAUNCH_APP_FAILED_START_FAILURE; |
| 505 |
break; |
| 506 |
} |
| 507 |
|
| 508 |
observer(appid.c_str(), ctype, user_data); |
| 509 |
}); |
| 510 |
}) |
| 511 |
) |
| 512 |
)); |
| 513 |
|
| 514 |
return TRUE; |
| 515 |
} |
| 516 |
|
| 517 |
gboolean |
| 518 |
lomiri_app_launch_observer_delete_app_failed (LomiriAppLaunchAppFailedObserver observer, gpointer user_data) |
| 519 |
{ |
| 520 |
return observer_delete<LomiriAppLaunchAppFailedObserver>(observer, user_data, appFailedObservers); |
| 521 |
} |
| 522 |
|
| 523 |
/** Handy helper for pause and resume here */ |
| 524 |
template <core::Signal<const std::shared_ptr<lomiri::app_launch::Application>&, const std::shared_ptr<lomiri::app_launch::Application::Instance>&, const std::vector<pid_t>&>& (*getSignal)(const std::shared_ptr<lomiri::app_launch::Registry>&)> |
| 525 |
static gboolean |
| 526 |
observer_add_pause (LomiriAppLaunchAppPausedResumedObserver observer, gpointer user_data, std::map<std::pair<LomiriAppLaunchAppPausedResumedObserver, gpointer>, core::ScopedConnection> &observers) |
| 527 |
{ |
| 528 |
auto context = share_glib(g_main_context_ref_thread_default()); |
| 529 |
|
| 530 |
observers.emplace(std::make_pair( |
| 531 |
std::make_pair(observer, user_data), |
| 532 |
core::ScopedConnection( |
| 533 |
getSignal(lomiri::app_launch::Registry::getDefault()) |
| 534 |
.connect([context, observer, user_data](std::shared_ptr<lomiri::app_launch::Application> app, std::shared_ptr<lomiri::app_launch::Application::Instance> instance, const std::vector<pid_t> &pids) { |
| 535 |
std::vector<pid_t> lpids = pids; |
| 536 |
lpids.emplace_back(0); |
| 537 |
|
| 538 |
std::string appid = app->appId(); |
| 539 |
|
| 540 |
executeOnContext(context, [appid, observer, user_data, lpids]() { |
| 541 |
observer(appid.c_str(), (int *)(lpids.data()), user_data); |
| 542 |
}); |
| 543 |
}) |
| 544 |
) |
| 545 |
)); |
| 546 |
|
| 547 |
return TRUE; |
| 548 |
} |
| 549 |
|
| 550 |
static std::map<std::pair<LomiriAppLaunchAppPausedResumedObserver, gpointer>, core::ScopedConnection> appPausedObservers; |
| 551 |
|
| 552 |
gboolean |
| 553 |
lomiri_app_launch_observer_add_app_paused (LomiriAppLaunchAppPausedResumedObserver observer, gpointer user_data) |
| 554 |
{ |
| 555 |
return observer_add_pause<&lomiri::app_launch::Registry::appPaused>(observer, user_data, appPausedObservers); |
| 556 |
} |
| 557 |
|
| 558 |
gboolean |
| 559 |
lomiri_app_launch_observer_delete_app_paused (LomiriAppLaunchAppPausedResumedObserver observer, gpointer user_data) |
| 560 |
{ |
| 561 |
return observer_delete<LomiriAppLaunchAppPausedResumedObserver>(observer, user_data, appPausedObservers); |
| 562 |
} |
| 563 |
|
| 564 |
static std::map<std::pair<LomiriAppLaunchAppPausedResumedObserver, gpointer>, core::ScopedConnection> appResumedObservers; |
| 565 |
|
| 566 |
gboolean |
| 567 |
lomiri_app_launch_observer_add_app_resumed (LomiriAppLaunchAppPausedResumedObserver observer, gpointer user_data) |
| 568 |
{ |
| 569 |
return observer_add_pause<&lomiri::app_launch::Registry::appResumed>(observer, user_data, appResumedObservers); |
| 570 |
} |
| 571 |
|
| 572 |
gboolean |
| 573 |
lomiri_app_launch_observer_delete_app_resumed (LomiriAppLaunchAppPausedResumedObserver observer, gpointer user_data) |
| 574 |
{ |
| 575 |
return observer_delete<LomiriAppLaunchAppPausedResumedObserver>(observer, user_data, appResumedObservers); |
| 576 |
} |
| 577 |
|
| 578 |
gchar ** |
| 579 |
lomiri_app_launch_list_running_apps (void) |
| 580 |
{ |
| 581 |
try { |
| 582 |
GArray * apps = g_array_new(TRUE, TRUE, sizeof(gchar *)); |
| 583 |
for (auto app : lomiri::app_launch::Registry::runningApps()) { |
| 584 |
std::string appid = app->appId(); |
| 585 |
g_debug("Adding AppID to list: %s", appid.c_str()); |
| 586 |
gchar * gappid = g_strdup(appid.c_str()); |
| 587 |
g_array_append_val(apps, gappid); |
| 588 |
} |
| 589 |
|
| 590 |
return (gchar **)g_array_free(apps, FALSE); |
| 591 |
} catch (const std::exception& e) { |
| 592 |
g_debug("Unable to list applications: %s", e.what()); |
| 593 |
return nullptr; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
GPid |
| 598 |
lomiri_app_launch_get_primary_pid (const gchar * appid) |
| 599 |
{ |
| 600 |
g_return_val_if_fail(appid != NULL, 0); |
| 601 |
|
| 602 |
try { |
| 603 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 604 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 605 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 606 |
return app->instances().at(0)->primaryPid(); |
| 607 |
} catch (const std::exception& e) { |
| 608 |
g_debug("Unable to get primary pid: %s", e.what()); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/* Get the PIDs for an AppID. If it's click or legacy single instance that's |
| 614 |
a simple call to the helper. But if it's not, we have to make a call for |
| 615 |
each instance of the app that we have running. */ |
| 616 |
GList * |
| 617 |
lomiri_app_launch_get_pids (const gchar * appid) |
| 618 |
{ |
| 619 |
try { |
| 620 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 621 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 622 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 623 |
auto pids = app->instances().at(0)->pids(); |
| 624 |
|
| 625 |
GList * retval = nullptr; |
| 626 |
for (auto pid : pids) { |
| 627 |
retval = g_list_prepend(retval, GINT_TO_POINTER(pid)); |
| 628 |
} |
| 629 |
|
| 630 |
return retval; |
| 631 |
} catch (...) { |
| 632 |
return nullptr; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
gboolean |
| 637 |
lomiri_app_launch_pid_in_app_id (GPid pid, const gchar * appid) |
| 638 |
{ |
| 639 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 640 |
try { |
| 641 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 642 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 643 |
auto app = lomiri::app_launch::Application::create(appId, registry); |
| 644 |
|
| 645 |
if (app->instances().at(0)->hasPid(pid)) { |
| 646 |
return TRUE; |
| 647 |
} else { |
| 648 |
return FALSE; |
| 649 |
} |
| 650 |
} catch (...) { |
| 651 |
return FALSE; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
gboolean |
| 656 |
lomiri_app_launch_app_id_parse (const gchar * appid, gchar ** package, gchar ** application, gchar ** version) |
| 657 |
{ |
| 658 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 659 |
|
| 660 |
try { |
| 661 |
auto appId = lomiri::app_launch::AppID::parse(appid); |
| 662 |
|
| 663 |
if (appId.empty()) { |
| 664 |
return FALSE; |
| 665 |
} |
| 666 |
|
| 667 |
if (package != nullptr) { |
| 668 |
*package = g_strdup(appId.package.value().c_str()); |
| 669 |
} |
| 670 |
|
| 671 |
if (application != nullptr) { |
| 672 |
*application = g_strdup(appId.appname.value().c_str()); |
| 673 |
} |
| 674 |
|
| 675 |
if (version != nullptr) { |
| 676 |
*version = g_strdup(appId.version.value().c_str()); |
| 677 |
} |
| 678 |
} catch (...) { |
| 679 |
return FALSE; |
| 680 |
} |
| 681 |
|
| 682 |
return TRUE; |
| 683 |
} |
| 684 |
|
| 685 |
/* Figure out whether we're a libertine container app or a click and then |
| 686 |
choose which function to use */ |
| 687 |
gchar * |
| 688 |
lomiri_app_launch_triplet_to_app_id (const gchar * pkg, const gchar * app, const gchar * ver) |
| 689 |
{ |
| 690 |
g_return_val_if_fail(pkg != NULL, NULL); |
| 691 |
|
| 692 |
std::string package{pkg}; |
| 693 |
std::string appname; |
| 694 |
std::string version; |
| 695 |
|
| 696 |
if (app != nullptr) { |
| 697 |
appname = app; |
| 698 |
} |
| 699 |
|
| 700 |
if (ver != nullptr) { |
| 701 |
version = ver; |
| 702 |
} |
| 703 |
|
| 704 |
auto appid = lomiri::app_launch::AppID::discover(package, appname, version); |
| 705 |
if (appid.empty()) { |
| 706 |
g_debug("Triplet lookup for '%s' '%s' '%s' returned empty", pkg, app, ver); |
| 707 |
return nullptr; |
| 708 |
} |
| 709 |
|
| 710 |
return g_strdup(std::string(appid).c_str()); |
| 711 |
} |
| 712 |
|
| 713 |
gboolean |
| 714 |
lomiri_app_launch_start_helper (const gchar * type, const gchar * appid, const gchar * const * uris) |
| 715 |
{ |
| 716 |
g_return_val_if_fail(type != NULL, FALSE); |
| 717 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 718 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, FALSE); |
| 719 |
|
| 720 |
try { |
| 721 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 722 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 723 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 724 |
|
| 725 |
if (!helper->instances().empty()) { |
| 726 |
throw std::runtime_error{"Instance already exits"}; |
| 727 |
} |
| 728 |
|
| 729 |
auto uriv = uriVector<lomiri::app_launch::Helper::URL>(uris); |
| 730 |
auto inst = helper->launch(uriv); |
| 731 |
|
| 732 |
if (!inst) { |
| 733 |
throw std::runtime_error{"Empty instance"}; |
| 734 |
} |
| 735 |
|
| 736 |
return TRUE; |
| 737 |
} catch (std::runtime_error &e) { |
| 738 |
g_warning("Unable to launch helper of type '%s' id '%s': %s", type, appid, e.what()); |
| 739 |
return FALSE; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
gchar * |
| 744 |
lomiri_app_launch_start_multiple_helper (const gchar * type, const gchar * appid, const gchar * const * uris) |
| 745 |
{ |
| 746 |
g_return_val_if_fail(type != NULL, NULL); |
| 747 |
g_return_val_if_fail(appid != NULL, NULL); |
| 748 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, NULL); |
| 749 |
|
| 750 |
try { |
| 751 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 752 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 753 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 754 |
|
| 755 |
auto uriv = uriVector<lomiri::app_launch::Helper::URL>(uris); |
| 756 |
auto inst = helper->launch(uriv); |
| 757 |
|
| 758 |
if (!inst) { |
| 759 |
throw std::runtime_error{"Empty instance"}; |
| 760 |
} |
| 761 |
|
| 762 |
return g_strdup(std::dynamic_pointer_cast<lomiri::app_launch::helper_impls::BaseInstance>(inst)->getInstanceId().c_str()); |
| 763 |
} catch (std::runtime_error &e) { |
| 764 |
g_warning("Unable to launch helper of type '%s' id '%s': %s", type, appid, e.what()); |
| 765 |
return nullptr; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
#ifdef LAL_USE_MIRCLIENT |
| 770 |
gchar * |
| 771 |
lomiri_app_launch_start_session_helper (const gchar * type, MirPromptSession * session, const gchar * appid, const gchar * const * uris) |
| 772 |
{ |
| 773 |
g_return_val_if_fail(type != NULL, NULL); |
| 774 |
g_return_val_if_fail(session != NULL, NULL); |
| 775 |
g_return_val_if_fail(appid != NULL, NULL); |
| 776 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, NULL); |
| 777 |
|
| 778 |
try { |
| 779 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 780 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 781 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 782 |
|
| 783 |
auto uriv = uriVector<lomiri::app_launch::Helper::URL>(uris); |
| 784 |
auto inst = helper->launch(session, uriv); |
| 785 |
|
| 786 |
if (!inst) { |
| 787 |
throw std::runtime_error{"Empty instance"}; |
| 788 |
} |
| 789 |
|
| 790 |
return g_strdup(std::dynamic_pointer_cast<lomiri::app_launch::helper_impls::BaseInstance>(inst)->getInstanceId().c_str()); |
| 791 |
} catch (std::runtime_error &e) { |
| 792 |
g_warning("Unable to launch helper of type '%s' id '%s': %s", type, appid, e.what()); |
| 793 |
return nullptr; |
| 794 |
} |
| 795 |
} |
| 796 |
#endif |
| 797 |
|
| 798 |
gboolean |
| 799 |
lomiri_app_launch_stop_helper (const gchar * type, const gchar * appid) |
| 800 |
{ |
| 801 |
g_return_val_if_fail(type != NULL, FALSE); |
| 802 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 803 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, FALSE); |
| 804 |
|
| 805 |
try { |
| 806 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 807 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 808 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 809 |
|
| 810 |
auto insts = helper->instances(); |
| 811 |
if (insts.empty()) { |
| 812 |
throw std::runtime_error{"No instances"}; |
| 813 |
} |
| 814 |
if (insts.size() > 1u) { |
| 815 |
throw std::runtime_error{"Expecting single instance but has multiple instances"}; |
| 816 |
} |
| 817 |
|
| 818 |
(*insts.begin())->stop(); |
| 819 |
|
| 820 |
return TRUE; |
| 821 |
} catch (std::runtime_error &e) { |
| 822 |
g_warning("Unable to stop helper of type '%s' id '%s': %s", type, appid, e.what()); |
| 823 |
return FALSE; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
gboolean |
| 828 |
lomiri_app_launch_stop_multiple_helper (const gchar * type, const gchar * appid, const gchar * instanceid) |
| 829 |
{ |
| 830 |
g_return_val_if_fail(type != NULL, FALSE); |
| 831 |
g_return_val_if_fail(appid != NULL, FALSE); |
| 832 |
g_return_val_if_fail(instanceid != NULL, FALSE); |
| 833 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, FALSE); |
| 834 |
|
| 835 |
try { |
| 836 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 837 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 838 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 839 |
|
| 840 |
auto inst = std::dynamic_pointer_cast<lomiri::app_launch::helper_impls::Base>(helper)->existingInstance(instanceid); |
| 841 |
if (!inst) { |
| 842 |
throw std::runtime_error{"No instances"}; |
| 843 |
} |
| 844 |
|
| 845 |
inst->stop(); |
| 846 |
|
| 847 |
return TRUE; |
| 848 |
} catch (std::runtime_error &e) { |
| 849 |
g_warning("Unable to stop helper of type '%s' id '%s' instance '%s': %s", type, appid, instanceid, e.what()); |
| 850 |
return FALSE; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
gchar ** |
| 855 |
lomiri_app_launch_list_helpers (const gchar * type) |
| 856 |
{ |
| 857 |
g_return_val_if_fail(type != NULL, FALSE); |
| 858 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, FALSE); |
| 859 |
|
| 860 |
try { |
| 861 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 862 |
auto helpers = registry->runningHelpers(lomiri::app_launch::Helper::Type::from_raw(type)); |
| 863 |
|
| 864 |
auto array = g_array_new(TRUE, TRUE, sizeof(gchar *)); |
| 865 |
|
| 866 |
for (const auto &helper : helpers) { |
| 867 |
gchar * appid = g_strdup(std::string(helper->appId()).c_str()); |
| 868 |
g_array_append_val(array, appid); |
| 869 |
} |
| 870 |
|
| 871 |
return (gchar **)g_array_free(array, FALSE); |
| 872 |
} catch (std::runtime_error &e) { |
| 873 |
g_warning("Unable to get helpers for type '%s': %s", type, e.what()); |
| 874 |
return NULL; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
gchar ** |
| 879 |
lomiri_app_launch_list_helper_instances (const gchar * type, const gchar * appid) |
| 880 |
{ |
| 881 |
g_return_val_if_fail(type != NULL, NULL); |
| 882 |
g_return_val_if_fail(g_strstr_len(type, -1, ":") == NULL, NULL); |
| 883 |
g_return_val_if_fail(appid != NULL, NULL); |
| 884 |
|
| 885 |
try { |
| 886 |
auto registry = lomiri::app_launch::Registry::getDefault(); |
| 887 |
auto appId = lomiri::app_launch::AppID::find(appid); |
| 888 |
auto helper = lomiri::app_launch::Helper::create(lomiri::app_launch::Helper::Type::from_raw(type), appId, registry); |
| 889 |
|
| 890 |
auto insts = helper->instances(); |
| 891 |
|
| 892 |
auto array = g_array_new(TRUE, TRUE, sizeof(gchar *)); |
| 893 |
|
| 894 |
for (const auto &inst : insts) { |
| 895 |
auto internalinst = std::dynamic_pointer_cast<lomiri::app_launch::helper_impls::BaseInstance>(inst); |
| 896 |
auto cstr = g_strdup(internalinst->getInstanceId().c_str()); |
| 897 |
g_array_append_val(array, cstr); |
| 898 |
} |
| 899 |
|
| 900 |
return (gchar **)g_array_free(array, FALSE); |
| 901 |
} catch (std::runtime_error &e) { |
| 902 |
g_warning("Unable to get helper instances for '%s' of type '%s': %s", appid, type, e.what()); |
| 903 |
return NULL; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
/** A handy helper function that is based of a function to get |
| 908 |
a signal and put it into a map. */ |
| 909 |
template <core::Signal<const std::shared_ptr<lomiri::app_launch::Helper>&, const std::shared_ptr<lomiri::app_launch::Helper::Instance>&>& (*getSignal)(lomiri::app_launch::Helper::Type type, const std::shared_ptr<lomiri::app_launch::Registry>&)> |
| 910 |
static gboolean |
| 911 |
helper_add (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data, std::map<std::tuple<LomiriAppLaunchHelperObserver, std::string, gpointer>, core::ScopedConnection> &observers) |
| 912 |
{ |
| 913 |
auto context = share_glib(g_main_context_ref_thread_default()); |
| 914 |
auto type = lomiri::app_launch::Helper::Type::from_raw(helper_type); |
| 915 |
|
| 916 |
observers.emplace(std::make_pair( |
| 917 |
std::make_tuple(observer, type.value(), user_data), |
| 918 |
core::ScopedConnection( |
| 919 |
getSignal(type, lomiri::app_launch::Registry::getDefault()) |
| 920 |
.connect([type, context, observer, user_data](std::shared_ptr<lomiri::app_launch::Helper> app, std::shared_ptr<lomiri::app_launch::Helper::Instance> instance) { |
| 921 |
auto internalinst = std::dynamic_pointer_cast<lomiri::app_launch::helper_impls::BaseInstance>(instance); |
| 922 |
|
| 923 |
std::string appid = app->appId(); |
| 924 |
std::string instanceid = internalinst->getInstanceId(); |
| 925 |
|
| 926 |
executeOnContext(context, [appid, instanceid, type, observer, user_data]() { |
| 927 |
observer(appid.c_str(), instanceid.c_str(), type.value().c_str(), user_data); |
| 928 |
}); |
| 929 |
}) |
| 930 |
) |
| 931 |
)); |
| 932 |
|
| 933 |
return TRUE; |
| 934 |
} |
| 935 |
|
| 936 |
static gboolean |
| 937 |
helper_delete (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data, std::map<std::tuple<LomiriAppLaunchHelperObserver, std::string, gpointer>, core::ScopedConnection> &observers) |
| 938 |
{ |
| 939 |
auto type = lomiri::app_launch::Helper::Type::from_raw(helper_type); |
| 940 |
auto iter = observers.find(std::make_tuple(observer, type.value(), user_data)); |
| 941 |
|
| 942 |
if (iter == observers.end()) { |
| 943 |
return FALSE; |
| 944 |
} |
| 945 |
|
| 946 |
observers.erase(iter); |
| 947 |
return TRUE; |
| 948 |
} |
| 949 |
|
| 950 |
/** Map of all the observers listening for helper started */ |
| 951 |
static std::map<std::tuple<LomiriAppLaunchHelperObserver, std::string, gpointer>, core::ScopedConnection> helperStartedObservers; |
| 952 |
static std::map<std::tuple<LomiriAppLaunchHelperObserver, std::string, gpointer>, core::ScopedConnection> helperStoppedObservers; |
| 953 |
|
| 954 |
gboolean |
| 955 |
lomiri_app_launch_observer_add_helper_started (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data) |
| 956 |
{ |
| 957 |
g_return_val_if_fail(observer != NULL, FALSE); |
| 958 |
g_return_val_if_fail(helper_type != NULL, FALSE); |
| 959 |
g_return_val_if_fail(g_strstr_len(helper_type, -1, ":") == NULL, FALSE); |
| 960 |
|
| 961 |
return helper_add<&lomiri::app_launch::Registry::helperStarted>(observer, helper_type, user_data, helperStartedObservers); |
| 962 |
} |
| 963 |
|
| 964 |
gboolean |
| 965 |
lomiri_app_launch_observer_add_helper_stop (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data) |
| 966 |
{ |
| 967 |
g_return_val_if_fail(observer != NULL, FALSE); |
| 968 |
g_return_val_if_fail(helper_type != NULL, FALSE); |
| 969 |
g_return_val_if_fail(g_strstr_len(helper_type, -1, ":") == NULL, FALSE); |
| 970 |
|
| 971 |
return helper_add<&lomiri::app_launch::Registry::helperStopped>(observer, helper_type, user_data, helperStoppedObservers); |
| 972 |
} |
| 973 |
|
| 974 |
gboolean |
| 975 |
lomiri_app_launch_observer_delete_helper_started (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data) |
| 976 |
{ |
| 977 |
g_return_val_if_fail(observer != NULL, FALSE); |
| 978 |
g_return_val_if_fail(helper_type != NULL, FALSE); |
| 979 |
g_return_val_if_fail(g_strstr_len(helper_type, -1, ":") == NULL, FALSE); |
| 980 |
|
| 981 |
return helper_delete(observer, helper_type, user_data, helperStartedObservers); |
| 982 |
} |
| 983 |
|
| 984 |
gboolean |
| 985 |
lomiri_app_launch_observer_delete_helper_stop (LomiriAppLaunchHelperObserver observer, const gchar * helper_type, gpointer user_data) |
| 986 |
{ |
| 987 |
g_return_val_if_fail(observer != NULL, FALSE); |
| 988 |
g_return_val_if_fail(helper_type != NULL, FALSE); |
| 989 |
g_return_val_if_fail(g_strstr_len(helper_type, -1, ":") == NULL, FALSE); |
| 990 |
|
| 991 |
return helper_delete(observer, helper_type, user_data, helperStoppedObservers); |
| 992 |
} |
| 993 |
|
| 994 |
gboolean |
| 995 |
lomiri_app_launch_helper_set_exec (const gchar * execline, const gchar * directory) |
| 996 |
{ |
| 997 |
g_return_val_if_fail(execline != NULL, FALSE); |
| 998 |
g_return_val_if_fail(execline[0] != '\0', FALSE); |
| 999 |
|
| 1000 |
g_debug("Helper Set Exec: %s", execline); |
| 1001 |
|
| 1002 |
GError * error{nullptr}; |
| 1003 |
gchar ** splitexec{nullptr}; |
| 1004 |
if (!g_shell_parse_argv(execline, nullptr, &splitexec, &error) || error != nullptr) { |
| 1005 |
g_warning("Unable to parse exec line '%s': %s", execline, error->message); |
| 1006 |
g_error_free(error); |
| 1007 |
return FALSE; |
| 1008 |
} |
| 1009 |
|
| 1010 |
std::vector<std::string> execvect; |
| 1011 |
for (auto i = 0; splitexec[i] != nullptr; i++) { |
| 1012 |
execvect.emplace_back(std::string{splitexec[i]}); |
| 1013 |
} |
| 1014 |
|
| 1015 |
g_strfreev(splitexec); |
| 1016 |
|
| 1017 |
try { |
| 1018 |
lomiri::app_launch::Helper::setExec(execvect); |
| 1019 |
return TRUE; |
| 1020 |
} catch (std::runtime_error &e) { |
| 1021 |
g_warning("Unable to set exec line '%s': %s", execline, e.what()); |
| 1022 |
return FALSE; |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
gboolean |
| 1027 |
lomiri_app_launch_application_info (const gchar * appid, gchar ** appdir, gchar ** appdesktop) |
| 1028 |
{ |
| 1029 |
/* TODO: Remove next ABI break */ |
| 1030 |
return FALSE; |
| 1031 |
} |