summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/PermissionTestUtils.sys.mjs
blob: 84601376baec3e37980eaa6395f5d732092c9d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Utility module for tests to access the PermissionManager
 * with uri or origin string parameters.
 */

let pm = Services.perms;

let secMan = Services.scriptSecurityManager;

/**
 * Convert origin string or uri to principal.
 * If passed an nsIPrincipal it will be returned without conversion.
 *
 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject - Subject to convert to principal
 * @returns {Ci.nsIPrincipal} Principal created from subject
 */
function convertToPrincipal(subject) {
  if (subject instanceof Ci.nsIPrincipal) {
    return subject;
  }
  if (typeof subject === "string") {
    return secMan.createContentPrincipalFromOrigin(subject);
  }
  if (subject === null || subject instanceof Ci.nsIURI) {
    return secMan.createContentPrincipal(subject, {});
  }
  throw new Error(
    "subject parameter must be an nsIURI an origin string or a principal."
  );
}

export let PermissionTestUtils = {
  /**
   * Add permission information for a given subject.
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  add(subject, ...args) {
    return pm.addFromPrincipal(convertToPrincipal(subject), ...args);
  },
  /**
   * Get all custom permissions for a given subject.
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  getAll(subject, ...args) {
    return pm.getAllForPrincipal(convertToPrincipal(subject), ...args);
  },
  /**
   * Remove permission information for a given subject and permission type
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  remove(subject, ...args) {
    return pm.removeFromPrincipal(convertToPrincipal(subject), ...args);
  },
  /**
   * Test whether a website has permission to perform the given action.
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  testPermission(subject, ...args) {
    return pm.testPermissionFromPrincipal(convertToPrincipal(subject), ...args);
  },
  /**
   * Test whether a website has permission to perform the given action.
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  testExactPermission(subject, ...args) {
    return pm.testExactPermissionFromPrincipal(
      convertToPrincipal(subject),
      ...args
    );
  },
  /**
   * Get the permission object associated with the given subject and action.
   * Subject can be a principal, uri or origin string.
   *
   * @see nsIPermissionManager for documentation
   *
   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
   * @param {*} args
   */
  getPermissionObject(subject, type, exactHost = false) {
    return pm.getPermissionObject(convertToPrincipal(subject), type, exactHost);
  },
};