1

I'm trying to use the simplestorage from my extension, but I can't retrieve values between browser sessions. Here's the thing: From my main code, I created a value this way:

var ss = require("sdk/simple-storage");
ss.storage.foo = [{id:"bar1", properties:{a:"aaa", b:"bbb"}}]
console.log(ss.storage.foo);

This is ok, I coud see the object through the log. But then I closed the browser, commented the "foo definition" (line 2) and the console log was "undefined".

I know cfx run by default uses a fresh profile each time it runs, so simple storage won't persist from one run to the next. But I'm using

cfx -b firefox run --profiledir=$HOME/.mozilla/firefox/nightly.ext-dev

So I'm sure I'm using the same profile everytime.

What could be happening? What am I missing? Any idea is welcome! Thanks in advance!

Thanks to the answer of Notidart, I could discover that the problem was the file is saved when you close Firefox in the right way. When you just kill it through console, it's not persisting data.

gal007
  • 6,911
  • 8
  • 47
  • 70
  • I recall you worked on firefox addons for mobile and desktop, would you have advice for this question here: http://stackoverflow.com/q/31041439/1828637 – Noitidart Jun 26 '15 at 03:28

3 Answers3

2

This is how simple storage works. It creates a folder in your ProfD folder which is your profile directory: https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/simple-storage.js#L188

let storeFile = Cc["@mozilla.org/file/directory_service;1"].
                getService(Ci.nsIProperties).
                get("ProfD", Ci.nsIFile);
storeFile.append(JETPACK_DIR_BASENAME);
storeFile.append(jpSelf.id);
storeFile.append("simple-storage");
file.mkpath(storeFile.path);
storeFile.append("store.json");
return storeFile.path;

The exact location of the file made is in a your profile folder, in a folder named jetpack then your addon id, then a folder called simple-storage, then in a file in that folder called store.json. Example path:

ProfD/jetpack/addon-id/simple-storage/store.json

It then writes data to that file. Every time your profile folder is recreated (due to the nature of temp profile, due to jpm / cfx), your data is erased.

You should just use OS.File to create your own file to save data. OS.File is better way then nsIFile which is what simple-storage does. Save it outside that ProfD folder, so but make sure to remove it on uninstall of your addon otherwise you pollute your users computers

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • The weird thing is I am forcing to use always the same profile, but I will use my own file and avoit all the problems :) thank you! – gal007 Jul 01 '15 at 14:15
  • 1
    Cound't resist... I readed the file and figures that nothing is being saved! Even during execution time! that's the readon it never persists. And yep, I looked in: $HOME/.mozilla/firefox/nightly.ext-dev/jetpack/jid1-8RskSx1RnMDgug@jetpack/simple-storage/ – gal007 Jul 01 '15 at 14:24
  • 1
    The problem was, the file is saved when you close Firefox in the right way. When you just kill it through console, it's not persisting data – gal007 Jul 01 '15 at 14:29
  • Very interesting share about not saving if improperly shutdown. They should have a backup way to handle that. Let's file a bug, do you know how to do that on http://bugzilla.mozilla.org/? – Noitidart Jul 01 '15 at 20:25
0

Just in case someone else finds this question while using jpm, note that --profiledir is removed from jpm, so to make jpm run using the same profile directory (and thereby the same simple-storage data), you have to run it with the --profile option pointing at the profile path - not the profile name.

jpm run --profile path/to/profile
groovecoder
  • 1,551
  • 1
  • 17
  • 27
0

For future readers, an alternative to @Noitidart's recommendation of using OS.File, is to use the Low-Level API io/file

You can create a file using fileIO.open(path). If the file doesn't exist, it will be created. You can read and write by including the second argument fileIO.open(path, mode).

The mode can be:

  • r - Read-only mode
  • w - Write-only Mode
  • b - Binary mode

It defaults to r. You can use this to read and write to a file (obviously the file cannot be in the ProfD folder or it will get removed each time jpm / cfx is run)

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54