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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* 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/. */
import { WebSocketConnection } from "chrome://remote/content/shared/WebSocketConnection.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
Log: "chrome://remote/content/shared/Log.sys.mjs",
processCapabilities:
"chrome://remote/content/shared/webdriver/Capabilities.sys.mjs",
quit: "chrome://remote/content/shared/Browser.sys.mjs",
RemoteAgent: "chrome://remote/content/components/RemoteAgent.sys.mjs",
WEBDRIVER_CLASSIC_CAPABILITIES:
"chrome://remote/content/shared/webdriver/Capabilities.sys.mjs",
});
ChromeUtils.defineLazyGetter(lazy, "logger", () =>
lazy.Log.get(lazy.Log.TYPES.WEBDRIVER_BIDI)
);
export class WebDriverBiDiConnection extends WebSocketConnection {
/**
* @param {WebSocket} webSocket
* The WebSocket server connection to wrap.
* @param {Connection} httpdConnection
* Reference to the httpd.js's connection needed for clean-up.
*/
constructor(webSocket, httpdConnection) {
super(webSocket, httpdConnection);
// Each connection has only a single associated WebDriver session.
this.session = null;
}
/**
* Perform required steps to end the session.
*/
endSession() {
// TODO Bug 1838269. Implement session ending logic
// for the case of classic + bidi session.
// We currently only support one session, see Bug 1720707.
lazy.RemoteAgent.webDriverBiDi.deleteSession();
}
/**
* Register a new WebDriver Session to forward the messages to.
*
* @param {Session} session
* The WebDriverSession to register.
*/
registerSession(session) {
if (this.session) {
throw new lazy.error.UnknownError(
"A WebDriver session has already been set"
);
}
this.session = session;
lazy.logger.debug(
`Connection ${this.id} attached to session ${session.id}`
);
}
/**
* Unregister the already set WebDriver session.
*/
unregisterSession() {
if (!this.session) {
return;
}
this.session.removeConnection(this);
this.session = null;
}
/**
* Send an error back to the WebDriver BiDi client.
*
* @param {number} id
* Id of the packet which lead to an error.
* @param {Error} err
* Error object with `status`, `message` and `stack` attributes.
*/
sendError(id, err) {
const webDriverError = lazy.error.wrap(err);
this.send({
type: "error",
id,
error: webDriverError.status,
message: webDriverError.message,
stacktrace: webDriverError.stack,
});
}
/**
* Send an event coming from a module to the WebDriver BiDi client.
*
* @param {string} method
* The event name. This is composed by a module name, a dot character
* followed by the event name, e.g. `log.entryAdded`.
* @param {object} params
* A JSON-serializable object, which is the payload of this event.
*/
sendEvent(method, params) {
this.send({ type: "event", method, params });
if (Services.profiler?.IsActive()) {
ChromeUtils.addProfilerMarker(
"BiDi: Event",
{ category: "Remote-Protocol" },
method
);
}
}
/**
* Send the result of a call to a module's method back to the
* WebDriver BiDi client.
*
* @param {number} id
* The request id being sent by the client to call the module's method.
* @param {object} result
* A JSON-serializable object, which is the actual result.
*/
sendResult(id, result) {
result = typeof result !== "undefined" ? result : {};
this.send({ type: "success", id, result });
}
observe(subject, topic) {
switch (topic) {
case "quit-application-requested":
this.endSession();
break;
}
}
// Transport hooks
/**
* Called by the `transport` when the connection is closed.
*/
onConnectionClose() {
this.unregisterSession();
super.onConnectionClose();
}
/**
* Receive a packet from the WebSocket layer.
*
* This packet is sent by a WebDriver BiDi client and is meant to execute
* a particular method on a given module.
*
* @param {object} packet
* JSON-serializable object sent by the client
*/
async onPacket(packet) {
super.onPacket(packet);
const { id, method, params } = packet;
const startTime = Cu.now();
try {
// First check for mandatory field in the command packet
lazy.assert.positiveInteger(id, "id: unsigned integer value expected");
lazy.assert.string(method, "method: string value expected");
lazy.assert.object(params, "params: object value expected");
// Extract the module and the command name out of `method` attribute
const { module, command } = splitMethod(method);
let result;
// Handle static commands first
if (module === "session" && command === "new") {
const processedCapabilities = lazy.processCapabilities(params);
result = await lazy.RemoteAgent.webDriverBiDi.createSession(
processedCapabilities,
this
);
// Since in Capabilities class we setup default values also for capabilities which are
// not relevant for bidi, we want to remove them from the payload before returning to a client.
result.capabilities = Array.from(result.capabilities.entries()).reduce(
(object, [key, value]) => {
if (!lazy.WEBDRIVER_CLASSIC_CAPABILITIES.includes(key)) {
object[key] = value;
}
return object;
},
{}
);
} else if (module === "session" && command === "status") {
result = lazy.RemoteAgent.webDriverBiDi.getSessionReadinessStatus();
} else {
lazy.assert.session(this.session);
// Bug 1741854 - Workaround to deny internal methods to be called
if (command.startsWith("_")) {
throw new lazy.error.UnknownCommandError(method);
}
// Finally, instruct the session to execute the command
result = await this.session.execute(module, command, params);
}
this.sendResult(id, result);
// Session clean up.
if (module === "session" && command === "end") {
this.endSession();
}
// Close the browser.
// TODO Bug 1842018. Refactor this part to return the response
// when the quitting of the browser is finished.
else if (module === "browser" && command === "close") {
// Register handler to run WebDriver BiDi specific shutdown code.
Services.obs.addObserver(this, "quit-application-requested");
// TODO Bug 1836282. Add as the third argument "moz:windowless" capability
// from the session, when this capability is supported by Webdriver BiDi.
await lazy.quit(["eForceQuit"], false);
Services.obs.removeObserver(this, "quit-application-requested");
}
} catch (e) {
this.sendError(id, e);
}
if (Services.profiler?.IsActive()) {
ChromeUtils.addProfilerMarker(
"BiDi: Command",
{ startTime, category: "Remote-Protocol" },
`${method} (${id})`
);
}
}
}
/**
* Splits a WebDriver BiDi method into module and command components.
*
* @param {string} method
* Name of the method to split, e.g. "session.subscribe".
*
* @returns {Object<string, string>}
* Object with the module ("session") and command ("subscribe")
* as properties.
*/
export function splitMethod(method) {
const parts = method.split(".");
if (parts.length != 2 || !parts[0].length || !parts[1].length) {
throw new TypeError(`Invalid method format: '${method}'`);
}
return {
module: parts[0],
command: parts[1],
};
}
|