Jed and I got confused the other day when trying to add hidden prefs for a
small Firefox OS application. We wanted to make a few advanced options
configurable via preferences (like those found in about:config
in Firefox)
but couldn't figure out why it wasn't possible to access them from within
our certified application.
The answer is that settings and prefs are entirely different things in FxOS land.
Preferences
This is how you set prefs in Gaia:
pref("devtools.debugger.forbid-certified-apps", false);
pref("dom.inter-app-communication-api.enabled", true);
from build/config/custom-prefs.js
.
These will be used by the Gecko layer like this:
if (!Preferences::GetBool("dom.inter-app-communication-api.enabled", false)) {
return false;
}
from within C++ code, and like this:
let restrictPrivileges = Services.prefs.getBoolPref("devtools.debugger.forbid-certified-apps");
from JavaScript code.
Preferences can be strings, integers or booleans.
Settings
Settings on the other hand are JSON objects which can be set like this:
"alarm.enabled": false,
in build/config/common-settings.json
and can then be read like this:
var req = navigator.mozSettings.createLock().get('alarm.enabled');
req.onsuccess = function() {
marionetteScriptFinished(req.result['alarm.enabled']);
};
as long as you have the following in your application manifest:
"permissions": {
...
"settings":{ "access": "readwrite" },
...
}
In other words, if you set something in build/config/custom-prefs.js
,
don't expect to be able to read it using navigator.mozSettings
or the
SettingsHelper!
For the permission, if you only need to read, you can use "settings":{ "access": "readonly" }.
You can also take a look at SettingsListener.observe which is pretty convenient.