|
| 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 | + |
| 5 | +const lazy = {}; |
| 6 | + |
| 7 | +ChromeUtils.defineESModuleGetters(lazy, { |
| 8 | + ContextualIdentityService: |
| 9 | + "resource://gre/modules/ContextualIdentityService.sys.mjs", |
| 10 | + |
| 11 | + ContextualIdentityListener: |
| 12 | + "chrome://remote/content/shared/listeners/ContextualIdentityListener.sys.mjs", |
| 13 | + generateUUID: "chrome://remote/content/shared/UUID.sys.mjs", |
| 14 | +}); |
| 15 | + |
| 16 | +/** |
| 17 | + * A UserContextManager instance keeps track of all public user contexts and |
| 18 | + * maps their internal platform. |
| 19 | + * |
| 20 | + * This class is exported for test purposes. Otherwise the UserContextManager |
| 21 | + * singleton should be used. |
| 22 | + */ |
| 23 | +export class UserContextManagerClass { |
| 24 | + #contextualIdentityListener; |
| 25 | + #userContextIds; |
| 26 | + |
| 27 | + DEFAULT_CONTEXT_ID = "default"; |
| 28 | + DEFAULT_INTERNAL_ID = 0; |
| 29 | + |
| 30 | + constructor() { |
| 31 | + // Map from internal ids (numbers) from the ContextualIdentityService to |
| 32 | + // opaque UUIDs (string). |
| 33 | + this.#userContextIds = new Map(); |
| 34 | + |
| 35 | + // The default user context is always using 0 as internal user context id |
| 36 | + // and should be exposed as "default" instead of a randomly generated id. |
| 37 | + this.#userContextIds.set(this.DEFAULT_INTERNAL_ID, this.DEFAULT_CONTEXT_ID); |
| 38 | + |
| 39 | + // Register other (non-default) public contexts. |
| 40 | + lazy.ContextualIdentityService.getPublicIdentities().forEach(identity => |
| 41 | + this.#registerIdentity(identity) |
| 42 | + ); |
| 43 | + |
| 44 | + this.#contextualIdentityListener = new lazy.ContextualIdentityListener(); |
| 45 | + this.#contextualIdentityListener.on("created", this.#onIdentityCreated); |
| 46 | + this.#contextualIdentityListener.on("deleted", this.#onIdentityDeleted); |
| 47 | + this.#contextualIdentityListener.startListening(); |
| 48 | + } |
| 49 | + |
| 50 | + destroy() { |
| 51 | + this.#contextualIdentityListener.off("created", this.#onIdentityCreated); |
| 52 | + this.#contextualIdentityListener.off("deleted", this.#onIdentityDeleted); |
| 53 | + this.#contextualIdentityListener.destroy(); |
| 54 | + |
| 55 | + this.#userContextIds = null; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new user context. |
| 60 | + * |
| 61 | + * @param {string} prefix |
| 62 | + * The prefix to use for the name of the user context. |
| 63 | + * |
| 64 | + * @returns {string} |
| 65 | + * The user context id of the new user context. |
| 66 | + */ |
| 67 | + createContext(prefix = "remote") { |
| 68 | + // Prepare the opaque id and name beforehand. |
| 69 | + const userContextId = lazy.generateUUID(); |
| 70 | + const name = `${prefix}-${userContextId}`; |
| 71 | + |
| 72 | + // Create the user context. |
| 73 | + const identity = lazy.ContextualIdentityService.create(name); |
| 74 | + const internalId = identity.userContextId; |
| 75 | + |
| 76 | + // An id has been set already by the contextual-identity-created observer. |
| 77 | + // Override it with `userContextId` to match the container name. |
| 78 | + this.#userContextIds.set(internalId, userContextId); |
| 79 | + |
| 80 | + return userContextId; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Retrieve the user context id corresponding to the provided internal id. |
| 85 | + * |
| 86 | + * @param {number} internalId |
| 87 | + * The internal user context id. |
| 88 | + * |
| 89 | + * @returns {string|null} |
| 90 | + * The corresponding user context id or null if the user context does not |
| 91 | + * exist. |
| 92 | + */ |
| 93 | + getIdByInternalId(internalId) { |
| 94 | + if (this.#userContextIds.has(internalId)) { |
| 95 | + return this.#userContextIds.get(internalId); |
| 96 | + } |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Retrieve the internal id corresponding to the provided user |
| 102 | + * context id. |
| 103 | + * |
| 104 | + * @param {string} userContextId |
| 105 | + * The user context id. |
| 106 | + * |
| 107 | + * @returns {number|null} |
| 108 | + * The internal user context id or null if the user context does not |
| 109 | + * exist. |
| 110 | + */ |
| 111 | + getInternalIdById(userContextId) { |
| 112 | + for (const [internalId, id] of this.#userContextIds) { |
| 113 | + if (userContextId == id) { |
| 114 | + return internalId; |
| 115 | + } |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns an array of all known user context ids. |
| 122 | + * |
| 123 | + * @returns {Array<string>} |
| 124 | + * The array of user context ids. |
| 125 | + */ |
| 126 | + getUserContextIds() { |
| 127 | + return Array.from(this.#userContextIds.values()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Checks if the provided user context id is known by this UserContextManager. |
| 132 | + * |
| 133 | + * @param {string} userContextId |
| 134 | + * The id of the user context to check. |
| 135 | + */ |
| 136 | + hasUserContextId(userContextId) { |
| 137 | + return this.getUserContextIds().includes(userContextId); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Removes a user context and closes all related container tabs. |
| 142 | + * |
| 143 | + * @param {string} userContextId |
| 144 | + * The id of the user context to remove. |
| 145 | + * @param {object=} options |
| 146 | + * @param {boolean=} options.closeContextTabs |
| 147 | + * Pass true if the tabs owned by the user context should also be closed. |
| 148 | + * Defaults to false. |
| 149 | + */ |
| 150 | + removeUserContext(userContextId, options = {}) { |
| 151 | + const { closeContextTabs = false } = options; |
| 152 | + |
| 153 | + if (!this.hasUserContextId(userContextId)) { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + const internalId = this.getInternalIdById(userContextId); |
| 158 | + if (closeContextTabs) { |
| 159 | + lazy.ContextualIdentityService.closeContainerTabs(internalId); |
| 160 | + } |
| 161 | + lazy.ContextualIdentityService.remove(internalId); |
| 162 | + } |
| 163 | + |
| 164 | + #onIdentityCreated = (eventName, data) => { |
| 165 | + this.#registerIdentity(data.identity); |
| 166 | + }; |
| 167 | + |
| 168 | + #onIdentityDeleted = (eventName, data) => { |
| 169 | + this.#userContextIds.delete(data.identity.userContextId); |
| 170 | + }; |
| 171 | + |
| 172 | + #registerIdentity(identity) { |
| 173 | + // Note: the id for identities created via UserContextManagerClass.createContext |
| 174 | + // are overridden in createContext. |
| 175 | + this.#userContextIds.set(identity.userContextId, lazy.generateUUID()); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// Expose a shared singleton. |
| 180 | +export const UserContextManager = new UserContextManagerClass(); |
0 commit comments