views:

555

answers:

2

Hello,

I have developed a Firefox add-on that needs to save some user-data in the local machine. Right now, I use the filesystem functionality that Firefox provides to create some folders and files in the user's system.

Portability is a problem. I designed it for Windows XP. But, Vista's folder structure is different (inside the app data). Linux is completely different. I do an OS check and create/read/write files accordingly. Suddenly, I need to support Macs!

I thought why not save the data in the Firefox preferences. I will use the Preference Manager.

On an average, the total amount of data hardly goes beyond 100KB. But the thing is, in rare cases, the data could get quite big - a few megabytes in the worst case. So, is it a good idea to save it as preference values?

I know that is not what it is meant for, but it will save me a lot of headaches.

Even if it is a bad idea, just let me know about the performance or other issues (if any) of storing that much data in preferences.

Thanks.

A: 

It is possible to locate the profile or extensions directory of firefox via code. You can even locate the directory of your own extension. Portability shouldn't be a problem using this approach.

You can find instructions at the MDC (File I/O).

I wouldn't advice saving data in the preferences. Anyway, instead of using bare file i/o you may look into sqlite-databases - ubiquity uses those as well.

Cheers.

CodeSalad
Someone may google 'ubiquity' as Majid reportedly did. If you do so beware of the first search result which points to a URL at "rocket.ryerson...;. This site is very suspicious. It tries to fool you to download a copy of the add-on hosted on his own server, only, he has crafted it to look exactly like Mozilla's. Thanks to Majid for this information.
Bill the Lizard
+1  A: 

With never versions of Firefox you can use SQLite to store data. The SQLite API in JavaScript isn't terribly good, but from Firefox 3.6 it's starting to get pretty solid (asynch fetching of data, binding multiple sets of data, etc).

You can open/create a file in the profile directory using the following code. It works on all platforms Firefox runs on:

var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");
Marius