Bug 1193469 - Make mozSettings more defensive. r=gwagner

This commit is contained in:
Alexandre Lissy 2015-08-13 14:43:00 -04:00
parent 9d73fcf7a8
commit 7d47e0727f
4 changed files with 91 additions and 4 deletions

View File

@ -39,6 +39,8 @@ XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
"@mozilla.org/uuid-generator;1",
"nsIUUIDGenerator");
const kObserverSoftLimit = 10;
/**
* In order to make SettingsManager work with Privileged Apps, we need the lock
* to be OOP. However, the lock state needs to be managed on the child process,
@ -363,14 +365,26 @@ SettingsManager.prototype = {
addObserver: function addObserver(aName, aCallback) {
if (VERBOSE) debug("addObserver " + aName);
if (!this._callbacks) {
this._callbacks = {};
}
if (!this._callbacks[aName]) {
this._callbacks[aName] = [aCallback];
} else {
this._callbacks[aName].push(aCallback);
}
let length = this._callbacks[aName].length;
if (length >= kObserverSoftLimit) {
debug("WARNING: MORE THAN " + kObserverSoftLimit + " OBSERVERS FOR " +
aName + ": " + length + " FROM" + (new Error).stack);
#ifdef DEBUG
throw Components.results.NS_ERROR_ABORT;
#endif
}
this.checkMessageRegistration();
},
@ -421,7 +435,7 @@ SettingsManager.prototype = {
}
let path;
if (length < 20) {
if (length < kObserverSoftLimit) {
path = "settings-observers";
} else {
path = "settings-observers-suspect/referent(topic=" +

View File

@ -5,8 +5,11 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
EXTRA_COMPONENTS += [
'SettingsManager.js',
'SettingsManager.manifest',
'SettingsManager.manifest'
]
EXTRA_PP_COMPONENTS += [
'SettingsManager.js'
]
if CONFIG['MOZ_B2G']:

View File

@ -12,4 +12,6 @@ support-files =
[test_settings_navigator_object.html]
[test_settings_onsettingchange.html]
[test_settings_bug1110872.html]
skip-if = !e10s
skip-if = !e10s
[test_settings_observer_killer.html]
skip-if = !debug

View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1193469
-->
<head>
<title>Test for Bug 1193469 Settings API</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="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1193469">Mozilla Bug 1193469</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe></iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
var url = SimpleTest.getTestFileURL("file_loadserver.js");
var script = SpecialPowers.loadChromeScript(url);
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPermissions([
{type: "settings-api-read", allow: 1, context: document},
{type: "settings-api-write", allow: 1, context: document},
{type: "settings-read", allow: 1, context: document},
{type: "settings-write", allow: 1, context: document},
{type: "settings-clear", allow: 1, context: document}
], function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozSettings.enabled", true]]
},
function () {
ok(true, "abusing observers");
var obs;
for (obs = 0; obs < 9; obs++) {
navigator.mozSettings.addObserver("fake.setting.key", function(v) {
// nothing to do for real ...
ok(false, "should not be called");
});
ok(true, "first: added observer #" + obs);
}
ok(true, "adding first observers, should not have thrown");
try {
ok(true, "second: adding new observer");
navigator.mozSettings.addObserver("fake.setting.key", function(v) {
// nothing to do for real ...
ok(false, "should not be called");
});
ok(false, "adding too many observers should have thrown");
} catch (ex) {
ok(true, "got exception when trying to add too many observers");
}
SimpleTest.finish();
});
});
</script>
</pre>
</body>
</html>