1 /* ide-runtime-provider.c
2 *
3 * Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #define G_LOG_DOMAIN "ide-runtime-provider"
22
23 #include "config.h"
24
25 #include <libide-threading.h>
26
27 #include "ide-pipeline.h"
28 #include "ide-config.h"
29 #include "ide-foundry-compat.h"
30 #include "ide-runtime.h"
31 #include "ide-runtime-manager.h"
32 #include "ide-runtime-provider.h"
33
G_DEFINE_INTERFACE(IdeRuntimeProvider,ide_runtime_provider,IDE_TYPE_OBJECT)34 G_DEFINE_INTERFACE (IdeRuntimeProvider, ide_runtime_provider, IDE_TYPE_OBJECT)
35
36 static void
37 ide_runtime_provider_real_load (IdeRuntimeProvider *self,
38 IdeRuntimeManager *manager)
39 {
40 }
41
42 static void
ide_runtime_provider_real_unload(IdeRuntimeProvider * self,IdeRuntimeManager * manager)43 ide_runtime_provider_real_unload (IdeRuntimeProvider *self,
44 IdeRuntimeManager *manager)
45 {
46 }
47
48 static gboolean
ide_runtime_provider_real_provides(IdeRuntimeProvider * self,const gchar * runtime_id)49 ide_runtime_provider_real_provides (IdeRuntimeProvider *self,
50 const gchar *runtime_id)
51 {
52 return FALSE;
53 }
54
55 static void
ide_runtime_provider_real_bootstrap_async(IdeRuntimeProvider * self,IdePipeline * pipeline,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)56 ide_runtime_provider_real_bootstrap_async (IdeRuntimeProvider *self,
57 IdePipeline *pipeline,
58 GCancellable *cancellable,
59 GAsyncReadyCallback callback,
60 gpointer user_data)
61 {
62 g_autoptr(IdeTask) task = NULL;
63
64 IDE_ENTRY;
65
66 g_assert (IDE_IS_RUNTIME_PROVIDER (self));
67 g_assert (IDE_IS_PIPELINE (pipeline));
68 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
69
70 task = ide_task_new (self, cancellable, callback, user_data);
71 ide_task_set_source_tag (task, ide_runtime_provider_real_bootstrap_async);
72 ide_task_set_priority (task, G_PRIORITY_LOW);
73
74 ide_task_return_new_error (task,
75 G_IO_ERROR,
76 G_IO_ERROR_NOT_SUPPORTED,
77 "Bootstrapping runtime not supported for %s",
78 G_OBJECT_TYPE_NAME (self));
79
80 IDE_EXIT;
81 }
82
83 static IdeRuntime *
ide_runtime_provider_real_bootstrap_finish(IdeRuntimeProvider * self,GAsyncResult * result,GError ** error)84 ide_runtime_provider_real_bootstrap_finish (IdeRuntimeProvider *self,
85 GAsyncResult *result,
86 GError **error)
87 {
88 return ide_task_propagate_pointer (IDE_TASK (result), error);
89 }
90
91 static void
ide_runtime_provider_default_init(IdeRuntimeProviderInterface * iface)92 ide_runtime_provider_default_init (IdeRuntimeProviderInterface *iface)
93 {
94 iface->load = ide_runtime_provider_real_load;
95 iface->unload = ide_runtime_provider_real_unload;
96 iface->provides = ide_runtime_provider_real_provides;
97 iface->bootstrap_async = ide_runtime_provider_real_bootstrap_async;
98 iface->bootstrap_finish = ide_runtime_provider_real_bootstrap_finish;
99 }
100
101 void
ide_runtime_provider_load(IdeRuntimeProvider * self,IdeRuntimeManager * manager)102 ide_runtime_provider_load (IdeRuntimeProvider *self,
103 IdeRuntimeManager *manager)
104 {
105 g_return_if_fail (IDE_IS_RUNTIME_PROVIDER (self));
106 g_return_if_fail (IDE_IS_RUNTIME_MANAGER (manager));
107
108 IDE_RUNTIME_PROVIDER_GET_IFACE (self)->load (self, manager);
109 }
110
111 void
ide_runtime_provider_unload(IdeRuntimeProvider * self,IdeRuntimeManager * manager)112 ide_runtime_provider_unload (IdeRuntimeProvider *self,
113 IdeRuntimeManager *manager)
114 {
115 g_return_if_fail (IDE_IS_RUNTIME_PROVIDER (self));
116 g_return_if_fail (IDE_IS_RUNTIME_MANAGER (manager));
117
118 IDE_RUNTIME_PROVIDER_GET_IFACE (self)->unload (self, manager);
119 }
120
121 gboolean
ide_runtime_provider_provides(IdeRuntimeProvider * self,const gchar * runtime_id)122 ide_runtime_provider_provides (IdeRuntimeProvider *self,
123 const gchar *runtime_id)
124 {
125 g_return_val_if_fail (IDE_IS_RUNTIME_PROVIDER (self), FALSE);
126 g_return_val_if_fail (runtime_id != NULL, FALSE);
127
128 return IDE_RUNTIME_PROVIDER_GET_IFACE (self)->provides (self, runtime_id);
129 }
130
131 /**
132 * ide_runtime_provider_bootstrap_async:
133 * @self: a #IdeRuntimeProvider
134 * @pipeline: an #IdePipeline
135 * @cancellable: (nullable): a #GCancellable or %NULL
136 * @callback: a #GAsyncReadyCallback or %NULL
137 * @user_data: closure data for @callback
138 *
139 * This function allows to the runtime provider to install dependent runtimes
140 * similar to ide_runtime_provider_install_async(), but with the added benefit
141 * that it can access the pipeline for more information. For example, it may
142 * want to check the architecture of the pipeline, or the connected device for
143 * tweaks as to what runtime to use.
144 *
145 * Some runtime providers like Flatpak might use this to locate SDK extensions
146 * and install those too.
147 *
148 * This function should be used instead of ide_runtime_provider_install_async().
149 *
150 * Since: 3.32
151 */
152 void
ide_runtime_provider_bootstrap_async(IdeRuntimeProvider * self,IdePipeline * pipeline,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)153 ide_runtime_provider_bootstrap_async (IdeRuntimeProvider *self,
154 IdePipeline *pipeline,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
157 gpointer user_data)
158 {
159 g_return_if_fail (IDE_IS_RUNTIME_PROVIDER (self));
160 g_return_if_fail (IDE_IS_PIPELINE (pipeline));
161 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
162
163 IDE_RUNTIME_PROVIDER_GET_IFACE (self)->bootstrap_async (self, pipeline, cancellable, callback, user_data);
164 }
165
166 /**
167 * ide_runtime_provider_bootstrap_finish:
168 * @self: a #IdeRuntimeProvider
169 * @result: a #GAsyncResult provided to callback
170 * @error: a location for a #GError, or %NULL
171 *
172 * Completes the asynchronous request to bootstrap.
173 *
174 * The resulting runtime will be set as the runtime to use for the build
175 * pipeline.
176 *
177 * Returns: (transfer full): an #IdeRuntime if successful; otherwise %NULL
178 * and @error is set.
179 *
180 * Since: 3.32
181 */
182 IdeRuntime *
ide_runtime_provider_bootstrap_finish(IdeRuntimeProvider * self,GAsyncResult * result,GError ** error)183 ide_runtime_provider_bootstrap_finish (IdeRuntimeProvider *self,
184 GAsyncResult *result,
185 GError **error)
186 {
187 IdeRuntime *ret;
188
189 g_return_val_if_fail (IDE_IS_RUNTIME_PROVIDER (self), FALSE);
190 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
191
192 ret = IDE_RUNTIME_PROVIDER_GET_IFACE (self)->bootstrap_finish (self, result, error);
193
194 g_return_val_if_fail (!ret || IDE_IS_RUNTIME (ret), NULL);
195
196 return ret;
197 }
198