Skip to content

Commit

Permalink
[java] Decode BiDi event response only once
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed May 25, 2022
1 parent f69abe3 commit 4ddffa6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
53 changes: 20 additions & 33 deletions java/src/org/openqa/selenium/bidi/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private void handle(CharSequence data) {
&& (raw.get("result") != null || raw.get("error") != null)) {
handleResponse(asString, raw);
} else if (raw.get("method") instanceof String && raw.get("params") instanceof Map) {
handleEventResponse(asString, raw);
handleEventResponse(raw);
} else {
LOG.warning(() -> "Unhandled type:" + data);
}
Expand Down Expand Up @@ -242,15 +242,14 @@ private void handleResponse(String rawDataString, Map<String, Object> rawDataMap
}
}

private void handleEventResponse(String rawDataString, Map<String, Object> rawDataMap) {
private void handleEventResponse(Map<String, Object> rawDataMap) {
LOG.log(
getDebugLogLevel(),
() -> "Method" + rawDataMap.get("method") + "called with" + eventCallbacks.keySet().size()
+ "callbacks available");
Lock lock = callbacksLock.readLock();
lock.lock();
try {
// TODO: Also only decode once.
eventCallbacks.keySet().stream()
.filter(event -> {
LOG.log(
Expand All @@ -259,36 +258,24 @@ private void handleEventResponse(String rawDataString, Map<String, Object> rawDa
return rawDataMap.get("method").equals(event.getMethod());
})
.forEach(event -> {
try (StringReader reader = new StringReader(rawDataString);
JsonInput input = JSON.newInput(reader)) {
Object value = null;
input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "params":
value = event.getMapper().apply(input);
break;

default:
input.skipValue();
break;
}
}
input.endObject();

if (value == null) {
return;
}

final Object finalValue = value;

for (Consumer<?> action : eventCallbacks.get(event)) {
@SuppressWarnings("unchecked") Consumer<Object> obj = (Consumer<Object>) action;
LOG.log(
getDebugLogLevel(),
String.format("Calling callback for %s using %s being passed %s", event, obj, finalValue));
obj.accept(finalValue);
}
Map<String, Object> params = (Map<String, Object>) rawDataMap.get("params");
Object value = null;
if (params != null) {
value = event.getMapper().apply(params);
}
if (value == null) {
return;
}

final Object finalValue = value;

for (Consumer<?> action : eventCallbacks.get(event)) {
@SuppressWarnings("unchecked") Consumer<Object> obj = (Consumer<Object>) action;
LOG.log(
getDebugLogLevel(),
String.format("Calling callback for %s using %s being passed %s", event, obj,
finalValue));
obj.accept(finalValue);
}
});
} finally {
Expand Down
7 changes: 4 additions & 3 deletions java/src/org/openqa/selenium/bidi/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;

import java.util.Map;
import java.util.function.Function;

public class Event<X> {

private final String method;
private final Function<JsonInput, X> mapper;
private final Function<Map<String,Object>, X> mapper;

public Event(String method, Function<JsonInput, X> mapper) {
public Event(String method, Function<Map<String,Object>, X> mapper) {
this.method = Require.nonNull("Event method", method);
this.mapper = Require.nonNull("Result mapper", mapper);
}
Expand All @@ -36,7 +37,7 @@ public String getMethod() {
return method;
}

public Function<JsonInput, X> getMapper() {
public Function<Map<String,Object>, X> getMapper() {
return mapper;
}

Expand Down

0 comments on commit 4ddffa6

Please sign in to comment.