| 1 |
/* exported FlatpakSharedModel */ |
| 2 |
|
| 3 |
/* shared.js |
| 4 |
* |
| 5 |
* Copyright 2020 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 |
const {GObject} = imports.gi; |
| 22 |
const {info} = imports.models; |
| 23 |
const {FlatsealOverrideStatus} = imports.models.overrideStatus; |
| 24 |
|
| 25 |
|
| 26 |
var FlatpakSharedModel = GObject.registerClass({ |
| 27 |
GTypeName: 'FlatpakSharedModel', |
| 28 |
}, class FlatpakSharedModel extends GObject.Object { |
| 29 |
_init() { |
| 30 |
super._init({}); |
| 31 |
this._info = info.getDefault(); |
| 32 |
this.reset(); |
| 33 |
} |
| 34 |
|
| 35 |
getPermissions() { |
| 36 |
return { |
| 37 |
'shared-network': { |
| 38 |
supported: this._info.supports('0.4.0'), |
| 39 |
description: _('Network'), |
| 40 |
option: 'network', |
| 41 |
value: this.constructor.getDefault(), |
| 42 |
example: 'share=network', |
| 43 |
}, |
| 44 |
'shared-ipc': { |
| 45 |
supported: this._info.supports('0.4.0'), |
| 46 |
description: _('Inter-process communications'), |
| 47 |
option: 'ipc', |
| 48 |
value: this.constructor.getDefault(), |
| 49 |
example: 'share=ipc', |
| 50 |
}, |
| 51 |
}; |
| 52 |
} |
| 53 |
|
| 54 |
static getGroup() { |
| 55 |
return 'Context'; |
| 56 |
} |
| 57 |
|
| 58 |
static getKey() { |
| 59 |
return 'shared'; |
| 60 |
} |
| 61 |
|
| 62 |
static getType() { |
| 63 |
return 'state'; |
| 64 |
} |
| 65 |
|
| 66 |
static getDefault() { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
static getStyle() { |
| 71 |
return 'shared'; |
| 72 |
} |
| 73 |
|
| 74 |
static getTitle() { |
| 75 |
return 'Share'; |
| 76 |
} |
| 77 |
|
| 78 |
static getDescription() { |
| 79 |
return _('List of subsystems shared with the host system'); |
| 80 |
} |
| 81 |
|
| 82 |
static serialize(values) { |
| 83 |
return values.join(';'); |
| 84 |
} |
| 85 |
|
| 86 |
static deserialize(value) { |
| 87 |
return value.split(';'); |
| 88 |
} |
| 89 |
|
| 90 |
getOptions() { |
| 91 |
return Object.entries(this.getPermissions()) |
| 92 |
.map(([, permission]) => permission.option); |
| 93 |
} |
| 94 |
|
| 95 |
updateFromProxyProperty(property, value) { |
| 96 |
const permission = this.getPermissions()[property]; |
| 97 |
const {option} = permission; |
| 98 |
const override = value ? option : `!${option}`; |
| 99 |
|
| 100 |
/* Determine if this value is an actual override */ |
| 101 |
|
| 102 |
/* Preserve previously negated overrides */ |
| 103 |
const matchesDefault = value === permission.value && !this._overrides.has(override); |
| 104 |
|
| 105 |
const fromOriginals = this._originals.has(override); |
| 106 |
const fromGlobals = this._globals.has(override); |
| 107 |
|
| 108 |
const seenInOriginals = this.constructor._isOverriden(this._originals, override); |
| 109 |
const seenInGlobals = this.constructor._isOverriden(this._globals, override); |
| 110 |
|
| 111 |
/* Assume it isn't */ |
| 112 |
this._overrides.delete(option); |
| 113 |
this._overrides.delete(`!${option}`); |
| 114 |
|
| 115 |
/* Ignore if it came from originals or from globals */ |
| 116 |
if (fromOriginals && !seenInGlobals || fromGlobals) |
| 117 |
return; |
| 118 |
|
| 119 |
/* Ignore if it's just the default value */ |
| 120 |
if (matchesDefault && !seenInGlobals && !seenInOriginals) |
| 121 |
return; |
| 122 |
|
| 123 |
/* It's an override */ |
| 124 |
this._overrides.add(override); |
| 125 |
} |
| 126 |
|
| 127 |
static _isOverriden(set, permission) { |
| 128 |
const option = permission.replace('!', ''); |
| 129 |
return set.has(option) || set.has(`!${option}`); |
| 130 |
} |
| 131 |
|
| 132 |
_getStatusForPermission(permission) { |
| 133 |
let status = FlatsealOverrideStatus.ORIGINAL; |
| 134 |
|
| 135 |
if (this._globals.has(permission) || this._globals.has(`!${permission}`)) |
| 136 |
status = FlatsealOverrideStatus.GLOBAL; |
| 137 |
if (this._overrides.has(permission) || this._overrides.has(`!${permission}`)) |
| 138 |
status = FlatsealOverrideStatus.USER; |
| 139 |
|
| 140 |
return status; |
| 141 |
} |
| 142 |
|
| 143 |
updateStatusProperty(proxy) { |
| 144 |
Object.entries(this.getPermissions()).forEach(([property, permission]) => { |
| 145 |
const {option} = permission; |
| 146 |
const statusProperty = `${property}-status`; |
| 147 |
const status = this._getStatusForPermission(option); |
| 148 |
|
| 149 |
proxy.set_property(statusProperty, status); |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
updateProxyProperty(proxy) { |
| 154 |
const originals = [...this._originals] |
| 155 |
.filter(o => !this.constructor._isOverriden(this._globals, o)) |
| 156 |
.filter(o => !this.constructor._isOverriden(this._overrides, o)); |
| 157 |
|
| 158 |
const globals = [...this._globals] |
| 159 |
.filter(g => !this.constructor._isOverriden(this._overrides, g)); |
| 160 |
|
| 161 |
const permissions = new Set([...originals, ...globals, ...this._overrides]); |
| 162 |
|
| 163 |
Object.entries(this.getPermissions()).forEach(([property, permission]) => { |
| 164 |
let value = this.constructor.getDefault(); |
| 165 |
|
| 166 |
const {option} = permission; |
| 167 |
if (permissions.has(option)) |
| 168 |
value = true; |
| 169 |
if (permissions.has(`!${option}`)) |
| 170 |
value = false; |
| 171 |
|
| 172 |
proxy.set_property(property, value); |
| 173 |
}); |
| 174 |
} |
| 175 |
|
| 176 |
_findProperSet(overrides, global) { |
| 177 |
if (overrides && global) |
| 178 |
return this._globals; |
| 179 |
if (overrides && !global) |
| 180 |
return this._overrides; |
| 181 |
return this._originals; |
| 182 |
} |
| 183 |
|
| 184 |
loadFromKeyFile(group, key, value, overrides, global) { |
| 185 |
const set = this._findProperSet(overrides, global); |
| 186 |
set.add(value); |
| 187 |
} |
| 188 |
|
| 189 |
saveToKeyFile(keyFile) { |
| 190 |
const group = this.constructor.getGroup(); |
| 191 |
const key = this.constructor.getKey(); |
| 192 |
|
| 193 |
this._overrides.forEach(value => { |
| 194 |
let _value = value; |
| 195 |
|
| 196 |
try { |
| 197 |
const existing = keyFile.get_value(group, key); |
| 198 |
_value = `${value};${existing}`; |
| 199 |
} catch (err) { |
| 200 |
_value = `${value}`; |
| 201 |
} |
| 202 |
|
| 203 |
keyFile.set_value(group, key, _value); |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
reset() { |
| 208 |
this._overrides = new Set(); |
| 209 |
this._globals = new Set(); |
| 210 |
this._originals = new Set(); |
| 211 |
} |
| 212 |
}); |