How can I read the source URL of a file downloaded with FireFox from an external application?

0

I have an C++ app I built which is registered as the default handler for a file with a specific extension. So when I download one of these files with Firefox from a website, it downloads it to a temp directory and then shell executes my app while passing the full path to the downloaded file on the command line.

What is the best way to figure out from the external app what the original download url of the file was, given only it's path on disk? Can I use XPCOM API calls to inspect the FireFox download manager database?

I've figured out that this data get's stored in the "%APPData%\Mozilla\Firefox\($profile)\downloads.sqlite" file which is a SqlLite db file, but I really rather not try to open this file directly as FireFox has an open write handle to the file while running.

After perusing the Mozilla developer center for a while, I ran accross the nsIDownloadManager service, which seems to be just the thing. But I can't seem to get access to it from XPCOM in a separate process?

Here's the code I am using:

nsresult rv;

//init XPCOM
nsCOMPtr<nsIServiceManager> servMgr; 
rv = NS_InitXPCOM2(getter_AddRefs(servMgr), nsnull, nsnull);
NS_ENSURE_SUCCESS(rv, rv);

//Get a download manager instance
nsCOMPtr<nsIDownloadManager> downloadMgr;
rv = servMgr->GetServiceByContractID(NS_DOWNLOADMANAGER_CONTRACTID,
       nsIDownloadManager::GetIID(),  getter_AddRefs(downloadMgr));
NS_ENSURE_SUCCESS(rv, rv);

When I run this, the GetServiceByContractID() call returns 0x8007000e, which is defined in nsError.h as NS_ERROR_OUT_OF_MEMORY. (which I find very weird).

Any ideas here? Am I barking up the right tree?

c++
firefox
xul
xpcom
asked on Stack Overflow Jul 30, 2009 by (unknown user) • edited Jun 20, 2020 by Community

2 Answers

1

No, you can't access Firefox's XPCOM objects from an external process, and you also shouldn't open the sqlite database while Firefox has it open. I don't know that there's any straightforward way to do what you want without writing a Firefox extension that has access to the Firefox internals.

answered on Stack Overflow Jul 30, 2009 by Ted Mielczarek
0

I'm a little hazy on the details right now, but, assuming that your download is served with a custom MIME type, it's possible to register a handler for that type; your handler can then cancel the download and pass the URL to your application.

answered on Stack Overflow Jan 29, 2011 by Neil

User contributions licensed under CC BY-SA 3.0