views:

207

answers:

1

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?

+1  A: 

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.

Ted Mielczarek
Hmmm, that's a shame. The IE version of this was pretty straight forward.Writing a custom FireFox extension just so I can ask for the site of origin seems like overkill to me.I wonder if I can make a copy of the sqlite file and open it read only...
OK, I have abandonded trying to communicate with FireFox externally am now writing an extension to listen for download events. My question now is, what is the best way to get the original download url over to my program?Is it posible to shell execute an external program from within an extension and pass in the origin url on the commandline?Or is there a better way?