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: this.EXPORTED_SYMBOLS = ["MockPermissionPrompt"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cm = Components.manager; michael@0: const Cu = Components.utils; michael@0: michael@0: const CONTRACT_ID = "@mozilla.org/content-permission/prompt;1"; michael@0: michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: var oldClassID, oldFactory; michael@0: var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID(); michael@0: var newFactory = { michael@0: createInstance: function(aOuter, aIID) { michael@0: if (aOuter) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return new MockPermissionPromptInstance().QueryInterface(aIID); michael@0: }, michael@0: lockFactory: function(aLock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) michael@0: }; michael@0: michael@0: this.MockPermissionPrompt = { michael@0: init: function() { michael@0: this.reset(); michael@0: if (!registrar.isCIDRegistered(newClassID)) { michael@0: try { michael@0: oldClassID = registrar.contractIDToCID(CONTRACT_ID); michael@0: oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: } catch (ex) { michael@0: oldClassID = ""; michael@0: oldFactory = null; michael@0: dump("TEST-INFO | can't get permission prompt registered component, " + michael@0: "assuming there is none"); michael@0: } michael@0: if (oldFactory) { michael@0: registrar.unregisterFactory(oldClassID, oldFactory); michael@0: } michael@0: registrar.registerFactory(newClassID, "", CONTRACT_ID, newFactory); michael@0: } michael@0: }, michael@0: michael@0: reset: function() { michael@0: }, michael@0: michael@0: cleanup: function() { michael@0: this.reset(); michael@0: if (oldFactory) { michael@0: registrar.unregisterFactory(newClassID, newFactory); michael@0: registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory); michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: function MockPermissionPromptInstance() { }; michael@0: MockPermissionPromptInstance.prototype = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]), michael@0: michael@0: promptResult: Ci.nsIPermissionManager.UNKNOWN_ACTION, michael@0: michael@0: prompt: function(request) { michael@0: michael@0: let perms = request.types.QueryInterface(Ci.nsIArray); michael@0: for (let idx = 0; idx < perms.length; idx++) { michael@0: let perm = perms.queryElementAt(idx, Ci.nsIContentPermissionType); michael@0: if (Services.perms.testExactPermissionFromPrincipal( michael@0: request.principal, perm.type) != Ci.nsIPermissionManager.ALLOW_ACTION) { michael@0: request.cancel(); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: request.allow(); michael@0: } michael@0: }; michael@0: michael@0: // Expose everything to content. We call reset() here so that all of the relevant michael@0: // lazy expandos get added. michael@0: MockPermissionPrompt.reset(); michael@0: function exposeAll(obj) { michael@0: var props = {}; michael@0: for (var prop in obj) michael@0: props[prop] = 'rw'; michael@0: obj.__exposedProps__ = props; michael@0: } michael@0: exposeAll(MockPermissionPrompt); michael@0: exposeAll(MockPermissionPromptInstance.prototype);