2012-05-31 17:04:52 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2013-08-20 12:18:13 +00:00
|
|
|
const UNKNOWN_FAIL = ["geolocation", "desktop-notification"];
|
|
|
|
|
2012-05-31 17:04:52 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://webapprt/modules/WebappRT.jsm");
|
|
|
|
|
|
|
|
function ContentPermission() {}
|
|
|
|
|
|
|
|
ContentPermission.prototype = {
|
|
|
|
classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
|
|
|
|
|
2013-08-20 12:18:13 +00:00
|
|
|
_getChromeWindow: function(aWindow) {
|
|
|
|
return aWindow
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow)
|
|
|
|
.QueryInterface(Ci.nsIDOMChromeWindow);
|
|
|
|
},
|
|
|
|
|
2012-05-31 17:04:52 +00:00
|
|
|
prompt: function(request) {
|
2014-02-09 20:34:40 +00:00
|
|
|
// Only allow exactly one permission rquest here.
|
|
|
|
let types = request.types.QueryInterface(Ci.nsIArray);
|
|
|
|
if (types.length != 1) {
|
|
|
|
request.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
|
|
|
|
|
2013-08-20 12:18:13 +00:00
|
|
|
// Reuse any remembered permission preferences
|
|
|
|
let result =
|
|
|
|
Services.perms.testExactPermissionFromPrincipal(request.principal,
|
2014-02-09 20:34:40 +00:00
|
|
|
perm.type);
|
2013-08-20 12:18:13 +00:00
|
|
|
|
|
|
|
// We used to use the name "geo" for the geolocation permission, now we're
|
|
|
|
// using "geolocation". We need to check both to support existing
|
|
|
|
// installations.
|
|
|
|
if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
|
|
|
|
result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
|
2014-02-09 20:34:40 +00:00
|
|
|
perm.type == "geolocation") {
|
2013-08-20 12:18:13 +00:00
|
|
|
let geoResult = Services.perms.testExactPermission(request.principal.URI,
|
|
|
|
"geo");
|
|
|
|
// We override the result only if the "geo" permission was allowed or
|
|
|
|
// denied.
|
|
|
|
if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
|
|
|
|
geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
result = geoResult;
|
|
|
|
}
|
2012-05-31 17:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
request.allow();
|
|
|
|
return;
|
2013-08-20 12:18:13 +00:00
|
|
|
} else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
|
|
|
|
(result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
|
2014-02-09 20:34:40 +00:00
|
|
|
UNKNOWN_FAIL.indexOf(perm.type) >= 0)) {
|
2012-05-31 17:04:52 +00:00
|
|
|
request.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display a prompt at the top level
|
2013-10-09 13:16:18 +00:00
|
|
|
let {name} = WebappRT.localeManifest;
|
2012-05-31 17:04:52 +00:00
|
|
|
let requestingWindow = request.window.top;
|
2013-08-20 12:18:13 +00:00
|
|
|
let chromeWin = this._getChromeWindow(requestingWindow);
|
2012-05-31 17:04:52 +00:00
|
|
|
let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
|
|
|
|
|
|
|
|
// Construct a prompt with share/don't and remember checkbox
|
|
|
|
let remember = {value: false};
|
|
|
|
let choice = Services.prompt.confirmEx(
|
|
|
|
chromeWin,
|
2014-02-09 20:34:40 +00:00
|
|
|
bundle.formatStringFromName(perm.type + ".title", [name], 1),
|
|
|
|
bundle.GetStringFromName(perm.type + ".description"),
|
2012-05-31 17:04:52 +00:00
|
|
|
// Set both buttons to strings with the cancel button being default
|
|
|
|
Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
|
2014-02-09 20:34:40 +00:00
|
|
|
bundle.GetStringFromName(perm.type + ".allow"),
|
|
|
|
bundle.GetStringFromName(perm.type + ".deny"),
|
2012-05-31 17:04:52 +00:00
|
|
|
null,
|
2014-02-09 20:34:40 +00:00
|
|
|
bundle.GetStringFromName(perm.type + ".remember"),
|
2012-05-31 17:04:52 +00:00
|
|
|
remember);
|
|
|
|
|
2013-08-20 12:18:13 +00:00
|
|
|
let action = Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
if (choice != 0) {
|
|
|
|
action = Ci.nsIPermissionManager.DENY_ACTION;
|
|
|
|
}
|
|
|
|
|
2012-05-31 17:04:52 +00:00
|
|
|
if (remember.value) {
|
2013-08-20 12:18:13 +00:00
|
|
|
// Persist the choice if the user wants to remember
|
2014-02-09 20:34:40 +00:00
|
|
|
Services.perms.addFromPrincipal(request.principal, perm.type, action);
|
2013-08-20 12:18:13 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise allow the permission for the current session
|
2014-02-09 20:34:40 +00:00
|
|
|
Services.perms.addFromPrincipal(request.principal, perm.type, action,
|
2013-08-20 12:18:13 +00:00
|
|
|
Ci.nsIPermissionManager.EXPIRE_SESSION);
|
2012-05-31 17:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger the selected choice
|
|
|
|
if (choice == 0) {
|
|
|
|
request.allow();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
request.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);
|