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-11-30 17:32:05 +00:00
|
|
|
"use strict"
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
function debug(str) {
|
|
|
|
//dump("-*- ContentPermissionPrompt: " + s + "\n");
|
2012-10-24 20:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-03-16 22:23:28 +00:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
const PROMPT_FOR_UNKNOWN = ["geolocation", "desktop-notification"];
|
2012-11-30 17:32:05 +00:00
|
|
|
|
2012-03-16 22:23:28 +00:00
|
|
|
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");
|
2012-12-15 01:32:30 +00:00
|
|
|
Cu.import("resource://gre/modules/PermissionsTable.jsm");
|
2012-10-24 20:19:01 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2013-01-05 22:02:29 +00:00
|
|
|
function rememberPermission(aPermission, aPrincipal, aSession)
|
2012-10-24 20:19:01 +00:00
|
|
|
{
|
|
|
|
function convertPermToAllow(aPerm, aPrincipal)
|
|
|
|
{
|
|
|
|
let type =
|
|
|
|
permissionManager.testExactPermissionFromPrincipal(aPrincipal, aPerm);
|
2012-11-30 17:32:05 +00:00
|
|
|
if (type == Ci.nsIPermissionManager.PROMPT_ACTION ||
|
|
|
|
(type == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
|
|
|
|
PROMPT_FOR_UNKNOWN.indexOf(aPermission) >= 0)) {
|
2013-01-05 22:02:29 +00:00
|
|
|
if (!aSession) {
|
|
|
|
permissionManager.addFromPrincipal(aPrincipal,
|
|
|
|
aPerm,
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
} else {
|
|
|
|
permissionManager.addFromPrincipal(aPrincipal,
|
|
|
|
aPerm,
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION,
|
|
|
|
Ci.nsIPermissionManager.EXPIRE_SESSION, 0);
|
|
|
|
}
|
2012-10-24 20:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2012-11-30 17:32:05 +00:00
|
|
|
} else {
|
2012-10-24 20:19:01 +00:00
|
|
|
convertPermToAllow(aPermission, aPrincipal);
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 22:23:28 +00:00
|
|
|
|
|
|
|
function ContentPermissionPrompt() {}
|
|
|
|
|
|
|
|
ContentPermissionPrompt.prototype = {
|
|
|
|
|
|
|
|
handleExistingPermission: function handleExistingPermission(request) {
|
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 ||
|
2012-11-30 17:32:05 +00:00
|
|
|
result == Ci.nsIPermissionManager.UNKNOWN_ACTION && PROMPT_FOR_UNKNOWN.indexOf(access) < 0) {
|
2012-03-16 22:23:28 +00:00
|
|
|
request.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2013-01-10 13:56:45 +00:00
|
|
|
handledByApp: function handledByApp(request) {
|
|
|
|
if (request.principal.appId == Ci.nsIScriptSecurityManager.NO_APP_ID ||
|
|
|
|
request.principal.appId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) {
|
|
|
|
// This should not really happen
|
|
|
|
request.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let appsService = Cc["@mozilla.org/AppsService;1"]
|
|
|
|
.getService(Ci.nsIAppsService);
|
|
|
|
let app = appsService.getAppByLocalId(request.principal.appId);
|
|
|
|
|
|
|
|
let url = Services.io.newURI(app.origin, null, null);
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(url, request.principal.appId,
|
|
|
|
/*mozbrowser*/false);
|
|
|
|
let access = (request.access && request.access !== "unused") ? request.type + "-" + request.access :
|
|
|
|
request.type;
|
|
|
|
let result = Services.perms.testExactPermissionFromPrincipal(principal, access);
|
|
|
|
|
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION ||
|
|
|
|
result == Ci.nsIPermissionManager.PROMPT_ACTION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.cancel();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
_id: 0,
|
2012-03-16 22:23:28 +00:00
|
|
|
prompt: function(request) {
|
2013-01-17 18:26:40 +00:00
|
|
|
if (secMan.isSystemPrincipal(request.principal)) {
|
|
|
|
request.allow();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 13:56:45 +00:00
|
|
|
if (this.handledByApp(request))
|
|
|
|
return;
|
|
|
|
|
2012-03-16 22:23:28 +00:00
|
|
|
// returns true if the request was handled
|
|
|
|
if (this.handleExistingPermission(request))
|
|
|
|
return;
|
|
|
|
|
2013-01-07 16:45:29 +00:00
|
|
|
let frame = request.element;
|
2013-05-23 15:52:04 +00:00
|
|
|
let requestId = this._id++;
|
2013-01-07 16:45:29 +00:00
|
|
|
|
|
|
|
if (!frame) {
|
2013-05-23 15:52:04 +00:00
|
|
|
this.delegatePrompt(request, requestId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = frame.wrappedJSObject;
|
|
|
|
var cancelRequest = function() {
|
|
|
|
frame.removeEventListener("mozbrowservisibilitychange", onVisibilityChange);
|
|
|
|
request.cancel();
|
2013-01-07 16:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
2013-05-23 15:52:04 +00:00
|
|
|
var onVisibilityChange = function(evt) {
|
|
|
|
if (evt.detail.visible === true)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self.cancelPrompt(request, requestId);
|
|
|
|
cancelRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the request was initiated from a hidden iframe
|
|
|
|
// we don't forward it to content and cancel it right away
|
|
|
|
let domRequest = frame.getVisible();
|
|
|
|
domRequest.onsuccess = function gv_success(evt) {
|
2013-01-07 16:45:29 +00:00
|
|
|
if (!evt.target.result) {
|
2013-05-23 15:52:04 +00:00
|
|
|
cancelRequest();
|
2013-01-07 16:45:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
// Monitor the frame visibility and cancel the request if the frame goes
|
|
|
|
// away but the request is still here.
|
|
|
|
frame.addEventListener("mozbrowservisibilitychange", onVisibilityChange);
|
|
|
|
|
|
|
|
self.delegatePrompt(request, requestId, function onCallback() {
|
|
|
|
frame.removeEventListener("mozbrowservisibilitychange", onVisibilityChange);
|
|
|
|
});
|
2013-01-07 16:45:29 +00:00
|
|
|
};
|
2013-05-23 15:52:04 +00:00
|
|
|
|
|
|
|
// Something went wrong. Let's cancel the request just in case.
|
|
|
|
domRequest.onerror = function gv_error() {
|
|
|
|
cancelRequest();
|
|
|
|
}
|
2013-01-07 16:45:29 +00:00
|
|
|
},
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
cancelPrompt: function(request, requestId) {
|
|
|
|
this.sendToBrowserWindow("cancel-permission-prompt", request, requestId);
|
|
|
|
},
|
2012-04-11 17:05:35 +00:00
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
delegatePrompt: function(request, requestId, callback) {
|
2012-11-19 20:31:25 +00:00
|
|
|
let access = (request.access && request.access !== "unused") ? request.type + "-" + request.access :
|
|
|
|
request.type;
|
2013-05-23 15:52:04 +00:00
|
|
|
let principal = request.principal;
|
2012-11-19 20:31:25 +00:00
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
this._permission = access;
|
|
|
|
this._uri = principal.URI.spec;
|
|
|
|
this._origin = principal.origin;
|
2012-04-11 17:05:35 +00:00
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
this.sendToBrowserWindow("permission-prompt", request, requestId, function(type, remember) {
|
|
|
|
if (type == "permission-allow") {
|
|
|
|
rememberPermission(request.type, principal, !remember);
|
2013-08-09 17:02:33 +00:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2012-04-11 17:05:35 +00:00
|
|
|
request.allow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-23 15:52:04 +00:00
|
|
|
if (remember) {
|
|
|
|
Services.perms.addFromPrincipal(principal, access,
|
2012-10-23 02:22:27 +00:00
|
|
|
Ci.nsIPermissionManager.DENY_ACTION);
|
2013-01-05 22:02:29 +00:00
|
|
|
} else {
|
2013-05-23 15:52:04 +00:00
|
|
|
Services.perms.addFromPrincipal(principal, access,
|
2013-01-05 22:02:29 +00:00
|
|
|
Ci.nsIPermissionManager.DENY_ACTION,
|
|
|
|
Ci.nsIPermissionManager.EXPIRE_SESSION, 0);
|
2012-10-23 02:22:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 17:02:33 +00:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2012-04-11 17:05:35 +00:00
|
|
|
request.cancel();
|
|
|
|
});
|
2013-05-23 15:52:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sendToBrowserWindow: function(type, request, requestId, callback) {
|
|
|
|
let browser = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
let content = browser.getContentWindow();
|
|
|
|
if (!content)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
content.addEventListener("mozContentEvent", function contentEvent(evt) {
|
|
|
|
let detail = evt.detail;
|
|
|
|
if (detail.id != requestId)
|
|
|
|
return;
|
|
|
|
evt.target.removeEventListener(evt.type, contentEvent);
|
|
|
|
|
|
|
|
callback(detail.type, detail.remember);
|
|
|
|
})
|
|
|
|
}
|
2012-04-11 17:05:35 +00:00
|
|
|
|
2012-09-12 18:00:55 +00:00
|
|
|
let principal = request.principal;
|
|
|
|
let isApp = principal.appStatus != Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED;
|
2013-01-09 12:43:15 +00:00
|
|
|
let remember = (principal.appStatus == Ci.nsIPrincipal.APP_STATUS_PRIVILEGED ||
|
|
|
|
principal.appStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED)
|
|
|
|
? true
|
|
|
|
: request.remember;
|
2012-09-12 18:00:55 +00:00
|
|
|
|
2012-07-13 02:28:19 +00:00
|
|
|
let details = {
|
2013-05-23 15:52:04 +00:00
|
|
|
type: type,
|
2012-09-12 18:00:55 +00:00
|
|
|
permission: request.type,
|
|
|
|
id: requestId,
|
|
|
|
origin: principal.origin,
|
2012-10-24 20:19:01 +00:00
|
|
|
isApp: isApp,
|
2012-12-21 23:04:31 +00:00
|
|
|
remember: remember
|
2012-07-13 02:28:19 +00:00
|
|
|
};
|
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);
|
2013-08-28 12:10:55 +00:00
|
|
|
DOMApplicationRegistry.getManifestFor(app.manifestURL, 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]);
|