michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const kEntities = { "geolocation": "geolocation", michael@0: "desktop-notification": "desktopNotification", michael@0: "contacts": "contacts" }; michael@0: michael@0: function ContentPermissionPrompt() {} michael@0: michael@0: ContentPermissionPrompt.prototype = { michael@0: classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]), michael@0: michael@0: handleExistingPermission: function handleExistingPermission(request, type, isApp) { michael@0: let result = Services.perms.testExactPermissionFromPrincipal(request.principal, type); michael@0: if (result == Ci.nsIPermissionManager.ALLOW_ACTION) { michael@0: request.allow(); michael@0: return true; michael@0: } michael@0: if (result == Ci.nsIPermissionManager.DENY_ACTION) { michael@0: request.cancel(); michael@0: return true; michael@0: } michael@0: michael@0: if (isApp && (result == Ci.nsIPermissionManager.UNKNOWN_ACTION && !!kEntities[type])) { michael@0: request.cancel(); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: }, michael@0: michael@0: getChromeWindow: function getChromeWindow(aWindow) { michael@0: let chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShellTreeItem) michael@0: .rootTreeItem michael@0: .QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow) michael@0: .QueryInterface(Ci.nsIDOMChromeWindow); michael@0: return chromeWin; michael@0: }, michael@0: michael@0: getChromeForRequest: function getChromeForRequest(request) { michael@0: if (request.window) { michael@0: let requestingWindow = request.window.top; michael@0: return this.getChromeWindow(requestingWindow).wrappedJSObject; michael@0: } michael@0: return request.element.ownerDocument.defaultView; michael@0: }, michael@0: michael@0: prompt: function(request) { michael@0: let isApp = request.principal.appId !== Ci.nsIScriptSecurityManager.NO_APP_ID && request.principal.appId !== Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID; michael@0: michael@0: // Only allow exactly one permission rquest here. michael@0: let types = request.types.QueryInterface(Ci.nsIArray); michael@0: if (types.length != 1) { michael@0: request.cancel(); michael@0: return; michael@0: } michael@0: let perm = types.queryElementAt(0, Ci.nsIContentPermissionType); michael@0: michael@0: // Returns true if the request was handled michael@0: if (this.handleExistingPermission(request, perm.type, isApp)) michael@0: return; michael@0: michael@0: let chromeWin = this.getChromeForRequest(request); michael@0: let tab = chromeWin.BrowserApp.getTabForWindow(request.window.top); michael@0: if (!tab) michael@0: return; michael@0: michael@0: let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); michael@0: let entityName = kEntities[perm.type]; michael@0: michael@0: let buttons = [{ michael@0: label: browserBundle.GetStringFromName(entityName + ".allow"), michael@0: callback: function(aChecked) { michael@0: // If the user checked "Don't ask again", make a permanent exception michael@0: if (aChecked) { michael@0: Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION); michael@0: } else if (isApp || entityName == "desktopNotification") { michael@0: // Otherwise allow the permission for the current session (if the request comes from an app or if it's a desktop-notification request) michael@0: Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION, Ci.nsIPermissionManager.EXPIRE_SESSION); michael@0: } michael@0: michael@0: request.allow(); michael@0: } michael@0: }, michael@0: { michael@0: label: browserBundle.GetStringFromName(entityName + ".dontAllow"), michael@0: callback: function(aChecked) { michael@0: // If the user checked "Don't ask again", make a permanent exception michael@0: if (aChecked) michael@0: Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.DENY_ACTION); michael@0: michael@0: request.cancel(); michael@0: } michael@0: }]; michael@0: michael@0: let requestor = chromeWin.BrowserApp.manifest ? "'" + chromeWin.BrowserApp.manifest.name + "'" : request.principal.URI.host; michael@0: let message = browserBundle.formatStringFromName(entityName + ".ask", [requestor], 1); michael@0: let options = { checkbox: browserBundle.GetStringFromName(entityName + ".dontAskAgain") }; michael@0: michael@0: chromeWin.NativeWindow.doorhanger.show(message, entityName + request.principal.URI.host, buttons, tab.id, options); michael@0: } michael@0: }; michael@0: michael@0: michael@0: //module initialization michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);