forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 437361. Propagate exceptions from showModalDialog's guts to scrip…
…t as needed instead of dropping them on the floor. r+sr=bzbarsky
- Loading branch information
Ben Newman
committed
Jul 29, 2008
1 parent
18be7b3
commit 83ca35e
Showing
5 changed files
with
172 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<!-- | ||
https://bugzilla.mozilla.org/show_bug.cgi?id=437361 | ||
--> | ||
<head> | ||
<title>Test for Bug 437361</title> | ||
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script> | ||
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> | ||
<script type="text/javascript" src="/mozprefs.js"></script> | ||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> | ||
|
||
<script class="testbody" type="text/javascript"> | ||
|
||
/** Test for Bug 437361 **/ | ||
|
||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | ||
|
||
function testModalDialogBlockedCleanly() { | ||
is(true, pref("dom.disable_open_during_load"), "mozprefs sanity check"); | ||
var rv = window.showModalDialog( // should be blocked without exception | ||
"data:text/html,<html><body onload='close(); returnValue = 1;' /></html>"); | ||
is(rv, null, "Modal dialog opened unexpectedly."); | ||
} | ||
|
||
function testModalDialogAllowed() { | ||
is(false, pref("dom.disable_open_during_load"), "mozprefs sanity check"); | ||
var rv = window.showModalDialog( // should not be blocked this time | ||
"data:text/html,<html><body onload='close(); returnValue = 1;' /></html>"); | ||
is(rv, 1, "Problem with modal dialog returnValue."); | ||
} | ||
|
||
function testOtherExceptionsNotTrapped() { | ||
is(false, pref("dom.disable_open_during_load"), "mozprefs sanity check"); | ||
window.showModalDialog('about:config'); // forbidden by SecurityCheckURL | ||
} | ||
|
||
function test(disableOpen, exceptionExpected, testFn, errorMsg) { | ||
try { | ||
pref("dom.disable_open_during_load", disableOpen, testFn); | ||
ok(!exceptionExpected, errorMsg); | ||
} catch (_) { | ||
ok(exceptionExpected, errorMsg); | ||
} | ||
} | ||
|
||
test(true, false, testModalDialogBlockedCleanly, | ||
"Blocked showModalDialog caused an exception."); | ||
|
||
test(false, false, testModalDialogAllowed, | ||
"showModalDialog was blocked even though dom.disable_open_during_load was false."); | ||
|
||
test(false, true, testOtherExceptionsNotTrapped, | ||
"Incorrectly suppressed insecure showModalDialog exception."); | ||
|
||
</script> | ||
</head> | ||
<body> | ||
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=437361">Mozilla Bug 437361</a> | ||
<p id="display"></p> | ||
<div id="content" style="display: none"> | ||
</div> | ||
<pre id="test"> | ||
</pre> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
(function() { | ||
|
||
// NOTE: You *must* also include this line in any test that uses this file: | ||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | ||
|
||
var prefService = Components.classes["@mozilla.org/preferences-service;1"] | ||
.getService(Components.interfaces.nsIPrefService); | ||
|
||
function determinePrefKind(branch, prefName) { | ||
switch (branch.getPrefType(prefName)) { | ||
case branch.PREF_STRING: return "CharPref"; | ||
case branch.PREF_INT: return "IntPref"; | ||
case branch.PREF_BOOL: return "BoolPref"; | ||
default: /* PREF_INVALID */ return "ComplexValue"; | ||
} | ||
} | ||
|
||
function memoize(fn, obj) { | ||
var cache = {}, sep = '___', | ||
join = Array.prototype.join; | ||
return function() { | ||
var key = join.call(arguments, sep); | ||
if (!(key in cache)) | ||
cache[key] = fn.apply(obj, arguments); | ||
return cache[key]; | ||
}; | ||
} | ||
|
||
var makeAccessor = memoize(function(pref) { | ||
var splat = pref.split('.'), | ||
basePref = splat.pop(), | ||
branch, kind; | ||
|
||
try { | ||
branch = prefService.getBranch(splat.join('.') + '.') | ||
} catch (e) { | ||
alert("Calling prefService.getBranch failed: " + | ||
"did you read the NOTE in mozprefs.js?"); | ||
throw e; | ||
} | ||
|
||
kind = determinePrefKind(branch, basePref); | ||
|
||
return function(value) { | ||
var oldValue = branch['get' + kind](basePref); | ||
if (arguments.length > 0) | ||
branch['set' + kind](basePref, value); | ||
return oldValue; | ||
}; | ||
}); | ||
|
||
/* function pref(name[, value[, fn[, obj]]]) | ||
* ----------------------------------------- | ||
* Use cases: | ||
* | ||
* 1. Get the value of the dom.disable_open_during_load preference: | ||
* | ||
* pref('dom.disable_open_during_load') | ||
* | ||
* 2. Set the preference to true, returning the old value: | ||
* | ||
* var oldValue = pref('dom.disable_open_during_load', true); | ||
* | ||
* 3. Set the value of the preference to true just for the duration | ||
* of the specified function's execution: | ||
* | ||
* pref('dom.disable_open_during_load', true, function() { | ||
* window.open(this.getUrl()); // fails if still loading | ||
* }, this); // for convenience, allow binding | ||
* | ||
* Rationale: Unless a great deal of care is taken to catch all | ||
* exceptions and restore original preference values, | ||
* manually setting & restoring preferences can lead | ||
* to unpredictable test behavior. The try-finally | ||
* block below eliminates that risk. | ||
*/ | ||
function pref(name, /*optional:*/ value, fn, obj) { | ||
var acc = makeAccessor(name); | ||
switch (arguments.length) { | ||
case 1: return acc(); | ||
case 2: return acc(value); | ||
default: | ||
var oldValue = acc(value), | ||
extra_args = [].slice.call(arguments, 4); | ||
try { return fn.apply(obj, extra_args) } | ||
finally { acc(oldValue) } // reset no matter what | ||
} | ||
}; | ||
|
||
window.pref = pref; // export | ||
|
||
})(); |