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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 | package Torello.Browser;
import static Torello.Java.C.BCYAN;
import static Torello.Java.C.BCYAN_BKGND;
import static Torello.Java.C.BBLACK;
import static Torello.Java.C.RESET;
import Torello.Java.StrIndent;
import Torello.Java.StringParse;
import Torello.Java.LFEC;
import Torello.Java.UnreachableError;
import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ReadOnlyArrayList;
import java.lang.reflect.Constructor;
import java.util.function.Consumer;
import javax.json.JsonObject;
// ************************************************************************************************
// ************************************************************************************************
// OVERVIEW
// ************************************************************************************************
// ************************************************************************************************
//
// Package-private class used internally by WebSocketSender to process asynchronous browser events
// received from the Chrome DevTools Protocol (CDP).
//
// Unlike command responses, browser events are spontaneous notifications emitted by Chrome over
// the active WebSocket connection. Examples include:
//
// "Page.loadEventFired"
// "Network.requestWillBeSent"
// "Debugger.paused"
// "Runtime.consoleAPICalled"
//
// Each event arrives as a JSON datagram containing:
//
// 1. An event-name string
// 2. A "params" JsonObject containing event-specific payload data
//
// This class is responsible for:
//
// * Normalizing the incoming event-name
// * Resolving the associated CDP domain
// * Detecting "marker events" (events without payload objects)
// * Dynamically loading the associated BrowserEvent class
// * Invoking that class' static fromJSON(JsonObject) factory method
// * Dispatching the resulting typed BrowserEvent instance to the user callback
//
// Event classes are generated automatically by the Torello Browser Tool code-generator and are
// implemented as nested types within their associated domain classes. For example:
//
// "Network.requestWillBeSent"
//
// maps to:
//
// Torello.Browser.BrowserAPI.Network$requestWillBeSent
//
// Marker events (events without fields or payload data) are treated specially and are routed
// through the lightweight MarkerEvent class instead of requiring a generated POJO.
//
// This class therefore acts as the central runtime bridge between:
//
// Raw Chrome WebSocket JSON
//
// and:
//
// Strongly typed Java BrowserEvent objects.
//
// ************************************************************************************************
// ************************************************************************************************
//
// Written By: Chat-GPT, May 7th, 2026
class HandleEvent
{
private static final String CTITLE =
BCYAN_BKGND + BBLACK + StringParse.rightSpacePad(" [Class HandleEvent]", 30) + RESET;
@SuppressWarnings("unchecked")
private static final ReadOnlyList<String> markerEventsList =
(ReadOnlyList<String>) LFEC.readObjectFromFile_JAR
(HandleEvent.class, "data-files/marker-events.roldat", true, ReadOnlyArrayList.class);
// Uncomment this to view the data file when this class is loaded by the class loader
// static { for (String me : markerEventsList) System.out.println(me); }
// A Browser Generated Event has fired. Report this even.
static void handle(
final JsonObject jo,
final String rawEventName,
final ConnRecord connRec
)
{
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// GENERATE SOME STRINGS, FIRST
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// These will be things such as:
// "RunTime$executionContextCreated"
//
// Just look at all of the events in both the JavaScript-API and the Browser-API to see
// the list of possible domains
//
// NOTE: The the Torello Browser Tool in Java-HTML occasionally uses the dollar-sign '$'
// in its class names, chrome does not. Thusly, the following 'replace' invocation
// is no big deal / just fine.
//
// ALSO: The 'Runtime' -> 'RunTime' thing is handled right here.
final String eventName = rawEventName.startsWith("Runtime.")
? ("RunTime." + rawEventName.substring(8))
: rawEventName;
final int pos = eventName.lastIndexOf('.');
if (pos == -1) throw new UnreachableError();
// These are strings like: 'policyUpdated', 'responseReceived' or 'executionContextCreated'
final String shortenedName = eventName.substring(pos + 1);
// These are strings such as: "RunTime", "DOM", "Page", "Network", "Target", etc..
final String domainName = eventName.substring(0, pos);
// Retrieve the "Domains" Enum-Constants
final Domains domainEnum = Domains.valueOf(domainName);
// The Full-Package Name of the Event Class
final String eventClassName =
domainEnum.getPackageName() + '.' + eventName.replace(".", "$");
final String msgEVENT =
CTITLE +
BCYAN + " Browser-Event Json Data:" + RESET + '\n' +
" Received Raw Event-Name: " + rawEventName + '\n' +
" Cleaned Event-Name: " + eventName + '\n' +
" Computed Domain-Name: " + domainName + '\n' +
" Computed Event ClassName: " + eventClassName + '\n';
connRec.app(msgEVENT);
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Check the Marker Events File (and inform the user of the name of the even that fired)
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
/*
System.out.println(
"eventName: " + eventName + '\n' +
markerEventsList.toString() + '\n'
);
*/
if (markerEventsList.contains(eventName))
{
final String msgSUCCESS =
CTITLE +
BCYAN + " Browser Marker-Event Received:" + RESET + '\n' +
"Event: " + eventName + '\n';
connRec.app(msgSUCCESS);
connRec.acceptEvent(MarkerEvent.create(domainEnum, shortenedName));
return;
}
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Load the class
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
final Class<?> c;
try
{ c = Class.forName(eventClassName); }
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// ERROR-CASE: The Class Loader thew an exception while attempting to load the Class
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
catch (Exception e)
{
final String errMsg =
"Event-Class Loading Attempt has Thrown Exception:\n" +
"Guessed Event-Class Name:\n" +
'[' + BCYAN + eventClassName + RESET + ']';
PrintUtil.ERR(CTITLE, connRec, errMsg, jo, e);
connRec.acceptRDPError(new RDPError(errMsg, jo, e));
return;
}
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// ERROR-CASE: The Class Loader seems to have returned 'null' from the Class.forName call
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
if (c == null)
{
// ERROR-CASE: "Method" Json-Property doesn't map to a type listed in the Data-File
final String errMsg =
"BrowserEvent Class-Loading Attempt has Returned a Null Class<?>:\n" +
"Guessed Event-Class Name:\n" +
'[' + BCYAN + eventClassName + RESET + ']';
PrintUtil.ERR(CTITLE, connRec, errMsg, jo);
connRec.acceptRDPError(new RDPError(errMsg, jo));
return;
}
final Object event;
try
{
final java.lang.reflect.Method m =
c.getMethod("fromJSON", JsonObject.class);
if (!java.lang.reflect.Modifier.isStatic(m.getModifiers()))
throw new NoSuchMethodException
("fromJSON(JsonObject) exists but is not static: " + c.getName());
// java.lang.reflect.Method:
// Object obj ==> the object the underlying method is invoked from
// Object... args ==> the arguments used for the method call
//
// If the underlying method is static, then the specified obj argument is ignored.
// It may be null.
//
// If the number of formal parameters required by the underlying method is 0, the
// supplied args array may be of length 0 or null.
//
// If the underlying method is static, the class that declared the method is
// initialized if it has not already been initialized.
final JsonObject params = jo.getJsonObject("params");
event = m.invoke(null, params);
}
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// ERROR-CASE: Retrieving the Class Constructor, or invoking it has caused an exception
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
catch (Exception e)
{
final String errMsg =
"Failed to build BrowserEvent Class using Constructor: " +
'[' + BCYAN + eventClassName + RESET + ']';
PrintUtil.ERR(CTITLE, connRec, errMsg, jo, e);
connRec.acceptRDPError(new RDPError(errMsg, jo, e));
return;
}
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// ERROR-CASE: (Likely Impossible) Constructor Invocation seems to have returned null
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
if (event == null)
{
final String errMsg =
"Failed to build event-class using constructor: " +
'[' + BCYAN + eventClassName + RESET + ']';
PrintUtil.ERR(CTITLE, connRec, errMsg, jo);
connRec.acceptRDPError(new RDPError(errMsg, jo));
return;
}
final BrowserEvent<?> browserEvent;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// ERROR-CASE: (Likely Impossible) Constructed Object is not an instance of BrowserEvent
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
if (! (event instanceof BrowserEvent))
{
final String errMsg =
"Event Class not an Instance of BrowserEvent: " +
'[' + BCYAN + eventClassName + RESET + ']';
PrintUtil.ERR(CTITLE, connRec, errMsg, jo);
connRec.acceptRDPError(new RDPError(errMsg, jo));
return;
}
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// SUCCESS
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
else browserEvent = (BrowserEvent<?>) event;
final String msgSUCCESS =
CTITLE +
BCYAN + " Browser Event Received:" + RESET + '\n' +
"Event-Class: " + event.getClass().getCanonicalName() + '\n' +
StrIndent.indent(event.toString(), 4) + '\n';
connRec.app(msgSUCCESS);
connRec.acceptEvent(browserEvent);
}
}
|