browser/metro/components/ContentPermissionPrompt.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/components/ContentPermissionPrompt.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cr = Components.results;
    1.10 +const Cu = Components.utils;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +
    1.15 +const kCountBeforeWeRemember = 5;
    1.16 +
    1.17 +const kEntities = {
    1.18 +  "geolocation": "geolocation2",
    1.19 +  "indexedDB": "offlineApps",
    1.20 +  "indexedDBQuota": "indexedDBQuota"
    1.21 +};
    1.22 +
    1.23 +const kIcons = {
    1.24 +  geolocation: "chrome://browser/skin/images/infobar-geolocation.png"
    1.25 +};
    1.26 +
    1.27 +function ContentPermissionPrompt() {}
    1.28 +
    1.29 +ContentPermissionPrompt.prototype = {
    1.30 +  classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
    1.31 +
    1.32 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
    1.33 +
    1.34 +  getChromeWindow: function getChromeWindow(aWindow) {
    1.35 +     let chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.36 +                            .getInterface(Ci.nsIWebNavigation)
    1.37 +                            .QueryInterface(Ci.nsIDocShellTreeItem)
    1.38 +                            .rootTreeItem
    1.39 +                            .QueryInterface(Ci.nsIInterfaceRequestor)
    1.40 +                            .getInterface(Ci.nsIDOMWindow)
    1.41 +                            .QueryInterface(Ci.nsIDOMChromeWindow);
    1.42 +     return chromeWin;
    1.43 +  },
    1.44 +
    1.45 +  getChromeWindowForRequest: function getChromeWindowForRequest(aRequest) {
    1.46 +    if (aRequest.window)
    1.47 +      return this.getChromeWindow(aRequest.window.top).wrappedJSObject;
    1.48 +    return aRequest.element.ownerDocument.defaultView;
    1.49 +  },
    1.50 +
    1.51 +  getNotificationBoxForRequest: function getNotificationBoxForRequest(request) {
    1.52 +    let chromeWin = this.getChromeWindowForRequest(request);
    1.53 +    if (request.window) {
    1.54 +      let requestingWindow = request.window.top;
    1.55 +      let windowID = request.window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
    1.56 +      let browser = chromeWin.Browser.getBrowserForWindowId(windowID);
    1.57 +      return chromeWin.getNotificationBox(browser);
    1.58 +    }
    1.59 +    return chromeWin.Browser.getNotificationBox(request.element);
    1.60 +  },
    1.61 +
    1.62 +  handleExistingPermission: function handleExistingPermission(request, type) {
    1.63 +    let result = Services.perms.testExactPermissionFromPrincipal(request.principal, type);
    1.64 +    if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
    1.65 +      request.allow();
    1.66 +      return true;
    1.67 +    }
    1.68 +    if (result == Ci.nsIPermissionManager.DENY_ACTION) {
    1.69 +      request.cancel();
    1.70 +      return true;
    1.71 +    }
    1.72 +    return false;
    1.73 +  },
    1.74 +
    1.75 +  prompt: function(request) {
    1.76 +    // Only allow exactly one permission rquest here.
    1.77 +    let types = request.types.QueryInterface(Ci.nsIArray);
    1.78 +    if (types.length != 1) {
    1.79 +      request.cancel();
    1.80 +      return;
    1.81 +    }
    1.82 +    let perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
    1.83 +
    1.84 +    // returns true if the request was handled
    1.85 +    if (this.handleExistingPermission(request, perm.type))
    1.86 +       return;
    1.87 +
    1.88 +    let pm = Services.perms;
    1.89 +    let notificationBox = this.getNotificationBoxForRequest(request);
    1.90 +    let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
    1.91 +    
    1.92 +    let notification = notificationBox.getNotificationWithValue(perm.type);
    1.93 +    if (notification)
    1.94 +      return;
    1.95 +
    1.96 +    let entityName = kEntities[perm.type];
    1.97 +    let icon = kIcons[perm.type] || "";
    1.98 +
    1.99 +    let buttons = [{
   1.100 +      label: browserBundle.GetStringFromName(entityName + ".allow"),
   1.101 +      accessKey: "",
   1.102 +      callback: function(notification) {
   1.103 +        request.allow();
   1.104 +      }
   1.105 +    },
   1.106 +    {
   1.107 +      label: browserBundle.GetStringFromName("contentPermissions.alwaysForSite"),
   1.108 +      accessKey: "",
   1.109 +      callback: function(notification) {
   1.110 +        Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION);
   1.111 +        request.allow();
   1.112 +      }
   1.113 +    },
   1.114 +    {
   1.115 +      label: browserBundle.GetStringFromName("contentPermissions.neverForSite"),
   1.116 +      accessKey: "",
   1.117 +      callback: function(notification) {
   1.118 +        Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.DENY_ACTION);
   1.119 +        request.cancel();
   1.120 +      }
   1.121 +    }];
   1.122 +
   1.123 +    let message = browserBundle.formatStringFromName(entityName + ".wantsTo",
   1.124 +                                                     [request.principal.URI.host], 1);
   1.125 +    let newBar = notificationBox.appendNotification(message,
   1.126 +                                                    perm.type,
   1.127 +                                                    icon,
   1.128 +                                                    notificationBox.PRIORITY_WARNING_MEDIUM,
   1.129 +                                                    buttons);
   1.130 +
   1.131 +    if (perm.type == "geolocation") {
   1.132 +      // Add the "learn more" link.
   1.133 +      let link = newBar.ownerDocument.createElement("label");
   1.134 +      link.setAttribute("value", browserBundle.GetStringFromName("geolocation.learnMore"));
   1.135 +      link.setAttribute("class", "text-link notification-link");
   1.136 +      newBar.insertBefore(link, newBar.firstChild);
   1.137 +
   1.138 +      let win = this.getChromeWindowForRequest(request);
   1.139 +      link.addEventListener("click", function() {
   1.140 +        let url = Services.urlFormatter.formatURLPref("browser.geolocation.warning.infoURL");
   1.141 +        win.BrowserUI.addAndShowTab(url, win.Browser.selectedTab);
   1.142 +      });
   1.143 +    }
   1.144 +  }
   1.145 +};
   1.146 +
   1.147 +
   1.148 +//module initialization
   1.149 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);

mercurial