Bug 1309384 - merge default and user prefs in Services shim; r=jdescottes

MozReview-Commit-ID: 19rvPSGOsOZ

--HG--
extra : rebase_source : 7d949a7a626056a5c6646cba6276b26095eb9bb3
This commit is contained in:
Tom Tromey 2016-10-14 09:27:44 -06:00
Родитель 524df273d6
Коммит 8f4c815751
3 изменённых файлов: 73 добавлений и 6 удалений

Просмотреть файл

@ -356,12 +356,15 @@ PrefBranch.prototype = {
* @param {String} keyName the full-qualified name of the preference.
* This is also the name of the key in local storage.
* @param {Any} userValue the user value to use if the pref does not exist
* @param {Any} defaultValue the default value to use if the pref
* does not exist
* @param {Boolean} hasUserValue if a new pref is created, whether
* the default value is also a user value
* @param {Any} defaultValue the default value to use if the pref
* does not exist
* @param {Boolean} init if true, then this call is initialization
* from local storage and should override the default prefs
*/
_findOrCreatePref: function (keyName, userValue, hasUserValue, defaultValue) {
_findOrCreatePref: function (keyName, userValue, hasUserValue, defaultValue,
init = false) {
let branch = this._createBranch(keyName.split("."));
if (hasUserValue && typeof (userValue) !== typeof (defaultValue)) {
@ -383,7 +386,7 @@ PrefBranch.prototype = {
throw new Error("unhandled argument type: " + typeof (defaultValue));
}
if (branch._type === PREF_INVALID) {
if (init || branch._type === PREF_INVALID) {
branch._storageUpdated(type, userValue, hasUserValue, defaultValue);
} else if (branch._type !== type) {
throw new Error("attempt to change type of pref " + keyName);
@ -422,7 +425,7 @@ PrefBranch.prototype = {
* Helper function to initialize the root PrefBranch.
*/
_initializeRoot: function () {
if (localStorage.length === 0 && Services._defaultPrefsEnabled) {
if (Services._defaultPrefsEnabled) {
/* eslint-disable no-eval */
let devtools = require("raw!prefs!devtools/client/preferences/devtools");
eval(devtools);
@ -439,7 +442,7 @@ PrefBranch.prototype = {
let {userValue, hasUserValue, defaultValue} =
JSON.parse(localStorage.getItem(keyName));
this._findOrCreatePref(keyName.slice(PREFIX.length), userValue,
hasUserValue, defaultValue);
hasUserValue, defaultValue, true);
}
}

Просмотреть файл

@ -5,3 +5,4 @@ support-files =
[test_service_appinfo.html]
[test_service_focus.html]
[test_service_prefs.html]
[test_service_prefs_defaults.html]

Просмотреть файл

@ -0,0 +1,63 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1309384
-->
<head>
<title>Test for Bug 1309384 - Services.prefs replacement defaults handling</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css"
href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript;version=1.8">
"use strict";
var exports = {}
var module = {exports};
// Allow one require("raw!prefs...") to return some defaults, with the
// others being ignored.
var firstTime = true;
function require(something) {
if (!something.startsWith("raw!prefs!")) {
throw new Error("whoops");
}
if (!firstTime) {
return "";
}
firstTime = false;
return "pref('pref1', 'pref1default');\n" +
"pref('pref2', 'pref2default');\n" +
"pref('pref3', 'pref3default');\n";
}
// Pretend that one of the prefs was modifed by the user in an earlier session.
localStorage.setItem("Services.prefs:pref3", JSON.stringify({
// string
type: 32,
defaultValue: "pref3default",
hasUserValue: true,
userValue: "glass winged butterfly"
}));
</script>
<script type="application/javascript;version=1.8"
src="resource://devtools/client/shared/shim/Services.js"></script>
</head>
<body>
<script type="application/javascript;version=1.8">
"use strict";
is(Services.prefs.getCharPref("pref1"), "pref1default", "pref1 value");
is(Services.prefs.getCharPref("pref2"), "pref2default", "pref2 value");
is(Services.prefs.getCharPref("pref3"), "glass winged butterfly", "pref3 value");
// Only pref3 should be in local storage at this point.
is(localStorage.length, 1, "local storage is correct");
// Clean up.
localStorage.clear();
</script>
</body>