Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const Ci = Components.interfaces; |
michael@0 | 6 | const Cr = Components.results; |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const kCountBeforeWeRemember = 5; |
michael@0 | 13 | |
michael@0 | 14 | const kEntities = { |
michael@0 | 15 | "geolocation": "geolocation2", |
michael@0 | 16 | "indexedDB": "offlineApps", |
michael@0 | 17 | "indexedDBQuota": "indexedDBQuota" |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | const kIcons = { |
michael@0 | 21 | geolocation: "chrome://browser/skin/images/infobar-geolocation.png" |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | function ContentPermissionPrompt() {} |
michael@0 | 25 | |
michael@0 | 26 | ContentPermissionPrompt.prototype = { |
michael@0 | 27 | classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"), |
michael@0 | 28 | |
michael@0 | 29 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]), |
michael@0 | 30 | |
michael@0 | 31 | getChromeWindow: function getChromeWindow(aWindow) { |
michael@0 | 32 | let chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 33 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 34 | .QueryInterface(Ci.nsIDocShellTreeItem) |
michael@0 | 35 | .rootTreeItem |
michael@0 | 36 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 37 | .getInterface(Ci.nsIDOMWindow) |
michael@0 | 38 | .QueryInterface(Ci.nsIDOMChromeWindow); |
michael@0 | 39 | return chromeWin; |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | getChromeWindowForRequest: function getChromeWindowForRequest(aRequest) { |
michael@0 | 43 | if (aRequest.window) |
michael@0 | 44 | return this.getChromeWindow(aRequest.window.top).wrappedJSObject; |
michael@0 | 45 | return aRequest.element.ownerDocument.defaultView; |
michael@0 | 46 | }, |
michael@0 | 47 | |
michael@0 | 48 | getNotificationBoxForRequest: function getNotificationBoxForRequest(request) { |
michael@0 | 49 | let chromeWin = this.getChromeWindowForRequest(request); |
michael@0 | 50 | if (request.window) { |
michael@0 | 51 | let requestingWindow = request.window.top; |
michael@0 | 52 | let windowID = request.window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID; |
michael@0 | 53 | let browser = chromeWin.Browser.getBrowserForWindowId(windowID); |
michael@0 | 54 | return chromeWin.getNotificationBox(browser); |
michael@0 | 55 | } |
michael@0 | 56 | return chromeWin.Browser.getNotificationBox(request.element); |
michael@0 | 57 | }, |
michael@0 | 58 | |
michael@0 | 59 | handleExistingPermission: function handleExistingPermission(request, type) { |
michael@0 | 60 | let result = Services.perms.testExactPermissionFromPrincipal(request.principal, type); |
michael@0 | 61 | if (result == Ci.nsIPermissionManager.ALLOW_ACTION) { |
michael@0 | 62 | request.allow(); |
michael@0 | 63 | return true; |
michael@0 | 64 | } |
michael@0 | 65 | if (result == Ci.nsIPermissionManager.DENY_ACTION) { |
michael@0 | 66 | request.cancel(); |
michael@0 | 67 | return true; |
michael@0 | 68 | } |
michael@0 | 69 | return false; |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | prompt: function(request) { |
michael@0 | 73 | // Only allow exactly one permission rquest here. |
michael@0 | 74 | let types = request.types.QueryInterface(Ci.nsIArray); |
michael@0 | 75 | if (types.length != 1) { |
michael@0 | 76 | request.cancel(); |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | let perm = types.queryElementAt(0, Ci.nsIContentPermissionType); |
michael@0 | 80 | |
michael@0 | 81 | // returns true if the request was handled |
michael@0 | 82 | if (this.handleExistingPermission(request, perm.type)) |
michael@0 | 83 | return; |
michael@0 | 84 | |
michael@0 | 85 | let pm = Services.perms; |
michael@0 | 86 | let notificationBox = this.getNotificationBoxForRequest(request); |
michael@0 | 87 | let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); |
michael@0 | 88 | |
michael@0 | 89 | let notification = notificationBox.getNotificationWithValue(perm.type); |
michael@0 | 90 | if (notification) |
michael@0 | 91 | return; |
michael@0 | 92 | |
michael@0 | 93 | let entityName = kEntities[perm.type]; |
michael@0 | 94 | let icon = kIcons[perm.type] || ""; |
michael@0 | 95 | |
michael@0 | 96 | let buttons = [{ |
michael@0 | 97 | label: browserBundle.GetStringFromName(entityName + ".allow"), |
michael@0 | 98 | accessKey: "", |
michael@0 | 99 | callback: function(notification) { |
michael@0 | 100 | request.allow(); |
michael@0 | 101 | } |
michael@0 | 102 | }, |
michael@0 | 103 | { |
michael@0 | 104 | label: browserBundle.GetStringFromName("contentPermissions.alwaysForSite"), |
michael@0 | 105 | accessKey: "", |
michael@0 | 106 | callback: function(notification) { |
michael@0 | 107 | Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION); |
michael@0 | 108 | request.allow(); |
michael@0 | 109 | } |
michael@0 | 110 | }, |
michael@0 | 111 | { |
michael@0 | 112 | label: browserBundle.GetStringFromName("contentPermissions.neverForSite"), |
michael@0 | 113 | accessKey: "", |
michael@0 | 114 | callback: function(notification) { |
michael@0 | 115 | Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.DENY_ACTION); |
michael@0 | 116 | request.cancel(); |
michael@0 | 117 | } |
michael@0 | 118 | }]; |
michael@0 | 119 | |
michael@0 | 120 | let message = browserBundle.formatStringFromName(entityName + ".wantsTo", |
michael@0 | 121 | [request.principal.URI.host], 1); |
michael@0 | 122 | let newBar = notificationBox.appendNotification(message, |
michael@0 | 123 | perm.type, |
michael@0 | 124 | icon, |
michael@0 | 125 | notificationBox.PRIORITY_WARNING_MEDIUM, |
michael@0 | 126 | buttons); |
michael@0 | 127 | |
michael@0 | 128 | if (perm.type == "geolocation") { |
michael@0 | 129 | // Add the "learn more" link. |
michael@0 | 130 | let link = newBar.ownerDocument.createElement("label"); |
michael@0 | 131 | link.setAttribute("value", browserBundle.GetStringFromName("geolocation.learnMore")); |
michael@0 | 132 | link.setAttribute("class", "text-link notification-link"); |
michael@0 | 133 | newBar.insertBefore(link, newBar.firstChild); |
michael@0 | 134 | |
michael@0 | 135 | let win = this.getChromeWindowForRequest(request); |
michael@0 | 136 | link.addEventListener("click", function() { |
michael@0 | 137 | let url = Services.urlFormatter.formatURLPref("browser.geolocation.warning.infoURL"); |
michael@0 | 138 | win.BrowserUI.addAndShowTab(url, win.Browser.selectedTab); |
michael@0 | 139 | }); |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | }; |
michael@0 | 143 | |
michael@0 | 144 | |
michael@0 | 145 | //module initialization |
michael@0 | 146 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]); |