Debian Codegraph @

src/models/portals.js

raw
1 /* exported FlatpakPortalsModel getDefault */
2 /* eslint class-methods-use-this: */
3
4 /* portals.js
5 *
6 * Copyright 2020-2021 Martin Abente Lahaye
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 const {Gio, GLib, GObject} = imports.gi;
23 const {info} = imports.models;
24
25 var PermissionsIface = `
26 <node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
27 <interface name="org.freedesktop.impl.portal.PermissionStore">
28 <method name="List">
29 <arg type="s" name="table" direction="in"/>
30 <arg type="as" name="ids" direction="out"/>
31 </method>
32 <method name="Lookup">
33 <arg type="s" name="table" direction="in"/>
34 <arg type="s" name="id" direction="in"/>
35 <arg type="a{sas}" name="permissions" direction="out"/>
36 <arg type="v" name="data" direction="out"/>
37 </method>
38 <method name="SetPermission">
39 <arg type="s" name="table" direction="in"/>
40 <arg type="b" name="create" direction="in"/>
41 <arg type="s" name="id" direction="in"/>
42 <arg type="s" name="app" direction="in"/>
43 <arg type="as" name="permissions" direction="in"/>
44 </method>
45 <method name="DeletePermission">
46 <arg name='table' type='s' direction='in'/>
47 <arg name='id' type='s' direction='in'/>
48 <arg name='app' type='s' direction='in'/>
49 </method>
50 <property name="version" type="u" access="read"/>
51 </interface>
52 </node>
53 `;
54
55
56 const SUPPORTED_SERVICE_VERSION = 2;
57 const SUPPORTED_FLATPAK_VERSION = '0.4.0';
58
59 var FlatpakPortalState = {
60 UNKNOWN: 0,
61 UNSUPPORTED: 1,
62 UNSET: 2,
63 DISALLOWED: 3,
64 ALLOWED: 4,
65 };
66
67
68 var FlatpakPortalsModel = GObject.registerClass({
69 GTypeName: 'FlatpakPortalsModel',
70 }, class FlatpakPortalsModel extends GObject.Object {
71 _init() {
72 super._init({});
73 this._backup = {};
74 this._proxy = null;
75
76 this._backgroundbackgroundSupported = null;
77 this._notificationsnotificationSupported = null;
78 this._devicesmicrophoneSupported = null;
79 this._devicesspeakersSupported = null;
80 this._devicescameraSupported = null;
81 this._locationlocationSupported = null;
82
83 this._backgroundbackgroundReason = '';
84 this._notificationsnotificationReason = '';
85 this._devicesmicrophoneReason = '';
86 this._devicesspeakersReason = '';
87 this._devicescameraReason = '';
88 this._locationlocationReason = '';
89
90 this._info = info.getDefault();
91 this._appId = '';
92 }
93
94 _setup() {
95 if (this._proxy !== null)
96 return;
97
98 let busName = GLib.getenv('FLATSEAL_PORTAL_BUS_NAME');
99 if (busName === null)
100 busName = 'org.freedesktop.impl.portal.PermissionStore';
101
102 const PermissionsProxy = Gio.DBusProxy.makeProxyWrapper(PermissionsIface);
103
104 try {
105 this._proxy = new PermissionsProxy(
106 Gio.DBus.session,
107 busName,
108 '/org/freedesktop/impl/portal/PermissionStore');
109 } catch (err) {
110 logError(err);
111 }
112 }
113
114 getPermissions() {
115 return {
116 'portals-background': {
117 supported: this.isSupported('background', 'background'),
118 description: _('Background'),
119 value: this.constructor.getDefault(),
120 example: _('Can run in the background'),
121 table: 'background',
122 id: 'background',
123 allowed: ['yes'],
124 disallowed: ['no'],
125 },
126 'portals-notification': {
127 supported: this.isSupported('notifications', 'notification'),
128 description: _('Notifications'),
129 value: this.constructor.getDefault(),
130 example: _('Can send notifications'),
131 table: 'notifications',
132 id: 'notification',
133 allowed: ['yes'],
134 disallowed: ['no'],
135 },
136 'portals-microphone': {
137 supported: this.isSupported('devices', 'microphone'),
138 description: _('Microphone'),
139 value: this.constructor.getDefault(),
140 example: _('Can listen to your microphone'),
141 table: 'devices',
142 id: 'microphone',
143 allowed: ['yes'],
144 disallowed: ['no'],
145 },
146 'portals-speakers': {
147 supported: this.isSupported('devices', 'speakers'),
148 description: _('Speakers'),
149 value: this.constructor.getDefault(),
150 example: _('Can play sounds to your speakers'),
151 table: 'devices',
152 id: 'speakers',
153 allowed: ['yes'],
154 disallowed: ['no'],
155 },
156 'portals-camera': {
157 supported: this.isSupported('devices', 'camera'),
158 description: _('Camera'),
159 value: this.constructor.getDefault(),
160 example: _('Can record videos with your camera'),
161 table: 'devices',
162 id: 'camera',
163 allowed: ['yes'],
164 disallowed: ['no'],
165 },
166 'portals-location': {
167 supported: this.isSupported('location', 'location'),
168 description: _('Location'),
169 value: this.constructor.getDefault(),
170 example: _('Can access your location'),
171 table: 'location',
172 id: 'location',
173 allowed: ['EXACT', '0'],
174 disallowed: ['NONE', '0'],
175 },
176 };
177 }
178
179 static getType() {
180 return 'portal';
181 }
182
183 static getDefault() {
184 return FlatpakPortalState.UNKNOWN;
185 }
186
187 static getMax() {
188 return FlatpakPortalState.ALLOWED;
189 }
190
191 static getMin() {
192 return FlatpakPortalState.UNKNOWN;
193 }
194
195 static getStyle() {
196 return 'portals';
197 }
198
199 static getGroup() {
200 return 'portals';
201 }
202
203 static getKey() {
204 return null;
205 }
206
207 getOptions() { // eslint-disable-line class-methods-use-this
208 return null;
209 }
210
211 static getTitle() {
212 return 'Portals';
213 }
214
215 static getDescription() {
216 return _('List of resources selectively granted to the application');
217 }
218
219 safeList(table) {
220 try {
221 return this._proxy.ListSync(table);
222 } catch (err) {
223 return [[]];
224 }
225 }
226
227 safeLookUp(table, id) {
228 try {
229 return this._proxy.LookupSync(table, id);
230 } catch (err) {
231 return [null];
232 }
233 }
234
235 safeSetPermission(table, id, appId, access) {
236 try {
237 this._proxy.SetPermissionSync(table, false, id, appId, access);
238 } catch (err) {
239 logError(err);
240 }
241 }
242
243 safeDeletePermission(table, id, appId) {
244 try {
245 this._proxy.DeletePermissionSync(table, id, appId);
246 } catch (err) {
247 logError(err);
248 }
249 }
250
251 isSupported(table, id) {
252 if (this[`_${table}${id}Supported`] !== null)
253 return this[`_${table}${id}Supported`];
254
255 if (this._info.supports(SUPPORTED_FLATPAK_VERSION) === false) {
256 this[`_${table}${id}Reason`] = _('Not supported by the installed version of Flatpak');
257 this[`_${table}${id}Supported`] = false;
258 return false;
259 }
260
261 this._setup();
262
263 if (this._proxy === null || this._proxy.version >= SUPPORTED_SERVICE_VERSION === false) {
264 this[`_${table}${id}Reason`] = _('Requires permission store version 2 or newer');
265 this[`_${table}${id}Supported`] = false;
266 return false;
267 }
268
269 const [appIds] = this.safeLookUp(table, id);
270
271 if (appIds === null) {
272 this[`_${table}${id}Reason`] = _('Portal data has not been set up yet');
273 this[`_${table}${id}Supported`] = false;
274 return false;
275 }
276
277 this[`_${table}${id}Reason`] = '';
278 this[`_${table}${id}Supported`] = true;
279 return true;
280 }
281
282 getUnsupportedReason(table, id) {
283 return this[`_${table}${id}Reason`];
284 }
285
286 updateFromProxyProperty(property, value) {
287 const permission = this.getPermissions()[property];
288
289 if (!this.isSupported(permission.table, permission.id))
290 return;
291
292 if (value === FlatpakPortalState.UNSET) {
293 this.unset(permission.table, permission.id);
294 } else if (value === FlatpakPortalState.ALLOWED) {
295 this.safeSetPermission(permission.table, permission.id, this.appId, permission.allowed);
296 } else if (value === FlatpakPortalState.DISALLOWED) {
297 this.safeSetPermission(permission.table, permission.id, this.appId, permission.disallowed);
298 } else {
299
300 /* unsupported */
301 }
302 }
303
304 updateStatusProperty() {
305
306 /* does not apply to this backend */
307 }
308
309 updateProxyProperty(proxy) {
310 Object.entries(this.getPermissions()).forEach(([property, permission]) => {
311 if (!this.isSupported(permission.table, permission.id)) {
312 proxy.set_property(property, FlatpakPortalState.UNSUPPORTED);
313 return;
314 }
315
316 const [appIds] = this.safeLookUp(permission.table, permission.id);
317
318 if (appIds === null)
319 proxy.set_property(property, FlatpakPortalState.UNSUPPORTED);
320 else if (!(this.appId in appIds))
321 proxy.set_property(property, FlatpakPortalState.UNSET);
322 else if (appIds[this.appId][0] === permission.allowed[0])
323 proxy.set_property(property, FlatpakPortalState.ALLOWED);
324 else
325 proxy.set_property(property, FlatpakPortalState.DISALLOWED);
326 });
327 }
328
329 backup() {
330 this._backup = {};
331
332 for (const [property, permission] of Object.entries(this.getPermissions())) {
333 if (!this.isSupported(permission.table, permission.id))
334 continue;
335
336 const [appIds] = this.safeLookUp(permission.table, permission.id);
337 if (appIds === null || !(this.appId in appIds))
338 continue;
339
340 this._backup[property] = appIds[this.appId];
341 }
342 }
343
344 restore() {
345 for (const [property, permission] of Object.entries(this.getPermissions())) {
346 if (!(property in this._backup))
347 continue;
348
349 this.safeSetPermission(permission.table, permission.id, this.appId, this._backup[property]);
350 }
351 }
352
353 unset(table, id) {
354 const [appIds] = this.safeLookUp(table, id);
355 if (appIds === null || !(this.appId in appIds))
356 return;
357
358 /* https://github.com/flatpak/xdg-desktop-portal/issues/573 */
359 if (Object.keys(appIds).length === 1)
360 this.safeSetPermission(table, id, '', []);
361
362 this.safeDeletePermission(table, id, this.appId);
363 }
364
365 forget() {
366 for (const [, permission] of Object.entries(this.getPermissions())) {
367 if (!this.isSupported(permission.table, permission.id))
368 continue;
369
370 this.unset(permission.table, permission.id);
371 }
372 }
373
374 changed() {
375 for (const [, permission] of Object.entries(this.getPermissions())) {
376 if (!this.isSupported(permission.table, permission.id))
377 continue;
378
379 const [appIds] = this.safeLookUp(permission.table, permission.id);
380 if (appIds !== null && this.appId in appIds)
381 return true;
382 }
383
384 return false;
385 }
386
387 saveToKeyFile() {
388
389 /* this backend has no file */
390 }
391
392 reset() {
393
394 /* this backends speaks directly to DBus */
395 }
396
397 reload() {
398 const permissions = this.getPermissions();
399
400 Object.keys(permissions).forEach(property => {
401 const permission = permissions[property];
402 this[`_${permission.table}${permission.id}Supported`] = null;
403 this.isSupported(permission.table, permission.id);
404 });
405 }
406
407 getVersion() {
408 if (this._proxy === null)
409 return null;
410
411 return this._proxy.version;
412 }
413
414 set appId(appId) {
415 this._appId = appId;
416 }
417
418 get appId() {
419 return this._appId;
420 }
421 });
422
423
424 var getDefault = (function() {
425 let instance;
426 return function() {
427 if (typeof instance === 'undefined')
428 instance = new FlatpakPortalsModel();
429 return instance;
430 };
431 }());