Bug 1589754 - Fix permissions.query in top-level document r=tnguyen a=pascalc
authorRob Wu <rob@robwu.nl>
Thu, 24 Oct 2019 12:57:23 +0000
changeset 559875 8902efec39559defebbecf5d422c3d79cc8d003a
parent 559874 fea34d94ec4fa5e757d114b546010d285375e7a5
child 559876 bd900d6ea7954047cded9c3de71b399d353f56d4
push id12250
push userdvarga@mozilla.com
push dateMon, 04 Nov 2019 11:28:25 +0000
treeherdermozilla-beta@5537d9a9bad3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstnguyen, pascalc
bugs1589754
milestone71.0
Bug 1589754 - Fix permissions.query in top-level document r=tnguyen a=pascalc Differential Revision: https://phabricator.services.mozilla.com/D50067
dom/notification/test/mochitest/mochitest.ini
dom/notification/test/mochitest/test_notification_permissions.html
extensions/permissions/PermissionDelegateHandler.cpp
--- a/dom/notification/test/mochitest/mochitest.ini
+++ b/dom/notification/test/mochitest/mochitest.ini
@@ -6,12 +6,14 @@ support-files =
   MockServices.js
   NotificationTest.js
 skip-if = toolkit == 'android' && !is_fennec # Bug 1531097
 
 [test_notification_basics.html]
 [test_notification_crossorigin_iframe.html]
 # This test needs to be run on HTTP (not HTTPS).
 [test_notification_insecure_context.html]
+[test_notification_permissions.html]
+scheme = https
 [test_notification_storage.html]
 [test_bug931307.html]
 [test_notification_tag.html]
 fail-if = fission
new file mode 100644
--- /dev/null
+++ b/dom/notification/test/mochitest/test_notification_permissions.html
@@ -0,0 +1,67 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+Tests that the Notification.requestPermission and navigator.permissions.query
+return values are consistent with the stored permission.
+https://bugzilla.mozilla.org/show_bug.cgi?id=1589754
+-->
+<head>
+  <title>Notification permissions and permissions API</title>
+  <script src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
+</head>
+<body>
+  <p id="display"></p>
+  <div id="content" style="display: none">
+  </div>
+  <pre id="test">
+<script class="testbody">
+
+add_task(async function test_notifications_permission() {
+  await SpecialPowers.pushPrefEnv({
+    set: [
+      // Set pref to exercise relevant code path for regression test.
+      ["permissions.delegation.enable", true],
+    ],
+  });
+
+  async function testPermissionInWindow(win) {
+    async function checkPermission(perm, expectedResult, expectedPermission) {
+      await SpecialPowers.pushPermissions([
+        {
+          type: "desktop-notification",
+          allow: SpecialPowers.Ci.nsIPermissionManager[perm],
+          context: document,
+        },
+      ]);
+      is(
+        await win.Notification.requestPermission(),
+        expectedResult,
+        `expected requestPermission() result for permission ${perm}`
+      );
+
+      let result =
+        await win.navigator.permissions.query({ name: "notifications" });
+      is(
+        result.state,
+        expectedPermission,
+        `expected permissions API result for permission ${perm}`
+      );
+    }
+
+    await checkPermission("UNKNOWN_ACTION", "default", "prompt");
+    await checkPermission("ALLOW_ACTION", "granted", "granted");
+    await checkPermission("DENY_ACTION", "denied", "denied");
+    await checkPermission("PROMPT_ACTION", "default", "prompt");
+  }
+
+  var win = window.open("blank.html");
+  await new Promise(resolve => { win.onload = resolve; });
+  await testPermissionInWindow(win);
+  win.close();
+});
+
+</script>
+  </pre>
+</body>
+</html>
--- a/extensions/permissions/PermissionDelegateHandler.cpp
+++ b/extensions/permissions/PermissionDelegateHandler.cpp
@@ -77,17 +77,18 @@ nsresult PermissionDelegateHandler::GetP
     return rv;
   }
 
   nsCOMPtr<nsIPrincipal> principal = mDocument->NodePrincipal();
   if (!Preferences::GetBool("permissions.delegation.enable", false)) {
     return permMgr->TestPermissionFromPrincipal(principal, aType, aPermission);
   }
 
-  if (info->mPolicy == DelegatePolicy::eDelegateUseIframeOrigin) {
+  if (mDocument->GetWindow()->IsTopLevelWindow() ||
+      info->mPolicy == DelegatePolicy::eDelegateUseIframeOrigin) {
     return permMgr->TestPermissionFromPrincipal(principal, aType, aPermission);
   }
 
   nsPIDOMWindowInner* window = mDocument->GetInnerWindow();
   nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(window);
   nsIPrincipal* topPrincipal = innerWindow->GetTopLevelPrincipal();
 
   // Permission is delegated in same origin