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/. */
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
9 const UNKNOWN_FAIL = ["geolocation", "desktop-notification"];
11 Cu.import("resource://gre/modules/Services.jsm");
12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 Cu.import("resource://webapprt/modules/WebappRT.jsm");
15 function ContentPermission() {}
17 ContentPermission.prototype = {
18 classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
19 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
21 _getChromeWindow: function(aWindow) {
23 .QueryInterface(Ci.nsIInterfaceRequestor)
24 .getInterface(Ci.nsIWebNavigation)
25 .QueryInterface(Ci.nsIDocShellTreeItem)
27 .QueryInterface(Ci.nsIInterfaceRequestor)
28 .getInterface(Ci.nsIDOMWindow)
29 .QueryInterface(Ci.nsIDOMChromeWindow);
32 prompt: function(request) {
33 // Only allow exactly one permission rquest here.
34 let types = request.types.QueryInterface(Ci.nsIArray);
35 if (types.length != 1) {
39 let perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
41 // Reuse any remembered permission preferences
43 Services.perms.testExactPermissionFromPrincipal(request.principal,
46 // We used to use the name "geo" for the geolocation permission, now we're
47 // using "geolocation". We need to check both to support existing
49 if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
50 result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
51 perm.type == "geolocation") {
52 let geoResult = Services.perms.testExactPermission(request.principal.URI,
54 // We override the result only if the "geo" permission was allowed or
56 if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
57 geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
62 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
65 } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
66 (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
67 UNKNOWN_FAIL.indexOf(perm.type) >= 0)) {
72 // Display a prompt at the top level
73 let {name} = WebappRT.localeManifest;
74 let requestingWindow = request.window.top;
75 let chromeWin = this._getChromeWindow(requestingWindow);
76 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
78 // Construct a prompt with share/don't and remember checkbox
79 let remember = {value: false};
80 let choice = Services.prompt.confirmEx(
82 bundle.formatStringFromName(perm.type + ".title", [name], 1),
83 bundle.GetStringFromName(perm.type + ".description"),
84 // Set both buttons to strings with the cancel button being default
85 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
86 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
87 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
88 bundle.GetStringFromName(perm.type + ".allow"),
89 bundle.GetStringFromName(perm.type + ".deny"),
91 bundle.GetStringFromName(perm.type + ".remember"),
94 let action = Ci.nsIPermissionManager.ALLOW_ACTION;
96 action = Ci.nsIPermissionManager.DENY_ACTION;
100 // Persist the choice if the user wants to remember
101 Services.perms.addFromPrincipal(request.principal, perm.type, action);
103 // Otherwise allow the permission for the current session
104 Services.perms.addFromPrincipal(request.principal, perm.type, action,
105 Ci.nsIPermissionManager.EXPIRE_SESSION);
108 // Trigger the selected choice
118 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);