2013-02-13 00:51:25 +04: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;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
const kCountBeforeWeRemember = 5;
|
|
|
|
|
2013-03-07 06:33:20 +04:00
|
|
|
const kEntities = {
|
|
|
|
"geolocation": "geolocation2",
|
|
|
|
"indexedDB": "offlineApps",
|
2013-03-09 01:51:24 +04:00
|
|
|
"indexedDBQuota": "indexedDBQuota"
|
2013-03-07 06:33:20 +04:00
|
|
|
};
|
2013-02-13 00:51:25 +04:00
|
|
|
|
2013-03-07 06:31:28 +04:00
|
|
|
const kIcons = {
|
|
|
|
geolocation: "chrome://browser/skin/images/infobar-geolocation.png"
|
|
|
|
};
|
|
|
|
|
2013-02-13 00:51:25 +04:00
|
|
|
function ContentPermissionPrompt() {}
|
|
|
|
|
|
|
|
ContentPermissionPrompt.prototype = {
|
|
|
|
classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2013-03-12 03:48:54 +04:00
|
|
|
getChromeWindowForRequest: function getChromeWindowForRequest(aRequest) {
|
|
|
|
if (aRequest.window)
|
|
|
|
return this.getChromeWindow(aRequest.window.top).wrappedJSObject :
|
|
|
|
return aRequest.element.ownerDocument.defaultView;
|
|
|
|
},
|
|
|
|
|
2013-02-13 00:51:25 +04:00
|
|
|
getNotificationBoxForRequest: function getNotificationBoxForRequest(request) {
|
2013-03-12 03:48:54 +04:00
|
|
|
let chromeWin = this.getChromeWindowForRequest(request);
|
2013-02-13 00:51:25 +04:00
|
|
|
if (request.window) {
|
|
|
|
let requestingWindow = request.window.top;
|
2013-02-16 01:47:19 +04:00
|
|
|
let windowID = request.window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
2013-02-13 00:51:25 +04:00
|
|
|
let browser = chromeWin.Browser.getBrowserForWindowId(windowID);
|
|
|
|
return chromeWin.getNotificationBox(browser);
|
|
|
|
}
|
|
|
|
return chromeWin.Browser.getNotificationBox(request.element);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleExistingPermission: function handleExistingPermission(request) {
|
|
|
|
let result = Services.perms.testExactPermissionFromPrincipal(request.principal, request.type);
|
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
request.allow();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (result == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
request.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
prompt: function(request) {
|
|
|
|
// returns true if the request was handled
|
|
|
|
if (this.handleExistingPermission(request))
|
|
|
|
return;
|
|
|
|
|
|
|
|
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 entityName = kEntities[request.type];
|
2013-03-07 06:31:28 +04:00
|
|
|
let icon = kIcons[request.type] || "";
|
2013-02-13 00:51:25 +04:00
|
|
|
|
|
|
|
let buttons = [{
|
|
|
|
label: browserBundle.GetStringFromName(entityName + ".allow"),
|
2013-03-07 06:33:20 +04:00
|
|
|
accessKey: "",
|
|
|
|
callback: function(notification) {
|
|
|
|
request.allow();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: browserBundle.GetStringFromName("contentPermissions.alwaysForSite"),
|
|
|
|
accessKey: "",
|
2013-02-13 00:51:25 +04:00
|
|
|
callback: function(notification) {
|
2013-03-07 06:33:20 +04:00
|
|
|
Services.perms.addFromPrincipal(request.principal, request.type, Ci.nsIPermissionManager.ALLOW_ACTION);
|
2013-02-13 00:51:25 +04:00
|
|
|
request.allow();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2013-03-07 06:33:20 +04:00
|
|
|
label: browserBundle.GetStringFromName("contentPermissions.neverForSite"),
|
|
|
|
accessKey: "",
|
2013-02-13 00:51:25 +04:00
|
|
|
callback: function(notification) {
|
2013-03-07 06:33:20 +04:00
|
|
|
Services.perms.addFromPrincipal(request.principal, request.type, Ci.nsIPermissionManager.DENY_ACTION);
|
2013-02-13 00:51:25 +04:00
|
|
|
request.cancel();
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
let message = browserBundle.formatStringFromName(entityName + ".wantsTo",
|
|
|
|
[request.principal.URI.host], 1);
|
|
|
|
let newBar = notificationBox.appendNotification(message,
|
|
|
|
request.type,
|
2013-03-07 06:31:28 +04:00
|
|
|
icon,
|
2013-02-13 00:51:25 +04:00
|
|
|
notificationBox.PRIORITY_WARNING_MEDIUM,
|
|
|
|
buttons);
|
2013-03-12 03:48:54 +04:00
|
|
|
|
|
|
|
if (request.type == "geolocation") {
|
|
|
|
// Add the "learn more" link.
|
|
|
|
let link = newBar.ownerDocument.createElement("label");
|
|
|
|
link.setAttribute("value", browserBundle.GetStringFromName("geolocation.learnMore"));
|
|
|
|
link.setAttribute("class", "text-link notification-link");
|
|
|
|
newBar.insertBefore(link, newBar.firstChild);
|
|
|
|
|
|
|
|
let win = this.getChromeWindowForRequest(request);
|
|
|
|
link.addEventListener("click", function() {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("browser.geolocation.warning.infoURL");
|
|
|
|
win.BrowserUI.newTab(url, win.Browser.selectedTab);
|
|
|
|
});
|
|
|
|
}
|
2013-02-13 00:51:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//module initialization
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);
|