webapprt/ContentPermission.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cu = Components.utils;
michael@0 8
michael@0 9 const UNKNOWN_FAIL = ["geolocation", "desktop-notification"];
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 Cu.import("resource://webapprt/modules/WebappRT.jsm");
michael@0 14
michael@0 15 function ContentPermission() {}
michael@0 16
michael@0 17 ContentPermission.prototype = {
michael@0 18 classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
michael@0 19 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
michael@0 20
michael@0 21 _getChromeWindow: function(aWindow) {
michael@0 22 return aWindow
michael@0 23 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 24 .getInterface(Ci.nsIWebNavigation)
michael@0 25 .QueryInterface(Ci.nsIDocShellTreeItem)
michael@0 26 .rootTreeItem
michael@0 27 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 28 .getInterface(Ci.nsIDOMWindow)
michael@0 29 .QueryInterface(Ci.nsIDOMChromeWindow);
michael@0 30 },
michael@0 31
michael@0 32 prompt: function(request) {
michael@0 33 // Only allow exactly one permission request here.
michael@0 34 let types = request.types.QueryInterface(Ci.nsIArray);
michael@0 35 if (types.length != 1) {
michael@0 36 request.cancel();
michael@0 37 return;
michael@0 38 }
michael@0 39 let perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
michael@0 40
michael@0 41 // Reuse any remembered permission preferences
michael@0 42 let result =
michael@0 43 Services.perms.testExactPermissionFromPrincipal(request.principal,
michael@0 44 perm.type);
michael@0 45
michael@0 46 // We used to use the name "geo" for the geolocation permission, now we're
michael@0 47 // using "geolocation". We need to check both to support existing
michael@0 48 // installations.
michael@0 49 if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
michael@0 50 result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
michael@0 51 perm.type == "geolocation") {
michael@0 52 let geoResult = Services.perms.testExactPermission(request.principal.URI,
michael@0 53 "geo");
michael@0 54 // We override the result only if the "geo" permission was allowed or
michael@0 55 // denied.
michael@0 56 if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
michael@0 57 geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
michael@0 58 result = geoResult;
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
michael@0 63 request.allow();
michael@0 64 return;
michael@0 65 } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
michael@0 66 (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
michael@0 67 UNKNOWN_FAIL.indexOf(perm.type) >= 0)) {
michael@0 68 request.cancel();
michael@0 69 return;
michael@0 70 }
michael@0 71
michael@0 72 // Display a prompt at the top level
michael@0 73 let {name} = WebappRT.localeManifest;
michael@0 74 let requestingWindow = request.window.top;
michael@0 75 let chromeWin = this._getChromeWindow(requestingWindow);
michael@0 76 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
michael@0 77
michael@0 78 // Construct a prompt with share/don't and remember checkbox
michael@0 79 let remember = {value: false};
michael@0 80 let choice = Services.prompt.confirmEx(
michael@0 81 chromeWin,
michael@0 82 bundle.formatStringFromName(perm.type + ".title", [name], 1),
michael@0 83 bundle.GetStringFromName(perm.type + ".description"),
michael@0 84 // Set both buttons to strings with the cancel button being default
michael@0 85 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
michael@0 86 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
michael@0 87 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
michael@0 88 bundle.GetStringFromName(perm.type + ".allow"),
michael@0 89 bundle.GetStringFromName(perm.type + ".deny"),
michael@0 90 null,
michael@0 91 bundle.GetStringFromName(perm.type + ".remember"),
michael@0 92 remember);
michael@0 93
michael@0 94 let action = Ci.nsIPermissionManager.ALLOW_ACTION;
michael@0 95 if (choice != 0) {
michael@0 96 action = Ci.nsIPermissionManager.DENY_ACTION;
michael@0 97 }
michael@0 98
michael@0 99 if (remember.value) {
michael@0 100 // Persist the choice if the user wants to remember
michael@0 101 Services.perms.addFromPrincipal(request.principal, perm.type, action);
michael@0 102 } else {
michael@0 103 // Otherwise allow the permission for the current session
michael@0 104 Services.perms.addFromPrincipal(request.principal, perm.type, action,
michael@0 105 Ci.nsIPermissionManager.EXPIRE_SESSION);
michael@0 106 }
michael@0 107
michael@0 108 // Trigger the selected choice
michael@0 109 if (choice == 0) {
michael@0 110 request.allow();
michael@0 111 }
michael@0 112 else {
michael@0 113 request.cancel();
michael@0 114 }
michael@0 115 }
michael@0 116 };
michael@0 117
michael@0 118 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);

mercurial