From b4b5d995c743fa0c6e762f5c4c2fbc6ba1a00e5c Mon Sep 17 00:00:00 2001 From: fanyong920 <1023079644@qq.com> Date: Mon, 30 Dec 2024 15:59:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=90=AF=E5=8A=A8=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=8E?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E6=98=AF=E5=90=A6=E7=AC=A6=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvppeteer/cdp/core/BrowserFetcher.java | 62 +++++++++++++++++++ .../jvppeteer/launch/ChromeLauncher.java | 3 +- .../jvppeteer/launch/FirefoxLauncher.java | 3 +- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserFetcher.java b/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserFetcher.java index 4e946bf..7db6a9e 100644 --- a/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserFetcher.java +++ b/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserFetcher.java @@ -697,6 +697,68 @@ private String relativeExecutablePath(String revision, String versionPath) { return executablePath; } + /** + * 获取浏览器的文件名 + * @param product 产品 + * @return 文件名 + */ + public static String fileName(Product product){ + String platform = detectBrowserPlatform(); + if (Product.Chrome.equals(product)) { + if (MAC_ARM64.equals(platform) || MAC_X64.equals(platform)) { + return "Google Chrome for Testing"; + } else if (LINUX.equals(platform)) { + return "chrome"; + } else if (WIN32.equals(platform) || WIN64.equals(platform)) { + return "chrome.exe"; + } else { + throw new IllegalArgumentException("Unsupported platform: " + platform); + } + } else if (Product.Chromium.equals(product)) { + if (MAC_ARM64.equals(platform) || MAC_X64.equals(platform)) { + return "Chromium"; + } else if (LINUX.equals(platform)) { + return "chrome"; + } else if (WIN32.equals(platform) || WIN64.equals(platform)) { + return "chrome.exe"; + } else { + throw new IllegalArgumentException("Unsupported platform: " + platform); + } + } else if (Product.Chromedriver.equals(product)) { + if (MAC_ARM64.equals(platform) || MAC_X64.equals(platform)) { + return "chromedriver"; + } else if (LINUX.equals(platform)) { + return "chromedriver"; + } else if (WIN32.equals(platform) || WIN64.equals(platform)) { + return "chromedriver.exe"; + } else { + throw new IllegalArgumentException("Unsupported platform: " + platform); + } + } else if (Product.Chrome_headless_shell.equals(product)) { + if (MAC_ARM64.equals(platform) || MAC_X64.equals(platform)) { + return "chrome-headless-shell"; + } else if (LINUX.equals(platform)) { + return "chrome-headless-shell"; + } else if (WIN32.equals(platform) || WIN64.equals(platform)) { + return "chrome-headless-shell.exe"; + } else { + throw new IllegalArgumentException("Unsupported platform: " + platform); + } + } else if (Product.Firefox.equals(product)) { + if (MAC_ARM64.equals(platform) || MAC_X64.equals(platform)) { + return "firefox"; + } else if (LINUX.equals(platform)) { + return "firefox"; + } else if (WIN32.equals(platform) || WIN64.equals(platform)) { + return "firefox.exe"; + } else { + throw new IllegalArgumentException("Unsupported platform: " + platform); + } + } else { + throw new IllegalArgumentException("Unsupported product: " + product); + } + } + /** * 检测给定的路径是否存在 * diff --git a/src/main/java/com/ruiyun/jvppeteer/launch/ChromeLauncher.java b/src/main/java/com/ruiyun/jvppeteer/launch/ChromeLauncher.java index c8c018b..5069b13 100644 --- a/src/main/java/com/ruiyun/jvppeteer/launch/ChromeLauncher.java +++ b/src/main/java/com/ruiyun/jvppeteer/launch/ChromeLauncher.java @@ -1,6 +1,7 @@ package com.ruiyun.jvppeteer.launch; import com.ruiyun.jvppeteer.api.core.Browser; +import com.ruiyun.jvppeteer.cdp.core.BrowserFetcher; import com.ruiyun.jvppeteer.cdp.entities.BrowserLaunchArgumentOptions; import com.ruiyun.jvppeteer.cdp.entities.LaunchOptions; import com.ruiyun.jvppeteer.common.Constant; @@ -33,7 +34,7 @@ public Browser launch(LaunchOptions options) throws IOException { options.setArgs(new ArrayList<>()); } this.executablePath = this.computeExecutablePath(options.getExecutablePath(), options.getPreferredRevision()); - if (!Paths.get(this.executablePath).getFileName().toString().toLowerCase().contains(options.getProduct().getProduct())) { + if (!Paths.get(this.executablePath).getFileName().toString().toLowerCase().contains(BrowserFetcher.fileName(options.getProduct()))) { throw new LaunchException("The ExecutablePath does not match the product, The executablePath is " + this.executablePath + ",but the product is " + options.getProduct()); } diff --git a/src/main/java/com/ruiyun/jvppeteer/launch/FirefoxLauncher.java b/src/main/java/com/ruiyun/jvppeteer/launch/FirefoxLauncher.java index 80d5120..43dbdec 100644 --- a/src/main/java/com/ruiyun/jvppeteer/launch/FirefoxLauncher.java +++ b/src/main/java/com/ruiyun/jvppeteer/launch/FirefoxLauncher.java @@ -1,6 +1,7 @@ package com.ruiyun.jvppeteer.launch; import com.ruiyun.jvppeteer.api.core.Browser; +import com.ruiyun.jvppeteer.cdp.core.BrowserFetcher; import com.ruiyun.jvppeteer.cdp.entities.LaunchOptions; import com.ruiyun.jvppeteer.cdp.entities.Protocol; import com.ruiyun.jvppeteer.common.Constant; @@ -40,7 +41,7 @@ public Browser launch(LaunchOptions options) throws IOException { options.setArgs(new ArrayList<>()); } this.executablePath = this.computeExecutablePath(options.getExecutablePath(), options.getPreferredRevision()); - if (!Paths.get(this.executablePath).getFileName().toString().toLowerCase().contains(options.getProduct().getProduct())) { + if (!Paths.get(this.executablePath).getFileName().toString().toLowerCase().contains(BrowserFetcher.fileName(options.getProduct()))) { throw new LaunchException("The ExecutablePath does not match the product, The executablePath is " + this.executablePath + ",but the product is " + options.getProduct()); } //临时的 UserDataDir