Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

firefox addon: how to debug simple-storage using addon builder


I'm using the online Firefox Addon Builder (https://builder.addons.mozilla.org) to develop my addon, and I'm having some problems with the simple-storage. For the code bellow, I get the same information on the console, for every time I run it (using the "test" option on the addon builder), ie, the value doesn't seem to be stored:

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

console.log("BEFORE INIT");
console.log(JSON.stringify(ss.storage));

if (!ss.storage.pages)
    ss.storage.pages = [];

console.log("AFTER INIT");
console.log(JSON.stringify(ss.storage));

console:

[14:32:38.892] info: xxx: BEFORE INIT
[14:32:38.893] info: xxx: {}
[14:32:38.893] info: xxx: AFTER INIT
[14:32:38.893] info: xxx: {"pages":[]}

I assume this happens because the addon builder creates a new test package every time I run the test, and the storage is private to the addon (and apparently to every test package within the same addon):

[14:31:36.165] "installing from /xpi/test/2d6e6appf76kg/"
...
[14:32:36.378] "installing from /xpi/test/2d6e6appf7r40/"

I tried restarting firefox and then it seems to work (shows me the right thing on the console). How to debug my code without constantly having to restart firefox?


Solution

  • The root of this is a mixture of intended behavior of the SDK and the way the builder-addon operates.

    • The SDK will actually purge the storage upon uninstall. This behavior is intended but broken when using the regular UI to uninstall add-ons.
    • The builder (actually the helper add-on) will always uninstall the old version and reinstall a new version and wipe out the simple-storage by doing so.

    There are several work-arounds that let you keep the simple-storage data

    • Don't use the builder, but the plain SDK with the --profiledir=PROFILEDIR
    • Always disable the add-on before updating it ((ab)uses the broken behavior mentioned above).
    • Don't use the simple-storage module at all. Like LocalStorage it is synchronous by nature doing synchronous I/O on the main thread, which really hurts performance. If you only have a few, short values, use the preferences, e.g. via the simple-prefs module instead. If you have much data, preferably use something like OS.File. If you need something relational, then consider using indexed-db or SQLite.jsm. For OS.File and/or SQLite.jsm, you'll need chrome access.