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/. */
|
|
|
|
|
|
|
|
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-03-16 22:23:28 +00:00
|
|
|
|
|
|
|
function ContentPermissionPrompt() {}
|
|
|
|
|
|
|
|
ContentPermissionPrompt.prototype = {
|
|
|
|
|
|
|
|
handleExistingPermission: function handleExistingPermission(request) {
|
2012-07-30 14:58:26 +00:00
|
|
|
let result = Services.perms.testExactPermissionFromPrincipal(request.principal, request.type);
|
2012-03-16 22:23:28 +00:00
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
request.allow();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (result == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Services.perms.addFromPrincipal(request.principal, request.type,
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
}
|
|
|
|
|
2012-04-11 17:05:35 +00:00
|
|
|
request.allow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-23 02:22:27 +00:00
|
|
|
if (evt.detail.remember) {
|
|
|
|
Services.perms.addFromPrincipal(request.principal, request.type,
|
|
|
|
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,
|
|
|
|
isApp: isApp
|
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);
|
|
|
|
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
|
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);
|