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
112
113
|
/* 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, {
Domain: "chrome://remote/content/cdp/domains/Domain.sys.mjs",
UnknownMethodError: "chrome://remote/content/cdp/Error.sys.mjs",
});
/**
* Lazy domain instance cache.
*
* Domains are loaded into each target's realm, and consequently
* there exists one domain cache per realm. Domains are preregistered
* with this cache and then constructed lazily upon request.
*
* @param {Session} session
* Session that domains should be associated with as they
* are constructed.
* @param {Map.<string, string>} modules
* Table defining JS modules available to this domain cache.
* This should be a mapping between domain name
* and JS module path passed to ChromeUtils.import.
*/
export class DomainCache {
constructor(session, modules) {
this.session = session;
this.modules = modules;
this.instances = new Map();
}
/** Test if domain supports method. */
domainSupportsMethod(name, method) {
const domain = this.modules[name];
if (domain) {
return domain.implements(method);
}
return false;
}
/**
* Gets the current instance of the domain, or creates a new one,
* and associates it with the predefined session.
*
* @throws {UnknownMethodError}
* If domain is not preregistered with this domain cache.
*/
get(name) {
let inst = this.instances.get(name);
if (!inst) {
const Cls = this.modules[name];
if (!Cls) {
throw new lazy.UnknownMethodError(name);
}
if (!isConstructor(Cls)) {
throw new TypeError("Domain cannot be constructed");
}
inst = new Cls(this.session);
if (!(inst instanceof lazy.Domain)) {
throw new TypeError("Instance not a domain");
}
inst.addEventListener(this.session);
this.instances.set(name, inst);
}
return inst;
}
/**
* Tells if a Domain of the given name is available
*/
has(name) {
return name in this.modules;
}
get size() {
return this.instances.size;
}
/**
* Execute the given command (function) of a given domain with the given parameters.
* If the command doesn't exists, it will throw.
* It returns the returned value of the command, which is most likely a promise.
*/
execute(domain, command, params) {
if (!this.domainSupportsMethod(domain, command)) {
throw new lazy.UnknownMethodError(domain, command);
}
const inst = this.get(domain);
return inst[command](params);
}
/** Calls destructor on each domain and clears the cache. */
clear() {
for (const inst of this.instances.values()) {
inst.destructor();
}
this.instances.clear();
}
toString() {
return `[object DomainCache ${this.size}]`;
}
}
function isConstructor(obj) {
return !!obj.prototype && !!obj.prototype.constructor.name;
}
|