Bug 905397 - Support permissions in desktop webrt. ContentPermission.js changes. r=myk
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Tue, 20 Aug 2013 08:18:13 -0400
changeset 143287 a1ba99bda3207c2111ad86dadc817434e69d065d
parent 143286 67173f884be3a8df5f8c3cda441bd743eefc0dfe
child 143288 092dd07390a1e224727a6f30c7c10c98433ff12b
push id25129
push userryanvm@gmail.com
push dateTue, 20 Aug 2013 20:26:54 +0000
treeherdermozilla-central@d136c8999d96 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmyk
bugs905397
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 905397 - Support permissions in desktop webrt. ContentPermission.js changes. r=myk DONTBUILD
webapprt/ContentPermission.js
webapprt/locales/en-US/webapprt/webapp.properties
--- a/webapprt/ContentPermission.js
+++ b/webapprt/ContentPermission.js
@@ -1,84 +1,105 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
+const UNKNOWN_FAIL = ["geolocation", "desktop-notification"];
+
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://webapprt/modules/WebappRT.jsm");
 
 function ContentPermission() {}
 
 ContentPermission.prototype = {
   classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
 
+  _getChromeWindow: function(aWindow) { 
+    return aWindow
+      .QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIWebNavigation)
+      .QueryInterface(Ci.nsIDocShellTreeItem)
+      .rootTreeItem
+      .QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIDOMWindow)
+      .QueryInterface(Ci.nsIDOMChromeWindow);
+  },
+
   prompt: function(request) {
-    // Only handle geolocation requests for now
-    if (request.type != "geolocation") {
-      return;
+    // Reuse any remembered permission preferences
+    let result =
+      Services.perms.testExactPermissionFromPrincipal(request.principal,
+                                                      request.type);
+
+    // We used to use the name "geo" for the geolocation permission, now we're
+    // using "geolocation".  We need to check both to support existing
+    // installations.
+    if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
+         result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
+        request.type == "geolocation") {
+      let geoResult = Services.perms.testExactPermission(request.principal.URI,
+                                                         "geo");
+      // We override the result only if the "geo" permission was allowed or
+      // denied.
+      if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
+          geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
+        result = geoResult;
+      }
     }
 
-    // Reuse any remembered permission preferences
-    let result = Services.perms.testExactPermissionFromPrincipal(request.principal, "geo");
     if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
       request.allow();
       return;
-    }
-    else if (result == Ci.nsIPermissionManager.DENY_ACTION) {
+    } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
+               (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
+                UNKNOWN_FAIL.indexOf(request.type) >= 0)) {
       request.cancel();
       return;
     }
 
-    function getChromeWindow(aWindow) {
-      var chromeWin = aWindow
-        .QueryInterface(Ci.nsIInterfaceRequestor)
-        .getInterface(Ci.nsIWebNavigation)
-        .QueryInterface(Ci.nsIDocShellTreeItem)
-        .rootTreeItem
-        .QueryInterface(Ci.nsIInterfaceRequestor)
-        .getInterface(Ci.nsIDOMWindow)
-        .QueryInterface(Ci.nsIDOMChromeWindow);
-      return chromeWin;
-    }
-
     // Display a prompt at the top level
     let {name} = WebappRT.config.app.manifest;
     let requestingWindow = request.window.top;
-    let chromeWin = getChromeWindow(requestingWindow);
+    let chromeWin = this._getChromeWindow(requestingWindow);
     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
 
     // Construct a prompt with share/don't and remember checkbox
     let remember = {value: false};
     let choice = Services.prompt.confirmEx(
       chromeWin,
-      bundle.formatStringFromName("geolocation.title", [name], 1),
-      bundle.GetStringFromName("geolocation.description"),
+      bundle.formatStringFromName(request.type + ".title", [name], 1),
+      bundle.GetStringFromName(request.type + ".description"),
       // Set both buttons to strings with the cancel button being default
       Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
-      bundle.GetStringFromName("geolocation.sharelocation"),
-      bundle.GetStringFromName("geolocation.dontshare"),
+      bundle.GetStringFromName(request.type + ".allow"),
+      bundle.GetStringFromName(request.type + ".deny"),
       null,
-      bundle.GetStringFromName("geolocation.remember"),
+      bundle.GetStringFromName(request.type + ".remember"),
       remember);
 
-    // Persist the choice if the user wants to remember
+    let action = Ci.nsIPermissionManager.ALLOW_ACTION;
+    if (choice != 0) {
+      action = Ci.nsIPermissionManager.DENY_ACTION;
+    }
+
     if (remember.value) {
-      let action = Ci.nsIPermissionManager.ALLOW_ACTION;
-      if (choice != 0) {
-        action = Ci.nsIPermissionManager.DENY_ACTION;
-      }
-      Services.perms.addFromPrincipal(request.principal, "geo", action);
+      // Persist the choice if the user wants to remember
+      Services.perms.addFromPrincipal(request.principal, request.type, action);
+    } else {
+      // Otherwise allow the permission for the current session
+      Services.perms.addFromPrincipal(request.principal, request.type, action,
+                                      Ci.nsIPermissionManager.EXPIRE_SESSION);
     }
 
     // Trigger the selected choice
     if (choice == 0) {
       request.allow();
     }
     else {
       request.cancel();
--- a/webapprt/locales/en-US/webapprt/webapp.properties
+++ b/webapprt/locales/en-US/webapprt/webapp.properties
@@ -15,20 +15,28 @@ quitApplicationCmdMac.label=Quit %S
 # LOCALIZATION NOTE (hideApplicationCmdMac.label): %S will be replaced with
 # the name of the webapp.
 hideApplicationCmdMac.label=Hide %S
 
 # LOCALIZATION NOTE (geolocation.title): %S will be replaced with the name of
 # the webapp.
 geolocation.title=%S - Share Location
 geolocation.description=Do you want to share your location?
-geolocation.sharelocation=Share Location
-geolocation.dontshare=Don't Share
+geolocation.allow=Share Location
+geolocation.deny=Don't Share
 geolocation.remember=Remember my choice
 
+# LOCALIZATION NOTE (desktop-notification.title): %S will be replaced with the
+# name of the webapp.
+desktop-notification.title=%S - Show notifications
+desktop-notification.description=Do you want to allow notifications?
+desktop-notification.allow=Show
+desktop-notification.deny=Don't show
+desktop-notification.remember=Remember my choice
+
 # LOCALIZATION NOTE (webapps.install.title): %S will be replaced with the name
 # of the webapp being installed.
 webapps.install.title=Install %S
 # LOCALIZATION NOTE (webapps.install.description): %S will be replaced with the
 # name of the webapp being installed.
 webapps.install.description=Do you want to install %S?
 webapps.install.install=Install App
 webapps.install.dontinstall=Don't Install