autorunManager: Promisify ContentTypeDiscoverer

Thanks to recent gjs changes - namely the ability to promisify
interface prototypes and promise-based D-Bus wrappers - we can
modernize the ContentTypeDiscoverer API to use an async function
instead of a callback.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2344>
This commit is contained in:
Florian Müllner 2022-06-23 17:02:55 +02:00 committed by Marge Bot
parent 119581a4cb
commit c6861c0a3d

View File

@ -7,6 +7,8 @@ const GnomeSession = imports.misc.gnomeSession;
const Main = imports.ui.main; const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray; const MessageTray = imports.ui.messageTray;
Gio._promisify(Gio.Mount.prototype, 'guess_content_type');
const { loadInterfaceXML } = imports.misc.fileUtils; const { loadInterfaceXML } = imports.misc.fileUtils;
// GSettings keys // GSettings keys
@ -81,64 +83,45 @@ function HotplugSniffer() {
} }
var ContentTypeDiscoverer = class { var ContentTypeDiscoverer = class {
constructor(callback) { constructor() {
this._callback = callback;
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA }); this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
} }
guessContentTypes(mount) { async guessContentTypes(mount) {
let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN); let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN);
let shouldScan = autorunEnabled && !isMountNonLocal(mount); let shouldScan = autorunEnabled && !isMountNonLocal(mount);
if (shouldScan) {
// guess mount's content types using GIO
mount.guess_content_type(false, null,
this._onContentTypeGuessed.bind(this));
} else {
this._emitCallback(mount, []);
}
}
_onContentTypeGuessed(mount, res) {
let contentTypes = []; let contentTypes = [];
if (shouldScan) {
try { try {
contentTypes = mount.guess_content_type_finish(res); contentTypes = await mount.guess_content_type(false, null);
} catch (e) { } catch (e) {
log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`); log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
} }
if (contentTypes.length) { if (contentTypes.length === 0) {
this._emitCallback(mount, contentTypes); const root = mount.get_root();
} else { const hotplugSniffer = new HotplugSniffer();
let root = mount.get_root(); [contentTypes] = hotplugSniffer.SniffURIAsync(root.get_uri());
let hotplugSniffer = new HotplugSniffer();
hotplugSniffer.SniffURIRemote(root.get_uri(),
result => {
[contentTypes] = result;
this._emitCallback(mount, contentTypes);
});
} }
} }
_emitCallback(mount, contentTypes = []) {
// we're not interested in win32 software content types here // we're not interested in win32 software content types here
contentTypes = contentTypes.filter( contentTypes = contentTypes.filter(
type => type !== 'x-content/win32-software'); type => type !== 'x-content/win32-software');
let apps = []; const apps = [];
contentTypes.forEach(type => { contentTypes.forEach(type => {
let app = Gio.app_info_get_default_for_type(type, false); const app = Gio.app_info_get_default_for_type(type, false);
if (app) if (app)
apps.push(app); apps.push(app);
}); });
if (apps.length == 0) if (apps.length === 0)
apps.push(Gio.app_info_get_default_for_type('inode/directory', false)); apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
this._callback(mount, apps, contentTypes); return [apps, contentTypes];
} }
}; };
@ -160,16 +143,15 @@ var AutorunManager = class {
this._volumeMonitor.disconnectObject(this); this._volumeMonitor.disconnectObject(this);
} }
_onMountAdded(monitor, mount) { async _onMountAdded(monitor, mount) {
// don't do anything if our session is not the currently // don't do anything if our session is not the currently
// active one // active one
if (!this._session.SessionIsActive) if (!this._session.SessionIsActive)
return; return;
let discoverer = new ContentTypeDiscoverer((m, apps, contentTypes) => { const discoverer = new ContentTypeDiscoverer();
const [apps, contentTypes] = await discoverer.guessContentTypes(mount);
this._dispatcher.addMount(mount, apps, contentTypes); this._dispatcher.addMount(mount, apps, contentTypes);
});
discoverer.guessContentTypes(mount);
} }
_onMountRemoved(monitor, mount) { _onMountRemoved(monitor, mount) {