pages tagged gaiaFeeding the Cloudhttps://feeding.cloud.geek.nz/tags/gaia/Feeding the Cloudikiwiki2021-06-11T20:43:57ZSettings v. Prefs in Gaia Developmenthttps://feeding.cloud.geek.nz/posts/settings_v_prefs_in_gaia_development/
<a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>
2021-06-11T20:43:57Z2014-04-28T08:30:00Z
<p><a href="http://jedparsons.com/">Jed</a> 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 <code>about:config</code> in Firefox)
but couldn't figure out why it wasn't possible to access them from within
our <a href="https://developer.mozilla.org/Apps/Build/App_permissions#Certified_app_permissions">certified</a> application.</p>
<p>The answer is that <strong>settings</strong> and <strong>prefs</strong> are entirely different things
in FxOS land.</p>
<h1 id="Preferences">Preferences</h1>
<p>This is how you set prefs in <a href="https://developer.mozilla.org/Firefox_OS/Platform/Gaia">Gaia</a>:</p>
<pre><code>pref("devtools.debugger.forbid-certified-apps", false);
pref("dom.inter-app-communication-api.enabled", true);
</code></pre>
<p>from <code>build/config/custom-prefs.js</code>.</p>
<p>These will be used by the <a href="https://developer.mozilla.org/docs/Mozilla/Gecko">Gecko</a> layer like this:</p>
<pre><code>if (!Preferences::GetBool("dom.inter-app-communication-api.enabled", false)) {
return false;
}
</code></pre>
<p>from within
<a href="https://mxr.mozilla.org/mozilla-central/source/dom/apps/src/InterAppComm.cpp#19">C++ code</a>,
and like this:</p>
<pre><code>let restrictPrivileges = Services.prefs.getBoolPref("devtools.debugger.forbid-certified-apps");
</code></pre>
<p>from
<a href="https://mxr.mozilla.org/mozilla-central/source/b2g/chrome/content/shell.js#1128">JavaScript code</a>.</p>
<p>Preferences can be strings, integers or booleans.</p>
<h1 id="Settings">Settings</h1>
<p>Settings on the other hand are
<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON#JavaScript_Object_Notation">JSON objects</a>
which can be set like this:</p>
<pre><code>"alarm.enabled": false,
</code></pre>
<p>in <code>build/config/common-settings.json</code> and can then be read <a href="https://mxr.mozilla.org/gaia/source/apps/clock/test/marionette/lib/alarm_actions.js#189">like this</a>:</p>
<pre><code>var req = navigator.mozSettings.createLock().get('alarm.enabled');
req.onsuccess = function() {
marionetteScriptFinished(req.result['alarm.enabled']);
};
</code></pre>
<p>as long as you have the following in your <a href="https://developer.mozilla.org/en-US/Apps/Build/Manifest">application manifest</a>:</p>
<pre><code>"permissions": {
...
"settings":{ "access": "readwrite" },
...
}
</code></pre>
<p>In other words, if you set something in <code>build/config/custom-prefs.js</code>,
don't expect to be able to read it using <code>navigator.mozSettings</code> or the
<a href="https://mxr.mozilla.org/gaia/source/shared/js/settings_helper.js">SettingsHelper</a>!</p>