Skip to content

Commit

Permalink
fix: 启动浏览器判断文件名与产品是否符合
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Dec 30, 2024
1 parent aca6fdf commit b4b5d99
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
62 changes: 62 additions & 0 deletions src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
* 检测给定的路径是否存在
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4b5d99

Please sign in to comment.