Skip to content

Commit d01fbaf

Browse files
committed
add getData, addDataCollector and removeDataCollector
1 parent b731bb4 commit d01fbaf

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

java/src/org/openqa/selenium/bidi/module/Network.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,41 @@
1717

1818
package org.openqa.selenium.bidi.module;
1919

20+
import java.io.StringReader;
2021
import java.util.Collections;
2122
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Set;
2526
import java.util.function.Consumer;
27+
import java.util.function.Function;
2628
import org.openqa.selenium.UsernameAndPassword;
2729
import org.openqa.selenium.WebDriver;
2830
import org.openqa.selenium.bidi.BiDi;
2931
import org.openqa.selenium.bidi.Command;
3032
import org.openqa.selenium.bidi.Event;
3133
import org.openqa.selenium.bidi.HasBiDi;
34+
import org.openqa.selenium.bidi.network.AddDataCollectorParameters;
3235
import org.openqa.selenium.bidi.network.AddInterceptParameters;
3336
import org.openqa.selenium.bidi.network.BeforeRequestSent;
37+
import org.openqa.selenium.bidi.network.BytesValue;
3438
import org.openqa.selenium.bidi.network.CacheBehavior;
3539
import org.openqa.selenium.bidi.network.ContinueRequestParameters;
3640
import org.openqa.selenium.bidi.network.ContinueResponseParameters;
3741
import org.openqa.selenium.bidi.network.FetchError;
42+
import org.openqa.selenium.bidi.network.GetDataParameters;
3843
import org.openqa.selenium.bidi.network.ProvideResponseParameters;
3944
import org.openqa.selenium.bidi.network.ResponseDetails;
4045
import org.openqa.selenium.internal.Require;
46+
import org.openqa.selenium.json.Json;
47+
import org.openqa.selenium.json.JsonInput;
4148

4249
public class Network implements AutoCloseable {
4350

4451
private final Set<String> browsingContextIds;
4552

53+
private static final Json JSON = new Json();
54+
4655
private final BiDi bidi;
4756

4857
private final Event<BeforeRequestSent> beforeRequestSentEvent =
@@ -60,6 +69,18 @@ public class Network implements AutoCloseable {
6069
private final Event<ResponseDetails> authRequired =
6170
new Event<>("network.authRequired", ResponseDetails::fromJsonMap);
6271

72+
private final Function<JsonInput, BytesValue> getDataResultMapper =
73+
jsonInput -> {
74+
Map<String, Object> result = jsonInput.read(Map.class);
75+
@SuppressWarnings("unchecked")
76+
Map<String, Object> bytesMap = (Map<String, Object>) result.get("bytes");
77+
78+
try (StringReader reader = new StringReader(JSON.toJson(bytesMap));
79+
JsonInput bytesInput = JSON.newInput(reader)) {
80+
return BytesValue.fromJson(bytesInput);
81+
}
82+
};
83+
6384
public Network(WebDriver driver) {
6485
this(new HashSet<>(), driver);
6586
}
@@ -155,6 +176,29 @@ public void setCacheBehavior(CacheBehavior cacheBehavior, List<String> contexts)
155176
Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts)));
156177
}
157178

179+
public String addDataCollector(AddDataCollectorParameters parameters) {
180+
Require.nonNull("Add data collector parameters", parameters);
181+
return this.bidi.send(
182+
new Command<>(
183+
"network.addDataCollector",
184+
parameters.toMap(),
185+
jsonInput -> {
186+
Map<String, Object> result = jsonInput.read(Map.class);
187+
return (String) result.get("collector");
188+
}));
189+
}
190+
191+
public void removeDataCollector(String collector) {
192+
Require.nonNull("Collector", collector);
193+
this.bidi.send(new Command<>("network.removeDataCollector", Map.of("collector", collector)));
194+
}
195+
196+
public BytesValue getData(GetDataParameters parameters) {
197+
Require.nonNull("Get data parameters", parameters);
198+
return this.bidi.send(
199+
new Command<>("network.getData", parameters.toMap(), getDataResultMapper));
200+
}
201+
158202
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
159203
if (browsingContextIds.isEmpty()) {
160204
this.bidi.addListener(beforeRequestSentEvent, consumer);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.network;
19+
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import org.openqa.selenium.internal.Require;
25+
26+
public class AddDataCollectorParameters {
27+
28+
private final List<String> dataTypes = new ArrayList<>();
29+
private final long maxEncodedDataSize;
30+
private String collectorType = "blob";
31+
private List<String> contexts;
32+
private List<String> userContexts;
33+
34+
public AddDataCollectorParameters(List<DataType> dataTypes, long maxEncodedDataSize) {
35+
Require.nonNull("Data types", dataTypes);
36+
if (maxEncodedDataSize <= 0) {
37+
throw new IllegalArgumentException("Max encoded data size must be positive");
38+
}
39+
40+
dataTypes.forEach(dataType -> this.dataTypes.add(dataType.toString()));
41+
this.maxEncodedDataSize = maxEncodedDataSize;
42+
}
43+
44+
public AddDataCollectorParameters collectorType(String collectorType) {
45+
this.collectorType = Require.nonNull("Collector type", collectorType);
46+
return this;
47+
}
48+
49+
public AddDataCollectorParameters contexts(List<String> contexts) {
50+
this.contexts = Require.nonNull("Contexts", contexts);
51+
return this;
52+
}
53+
54+
public AddDataCollectorParameters userContexts(List<String> userContexts) {
55+
this.userContexts = Require.nonNull("User contexts", userContexts);
56+
return this;
57+
}
58+
59+
public Map<String, Object> toMap() {
60+
Map<String, Object> map = new HashMap<>();
61+
map.put("dataTypes", dataTypes);
62+
map.put("maxEncodedDataSize", maxEncodedDataSize);
63+
64+
if (collectorType != null) {
65+
map.put("collectorType", collectorType);
66+
}
67+
68+
if (contexts != null && !contexts.isEmpty()) {
69+
map.put("contexts", contexts);
70+
}
71+
72+
if (userContexts != null && !userContexts.isEmpty()) {
73+
map.put("userContexts", userContexts);
74+
}
75+
76+
return Map.copyOf(map);
77+
}
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.network;
19+
20+
public enum DataType {
21+
RESPONSE("response");
22+
23+
private final String value;
24+
25+
DataType(String value) {
26+
this.value = value;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return value;
32+
}
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.network;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import org.openqa.selenium.internal.Require;
23+
24+
public class GetDataParameters {
25+
26+
private final DataType dataType;
27+
private final String request;
28+
private String collector;
29+
private boolean disown = false;
30+
31+
public GetDataParameters(DataType dataType, String request) {
32+
this.dataType = Require.nonNull("Data type", dataType);
33+
this.request = Require.nonNull("Request", request);
34+
}
35+
36+
public GetDataParameters collector(String collector) {
37+
this.collector = Require.nonNull("Collector", collector);
38+
return this;
39+
}
40+
41+
public GetDataParameters disown(boolean disown) {
42+
this.disown = disown;
43+
return this;
44+
}
45+
46+
public Map<String, Object> toMap() {
47+
Map<String, Object> map = new HashMap<>();
48+
map.put("dataType", dataType.toString());
49+
map.put("request", request);
50+
51+
if (collector != null) {
52+
map.put("collector", collector);
53+
}
54+
55+
map.put("disown", disown);
56+
57+
return Map.copyOf(map);
58+
}
59+
}

0 commit comments

Comments
 (0)