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 Cu.import("resource://gre/modules/Services.jsm");
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://webapprt/modules/WebappRT.jsm");
13 function ContentPermission() {}
15 ContentPermission.prototype = {
16 classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
17 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
19 prompt: function(request) {
20 // Only handle geolocation requests for now
21 if (request.type != "geolocation") {
25 // Reuse any remembered permission preferences
26 let result = Services.perms.testExactPermissionFromPrincipal(request.principal, "geo");
27 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
31 else if (result == Ci.nsIPermissionManager.DENY_ACTION) {
36 function getChromeWindow(aWindow) {
37 var chromeWin = aWindow
38 .QueryInterface(Ci.nsIInterfaceRequestor)
39 .getInterface(Ci.nsIWebNavigation)
40 .QueryInterface(Ci.nsIDocShellTreeItem)
42 .QueryInterface(Ci.nsIInterfaceRequestor)
43 .getInterface(Ci.nsIDOMWindow)
44 .QueryInterface(Ci.nsIDOMChromeWindow);
48 // Display a prompt at the top level
49 let {name} = WebappRT.config.app.manifest;
50 let requestingWindow = request.window.top;
51 let chromeWin = getChromeWindow(requestingWindow);
52 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
54 // Construct a prompt with share/don't and remember checkbox
55 let remember = {value: false};
56 let choice = Services.prompt.confirmEx(
58 bundle.formatStringFromName("geolocation.title", [name], 1),
59 bundle.GetStringFromName("geolocation.description"),
60 // Set both buttons to strings with the cancel button being default
61 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
62 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
63 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
64 bundle.GetStringFromName("geolocation.sharelocation"),
65 bundle.GetStringFromName("geolocation.dontshare"),
67 bundle.GetStringFromName("geolocation.remember"),
70 // Persist the choice if the user wants to remember
72 let action = Ci.nsIPermissionManager.ALLOW_ACTION;
74 action = Ci.nsIPermissionManager.DENY_ACTION;
76 Services.perms.addFromPrincipal(request.principal, "geo", action);
79 // Trigger the selected choice
89 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);