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 | package Torello.Browser;
import Torello.Java.Additional.Ret0;
import Torello.Java.Additional.RetN;// Needed for @link
import java.util.Objects;
import java.util.function.Function;
import javax.json.JsonObject;
/**
* The class script simply sends an asynchronous request, and returns an instance of {@link Promise}
* which may be awaited.
*
* <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=SCRIPT>
*
* @param <RESULT>
* <a id=typeParam-RESULT></a>
* <EMBED CLASS='external-html' DATA-FILE-ID=RESULT>
*/
public class Script<RESULT> implements java.io.Serializable
{
/** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
protected static final long serialVersionUID = 1;
/** The Browser or Java-Script Domain which maintains the external browser command. */
public final Domains domain;
/** The Name of the Web-Browser Command that is being executed by this {@code Script}. */
public final String commandName;
/** The request-{@code String} that will be sent over the Web-Socket */
public final String requestJSONString;
/**
* This function pointer performs the <I><B STYLE='color: red;'>Json-Binding</B></I> needed to
* convert a browser-generated {@link JsonObject} into a Java POJO (ultimately having type
* {@code 'RESULT'}).
*/
public final Function<JsonObject, RESULT> receiver;
/**
* The Java-{@code Class} / Type of the post-processed object which is ultimately returned by
* this browser command. This {@code public, final} field is nothing more than a "reified"
* Java Generic Type-Parameter for this class'
* <B><A HREF='#typeParam-RESULT'>{@code <RESULT>}</A></B> generic type.
*/
public final Class<RESULT> resultClass;
/**
*
* @param domain The domain in which this command is defined
* @param commandName The name of the command, as per the Google CDP API
* @param requestJSONString The command, formulated a a JSON request
*
* @param receiver This is a function pointer which "translates" the browser's JSON response
* into a Java {@link RetN} POJO.
*
* @param resultClass This is used solely to facilate {@code javac's} type parameter "capture"
* logic.
*/
@SuppressWarnings("unchecked")
public Script(
final Domains domain,
final String commandName,
final String requestJSONString,
final Function<JsonObject, RESULT> receiver,
final Class<?> resultClass
)
{
this.requestJSONString = requestJSONString;
this.receiver = receiver;
this.domain = domain;
this.commandName = commandName;
// This may seem silly, and it is, but occasionally, that happens
this.resultClass = (Class<RESULT>) resultClass;
}
/**
* This is a pre-defined, and repeatedly used, CDP browser response translator function. This
* is defined solely for use with the static builder method named {@link #NO_RET}.
* @see #NO_RET(Domains, String, String)
*/
public static final Function<JsonObject, Void> NO_RETURN_VALUES = (JsonObject jo) -> null;
/**
* This is used as an alternative to a constructor, since the type parameter
* <A HREF='#type-parameter-RESULT'><CODE>RESULT</CODE></A> is restricted to the class
* {@link Ret0}.
*
* @param domain The domain in which this command is defined
* @param commandName The name of the command, as per the Google CDP API
* @param requestJSONString The command, formulated a a JSON request
*
* @see #NO_RETURN_VALUES
*/
public static Script<Void> NO_RET(
final Domains domain,
final String commandName,
final String requestJSONString
)
{ return new Script<>(domain, commandName, requestJSONString, NO_RETURN_VALUES, Void.class); }
/**
* If there are reasons to use a different sender - <I>because certain configurations need to
* change</I> - then using this {@code exec} method allows a user to change senders.
*
* @param wss The {@link WebSocketSender} Instance to use when executing this {@link Script}
* object-instance.
*
* <BR /><BR />In the Browser Remote Debug Package (Headless Chrome), the {@code 'Script'}
* instances which are generated expect the user to provide their own constructed
* {@code WebSocketSender} instances to facilitate:
*
* <BR /><BR /><UL CLASS=JDUL>
* <LI>Pre-Compiling Scripts ahead of time for future use, using future Browser-Connections</LI>
* <LI>Utilizing alternate & various Page-Connections, for different Open-Pages</LI>
* <LI>Re-using Pre-Compiled Script Components</LI>
* </UL>
*
* @return <EMBED CLASS='external-html' DATA-FILE-ID=SCRIPTRET>
*/
public Promise<RESULT> exec(final WebSocketSender wss)
{
final Promise<RESULT> promise = new Promise<>(this);
wss.send(this, promise);
return promise;
}
}
|