Where does firefox get the “default” applications for opening files from?

10,372

Solution 1

A link to a “similar question” (xdg-open default applications behavior – not obviously related, but some experimentation showed that the behaviour is indeed equivalent to the one of xdg-open) led me deeper down the rabbit hole. While Firefox does not rely on, or inherit rules from, xdg-open, it uses the MIME specification files just as xdg-open does.

On a user basis, the MIME opening behaviour is configured by the specification file ~/.local/share/applications/mimeapps.list.

For me, this file contains just a few reasonable protocols and HTML (and similar) files connected to userapp-Firefox-??????.desktop, but you could easily add a line like

application/pdf=evince.desktop

to solve that problem on a per-user basis. If the file does not exist yet, make sure to add a section header, such as

[Default Applications]
application/pdf=evince.desktop

Deeper down, the mime types are defined in /usr/local/share/applications/mimeinfo.cache (this may be /usr/share/… if you are not on a FreeBSD system), which does list application/pdf=inkscape.desktop;evince.desktop;. Both evince.desktop and inkscape.desktop in that folder contain MimeType=[…]application/pdf;[…].

The mimeinfo.cache is automatically generated from the mime types listed in the .desktop files without any well-defined order, so you will have to either remove the PDF mime type from Inkscape and regenerate the cache using update-mime-database, or generate a mimeapps.list (either globally in /usr/local/share/applications/, or for your user in ~/.local/share/applications/mimeapps.list).

Solution 2

In Firefox 38.8.0 ESR, under Debian Lenny with GNOME 2, I discovered that the application associated via XDG (described in the answer by the OP) is used only when opening a downloaded file from Downloads in the Library.

And even there, the implementation gives results inconsistent with xdg-open as it ignores GNOME-specific paths (in my case /usr/share/gnome/applications/defaults.list, which is a symlink to /etc/gnome-vfs-2.0/defaults.list). I had to specify application/pdf=evince.desktop in my ~/.local/share/applications/defaults.list. (Note that defaults.list was merged in mimeapps.list in more recent versions of XDG.)

Instead of XDG, metamail capabilities (mailcap) are used to resolve the associated application for the following use cases:

  • opening downloaded file from the download panel (displayed after pressing the download button in toolbar)
  • default application in the drop-down in file download dialog
  • default application in the drop-down in Preferences → Applications (about:preferences#applications)

The preference in about:preferences#applications is used when navigating to an URL with that content type, unless download is forced.

Mailcap is a mechanism older than XDG and has its roots in e-mail, where MIME comes from, too. The run-mailcap utility is analogical to xdg-open. Its configuration comprises three files (in decreasing precedence):

  • ~/.mailcap — user entries
  • /etc/mailcap.order — system-wide entries ordering specification
  • /etc/mailcap — system-wide entries (same format as user entries)

Together they produce a list of one-line entries that specify the application to be used for any given MIME type. The entries are matched top-down, so what comes first takes precedence.

To make a system-wide change of entries precedence, you need to edit /etc/mailcap.order and run update-mime.

To override some of the entries or specify your own, system-wide, you need to edit the top section (meant for manually created entries) of /etc/mailcap and run update-mime. See the comments in the top section.

For user entries and overrides, use ~/.mailcap. The changes take effect immediately. No ~/.mailcap.order exists; if you want to change entries precedence, reorder them physically (if they are in ~/.mailcap), or override them with the preferred version (if they are in /etc/mailcap).

See man update-mime, man mailcap and man mailcap.order.

Note that some applications use generic utilities (such as x-www-browser or pager) instead of detection of the preferred application. You may need to use update-alternatives or a similar mechanism for choosing the preferred implementation of a generic utility in your distro. Or, to override a system-wide preferred alternative for a specific user, set environment variables such as EDITOR (or VISUAL), PAGER and BROWSER. They are supported by sensible-browser and other, often more specific tools (e.g. git commit uses EDITOR and VISUAL).

Share:
10,372

Related videos on Youtube

Anaphory
Author by

Anaphory

Updated on September 18, 2022

Comments

  • Anaphory
    Anaphory almost 2 years

    By default, the Firefox (33.0) on my FreeBSD 11.0-CURRENT has the default application for opening PDF files set to Inkscape. Firefox does remember my previous choice, evince, in the “What should Firefox do with this file?” dialog, so until recently I was just confused where this configuration came from, but mostly ignored it because it did barely concern me.

    I have however recently started using Zotero as my literature database. Zotero runs as a firefox plugin, and ignores the choices made in my Firefox preferences to open PDF files with evince or ask me what to do with them, and just opens them using Inkscape. This made me try to follow up on this particular configuration item of Firefox, but I could not find where that default is set.

    I could not find the string Inkscape (or inkscape) in any Firefox-related file in my home directory.

  • Palec
    Palec about 7 years
    On my ancient Debian Linux, update-mime-database does something different, I needed update-desktop-database ~/.local/share/applications.
  • Palec
    Palec about 7 years
    Also, xdg-open file.pdf opens Evince (as does Nautilus), while Firefox opens xpdf. Using find ~/.local/share/applications/ /usr/share/applications/ -type f -exec grep -i pdf '{}' + I found that PDF is mentioned nowhere in user configuration, only in /usr/share/applications/mimeinfo.cache and respective *.desktop files. The line in mimeinfo.cache is application/pdf=xpdf.desktop;evince.desktop;xournal.desktop;‌​. I guess FF takes the first item. But how does xdg-open know I want Evince?
  • Palec
    Palec about 7 years
    Using strace, I discovered that xdg-open calls gnome-open and gnome-open reads /usr/share/gnome/applications/defaults.list, which is a symlink to /etc/gnome-vfs-2.0/defaults.list, which specifies application/pdf=evince.desktop. Obviously, vanilla FF lacks GNOME integration and thus ignores this preference.
  • u_Ltd.
    u_Ltd. over 4 years
    This answer ist still valid in Firefox Quantum (64.0). However, mailcap performs better than actual firefox, so you cann't fully rely on run-mailcap --debug --norun. I have an entry like this: application/pdf; sh -c "MEANINGFUL_VAR=preset /usr/bin/evince '%s'". Firefox then invokes sh downloaded_file.pdf. On my console I then see, amongst others /tmp/mozilla_user/downloaded_file.pdf: line 3: 5: command not found etc. This behaviour will remain forever, unless someone fixes this. See Mozilla Bugzilla.
  • zenlord
    zenlord over 4 years
    So, in order to create a consistent experience for his/her users, an admin deploying a combination of Debian with Gnome, should set mime defaults using no less than 4 utilities: update-mime (to update mailcap), xdg-settings (to update xdg-open), gio (to update gnome-open) and update-alternatives (to update the so-called generic utilities). And of course he/she should not forget to manage the settings both system-wide and for each user separately (probably best to block the user's ability to change the defaults)... - ah, and FF's handlers.json file as well: mzl.la/1xKrLAq