Skip to content

Commit

Permalink
[java] handle events only once
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Aug 27, 2023
1 parent edb4c75 commit bbe2ac5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public NetworkInterceptor(WebDriver driver, Filter filter) {
@Override
public void close() {
tools.getDomains().network().resetNetworkFilter();
tools.getDomains().network().markNetworkInterceptorClosed();
}

protected HttpMethod convertFromCdpHttpMethod(String method) {
Expand Down
18 changes: 10 additions & 8 deletions java/src/org/openqa/selenium/devtools/idealized/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ public abstract class Network<AUTHREQUIRED, REQUESTPAUSED> {
private volatile Filter filter = defaultFilter;
protected final DevTools devTools;

private final AtomicBoolean networkInterceptorClosed = new AtomicBoolean();
private final AtomicBoolean fetchEnabled = new AtomicBoolean();

public Network(DevTools devtools) {
this.devTools = Require.nonNull("DevTools", devtools);
}

public void disable() {
fetchEnabled.set(false);
devTools.send(disableFetch());
devTools.send(enableNetworkCaching());

Expand Down Expand Up @@ -141,10 +142,6 @@ public void resetNetworkFilter() {
filter = defaultFilter;
}

public void markNetworkInterceptorClosed() {
networkInterceptorClosed.set(true);
}

public void interceptTrafficWith(Filter filter) {
Require.nonNull("HTTP filter", filter);

Expand All @@ -153,6 +150,11 @@ public void interceptTrafficWith(Filter filter) {
}

public void prepareToInterceptTraffic() {
if (fetchEnabled.getAndSet(true)) {
// ensure we do not register the listeners multiple times, otherwise the events are handled
// multiple times
return;
}
devTools.send(disableNetworkCaching());

devTools.addListener(
Expand Down Expand Up @@ -221,7 +223,7 @@ public void prepareToInterceptTraffic() {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
} catch (ExecutionException e) {
if (!networkInterceptorClosed.get()) {
if (fetchEnabled.get()) {
LOG.log(WARNING, e, () -> "Unable to process request");
}
return new HttpResponse();
Expand All @@ -236,8 +238,8 @@ public void prepareToInterceptTraffic() {

devTools.send(fulfillRequest(pausedRequest, forBrowser));
} catch (TimeoutException e) {
if (!networkInterceptorClosed.get()) {
throw new WebDriverException(e);
if (fetchEnabled.get()) {
throw e;
}
}
});
Expand Down

0 comments on commit bbe2ac5

Please sign in to comment.