2012-03-16 22:23:28 +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/. */
|
|
|
|
|
2012-10-24 20:19:01 +00:00
|
|
|
let DEBUG = 0;
|
|
|
|
let debug;
|
|
|
|
if (DEBUG) {
|
|
|
|
debug = function (s) { dump("-*- ContentPermissionPrompt: " + s + "\n"); };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
debug = function (s) {};
|
|
|
|
}
|
|
|
|
|
2012-03-16 22:23:28 +00:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-09-12 18:00:55 +00:00
|
|
|
Cu.import("resource://gre/modules/Webapps.jsm");
|
2012-10-03 05:38:03 +00:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2012-10-24 20:19:01 +00:00
|
|
|
Cu.import("resource://gre/modules/PermissionsInstaller.jsm");
|
|
|
|
|
|
|
|
var permissionManager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
|
|
|
|
var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this,
|
|
|
|
"PermSettings",
|
|
|
|
"@mozilla.org/permissionSettings;1",
|
|
|
|
"nsIDOMPermissionSettings");
|
|
|
|
|
|
|
|
function rememberPermission(aPermission, aPrincipal)
|
|
|
|
{
|
|
|
|
function convertPermToAllow(aPerm, aPrincipal)
|
|
|
|
{
|
|
|
|
let type =
|
|
|
|
permissionManager.testExactPermissionFromPrincipal(aPrincipal, aPerm);
|
|
|
|
if (type == Ci.nsIPermissionManager.PROMPT_ACTION) {
|
|
|
|
permissionManager.addFromPrincipal(aPrincipal,
|
|
|
|
aPerm,
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the permission to see if we have multiple access properties to convert
|
|
|
|
let access = PermissionsTable[aPermission].access;
|
|
|
|
if (access) {
|
|
|
|
for (let idx in access) {
|
|
|
|
convertPermToAllow(aPermission + "-" + access[idx], aPrincipal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
convertPermToAllow(aPermission, aPrincipal);
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 22:23:28 +00:00
|
|
|
|
|
|
|
function ContentPermissionPrompt() {}
|
|
|
|
|
|
|
|
ContentPermissionPrompt.prototype = {
|
|
|
|
|
|
|
|
handleExistingPermission: function handleExistingPermission(request) {
|
2012-11-28 01:31:45 +00:00
|
|
|
let promptForUnknown = ['geolocation'];
|
2012-11-19 20:31:25 +00:00
|
|
|
let access = (request.access && request.access !== "unused") ? request.type + "-" + request.access :
|
|
|
|
request.type;
|
|
|
|
let result = Services.perms.testExactPermissionFromPrincipal(request.principal, access);
|
2012-03-16 22:23:28 +00:00
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
request.allow();
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 01:31:45 +00:00
|
|
|
if (result == Ci.nsIPermissionManager.DENY_ACTION ||
|
|
|
|
result == Ci.nsIPermissionManager.UNKNOWN_ACTION && promptForUnknown.indexOf(access) < 0) {
|
2012-03-16 22:23:28 +00:00
|
|
|
request.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
_id: 0,
|
2012-03-16 22:23:28 +00:00
|
|
|
prompt: function(request) {
|
|
|
|
// returns true if the request was handled
|
|
|
|
if (this.handleExistingPermission(request))
|
|
|
|
return;
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
let browser = Services.wm.getMostRecentWindow("navigator:browser");
|
2012-07-13 02:28:19 +00:00
|
|
|
let content = browser.getContentWindow();
|
2012-04-11 17:05:35 +00:00
|
|
|
if (!content)
|
|
|
|
return;
|
|
|
|
|
2012-11-19 20:31:25 +00:00
|
|
|
let access = (request.access && request.access !== "unused") ? request.type + "-" + request.access :
|
|
|
|
request.type;
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
let requestId = this._id++;
|
|
|
|
content.addEventListener("mozContentEvent", function contentEvent(evt) {
|
|
|
|
if (evt.detail.id != requestId)
|
|
|
|
return;
|
2012-07-13 02:28:19 +00:00
|
|
|
evt.target.removeEventListener(evt.type, contentEvent);
|
2012-04-11 17:05:35 +00:00
|
|
|
|
|
|
|
if (evt.detail.type == "permission-allow") {
|
2012-10-23 02:22:27 +00:00
|
|
|
if (evt.detail.remember) {
|
2012-10-24 20:19:01 +00:00
|
|
|
rememberPermission(request.type, request.principal);
|
2012-10-23 02:22:27 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
request.allow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-23 02:22:27 +00:00
|
|
|
if (evt.detail.remember) {
|
2012-11-19 20:31:25 +00:00
|
|
|
Services.perms.addFromPrincipal(request.principal, access,
|
2012-10-23 02:22:27 +00:00
|
|
|
Ci.nsIPermissionManager.DENY_ACTION);
|
|
|
|
}
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
request.cancel();
|
|
|
|
});
|
|
|
|
|
2012-09-12 18:00:55 +00:00
|
|
|
let principal = request.principal;
|
|
|
|
let isApp = principal.appStatus != Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED;
|
|
|
|
|
2012-07-13 02:28:19 +00:00
|
|
|
let details = {
|
2012-09-12 18:00:55 +00:00
|
|
|
type: "permission-prompt",
|
|
|
|
permission: request.type,
|
|
|
|
id: requestId,
|
|
|
|
origin: principal.origin,
|
2012-10-24 20:19:01 +00:00
|
|
|
isApp: isApp,
|
|
|
|
remember: request.remember
|
2012-07-13 02:28:19 +00:00
|
|
|
};
|
2012-09-12 18:00:55 +00:00
|
|
|
|
2012-11-19 20:31:25 +00:00
|
|
|
this._permission = access;
|
2012-10-24 20:19:01 +00:00
|
|
|
this._uri = request.principal.URI.spec;
|
|
|
|
this._origin = request.principal.origin;
|
|
|
|
|
2012-09-12 18:00:55 +00:00
|
|
|
if (!isApp) {
|
|
|
|
browser.shell.sendChromeEvent(details);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When it's an app, get the manifest to add the l10n application name.
|
|
|
|
let app = DOMApplicationRegistry.getAppByLocalId(principal.appId);
|
|
|
|
DOMApplicationRegistry.getManifestFor(app.origin, function getManifest(aManifest) {
|
2012-10-03 05:38:03 +00:00
|
|
|
let helper = new ManifestHelper(aManifest, app.origin);
|
2012-09-12 18:00:55 +00:00
|
|
|
details.appName = helper.name;
|
|
|
|
browser.shell.sendChromeEvent(details);
|
|
|
|
});
|
2012-03-16 22:23:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{8c719f03-afe0-4aac-91ff-6c215895d467}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt])
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//module initialization
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);
|