Skip to content
44 changes: 44 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.network.AddDataCollectorParameters;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.BytesValue;
import org.openqa.selenium.bidi.network.CacheBehavior;
import org.openqa.selenium.bidi.network.ContinueRequestParameters;
import org.openqa.selenium.bidi.network.ContinueResponseParameters;
import org.openqa.selenium.bidi.network.FetchError;
import org.openqa.selenium.bidi.network.GetDataParameters;
import org.openqa.selenium.bidi.network.ProvideResponseParameters;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;

public class Network implements AutoCloseable {

Expand All @@ -60,6 +65,22 @@ public class Network implements AutoCloseable {
private final Event<ResponseDetails> authRequired =
new Event<>("network.authRequired", ResponseDetails::fromJsonMap);

private final Function<JsonInput, BytesValue> getDataResultMapper =
jsonInput -> {
jsonInput.beginObject();
BytesValue bytes = null;
while (jsonInput.hasNext()) {
String name = jsonInput.nextName();
if ("bytes".equals(name)) {
bytes = BytesValue.fromJson(jsonInput);
} else {
jsonInput.skipValue();
}
}
jsonInput.endObject();
return bytes;
};

public Network(WebDriver driver) {
this(new HashSet<>(), driver);
}
Expand Down Expand Up @@ -155,6 +176,29 @@ public void setCacheBehavior(CacheBehavior cacheBehavior, List<String> contexts)
Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts)));
}

public String addDataCollector(AddDataCollectorParameters parameters) {
Require.nonNull("Add data collector parameters", parameters);
return this.bidi.send(
new Command<>(
"network.addDataCollector",
parameters.toMap(),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("collector");
}));
}

public void removeDataCollector(String collector) {
Require.nonNull("Collector", collector);
this.bidi.send(new Command<>("network.removeDataCollector", Map.of("collector", collector)));
}

public BytesValue getData(GetDataParameters parameters) {
Require.nonNull("Get data parameters", parameters);
return this.bidi.send(
new Command<>("network.getData", parameters.toMap(), getDataResultMapper));
}

public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(beforeRequestSentEvent, consumer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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 org.openqa.selenium.bidi.network;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openqa.selenium.internal.Require;

public class AddDataCollectorParameters {

private final List<String> dataTypes = new ArrayList<>();
private final long maxEncodedDataSize;
private String collectorType = "blob";
private List<String> contexts;
private List<String> userContexts;

public AddDataCollectorParameters(List<DataType> dataTypes, long maxEncodedDataSize) {
Require.nonNull("Data types", dataTypes);
if (maxEncodedDataSize <= 0) {
throw new IllegalArgumentException("Max encoded data size must be positive");
}

dataTypes.forEach(dataType -> this.dataTypes.add(dataType.toString()));
this.maxEncodedDataSize = maxEncodedDataSize;
}

public AddDataCollectorParameters collectorType(String collectorType) {
this.collectorType = Require.nonNull("Collector type", collectorType);
return this;
}

public AddDataCollectorParameters contexts(List<String> contexts) {
this.contexts = Require.nonNull("Contexts", contexts);
return this;
}

public AddDataCollectorParameters userContexts(List<String> userContexts) {
this.userContexts = Require.nonNull("User contexts", userContexts);
return this;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("dataTypes", dataTypes);
map.put("maxEncodedDataSize", maxEncodedDataSize);

if (collectorType != null) {
map.put("collectorType", collectorType);
}

if (contexts != null && !contexts.isEmpty()) {
map.put("contexts", contexts);
}

if (userContexts != null && !userContexts.isEmpty()) {
map.put("userContexts", userContexts);
}

return Map.copyOf(map);
}
}
33 changes: 33 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/DataType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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 org.openqa.selenium.bidi.network;

public enum DataType {
RESPONSE("response");

private final String value;

DataType(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}
59 changes: 59 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/GetDataParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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 org.openqa.selenium.bidi.network;

import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.internal.Require;

public class GetDataParameters {

private final DataType dataType;
private final String request;
private String collector;
private boolean disown = false;

public GetDataParameters(DataType dataType, String request) {
this.dataType = Require.nonNull("Data type", dataType);
this.request = Require.nonNull("Request", request);
}

public GetDataParameters collector(String collector) {
this.collector = Require.nonNull("Collector", collector);
return this;
}

public GetDataParameters disown(boolean disown) {
this.disown = disown;
return this;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("dataType", dataType.toString());
map.put("request", request);

if (collector != null) {
map.put("collector", collector);
}

map.put("disown", disown);

return Map.copyOf(map);
}
}
Loading
Loading