testing/specialpowers/content/MockPermissionPrompt.jsm

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 this.EXPORTED_SYMBOLS = ["MockPermissionPrompt"];
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cm = Components.manager;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 const CONTRACT_ID = "@mozilla.org/content-permission/prompt;1";
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/FileUtils.jsm");
michael@0 15 Cu.import("resource://gre/modules/Services.jsm");
michael@0 16 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 17
michael@0 18 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 19 var oldClassID, oldFactory;
michael@0 20 var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
michael@0 21 var newFactory = {
michael@0 22 createInstance: function(aOuter, aIID) {
michael@0 23 if (aOuter)
michael@0 24 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 25 return new MockPermissionPromptInstance().QueryInterface(aIID);
michael@0 26 },
michael@0 27 lockFactory: function(aLock) {
michael@0 28 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
michael@0 29 },
michael@0 30 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
michael@0 31 };
michael@0 32
michael@0 33 this.MockPermissionPrompt = {
michael@0 34 init: function() {
michael@0 35 this.reset();
michael@0 36 if (!registrar.isCIDRegistered(newClassID)) {
michael@0 37 try {
michael@0 38 oldClassID = registrar.contractIDToCID(CONTRACT_ID);
michael@0 39 oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
michael@0 40 } catch (ex) {
michael@0 41 oldClassID = "";
michael@0 42 oldFactory = null;
michael@0 43 dump("TEST-INFO | can't get permission prompt registered component, " +
michael@0 44 "assuming there is none");
michael@0 45 }
michael@0 46 if (oldFactory) {
michael@0 47 registrar.unregisterFactory(oldClassID, oldFactory);
michael@0 48 }
michael@0 49 registrar.registerFactory(newClassID, "", CONTRACT_ID, newFactory);
michael@0 50 }
michael@0 51 },
michael@0 52
michael@0 53 reset: function() {
michael@0 54 },
michael@0 55
michael@0 56 cleanup: function() {
michael@0 57 this.reset();
michael@0 58 if (oldFactory) {
michael@0 59 registrar.unregisterFactory(newClassID, newFactory);
michael@0 60 registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory);
michael@0 61 }
michael@0 62 },
michael@0 63 };
michael@0 64
michael@0 65 function MockPermissionPromptInstance() { };
michael@0 66 MockPermissionPromptInstance.prototype = {
michael@0 67 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
michael@0 68
michael@0 69 promptResult: Ci.nsIPermissionManager.UNKNOWN_ACTION,
michael@0 70
michael@0 71 prompt: function(request) {
michael@0 72
michael@0 73 let perms = request.types.QueryInterface(Ci.nsIArray);
michael@0 74 for (let idx = 0; idx < perms.length; idx++) {
michael@0 75 let perm = perms.queryElementAt(idx, Ci.nsIContentPermissionType);
michael@0 76 if (Services.perms.testExactPermissionFromPrincipal(
michael@0 77 request.principal, perm.type) != Ci.nsIPermissionManager.ALLOW_ACTION) {
michael@0 78 request.cancel();
michael@0 79 return;
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 request.allow();
michael@0 84 }
michael@0 85 };
michael@0 86
michael@0 87 // Expose everything to content. We call reset() here so that all of the relevant
michael@0 88 // lazy expandos get added.
michael@0 89 MockPermissionPrompt.reset();
michael@0 90 function exposeAll(obj) {
michael@0 91 var props = {};
michael@0 92 for (var prop in obj)
michael@0 93 props[prop] = 'rw';
michael@0 94 obj.__exposedProps__ = props;
michael@0 95 }
michael@0 96 exposeAll(MockPermissionPrompt);
michael@0 97 exposeAll(MockPermissionPromptInstance.prototype);

mercurial