Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | /* static functions */ |
michael@0 | 8 | let DEBUG = 0; |
michael@0 | 9 | let debug; |
michael@0 | 10 | if (DEBUG) { |
michael@0 | 11 | debug = function (s) { dump("-*- PermissionPromptService: " + s + "\n"); }; |
michael@0 | 12 | } |
michael@0 | 13 | else { |
michael@0 | 14 | debug = function (s) {}; |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | const Cc = Components.classes; |
michael@0 | 18 | const Ci = Components.interfaces; |
michael@0 | 19 | const Cu = Components.utils; |
michael@0 | 20 | |
michael@0 | 21 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 22 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 23 | Cu.import("resource://gre/modules/PermissionsInstaller.jsm"); |
michael@0 | 24 | |
michael@0 | 25 | const PERMISSIONPROMPTSERVICE_CONTRACTID = "@mozilla.org/permission-prompt-service;1"; |
michael@0 | 26 | const PERMISSIONPROMPTSERVICE_CID = Components.ID("{e5f953b3-a6ca-444e-a88d-cdc81383741c}"); |
michael@0 | 27 | const permissionPromptService = Ci.nsIPermissionPromptService; |
michael@0 | 28 | |
michael@0 | 29 | var permissionManager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager); |
michael@0 | 30 | var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager); |
michael@0 | 31 | |
michael@0 | 32 | function makePrompt() |
michael@0 | 33 | { |
michael@0 | 34 | return Cc["@mozilla.org/content-permission/prompt;1"].createInstance(Ci.nsIContentPermissionPrompt); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function PermissionPromptService() |
michael@0 | 38 | { |
michael@0 | 39 | debug("Constructor"); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | PermissionPromptService.prototype = { |
michael@0 | 43 | |
michael@0 | 44 | classID : PERMISSIONPROMPTSERVICE_CID, |
michael@0 | 45 | |
michael@0 | 46 | QueryInterface : XPCOMUtils.generateQI([permissionPromptService, Ci.nsIObserver]), |
michael@0 | 47 | |
michael@0 | 48 | classInfo : XPCOMUtils.generateCI({classID: PERMISSIONPROMPTSERVICE_CID, |
michael@0 | 49 | contractID: PERMISSIONPROMPTSERVICE_CONTRACTID, |
michael@0 | 50 | classDescription: "PermissionPromptService", |
michael@0 | 51 | interfaces: [permissionPromptService] |
michael@0 | 52 | }), |
michael@0 | 53 | /** |
michael@0 | 54 | * getPermission |
michael@0 | 55 | * Ask for permission for an API, device, etc. |
michael@0 | 56 | * @param nsIContentPermissionRequest aRequest |
michael@0 | 57 | * @returns void |
michael@0 | 58 | **/ |
michael@0 | 59 | getPermission: function PS_getPermission(aRequest) |
michael@0 | 60 | { |
michael@0 | 61 | if (!(aRequest instanceof Ci.nsIContentPermissionRequest)) { |
michael@0 | 62 | throw new Error("PermissionService.getPermission: " |
michael@0 | 63 | + "2nd argument must be type 'nsIContentPermissionRequest'"); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Only allow exactly one permission request here. |
michael@0 | 67 | let types = aRequest.types.QueryInterface(Ci.nsIArray); |
michael@0 | 68 | if (types.length != 1) { |
michael@0 | 69 | aRequest.cancel(); |
michael@0 | 70 | return; |
michael@0 | 71 | } |
michael@0 | 72 | let reqType = types.queryElementAt(0, Ci.nsIContentPermissionType); |
michael@0 | 73 | |
michael@0 | 74 | let type = reqType.access !== "unused" ? reqType.type + "-" + reqType.access |
michael@0 | 75 | : reqType.type; |
michael@0 | 76 | let perm = |
michael@0 | 77 | permissionManager.testExactPermissionFromPrincipal(aRequest.principal, type); |
michael@0 | 78 | |
michael@0 | 79 | switch (perm) { |
michael@0 | 80 | case Ci.nsIPermissionManager.ALLOW_ACTION: |
michael@0 | 81 | aRequest.allow(); |
michael@0 | 82 | break; |
michael@0 | 83 | case Ci.nsIPermissionManager.PROMPT_ACTION: |
michael@0 | 84 | makePrompt().prompt(aRequest); |
michael@0 | 85 | break; |
michael@0 | 86 | case Ci.nsIPermissionManager.DENY_ACTION: |
michael@0 | 87 | case Ci.nsIPermissionManager.UNKNOWN_ACTION: |
michael@0 | 88 | default: |
michael@0 | 89 | aRequest.cancel(); |
michael@0 | 90 | break; |
michael@0 | 91 | } |
michael@0 | 92 | }, |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PermissionPromptService]); |