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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs",
});
const OBSERVER_TOPIC_CREATED = "contextual-identity-created";
const OBSERVER_TOPIC_DELETED = "contextual-identity-deleted";
/**
* The ContextualIdentityListener can be used to listen for notifications about
* contextual identities (containers) being created or deleted.
*
* Example:
* ```
* const listener = new ContextualIdentityListener();
* listener.on("created", onCreated);
* listener.startListening();
*
* const onCreated = (eventName, data = {}) => {
* const { identity } = data;
* ...
* };
* ```
*
* @fires message
* The ContextualIdentityListener emits "created" and "deleted" events,
* with the following object as payload:
* - {object} identity
* The contextual identity which was created or deleted.
*/
export class ContextualIdentityListener {
#listening;
/**
* Create a new BrowsingContextListener instance.
*/
constructor() {
lazy.EventEmitter.decorate(this);
this.#listening = false;
}
destroy() {
this.stopListening();
}
observe(subject, topic) {
switch (topic) {
case OBSERVER_TOPIC_CREATED:
this.emit("created", { identity: subject.wrappedJSObject });
break;
case OBSERVER_TOPIC_DELETED:
this.emit("deleted", { identity: subject.wrappedJSObject });
break;
}
}
startListening() {
if (this.#listening) {
return;
}
Services.obs.addObserver(this, OBSERVER_TOPIC_CREATED);
Services.obs.addObserver(this, OBSERVER_TOPIC_DELETED);
this.#listening = true;
}
stopListening() {
if (!this.#listening) {
return;
}
Services.obs.removeObserver(this, OBSERVER_TOPIC_CREATED);
Services.obs.removeObserver(this, OBSERVER_TOPIC_DELETED);
this.#listening = false;
}
}
|