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 MessageTray = imports.ui.messageTray;
Gio._promisify(Gio.Mount.prototype, 'guess_content_type');
const { loadInterfaceXML } = imports.misc.fileUtils;
// GSettings keys
@ -81,64 +83,45 @@ function HotplugSniffer() {
}
var ContentTypeDiscoverer = class {
constructor(callback) {
this._callback = callback;
constructor() {
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
}
guessContentTypes(mount) {
async guessContentTypes(mount) {
let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN);
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 = [];
if (shouldScan) {
try {
contentTypes = mount.guess_content_type_finish(res);
contentTypes = await mount.guess_content_type(false, null);
} catch (e) {
log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
}
if (contentTypes.length) {
this._emitCallback(mount, contentTypes);
} else {
let root = mount.get_root();
let hotplugSniffer = new HotplugSniffer();
hotplugSniffer.SniffURIRemote(root.get_uri(),
result => {
[contentTypes] = result;
this._emitCallback(mount, contentTypes);
});
if (contentTypes.length === 0) {
const root = mount.get_root();
const hotplugSniffer = new HotplugSniffer();
[contentTypes] = hotplugSniffer.SniffURIAsync(root.get_uri());
}
}
_emitCallback(mount, contentTypes = []) {
// we're not interested in win32 software content types here
contentTypes = contentTypes.filter(
type => type !== 'x-content/win32-software');
let apps = [];
const apps = [];
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)
apps.push(app);
});
if (apps.length == 0)
if (apps.length === 0)
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);
}
_onMountAdded(monitor, mount) {
async _onMountAdded(monitor, mount) {
// don't do anything if our session is not the currently
// active one
if (!this._session.SessionIsActive)
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);
});
discoverer.guessContentTypes(mount);
}
_onMountRemoved(monitor, mount) {