|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /* PermissionPromptHelper checks the permissionDB for a given permission |
|
6 * name and performs prompting if needed. |
|
7 * Usage: send PermissionPromptHelper:AskPermission via the FrameMessageManager with: |
|
8 * |origin|, |appID|, |browserFlag| -> used for getting the principal and |
|
9 * |type| and |access| to call testExactPermissionFromPrincipal. |
|
10 * Note that |access| isn't currently used. |
|
11 * Other arugments are: |
|
12 * requestID: ID that gets returned with the result message. |
|
13 * |
|
14 * Once the permission is checked, it returns with the message |
|
15 * "PermissionPromptHelper:AskPermission:OK" |
|
16 * The result contains the |result| e.g.Ci.nsIPermissionManager.ALLOW_ACTION |
|
17 * and a requestID that |
|
18 */ |
|
19 |
|
20 "use strict"; |
|
21 |
|
22 let DEBUG = 0; |
|
23 let debug; |
|
24 if (DEBUG) |
|
25 debug = function (s) { dump("-*- Permission Prompt Helper component: " + s + "\n"); } |
|
26 else |
|
27 debug = function (s) {} |
|
28 |
|
29 const Cu = Components.utils; |
|
30 const Cc = Components.classes; |
|
31 const Ci = Components.interfaces; |
|
32 |
|
33 this.EXPORTED_SYMBOLS = ["PermissionPromptHelper"]; |
|
34 |
|
35 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
36 Cu.import("resource://gre/modules/Services.jsm"); |
|
37 |
|
38 XPCOMUtils.defineLazyServiceGetter(this, "ppmm", |
|
39 "@mozilla.org/parentprocessmessagemanager;1", |
|
40 "nsIMessageListenerManager"); |
|
41 |
|
42 XPCOMUtils.defineLazyServiceGetter(this, "permissionPromptService", |
|
43 "@mozilla.org/permission-prompt-service;1", |
|
44 "nsIPermissionPromptService"); |
|
45 |
|
46 let appsService = Cc["@mozilla.org/AppsService;1"].getService(Ci.nsIAppsService); |
|
47 |
|
48 this.PermissionPromptHelper = { |
|
49 init: function init() { |
|
50 debug("Init"); |
|
51 ppmm.addMessageListener("PermissionPromptHelper:AskPermission", this); |
|
52 Services.obs.addObserver(this, "profile-before-change", false); |
|
53 }, |
|
54 |
|
55 askPermission: function askPermission(aMessage, aCallbacks) { |
|
56 let msg = aMessage.json; |
|
57 |
|
58 let access = msg.type; |
|
59 if (msg.access) { |
|
60 access = access + "-" + msg.access; |
|
61 } |
|
62 |
|
63 let uri = Services.io.newURI(msg.origin, null, null); |
|
64 let principal = |
|
65 Services.scriptSecurityManager.getAppCodebasePrincipal(uri, msg.appID, msg.browserFlag); |
|
66 |
|
67 let permValue = |
|
68 Services.perms.testExactPermissionFromPrincipal(principal, access); |
|
69 |
|
70 if (permValue == Ci.nsIPermissionManager.DENY_ACTION || |
|
71 permValue == Ci.nsIPermissionManager.UNKNOWN_ACTION) { |
|
72 aCallbacks.cancel(); |
|
73 return; |
|
74 } |
|
75 |
|
76 if (permValue == Ci.nsIPermissionManager.PROMPT_ACTION) { |
|
77 |
|
78 // create the options from permission request. |
|
79 let options = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray); |
|
80 if (msg.options) { |
|
81 for (let option of options) { |
|
82 options.appendElement(option); |
|
83 } |
|
84 } |
|
85 |
|
86 // create an array with a nsIContentPermissionType element |
|
87 let type = { |
|
88 type: msg.type, |
|
89 access: msg.access ? msg.access : "unused", |
|
90 options: options, |
|
91 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionType]) |
|
92 }; |
|
93 let typeArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray); |
|
94 typeArray.appendElement(type, false); |
|
95 |
|
96 // create a nsIContentPermissionRequest |
|
97 let request = { |
|
98 types: typeArray, |
|
99 principal: principal, |
|
100 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionRequest]), |
|
101 allow: aCallbacks.allow, |
|
102 cancel: aCallbacks.cancel, |
|
103 window: Services.wm.getOuterWindowWithId(msg.windowID) |
|
104 }; |
|
105 |
|
106 permissionPromptService.getPermission(request); |
|
107 return; |
|
108 } |
|
109 |
|
110 if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) { |
|
111 aCallbacks.allow(); |
|
112 return; |
|
113 } |
|
114 }, |
|
115 |
|
116 observe: function observe(aSubject, aTopic, aData) { |
|
117 ppmm.removeMessageListener("PermissionPromptHelper:AskPermission", this); |
|
118 Services.obs.removeObserver(this, "profile-before-change"); |
|
119 ppmm = null; |
|
120 }, |
|
121 |
|
122 receiveMessage: function receiveMessage(aMessage) { |
|
123 debug("PermissionPromptHelper::receiveMessage " + aMessage.name); |
|
124 let mm = aMessage.target; |
|
125 let msg = aMessage.data; |
|
126 |
|
127 let result; |
|
128 if (aMessage.name == "PermissionPromptHelper:AskPermission") { |
|
129 this.askPermission(aMessage, { |
|
130 cancel: function() { |
|
131 mm.sendAsyncMessage("PermissionPromptHelper:AskPermission:OK", |
|
132 { result: Ci.nsIPermissionManager.DENY_ACTION, |
|
133 requestID: msg.requestID }); |
|
134 }, |
|
135 allow: function(aChoice) { |
|
136 mm.sendAsyncMessage("PermissionPromptHelper:AskPermission:OK", |
|
137 { result: Ci.nsIPermissionManager.ALLOW_ACTION, |
|
138 choice: aChoice, |
|
139 requestID: msg.requestID }); |
|
140 } |
|
141 }); |
|
142 } |
|
143 } |
|
144 } |
|
145 |
|
146 PermissionPromptHelper.init(); |