| 1 |
/* eslint class-methods-use-this:, no-unused-vars: */ |
| 2 |
|
| 3 |
/* service.js |
| 4 |
* |
| 5 |
* Copyright 2021 Martin Abente Lahaye |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
imports.gi.versions.Gtk = '4.0'; |
| 22 |
|
| 23 |
const {Gio, GLib, Gtk} = imports.gi; |
| 24 |
|
| 25 |
const 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 |
<method name="testPartialTable"> |
| 51 |
</method> |
| 52 |
<property name="version" type="u" access="read"/> |
| 53 |
</interface> |
| 54 |
</node> |
| 55 |
`; |
| 56 |
|
| 57 |
|
| 58 |
class MockPermissionsStore { |
| 59 |
constructor() { |
| 60 |
this._store = { |
| 61 |
background: { |
| 62 |
background: {}, |
| 63 |
}, |
| 64 |
notifications: { |
| 65 |
notification: {}, |
| 66 |
}, |
| 67 |
devices: { |
| 68 |
speakers: {}, |
| 69 |
microphone: {}, |
| 70 |
camera: {}, |
| 71 |
}, |
| 72 |
location: { |
| 73 |
location: {}, |
| 74 |
}, |
| 75 |
}; |
| 76 |
|
| 77 |
this._version = new GLib.Variant('u', 2); |
| 78 |
this._dbusId = null; |
| 79 |
this._nameId = Gio.bus_own_name( |
| 80 |
Gio.BusType.SESSION, |
| 81 |
'com.github.tchx84.Flatseal.PermissionStore', |
| 82 |
Gio.BusNameOwnerFlags.NONE, |
| 83 |
this._onBusAcquired.bind(this), |
| 84 |
null, |
| 85 |
null, |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
_onBusAcquired(connection, name) { |
| 90 |
const info = Gio.DBusNodeInfo.new_for_xml(PermissionsIface); |
| 91 |
const activationId = connection.register_object( |
| 92 |
'/org/freedesktop/impl/portal/PermissionStore', |
| 93 |
info.interfaces[0], |
| 94 |
this._onCalled.bind(this), |
| 95 |
this._onProperty.bind(this), |
| 96 |
null, |
| 97 |
); |
| 98 |
|
| 99 |
if (activationId <= 0) |
| 100 |
throw new Error('activationId is ZERO'); |
| 101 |
} |
| 102 |
|
| 103 |
_onCalled(connection, sender, path, iface, method, params, invocation) { |
| 104 |
if (method === 'Lookup') { |
| 105 |
const [table, id] = params.deep_unpack(); |
| 106 |
|
| 107 |
if (!(table in this._store) || !(id in this._store[table])) |
| 108 |
invocation.return_dbus_error('org.freedesktop.portal.Error.NotFound', ''); |
| 109 |
|
| 110 |
const data = new GLib.Variant('b', true); |
| 111 |
const permissions = new GLib.Variant('(a{sas}v)', [this._store[table][id], data]); |
| 112 |
|
| 113 |
invocation.return_value(permissions); |
| 114 |
} else if (method === 'SetPermission') { |
| 115 |
const [table, create, id, appId, permissions] = params.deep_unpack(); |
| 116 |
|
| 117 |
this._store[table][id][appId] = permissions; |
| 118 |
|
| 119 |
invocation.return_value(null); |
| 120 |
} else if (method === 'List') { |
| 121 |
var ids = []; |
| 122 |
const [table] = params.deep_unpack(); |
| 123 |
|
| 124 |
if (table in this._store) |
| 125 |
ids = Object.keys(this._store[table]); |
| 126 |
|
| 127 |
const value = new GLib.Variant('(as)', [ids]); |
| 128 |
invocation.return_value(value); |
| 129 |
} else if (method === 'DeletePermission') { |
| 130 |
const [table, id, appId] = params.deep_unpack(); |
| 131 |
|
| 132 |
if (table in this._store && id in this._store[table] && appId in this._store[table][id]) |
| 133 |
delete this._store[table][id][appId]; |
| 134 |
|
| 135 |
invocation.return_value(null); |
| 136 |
} else if (method === 'testPartialTable') { |
| 137 |
delete this._store['devices']['microphone']; |
| 138 |
|
| 139 |
invocation.return_value(null); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
_onProperty(connection, sender, path, iface, key) { |
| 144 |
return this._version; |
| 145 |
} |
| 146 |
|
| 147 |
shutdown() { |
| 148 |
Gio.bus_unown_name(this._nameId); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
Gtk.init(); |
| 153 |
|
| 154 |
const service = new MockPermissionsStore(); |
| 155 |
|
| 156 |
const loop = new GLib.MainLoop(null, false); |
| 157 |
loop.run(); |