|
| 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; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | + |
| 22 | +import java.io.Closeable; |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import org.openqa.selenium.internal.Require; |
| 28 | + |
| 29 | +public class BiDi implements Closeable { |
| 30 | + |
| 31 | + private final Duration timeout = Duration.ofSeconds(30); |
| 32 | + private final Connection connection; |
| 33 | + private final BiDiSessionStatus status; |
| 34 | + |
| 35 | + public BiDi(Connection connection) { |
| 36 | + this.connection = Require.nonNull("WebSocket connection", connection); |
| 37 | + this.status = |
| 38 | + send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class)); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void close() { |
| 43 | + clearListeners(); |
| 44 | + disconnectSession(); |
| 45 | + } |
| 46 | + |
| 47 | + public void disconnectSession() { |
| 48 | + // TODO: Identify how to close a BiDi session. |
| 49 | + // Seems like https://w3c.github.io/webdriver-bidi/#issue-9f7aff26 needs to be fleshed out. |
| 50 | + } |
| 51 | + |
| 52 | + public <X> X send(Command<X> command) { |
| 53 | + Require.nonNull("Command to send", command); |
| 54 | + return connection.sendAndWait(command, timeout); |
| 55 | + } |
| 56 | + |
| 57 | + public <X> void addListener(Event<X> event, Consumer<X> handler) { |
| 58 | + Require.nonNull("Event to listen for", event); |
| 59 | + Require.nonNull("Handler to call", handler); |
| 60 | + |
| 61 | + send(new Command<>("session.subscribe", |
| 62 | + ImmutableMap.of("events", Collections.singletonList(event.getMethod())))); |
| 63 | + |
| 64 | + connection.addListener(event, handler); |
| 65 | + } |
| 66 | + |
| 67 | + public void clearListeners() { |
| 68 | + send(new Command<>("session.unsubscribe", Collections.emptyMap())); |
| 69 | + connection.clearListeners(); |
| 70 | + } |
| 71 | + |
| 72 | + public BiDiSessionStatus getBidiSessionStatus() { |
| 73 | + return status; |
| 74 | + } |
| 75 | +} |
0 commit comments