Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cdp] Look up CDP implementation dynamically
- Loading branch information
Showing
9 changed files
with
355 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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.devtools; | ||
|
||
import org.openqa.selenium.devtools.idealized.Domains; | ||
import org.openqa.selenium.internal.Require; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public abstract class CdpInfo implements Comparable<CdpInfo> { | ||
|
||
private final int majorVersion; | ||
private final Supplier<Domains> domains; | ||
|
||
public CdpInfo(int majorVersion, Supplier<Domains> domains) { | ||
this.majorVersion = majorVersion; | ||
this.domains = Require.nonNull("Domain supplier", domains); | ||
} | ||
|
||
public int getMajorVersion() { | ||
return majorVersion; | ||
} | ||
|
||
public Domains getDomains() { | ||
return domains.get(); | ||
} | ||
|
||
@Override | ||
public int compareTo(CdpInfo that) { | ||
return Integer.compare(this.getMajorVersion(), that.getMajorVersion()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CDP version: " + getMajorVersion(); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
java/client/src/org/openqa/selenium/devtools/CdpVersionFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// 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.devtools; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import org.openqa.selenium.internal.Require; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class CdpVersionFinder { | ||
private final int fudgeFactor; | ||
private final Set<CdpInfo> infos; | ||
private static final Pattern MAJOR_VERSION_EXTRACTOR = Pattern.compile(".*/(\\d+)\\..*"); | ||
private static final Pattern BROWSER_NAME_VERSION = Pattern.compile("(\\d+)\\..*"); | ||
|
||
public CdpVersionFinder() { | ||
this( | ||
5, | ||
StreamSupport.stream(ServiceLoader.load(CdpInfo.class).spliterator(), false).collect(Collectors.toSet())); | ||
} | ||
|
||
public CdpVersionFinder(int versionFudgeFactor, Collection<CdpInfo> infos) { | ||
this.fudgeFactor = Require.nonNegative("Version fudge factor", versionFudgeFactor); | ||
|
||
Require.nonNull("CDP versions", infos); | ||
|
||
this.infos = ImmutableSet.copyOf(infos); | ||
} | ||
|
||
/** | ||
* Take the output of `/json/version` from a CDP-enabled tool and uses | ||
* that information to find a match. | ||
*/ | ||
public Optional<CdpInfo> match(Map<String, Object> versionJson) { | ||
/* The json may look like: | ||
{ | ||
"Browser": "Chrome/85.0.4183.69", | ||
"Protocol-Version": "1.3", | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.69 Safari/537.36", | ||
"V8-Version": "8.5.210.19", | ||
"WebKit-Version": "537.36 (@4554ea1a1171bd8d06951a4b7d9336afe6c59967)", | ||
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/c0ef43a1-7bb0-48e3-9cec-d6bb048cb720" | ||
} | ||
{ | ||
"Browser": "Edg/84.0.522.59", | ||
"Protocol-Version": "1.3", | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59", | ||
"V8-Version": "8.4.371.23", | ||
"WebKit-Version": "537.36 (@52ea6e40afcc988eef78d29d50f9077893fa1a12)", | ||
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/c7922624-12e8-4301-8b08-fa446944c5cc" | ||
} | ||
*/ | ||
Require.nonNull("JSON", versionJson); | ||
|
||
// We are assured by MS and Google that the `Browser` major version | ||
// should match the version of chromium used, so let's grab that. | ||
|
||
Object rawBrowser = versionJson.get("Browser"); | ||
if (!(rawBrowser instanceof String)) { | ||
return Optional.empty(); | ||
} | ||
|
||
Matcher matcher = MAJOR_VERSION_EXTRACTOR.matcher(rawBrowser.toString()); | ||
return fromMatcher(matcher); | ||
} | ||
|
||
/** | ||
* Takes a `browserVersion` from a {@link org.openqa.selenium.Capabilities} | ||
* instance and returns the matching CDP version. | ||
*/ | ||
public Optional<CdpInfo> match(String browserVersion) { | ||
Require.nonNull("Browser version", browserVersion); | ||
|
||
Matcher matcher = BROWSER_NAME_VERSION.matcher(browserVersion); | ||
return fromMatcher(matcher); | ||
} | ||
|
||
private Optional<CdpInfo> fromMatcher(Matcher matcher) { | ||
if (matcher.matches()) { | ||
String major = matcher.group(1); | ||
try { | ||
int version = Integer.parseInt(major); | ||
|
||
return findNearestMatch(version); | ||
} catch (NumberFormatException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private Optional<CdpInfo> findNearestMatch(int version) { | ||
CdpInfo nearestMatch = null; | ||
|
||
for (CdpInfo info : infos) { | ||
if (info.getMajorVersion() == version) { | ||
return Optional.of(info); | ||
} | ||
|
||
// Never return a higher version | ||
if (info.getMajorVersion() > version) { | ||
continue; | ||
} | ||
|
||
if (version - info.getMajorVersion() < fudgeFactor) { | ||
if (nearestMatch == null || info.getMajorVersion() > nearestMatch.getMajorVersion()) { | ||
nearestMatch = info; | ||
} | ||
} | ||
} | ||
|
||
return Optional.ofNullable(nearestMatch); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
java/client/src/org/openqa/selenium/devtools/v84/V84CdpInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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.devtools.v84; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.openqa.selenium.devtools.CdpInfo; | ||
import org.openqa.selenium.devtools.idealized.Domains; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@AutoService(CdpInfo.class) | ||
public class V84CdpInfo extends CdpInfo { | ||
|
||
public V84CdpInfo() { | ||
super(84, V84Domains::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.