Skip to content

Commit

Permalink
fix:BrowserLauncher.getBrowserPid()方法添加cdp协议获取pid
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Feb 6, 2025
1 parent 33ba317 commit b059e4b
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/ruiyun/jvppeteer/launch/BrowserLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -264,7 +265,7 @@ private CdpBrowser createCdpBrowser(LaunchOptions options, List<String> defaultA
}
}
});
runner.setPid(getBrowserPid(runner.getProcess()));
runner.setPid(getBrowserPid(connection,runner.getProcess()));
return cdpBrowser;
}

Expand All @@ -275,12 +276,27 @@ private Browser createBiDiBrowser(BidiConnection connection, Runnable closeCallb
/**
* 通过cdp的SystemInfo.getProcessInfo获取浏览器pid,如果通过cdp没获取pid,并且是mac或者linux平台,那么尝试通过反射获取pid
*/
private String getBrowserPid(Process process) {
private String getBrowserPid(Connection connection,Process process) {
long pid = -1;
try {
pid = Helper.getPidForLinuxOrMac(process);
JsonNode response = connection.send("SystemInfo.getProcessInfo");
Iterator<JsonNode> processInfos = response.get("processInfo").elements();
while (processInfos.hasNext()) {
JsonNode processInfo = processInfos.next();
if (processInfo.get(Constant.TYPE).asText().equals("browser")) {
pid = processInfo.get(Constant.ID).asLong();
break;
}
}
} catch (Exception e) {
LOGGER.error("get browser pid error by cdp: ", e);
}
try {
if (pid == -1) {
pid = Helper.getPidForLinuxOrMac(process);
}
} catch (Exception e) {
LOGGER.error("get browser pid error: ", e);
LOGGER.error("get browser pid error by reflection: ", e);
}
return String.valueOf(pid);
}
Expand Down

0 comments on commit b059e4b

Please sign in to comment.