views:

45

answers:

1

I am writing an extension that opens new tabs and I need to save the value of a var in the current tab, such that it will exist even if the user restarts Firefox. How can I do this?

+1  A: 

Use nsIPrefService and nsIPrefBranch.

Something like:

// Get pref service.
nsresult rv;
nsCOMPtr<nsIPrefService> service(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, false);

// Get your extensions branch.
nsCOMPtr<nsIPrefBranch> branch;
service->GetBranch("extensions.YOUREXTENSION.", getter_AddRefs(branch));
NS_ENSURE_SUCCESS(rv, false);

Then you can use GetCharPref() and SetCharPref().

// Get value
char *buf = nsnull;
rv = branch->GetCharPref("YOURPREF", &buf);

To clear a pref, I suspect you would use clearUserPref().

jeffamaphone
Ok, thanks. And how to remove saved pref?
Use ClearUserPref()?
jeffamaphone