Does simple-storage work?

Hi Mozillans,

I’m writing an add-on using jpm and need some sort of non-volatile storage that persists across ff restarts.

Enter simple-storage(https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/simple-storage).

I have tried everything I can think of but I cannot get simple-storage to actually store anything. My code is as simple as this.

var ss = require(“sdk/simple-storage”);

ss.storage.derp = “DERP”;
ss.storage.arr = [{id: “arr”}];
ss.storage.exists = true;
ss.storage.keys = {};

None of these values ever get stored. If I view the “ss” object in the add-on debugger I don’t see any values being set, and after a restart nothing is present. So nothing is being stored.

What am I doing wrong?

To further debug, I calculated MD5 sums for every file in the profile directory and watched to see which change between restarts. None of the files that change between restarts have anything to do with simple storage. So I’m left wondering if this is even intended to work.

I’ve read this(https://bugzilla.mozilla.org/show_bug.cgi?id=1091244), which seems to be saying that SS is incredibly buggy and will be deprecated. So maybe I should use this(https://www.npmjs.com/package/async-storage).

Does anyone have an opinion on this?

Any help would be much appreciated.

Thanks,
Andrew

simple-storage essentially works although it certainly has bugs. It is also a synchronous API so Mozilla will always be looking for a replacement. If you are using “jpm run” then simple storage doesn’t work, full stop. simple-storage stores data specific to the scope of one addon in one profile. jpm run by default creates a new profile for each run so no data will be persisted. I think the debugger suffers from essentially the same problem, where it doesn’t see the correct scope.

You can use jpm to run with a specific profile directory (not just a profile name) and that should work.

Note that simple storage is a “synchronous” API. Although IO is not actually performed synchronously with each access, it may still cause some jank if it is run on the main thread. There are other possible solutions although I don’t think I’d install a third party module unless there were real big problems.

Thanks for your reply.

I launch jpm like this.
/usr/local/bin/jpm run --profile /Users/smutt/Library/Application\ Support/Firefox/Profiles/1ih26998.bob

I can ‘cd’ to this directory, so no problem there.

Do you know what actual files SS creates or modifies so that I can poke it via the filesystem?

Thanks,
Andrew

Simple storage is stored in jetpack/{{addon-uuid}}/simple-storage/store.json. The file is just used as a backing store and may not be written at all. Any data will be flushed every 5 minutes, when Firefox is shut down cleanly, or when an addon is uninstalled. There is also a quota limit, 5MB by default.

I can see those files for the “default” profile. But I don’t see anything for my development profiles. Including the “bob” profile that I reference above. My development profiles do not contain “jetpack” sub-directories.

I wonder why that is? Any ideas?

So I installed Ghostery on my “bob” development profile and it created the jetpack directory, and subsequent Ghostery simple-storage sub-directories.(jetpack/firefox@ghostery.com/simple-storage)

However, after running my add-on a few times it still did not create any directories for my add-on’s simple-storage.

I see these log messages that look like they might relate to storage.

1453832853904 DeferredSave.extensions.json DEBUG Write succeeded
1453832975557 addons.manager INFO Skipping background update check
1453833220241 DeferredSave.extensions.json DEBUG Save changes
1453833220242 DeferredSave.extensions.json DEBUG Starting timer
1453833230139 DeferredSave.extensions.json DEBUG Starting write
1453833230143 DeferredSave.extensions.json DEBUG Write succeeded

You also need to pass --no-copy so jpm doesn’t copy the profile for jpm run since it may apply some changes to it. See also https://developer.mozilla.org/en-US/Add-ons/SDK/Tools/jpm#jpm_run

Thank you!

This is the magic I was looking for. I see things being stored now.

Maybe they should mention this somewhere on the simple-storage page.

If the simple storage pages suggests using the profile argument it should. A hint that the profile is copied should be everywhere the profile argument is used. Luckily MDN is a wimi, so you can help fix that :smile:

The simple storage documentation mentions using jpm run with a profile directory, but does not mention using the no-copy flag.

As I tried to say before: MDN is a wiki. And you can login with persona or github. So please feel free and add it where you think it will help others the most.

Thanks everyone for their help. As penance, I have edited the simple-storage documentation to include reference to the --no-copy option :smile:

Thanks,
Andrew

1 Like

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.

I like to just use OS.File (as it is async) to create your own file (at same path as simple-storage module) 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.