Debian Codegraph @

liblomiri-app-launch/application-impl-click.cpp

raw
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 "application-impl-click.h"
21 #include "application-info-desktop.h"
22 #include "registry-impl.h"
23
24 #include <glib.h>
25
26 #include <algorithm>
27
28 namespace lomiri
29 {
30 namespace app_launch
31 {
32 namespace app_impls
33 {
34
35 std::pair<std::shared_ptr<GKeyFile>, std::string> manifestAppDesktop(const std::shared_ptr<JsonObject>& manifest,
36 const std::string& package,
37 const std::string& app,
38 const std::string& clickDir);
39
40 Click::Click(const AppID& appid, const std::shared_ptr<Registry::Impl>& registry)
41 : Click(appid, registry->getClickManifest(appid.package), registry)
42 {
43 }
44
45 Click::Click(const AppID& appid, const std::shared_ptr<JsonObject>& manifest, const std::shared_ptr<Registry::Impl>& registry)
46 : Base(registry)
47 , _appid(appid)
48 , _manifest(manifest)
49 , _clickDir(registry->getClickDir(appid.package))
50 {
51 std::tie(_keyfile, desktopPath_) = manifestAppDesktop(_manifest, appid.package, appid.appname, _clickDir);
52 if (!_keyfile)
53 throw std::runtime_error{"No keyfile found for click application: " + std::string(appid)};
54
55 g_debug("Application Click object for appid '%s'", std::string(appid).c_str());
56 }
57
58 AppID Click::appId() const
59 {
60 return _appid;
61 }
62
63 std::shared_ptr<Application::Info> Click::info()
64 {
65 if (!_info)
66 {
67 _info = std::make_shared<app_info::Desktop>(appId(), _keyfile, _clickDir, _clickDir,
68 app_info::DesktopFlags::NONE, nullptr);
69 }
70
71 return _info;
72 }
73
74 AppID::Version manifestVersion(const std::shared_ptr<JsonObject>& manifest)
75 {
76 const gchar* cstr = nullptr;
77 if (!json_object_has_member(manifest.get(), "version") ||
78 (cstr = json_object_get_string_member(manifest.get(), "version")) == nullptr)
79 {
80 throw std::runtime_error("Unable to find version number in manifest: " + Registry::Impl::printJson(manifest));
81 }
82
83 auto cppstr = AppID::Version::from_raw(cstr);
84 return cppstr;
85 }
86
87 std::pair<std::shared_ptr<GKeyFile>, std::string> manifestAppDesktop(const std::shared_ptr<JsonObject>& manifest,
88 const std::string& package,
89 const std::string& app,
90 const std::string& clickDir)
91 {
92 if (!manifest)
93 {
94 throw std::runtime_error("No manifest for package '" + package + "'");
95 }
96
97 JsonObject* hooks = nullptr;
98 if (!json_object_has_member(manifest.get(), "hooks") ||
99 (hooks = json_object_get_object_member(manifest.get(), "hooks")) == nullptr)
100 {
101 throw std::runtime_error("Manifest for application '" + app +
102 "' does not have a 'hooks' field: " + Registry::Impl::printJson(manifest));
103 }
104
105 auto gapps = json_object_get_members(hooks);
106 if (gapps == nullptr)
107 {
108 throw std::runtime_error("GLib JSON confusion, please talk to your library vendor");
109 }
110 else
111 {
112 g_list_free(gapps);
113 }
114
115 JsonObject* hooklist = nullptr;
116 if (!json_object_has_member(hooks, app.c_str()) ||
117 (hooklist = json_object_get_object_member(hooks, app.c_str())) == nullptr)
118 {
119 throw std::runtime_error("Manifest for does not have an application '" + app +
120 "': " + Registry::Impl::printJson(manifest));
121 }
122
123 auto desktoppath = json_object_get_string_member(hooklist, "desktop");
124 if (desktoppath == nullptr)
125 throw std::runtime_error("Manifest for application '" + app +
126 "' does not have a 'desktop' hook: " + Registry::Impl::printJson(manifest));
127
128 auto path = std::shared_ptr<gchar>(g_build_filename(clickDir.c_str(), desktoppath, nullptr), g_free);
129
130 std::shared_ptr<GKeyFile> keyfile(g_key_file_new(), g_key_file_free);
131 GError* error = nullptr;
132 g_key_file_load_from_file(keyfile.get(), path.get(), G_KEY_FILE_NONE, &error);
133 if (error != nullptr)
134 {
135 auto perror = std::shared_ptr<GError>(error, g_error_free);
136 throw std::runtime_error(perror.get()->message);
137 }
138
139 return std::make_pair(keyfile, std::string(path.get()));
140 }
141
142 std::vector<std::shared_ptr<Application::Instance>> Click::instances()
143 {
144 auto vbase = registry_->jobs()->instances(appId(), "application-click");
145 return std::vector<std::shared_ptr<Application::Instance>>(vbase.begin(), vbase.end());
146 }
147
148 /** Grabs all the environment variables for the application to
149 launch in. It sets up the confinement ones and then adds in
150 the APP_EXEC line and whether to use XMir */
151 std::list<std::pair<std::string, std::string>> Click::launchEnv()
152 {
153 auto retval = confinedEnv(_appid.package, _clickDir);
154
155 retval.emplace_back(std::make_pair("APP_DIR", _clickDir));
156 retval.emplace_back(std::make_pair("APP_DESKTOP_FILE_PATH", desktopPath_));
157
158 retval.emplace_back(std::make_pair("QML2_IMPORT_PATH", _clickDir + "/lib/" + LOMIRI_APP_LAUNCH_ARCH + ":" + _clickDir + "/lib/" + LOMIRI_APP_LAUNCH_ARCH + "/qml"));
159 retval.emplace_back(std::make_pair("LD_LIBRARY_PATH", _clickDir + "/lib/" + LOMIRI_APP_LAUNCH_ARCH + ":" + _clickDir + "/lib"));
160 retval.emplace_back(std::make_pair("PATH", _clickDir + "/lib/" + LOMIRI_APP_LAUNCH_ARCH + "/bin:" + _clickDir + ":" + g_getenv("PATH")));
161
162 info();
163
164 retval.emplace_back(std::make_pair("APP_XMIR_ENABLE", _info->xMirEnable().value() ? "1" : "0"));
165 retval.emplace_back(std::make_pair("APP_EXEC", _info->execLine().value()));
166
167 retval.emplace_back(std::make_pair("APP_EXEC_POLICY", std::string(appId())));
168
169 return retval;
170 }
171
172 std::shared_ptr<Application::Instance> Click::launch(const std::vector<Application::URL>& urls)
173 {
174 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); };
175 return registry_->jobs()->launch(appId(), "application-click", {}, urls, jobs::manager::launchMode::STANDARD,
176 envfunc);
177 }
178
179 std::shared_ptr<Application::Instance> Click::launchTest(const std::vector<Application::URL>& urls)
180 {
181 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); };
182 return registry_->jobs()->launch(appId(), "application-click", {}, urls, jobs::manager::launchMode::TEST,
183 envfunc);
184 }
185
186 std::shared_ptr<Application::Instance> Click::findInstance(const std::string& instanceid)
187 {
188 return registry_->jobs()->existing(appId(), "application-click", instanceid, std::vector<Application::URL>{});
189 }
190
191 } // namespace app_impls
192 } // namespace app_launch
193 } // namespace lomiri