Bug 1388724 - 4. Use universal doorhanger API for content permissions; r=esawin draft
authorJim Chen <nchen@mozilla.com>
Fri, 18 Aug 2017 14:27:31 -0400
changeset 649164 83aff805bd08c820a9b79aea8ba418c226b32c79
parent 649163 ae4e55780619817cb3e164c19489dab58e2cd81c
child 649165 b6d4613ee53eeb847759adb51e2eb4633641082b
push id74974
push userbmo:nchen@mozilla.com
push dateFri, 18 Aug 2017 18:32:22 +0000
reviewersesawin
bugs1388724
milestone57.0a1
Bug 1388724 - 4. Use universal doorhanger API for content permissions; r=esawin Use the new universal doorhanger API for showing content permissions like geolocation. MozReview-Commit-ID: 7dNqwo4VGW4
mobile/android/components/ContentPermissionPrompt.js
--- a/mobile/android/components/ContentPermissionPrompt.js
+++ b/mobile/android/components/ContentPermissionPrompt.js
@@ -2,20 +2,25 @@
  * 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 Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 const Cc = Components.classes;
 
-Cu.import("resource://gre/modules/RuntimePermissions.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "RuntimePermissions",
+                                  "resource://gre/modules/RuntimePermissions.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "DoorHanger",
+                                  "resource://gre/modules/Prompt.jsm");
+
 const kEntities = {
   "contacts": "contacts",
   "desktop-notification": "desktopNotification2",
   "geolocation": "geolocation",
   "flyweb-publish-server": "flyWebPublishServer",
 };
 
 // For these types, prompt for permission if action is unknown.
@@ -101,22 +106,16 @@ ContentPermissionPrompt.prototype = {
     // Returns true if the request was handled
     let access = (perm.access && perm.access !== "unused") ?
                  (perm.type + "-" + perm.access) : perm.type;
     if (this.handleExistingPermission(request, access,
           /* denyUnknown */ isApp || PROMPT_FOR_UNKNOWN.indexOf(perm.type) < 0, callback)) {
        return;
     }
 
-    let chromeWin = this.getChromeForRequest(request);
-    let tab = chromeWin.BrowserApp.getTabForWindow(request.window.top);
-    if (!tab) {
-      return;
-    }
-
     let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
     let entityName = kEntities[perm.type];
 
     let buttons = [{
       label: browserBundle.GetStringFromName(entityName + ".dontAllow"),
       callback: function(aChecked) {
         // If the user checked "Don't ask again" or this is a desktopNotification, make a permanent exception
         if (aChecked || entityName == "desktopNotification2")
@@ -136,30 +135,34 @@ ContentPermissionPrompt.prototype = {
           Services.perms.addFromPrincipal(request.principal, access, Ci.nsIPermissionManager.ALLOW_ACTION, Ci.nsIPermissionManager.EXPIRE_SESSION);
         }
 
         callback(/* allow */ true);
       },
       positive: true
     }];
 
-    let requestor = chromeWin.BrowserApp.manifest ? "'" + chromeWin.BrowserApp.manifest.name + "'" : request.principal.URI.host;
+    let chromeWin = this.getChromeForRequest(request);
+    let requestor = (chromeWin.BrowserApp && chromeWin.BrowserApp.manifest) ?
+        "'" + chromeWin.BrowserApp.manifest.name + "'" : request.principal.URI.host;
     let message = browserBundle.formatStringFromName(entityName + ".ask", [requestor], 1);
     // desktopNotification doesn't have a checkbox
     let options;
     if (entityName == "desktopNotification2") {
       options = {
         link: {
           label: browserBundle.GetStringFromName("doorhanger.learnMore"),
           url: "https://www.mozilla.org/firefox/push/"
         }
       };
     } else {
       options = { checkbox: browserBundle.GetStringFromName(entityName + ".dontAskAgain") };
     }
 
-    chromeWin.NativeWindow.doorhanger.show(message, entityName + request.principal.URI.host, buttons, tab.id, options, entityName.toUpperCase());
+    DoorHanger.show(request.window || request.element.ownerGlobal,
+                    message, entityName + request.principal.URI.host,
+                    buttons, options, entityName.toUpperCase());
   }
 };
 
 
 // module initialization
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);