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 // Reuse any remembered permission preferences
35 Services.perms.testExactPermissionFromPrincipal(request.principal,
38 // We used to use the name "geo" for the geolocation permission, now we're
39 // using "geolocation". We need to check both to support existing
41 if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
42 result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
43 request.type == "geolocation") {
44 let geoResult = Services.perms.testExactPermission(request.principal.URI,
46 // We override the result only if the "geo" permission was allowed or
48 if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
49 geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
54 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
57 } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
58 (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
59 UNKNOWN_FAIL.indexOf(request.type) >= 0)) {
64 // Display a prompt at the top level
65 let {name} = WebappRT.localeManifest;
66 let requestingWindow = request.window.top;
67 let chromeWin = this._getChromeWindow(requestingWindow);
68 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
70 // Construct a prompt with share/don't and remember checkbox
71 let remember = {value: false};
72 let choice = Services.prompt.confirmEx(
74 bundle.formatStringFromName(request.type + ".title", [name], 1),
75 bundle.GetStringFromName(request.type + ".description"),
76 // Set both buttons to strings with the cancel button being default
77 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
78 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
79 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
80 bundle.GetStringFromName(request.type + ".allow"),
81 bundle.GetStringFromName(request.type + ".deny"),
83 bundle.GetStringFromName(request.type + ".remember"),
86 let action = Ci.nsIPermissionManager.ALLOW_ACTION;
88 action = Ci.nsIPermissionManager.DENY_ACTION;
92 // Persist the choice if the user wants to remember
93 Services.perms.addFromPrincipal(request.principal, request.type, action);
95 // Otherwise allow the permission for the current session
96 Services.perms.addFromPrincipal(request.principal, request.type, action,
97 Ci.nsIPermissionManager.EXPIRE_SESSION);
100 // Trigger the selected choice
110 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);