One of the reasons that I enjoy my work customizing Firefox and building add-ons so much is that just about every request I get is a puzzle. They usually start with a question like “Is it possible for Firefox to…” or “Can you make Firefox…” When one of these questions is asked, I start going down the
rabbitfox hole and usually end up with some way to do what the client has asked. A lot of the same questions are asked so often that most of the time the answer involves giving them something I’ve already done or piecing together things I’ve already built. Occasionally, though, I get asked to do something that I’ve never done before. And that’s a lot of fun.
Lately I’ve been helping out a project called Webconverger which is an open source web kiosk. There’s also an add-on on AMO. Recently Kai Hendry, the maintainer, asked an interesting question – “Can you prevent Firefox from automatically downloading unknown files from web pages?” To understand why that is an issue, you need to understand what Firefox does when you click on a link to download a file.
When you go to a page that has an EXE or an ISO or something like that and you click the link, a dialog is displayed to ask you what to do with the file, but in the background, Firefox has already begun the download. After you make your selection, Firefox will move the file to the appropriate place. What this means is that even if you cancel the download, the bandwidth is already used. There are some kiosks that are in limited bandwidth situations, so they want to prevent this from happening at all.
As I began to investigate the problem, I looked into common things like nsIContentPolicy to see if I could just stop the files from being downloaded. Unfortunately at that point, we don’t have mime types or anything interesting that will help us determine that it’s not a file we want to download. So I turned my focus to the dialogs themselves. When you download an unknown file you get a dialog that asks you what to do. I could override that dialog and not display it, but that still doesn’t cancel the download. It also doesn’t help in cases where you are presented with a save dialog immediately. As I dug deeper into the problem, I discovered that one interface controlled both of those cases – nsIHelperAppLauncherDialog.
And that solved the puzzle.
If there’s anything I’ve learned as I’ve worked with Firefox over the years, there’s many ways to solve the same problem, so if you have a better way that I could have done this, please let me know.