-
Notifications
You must be signed in to change notification settings - Fork 191
/
Connection.java
324 lines (298 loc) · 10.4 KB
/
Connection.java
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
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.TimeoutError;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import static com.microsoft.playwright.impl.Serialization.gson;
class Message {
int id;
String guid;
String method;
JsonObject params;
JsonElement result;
SerializedError error;
@Override
public String toString() {
return "Message{" +
"id='" + id + '\'' +
", guid='" + guid + '\'' +
", method='" + method + '\'' +
", params=" + (params == null ? null : "<...>") +
", result='" + result + '\'' +
", error='" + error + '\'' +
'}';
}
}
public class Connection {
private final Transport transport;
private final Map<String, ChannelOwner> objects = new HashMap<>();
private final Root root;
private int lastId = 0;
private final Path srcDir;
private final Map<Integer, WaitableResult<JsonElement>> callbacks = new HashMap<>();
class Root extends ChannelOwner {
Root(Connection connection) {
super(connection, "", "");
}
}
Connection(Transport transport) {
this.transport = transport;
root = new Root(this);
String srcRoot = System.getenv("PLAYWRIGHT_JAVA_SRC");
if (srcRoot == null) {
srcDir = null;
} else {
srcDir = Paths.get(srcRoot);
if (!Files.exists(srcDir)) {
throw new PlaywrightException("PLAYWRIGHT_JAVA_SRC environment variable points to non-existing location: '" + srcRoot + "'");
}
}
}
void close() throws IOException {
transport.close();
}
public JsonElement sendMessage(String guid, String method, JsonObject params) {
return root.runUntil(() -> {}, sendMessageAsync(guid, method, params));
}
public WaitableResult<JsonElement> sendMessageAsync(String guid, String method, JsonObject params) {
return internalSendMessage(guid, method, params);
}
private String sourceFile(StackTraceElement frame) {
String pkg = frame.getClassName();
int lastDot = pkg.lastIndexOf('.');
if (lastDot == -1) {
pkg = "";
} else {
pkg = frame.getClassName().substring(0, lastDot + 1);
}
pkg = pkg.replace('.', File.separatorChar);
return srcDir.resolve(pkg).resolve(frame.getFileName()).toString();
}
private JsonArray currentStackTrace() {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
int index = 0;
while (index < stack.length && !stack[index].getClassName().equals(getClass().getName())) {
index++;
};
// Find Playwright API call
while (index < stack.length && stack[index].getClassName().startsWith("com.microsoft.playwright.")) {
// hack for tests
if (stack[index].getClassName().startsWith("com.microsoft.playwright.Test")) {
break;
}
index++;
}
JsonArray jsonStack = new JsonArray();
for (; index < stack.length; index++) {
StackTraceElement frame = stack[index];
JsonObject jsonFrame = new JsonObject();
jsonFrame.addProperty("file", sourceFile(frame));
jsonFrame.addProperty("line", frame.getLineNumber());
jsonFrame.addProperty("function", frame.getClassName() + "." + frame.getMethodName());
jsonStack.add(jsonFrame);
}
return jsonStack;
}
private WaitableResult<JsonElement> internalSendMessage(String guid, String method, JsonObject params) {
int id = ++lastId;
WaitableResult<JsonElement> result = new WaitableResult<>();
callbacks.put(id, result);
JsonObject message = new JsonObject();
message.addProperty("id", id);
message.addProperty("guid", guid);
message.addProperty("method", method);
message.add("params", params);
if (srcDir != null) {
JsonObject metadata = new JsonObject();
metadata.add("stack", currentStackTrace());
message.add("metadata", metadata);
}
transport.send(gson().toJson(message));
return result;
}
public ChannelOwner waitForObjectWithKnownName(String guid) {
while (!objects.containsKey(guid)) {
processOneMessage();
}
return objects.get(guid);
}
public <T> T getExistingObject(String guid) {
@SuppressWarnings("unchecked") T result = (T) objects.get(guid);
if (result == null)
throw new PlaywrightException("Object doesn't exist: " + guid);
return result;
}
void registerObject(String guid, ChannelOwner object) {
objects.put(guid, object);
}
void unregisterObject(String guid) {
objects.remove(guid);
}
void processOneMessage() {
String messageString = transport.poll(Duration.ofMillis(10));
if (messageString == null) {
return;
}
Gson gson = gson();
Message message = gson.fromJson(messageString, Message.class);
dispatch(message);
}
private void dispatch(Message message) {
// System.out.println("Message: " + message.method + " " + message.id);
if (message.id != 0) {
WaitableResult<JsonElement> callback = callbacks.get(message.id);
if (callback == null) {
throw new PlaywrightException("Cannot find command to respond: " + message.id);
}
callbacks.remove(message.id);
// System.out.println("Message: " + message.id + " " + message);
if (message.error == null) {
callback.complete(message.result);
} else {
if (message.error.error == null) {
callback.completeExceptionally(new PlaywrightException(message.error.toString()));
} else if ("TimeoutError".equals(message.error.error.name)) {
callback.completeExceptionally(new TimeoutError(message.error.error.toString()));
} else {
callback.completeExceptionally(new DriverException(message.error.error));
}
}
return;
}
// TODO: throw?
if (message.method == null) {
return;
}
if (message.method.equals("__create__")) {
createRemoteObject(message.guid, message.params);
return;
}
if (message.method.equals("__dispose__")) {
ChannelOwner object = objects.get(message.guid);
if (object == null) {
throw new PlaywrightException("Cannot find object to dispose: " + message.guid);
}
object.disconnect();
return;
}
ChannelOwner object = objects.get(message.guid);
if (object == null) {
throw new PlaywrightException("Cannot find object to call " + message.method + ": " + message.guid);
}
object.handleEvent(message.method, message.params);
}
private ChannelOwner createRemoteObject(String parentGuid, JsonObject params) {
String type = params.get("type").getAsString();
String guid = params.get("guid").getAsString();
ChannelOwner parent = objects.get(parentGuid);
if (parent == null) {
throw new PlaywrightException("Cannot find parent object " + parentGuid + " to create " + guid);
}
JsonObject initializer = params.getAsJsonObject("initializer");
ChannelOwner result = null;
switch (type) {
case "Android":
// result = new Android(parent, type, guid, initializer);
break;
case "AndroidSocket":
// result = new AndroidSocket(parent, type, guid, initializer);
break;
case "AndroidDevice":
// result = new AndroidDevice(parent, type, guid, initializer);
break;
case "Artifact":
result = new ArtifactImpl(parent, type, guid, initializer);
break;
case "BindingCall":
result = new BindingCall(parent, type, guid, initializer);
break;
case "BrowserType":
result = new BrowserTypeImpl(parent, type, guid, initializer);
break;
case "Browser":
result = new BrowserImpl(parent, type, guid, initializer);
break;
case "BrowserContext":
result = new BrowserContextImpl(parent, type, guid, initializer);
break;
case "ConsoleMessage":
result = new ConsoleMessageImpl(parent, type, guid, initializer);
break;
case "Dialog":
result = new DialogImpl(parent, type, guid, initializer);
break;
case "Electron":
// result = new Playwright(parent, type, guid, initializer);
break;
case "ElementHandle":
result = new ElementHandleImpl(parent, type, guid, initializer);
break;
case "Frame":
result = new FrameImpl(parent, type, guid, initializer);
break;
case "JSHandle":
result = new JSHandleImpl(parent, type, guid, initializer);
break;
case "Page":
result = new PageImpl(parent, type, guid, initializer);
break;
case "Playwright":
result = new PlaywrightImpl(parent, type, guid, initializer);
break;
case "Request":
result = new RequestImpl(parent, type, guid, initializer);
break;
case "Response":
result = new ResponseImpl(parent, type, guid, initializer);
break;
case "Route":
result = new RouteImpl(parent, type, guid, initializer);
break;
case "Stream":
result = new Stream(parent, type, guid, initializer);
break;
case "Selectors":
result = new SelectorsImpl(parent, type, guid, initializer);
break;
case "WebSocket":
result = new WebSocketImpl(parent, type, guid, initializer);
break;
case "Worker":
result = new WorkerImpl(parent, type, guid, initializer);
break;
default:
throw new PlaywrightException("Unknown type " + type);
}
return result;
}
}