@@ -17,9 +17,9 @@ import { makeComponentLogger } from "./DesktopObservability.ts";
1717// Electron's app.setAsDefaultProtocolClient resolves the desktop id from
1818// setDesktopName, which cannot match those files — so the browser keeps
1919// prompting "Choose an application" for every OAuth callback. Instead, write
20- // our own handler entry pointing at the current AppImage and claim the
21- // scheme default via xdg-mime, exactly what the file manager's "set as
22- // default" checkbox would record in mimeapps.list.
20+ // our own handler entry pointing at the current AppImage, refresh the desktop
21+ // MIME cache so desktop environments recognize that entry as a handler, and
22+ // use xdg-mime to record it as the scheme default in mimeapps.list.
2323const { logInfo, logWarning } = makeComponentLogger ( "desktop-linux-url-handler" ) ;
2424
2525export class DesktopLinuxUrlHandlerRegistrationError extends Schema . TaggedError < DesktopLinuxUrlHandlerRegistrationError > ( ) (
@@ -40,6 +40,23 @@ export class DesktopLinuxUrlHandlerRegistrationError extends Schema.TaggedError<
4040
4141const isRegistrationError = Schema . is ( DesktopLinuxUrlHandlerRegistrationError ) ;
4242
43+ export class DesktopLinuxUrlHandlerCacheRefreshError extends Schema . TaggedError < DesktopLinuxUrlHandlerCacheRefreshError > ( ) (
44+ "DesktopLinuxUrlHandlerCacheRefreshError" ,
45+ {
46+ applicationsDir : Schema . String ,
47+ exitCode : Schema . optionalKey ( Schema . Number ) ,
48+ cause : Schema . optionalKey ( Schema . Defect ( ) ) ,
49+ } ,
50+ ) {
51+ override get message ( ) : string {
52+ const exitCode =
53+ this . exitCode === undefined ? "" : `, update-desktop-database exit code ${ this . exitCode } ` ;
54+ return `Failed to refresh the desktop MIME cache at ${ this . applicationsDir } ${ exitCode } .` ;
55+ }
56+ }
57+
58+ const isCacheRefreshError = Schema . is ( DesktopLinuxUrlHandlerCacheRefreshError ) ;
59+
4360const escapeDesktopEntryString = ( value : string ) : string =>
4461 value
4562 . replaceAll ( "\\" , "\\\\" )
@@ -131,6 +148,37 @@ export const make = Effect.gen(function* () {
131148 ) ,
132149 ) ;
133150
151+ const updateDesktopDatabase = Effect . scoped (
152+ Effect . gen ( function * ( ) {
153+ const command = ChildProcess . make (
154+ "update-desktop-database" ,
155+ [ environment . linuxApplicationsDir ] ,
156+ {
157+ stdin : "ignore" ,
158+ stdout : "ignore" ,
159+ stderr : "ignore" ,
160+ } ,
161+ ) ;
162+ const handle = yield * spawner . spawn ( command ) ;
163+ const exitCode = yield * handle . exitCode ;
164+ if ( exitCode !== 0 ) {
165+ return yield * new DesktopLinuxUrlHandlerCacheRefreshError ( {
166+ applicationsDir : environment . linuxApplicationsDir ,
167+ exitCode,
168+ } ) ;
169+ }
170+ } ) ,
171+ ) . pipe (
172+ Effect . mapError ( ( error ) =>
173+ isCacheRefreshError ( error )
174+ ? error
175+ : new DesktopLinuxUrlHandlerCacheRefreshError ( {
176+ applicationsDir : environment . linuxApplicationsDir ,
177+ cause : error ,
178+ } ) ,
179+ ) ,
180+ ) ;
181+
134182 const setDefaultHandler = Effect . scoped (
135183 Effect . gen ( function * ( ) {
136184 const command = ChildProcess . make (
@@ -170,6 +218,21 @@ export const make = Effect.gen(function* () {
170218 }
171219 yield * writeDesktopEntry ;
172220 if ( ! environment . isPackaged ) return ;
221+
222+ yield * updateDesktopDatabase . pipe (
223+ // Some MIME implementations, including GIO, use mimeinfo.cache to verify
224+ // that a desktop entry is associated with a scheme. Cache refresh is
225+ // independently best-effort so a missing update-desktop-database executable
226+ // does not prevent xdg-mime from recording the requested default.
227+ Effect . catch ( ( error ) =>
228+ logWarning ( "desktop MIME cache refresh failed" , {
229+ applicationsDir : environment . linuxApplicationsDir ,
230+ message : error . message ,
231+ ...( error . exitCode === undefined ? { } : { exitCode : error . exitCode } ) ,
232+ } ) ,
233+ ) ,
234+ ) ;
235+
173236 yield * setDefaultHandler ;
174237 yield * logInfo ( "registered URL scheme handler" , { scheme } ) ;
175238 } ) . pipe (
0 commit comments