Bug 874090 - Crash in mozilla::dom::Notification::GetPermissionInternal, r=wchen

This commit is contained in:
Andrea Marchesini 2013-05-23 15:38:00 -07:00
parent 2fb9924e99
commit 850c873bc9
3 changed files with 111 additions and 13 deletions

View File

@ -105,13 +105,20 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(NotificationPermissionRequest)
NS_IMETHODIMP
NotificationPermissionRequest::Run()
{
// File are automatically granted permission.
nsCOMPtr<nsIURI> uri;
mPrincipal->GetURI(getter_AddRefs(uri));
bool isFile;
uri->SchemeIs("file", &isFile);
if (isFile) {
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
mPermission = NotificationPermission::Granted;
} else {
// File are automatically granted permission.
nsCOMPtr<nsIURI> uri;
mPrincipal->GetURI(getter_AddRefs(uri));
if (uri) {
bool isFile;
uri->SchemeIs("file", &isFile);
if (isFile) {
mPermission = NotificationPermission::Granted;
}
}
}
// Grant permission if pref'ed on.
@ -398,15 +405,21 @@ Notification::GetPermissionInternal(nsISupports* aGlobal, ErrorResult& aRv)
aRv.Throw(NS_ERROR_UNEXPECTED);
return NotificationPermission::Denied;
}
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
// Allow files to show notifications by default.
nsCOMPtr<nsIURI> uri;
principal->GetURI(getter_AddRefs(uri));
bool isFile;
uri->SchemeIs("file", &isFile);
if (isFile) {
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
if (nsContentUtils::IsSystemPrincipal(principal)) {
return NotificationPermission::Granted;
} else {
// Allow files to show notifications by default.
nsCOMPtr<nsIURI> uri;
principal->GetURI(getter_AddRefs(uri));
if (uri) {
bool isFile;
uri->SchemeIs("file", &isFile);
if (isFile) {
return NotificationPermission::Granted;
}
}
}
// We also allow notifications is they are pref'ed on.

View File

@ -10,6 +10,10 @@ relativesrcdir = @relativesrcdir@
include $(DEPTH)/config/autoconf.mk
MOCHITEST_CHROME_FILES = \
test_system_principal.xul \
$(NULL)
MOCHITEST_FILES = \
test_basic_notification.html \
test_basic_notification_click.html \

View File

@ -0,0 +1,81 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=874090
-->
<window title="Mozilla Bug 874090" onload="runTests()"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=874090"
target="_blank">Mozilla Bug 874090</a>
</body>
<!-- test code goes here -->
<script type="application/javascript">
<![CDATA[
/** Test for Bug 874090 **/
const MOCK_CID = Components.ID("{2a0f83c4-8818-4914-a184-f1172b4eaaa7}");
const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
var mockAlertsService = {
showAlertNotification: function(imageUrl, title, text, textClickable,
cookie, alertListener, name, dir, lang) {
ok(true, "System principal was granted permission and is able to call showAlertNotification.");
unregisterMock();
SimpleTest.finish();
},
QueryInterface: function(aIID) {
if (aIID.equals(Components.interfaces.nsISupports) ||
aIID.equals(Components.interfaces.nsIAlertsService)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
},
createInstance: function(aOuter, aIID) {
if (aOuter != null) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
return this.QueryInterface(aIID);
}
};
function registerMock() {
Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar).
registerFactory(MOCK_CID, "alerts service", ALERTS_SERVICE_CONTRACT_ID, mockAlertsService);
}
function unregisterMock() {
Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar).
unregisterFactory(MOCK_CID, mockAlertsService);
}
function runTests() {
registerMock();
is(Notification.permission, "granted", "System principal should be automatically granted permission.");
Notification.requestPermission(function(permission) {
is(permission, "granted", "System principal should be granted permission when calling requestPermission.");
if (permission == "granted") {
// Create a notification and make sure that it is able to call into
// the mock alert service to show the notification.
new Notification("Hello");
} else {
unregisterMock();
SimpleTest.finish();
}
});
}
SimpleTest.waitForExplicitFinish();
]]>
</script>
</window>