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