mobile/android/components/ContentPermissionPrompt.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 const Cc = Components.classes;
michael@0 9
michael@0 10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 const kEntities = { "geolocation": "geolocation",
michael@0 14 "desktop-notification": "desktopNotification",
michael@0 15 "contacts": "contacts" };
michael@0 16
michael@0 17 function ContentPermissionPrompt() {}
michael@0 18
michael@0 19 ContentPermissionPrompt.prototype = {
michael@0 20 classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
michael@0 21
michael@0 22 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
michael@0 23
michael@0 24 handleExistingPermission: function handleExistingPermission(request, type, isApp) {
michael@0 25 let result = Services.perms.testExactPermissionFromPrincipal(request.principal, type);
michael@0 26 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
michael@0 27 request.allow();
michael@0 28 return true;
michael@0 29 }
michael@0 30 if (result == Ci.nsIPermissionManager.DENY_ACTION) {
michael@0 31 request.cancel();
michael@0 32 return true;
michael@0 33 }
michael@0 34
michael@0 35 if (isApp && (result == Ci.nsIPermissionManager.UNKNOWN_ACTION && !!kEntities[type])) {
michael@0 36 request.cancel();
michael@0 37 return true;
michael@0 38 }
michael@0 39
michael@0 40 return false;
michael@0 41 },
michael@0 42
michael@0 43 getChromeWindow: function getChromeWindow(aWindow) {
michael@0 44 let chromeWin = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 45 .getInterface(Ci.nsIWebNavigation)
michael@0 46 .QueryInterface(Ci.nsIDocShellTreeItem)
michael@0 47 .rootTreeItem
michael@0 48 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 49 .getInterface(Ci.nsIDOMWindow)
michael@0 50 .QueryInterface(Ci.nsIDOMChromeWindow);
michael@0 51 return chromeWin;
michael@0 52 },
michael@0 53
michael@0 54 getChromeForRequest: function getChromeForRequest(request) {
michael@0 55 if (request.window) {
michael@0 56 let requestingWindow = request.window.top;
michael@0 57 return this.getChromeWindow(requestingWindow).wrappedJSObject;
michael@0 58 }
michael@0 59 return request.element.ownerDocument.defaultView;
michael@0 60 },
michael@0 61
michael@0 62 prompt: function(request) {
michael@0 63 let isApp = request.principal.appId !== Ci.nsIScriptSecurityManager.NO_APP_ID && request.principal.appId !== Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID;
michael@0 64
michael@0 65 // Only allow exactly one permission rquest here.
michael@0 66 let types = request.types.QueryInterface(Ci.nsIArray);
michael@0 67 if (types.length != 1) {
michael@0 68 request.cancel();
michael@0 69 return;
michael@0 70 }
michael@0 71 let perm = types.queryElementAt(0, Ci.nsIContentPermissionType);
michael@0 72
michael@0 73 // Returns true if the request was handled
michael@0 74 if (this.handleExistingPermission(request, perm.type, isApp))
michael@0 75 return;
michael@0 76
michael@0 77 let chromeWin = this.getChromeForRequest(request);
michael@0 78 let tab = chromeWin.BrowserApp.getTabForWindow(request.window.top);
michael@0 79 if (!tab)
michael@0 80 return;
michael@0 81
michael@0 82 let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
michael@0 83 let entityName = kEntities[perm.type];
michael@0 84
michael@0 85 let buttons = [{
michael@0 86 label: browserBundle.GetStringFromName(entityName + ".allow"),
michael@0 87 callback: function(aChecked) {
michael@0 88 // If the user checked "Don't ask again", make a permanent exception
michael@0 89 if (aChecked) {
michael@0 90 Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 91 } else if (isApp || entityName == "desktopNotification") {
michael@0 92 // 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 93 Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.ALLOW_ACTION, Ci.nsIPermissionManager.EXPIRE_SESSION);
michael@0 94 }
michael@0 95
michael@0 96 request.allow();
michael@0 97 }
michael@0 98 },
michael@0 99 {
michael@0 100 label: browserBundle.GetStringFromName(entityName + ".dontAllow"),
michael@0 101 callback: function(aChecked) {
michael@0 102 // If the user checked "Don't ask again", make a permanent exception
michael@0 103 if (aChecked)
michael@0 104 Services.perms.addFromPrincipal(request.principal, perm.type, Ci.nsIPermissionManager.DENY_ACTION);
michael@0 105
michael@0 106 request.cancel();
michael@0 107 }
michael@0 108 }];
michael@0 109
michael@0 110 let requestor = chromeWin.BrowserApp.manifest ? "'" + chromeWin.BrowserApp.manifest.name + "'" : request.principal.URI.host;
michael@0 111 let message = browserBundle.formatStringFromName(entityName + ".ask", [requestor], 1);
michael@0 112 let options = { checkbox: browserBundle.GetStringFromName(entityName + ".dontAskAgain") };
michael@0 113
michael@0 114 chromeWin.NativeWindow.doorhanger.show(message, entityName + request.principal.URI.host, buttons, tab.id, options);
michael@0 115 }
michael@0 116 };
michael@0 117
michael@0 118
michael@0 119 //module initialization
michael@0 120 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);

mercurial