2009-02-18 12:57:20 -08:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-07-13 07:36:09 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2009-05-29 18:28:12 -07:00
|
|
|
const kCountBeforeWeRemember = 5;
|
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
function setPagePermission(type, uri, allow) {
|
|
|
|
let pm = Services.perms;
|
|
|
|
let contentPrefs = Services.contentPrefs;
|
|
|
|
let contentPrefName = type + ".request.remember";
|
|
|
|
|
|
|
|
if (!contentPrefs.hasPref(uri, contentPrefName))
|
|
|
|
contentPrefs.setPref(uri, contentPrefName, 0);
|
|
|
|
|
|
|
|
let count = contentPrefs.getPref(uri, contentPrefName);
|
|
|
|
|
|
|
|
if (allow == false)
|
|
|
|
count--;
|
|
|
|
else
|
|
|
|
count++;
|
|
|
|
|
|
|
|
contentPrefs.setPref(uri, contentPrefName, count);
|
|
|
|
if (count == kCountBeforeWeRemember)
|
|
|
|
pm.add(uri, type, Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
else if (count == -kCountBeforeWeRemember)
|
|
|
|
pm.add(uri, type, Ci.nsIPermissionManager.DENY_ACTION);
|
|
|
|
}
|
|
|
|
|
2010-09-09 23:56:50 -07:00
|
|
|
function ContentPermissionPrompt() {}
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-09-09 23:56:50 -07:00
|
|
|
ContentPermissionPrompt.prototype = {
|
2010-07-21 16:41:25 -07:00
|
|
|
classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-09-09 23:56:50 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
|
2010-07-21 16:41:25 -07:00
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
getChromeWindow: function getChromeWindow(aWindow) {
|
|
|
|
let chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow)
|
|
|
|
.QueryInterface(Ci.nsIDOMChromeWindow);
|
|
|
|
return chromeWin;
|
|
|
|
},
|
|
|
|
|
|
|
|
getNotificationBoxForRequest: function getNotificationBoxForRequest(request) {
|
|
|
|
let notificationBox = null;
|
|
|
|
if (request.window) {
|
|
|
|
let requestingWindow = request.window.top;
|
|
|
|
let chromeWin = this.getChromeWindow(requestingWindow).wrappedJSObject;
|
|
|
|
return chromeWin.getNotificationBox(requestingWindow);
|
|
|
|
}
|
2010-09-09 23:56:50 -07:00
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
let chromeWin = request.element.ownerDocument.defaultView;
|
|
|
|
return chromeWin.Browser.getNotificationBox();
|
|
|
|
},
|
2010-07-21 16:41:25 -07:00
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
handleExistingPermission: function handleExistingPermission(request) {
|
|
|
|
let result = Services.perms.testExactPermission(request.uri, request.type);
|
2009-05-29 18:28:12 -07:00
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
2010-09-11 08:31:21 -07:00
|
|
|
request.allow();
|
|
|
|
return true;
|
2009-05-29 18:28:12 -07:00
|
|
|
}
|
2010-09-11 08:31:21 -07:00
|
|
|
if (result == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
request.cancel();
|
|
|
|
return true;
|
2009-05-29 18:28:12 -07:00
|
|
|
}
|
2010-09-11 08:31:21 -07:00
|
|
|
return false;
|
|
|
|
},
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
prompt: function(request) {
|
|
|
|
// returns true if the request was handled
|
|
|
|
if (this.handleExistingPermission(request))
|
|
|
|
return;
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-09-11 08:31:21 -07:00
|
|
|
let pm = Services.perms;
|
|
|
|
let notificationBox = this.getNotificationBoxForRequest(request);
|
|
|
|
let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
|
|
|
|
|
|
|
|
let notification = notificationBox.getNotificationWithValue(request.type);
|
|
|
|
if (notification)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let buttons = [{
|
|
|
|
label: browserBundle.GetStringFromName(request.type + ".allow"),
|
|
|
|
accessKey: null,
|
|
|
|
callback: function(notification) {
|
|
|
|
setPagePermission(request.type, request.uri, true);
|
|
|
|
request.allow();},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: browserBundle.GetStringFromName(request.type + ".dontAllow"),
|
|
|
|
accessKey: null,
|
|
|
|
callback: function(notification) {
|
|
|
|
setPagePermission(request.type, request.uri, false);
|
|
|
|
request.cancel();},
|
|
|
|
}];
|
|
|
|
|
|
|
|
let message = browserBundle.formatStringFromName(request.type + ".siteWantsTo",
|
|
|
|
[request.uri.host], 1);
|
|
|
|
let newBar = notificationBox.appendNotification(message,
|
|
|
|
request.type,
|
|
|
|
"", // Notifications in Fennec do not display images.
|
|
|
|
notificationBox.PRIORITY_WARNING_MEDIUM,
|
|
|
|
buttons);
|
|
|
|
return;
|
|
|
|
},
|
2009-02-18 12:57:20 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//module initialization
|
2010-09-09 23:56:50 -07:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);
|