Skip to content

Commit 379f828

Browse files
authored
[java][BiDi] implement browsingContext.historyUpdated (#15901)
1 parent 4d99963 commit 379f828

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.browsingcontext;
19+
20+
import static java.util.Collections.unmodifiableMap;
21+
22+
import java.util.Map;
23+
import java.util.TreeMap;
24+
import org.openqa.selenium.json.JsonInput;
25+
26+
public class HistoryUpdated {
27+
28+
private final String browsingContextId;
29+
30+
private final int timestamp;
31+
32+
private final String url;
33+
34+
private HistoryUpdated(String browsingContextId, int timestamp, String url) {
35+
this.browsingContextId = browsingContextId;
36+
this.timestamp = timestamp;
37+
this.url = url;
38+
}
39+
40+
public static HistoryUpdated fromJson(JsonInput input) {
41+
String browsingContextId = null;
42+
int timestamp = 0;
43+
String url = null;
44+
45+
input.beginObject();
46+
while (input.hasNext()) {
47+
switch (input.nextName()) {
48+
case "context":
49+
browsingContextId = input.read(String.class);
50+
break;
51+
52+
case "timestamp":
53+
timestamp = input.read(int.class);
54+
break;
55+
56+
case "url":
57+
url = input.read(String.class);
58+
break;
59+
60+
default:
61+
input.skipValue();
62+
break;
63+
}
64+
}
65+
66+
input.endObject();
67+
68+
return new HistoryUpdated(browsingContextId, timestamp, url);
69+
}
70+
71+
public String getBrowsingContextId() {
72+
return browsingContextId;
73+
}
74+
75+
public int getTimestamp() {
76+
return timestamp;
77+
}
78+
79+
public String getUrl() {
80+
return url;
81+
}
82+
83+
private Map<String, Object> toJson() {
84+
Map<String, Object> toReturn = new TreeMap<>();
85+
86+
toReturn.put("browsingContextId", this.getBrowsingContextId());
87+
toReturn.put("timestamp", this.getTimestamp());
88+
toReturn.put("url", this.getUrl());
89+
90+
return unmodifiableMap(toReturn);
91+
}
92+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.openqa.selenium.bidi.Event;
3030
import org.openqa.selenium.bidi.HasBiDi;
3131
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
32+
import org.openqa.selenium.bidi.browsingcontext.HistoryUpdated;
3233
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
3334
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
3435
import org.openqa.selenium.bidi.browsingcontext.UserPromptOpened;
@@ -88,6 +89,16 @@ public class BrowsingContextInspector implements AutoCloseable {
8889
}
8990
});
9091

92+
private final Event<HistoryUpdated> historyUpdated =
93+
new Event<>(
94+
"browsingContext.historyUpdated",
95+
params -> {
96+
try (StringReader reader = new StringReader(JSON.toJson(params));
97+
JsonInput input = JSON.newInput(reader)) {
98+
return input.read(HistoryUpdated.class);
99+
}
100+
});
101+
91102
public BrowsingContextInspector(WebDriver driver) {
92103
this(new HashSet<>(), driver);
93104
}
@@ -172,6 +183,14 @@ public void onUserPromptOpened(Consumer<UserPromptOpened> consumer) {
172183
}
173184
}
174185

186+
public void onHistoryUpdated(Consumer<HistoryUpdated> consumer) {
187+
if (browsingContextIds.isEmpty()) {
188+
this.bidi.addListener(historyUpdated, consumer);
189+
} else {
190+
this.bidi.addListener(browsingContextIds, historyUpdated, consumer);
191+
}
192+
}
193+
175194
private void addNavigationEventListener(String name, Consumer<NavigationInfo> consumer) {
176195
Event<NavigationInfo> navigationEvent = new Event<>(name, navigationInfoMapper);
177196

@@ -190,6 +209,7 @@ public void close() {
190209
this.bidi.clearListener(browsingContextDestroyed);
191210
this.bidi.clearListener(userPromptOpened);
192211
this.bidi.clearListener(userPromptClosed);
212+
this.bidi.clearListener(historyUpdated);
193213

194214
navigationEventSet.forEach(this.bidi::clearListener);
195215
}

0 commit comments

Comments
 (0)