From 5acee82d76f4f462992d6f114c44eb155a277f08 Mon Sep 17 00:00:00 2001 From: idk Date: Sat, 27 Aug 2022 21:36:02 -0400 Subject: [PATCH] start browser discovery code for Windows Former-commit-id: 1060e6959d694eb80cb8e82e76aec9e4bb89ca3d Former-commit-id: 006294d2bc4e20cb278a2c8679d8263fe1f13338 --- .../i2pfirefox/I2PGenericUnsafeBrowser.java | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/java/net/i2p/i2pfirefox/I2PGenericUnsafeBrowser.java b/src/java/net/i2p/i2pfirefox/I2PGenericUnsafeBrowser.java index 1b88ff8..f063144 100644 --- a/src/java/net/i2p/i2pfirefox/I2PGenericUnsafeBrowser.java +++ b/src/java/net/i2p/i2pfirefox/I2PGenericUnsafeBrowser.java @@ -46,32 +46,51 @@ public class I2PGenericUnsafeBrowser { * the worst-case scenario but in case it isn't, we can use this function to figure it out. * Adapted from: * https://stackoverflow.com/questions/15852885/method-returning-default-browser-as-a-string + * and from: + * https://github.com/i2p/i2p.i2p/blob/master/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java * * @return path to the default browser ready for execution. Empty string on Linux and OSX. */ public static String getDefaultWindowsBrowser() { if (getOperatingSystem() == "Windows"){ - try { - // Get registry where we find the default browser - Process process = Runtime.getRuntime().exec("REG QUERY HKEY_CLASSES_ROOT\\http\\shell\\open\\command"); - Scanner kb = new Scanner(process.getInputStream()); - while (kb.hasNextLine()) { - String line = kb.nextLine(); - if (line.contains("(Default")){ - String[] splitLine = line.split(" "); - kb.close(); - return splitLine[splitLine.length-1].replace("%1", "").replaceAll("\\s+$", ""); - } - } - // Match wasn't found, still need to close Scanner - kb.close(); - } catch (Exception e) { - e.printStackTrace(); - } + String defaultBrowser = getDefaultOutOfRegistry("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\https\\UserChoice") + if (defaultBrowser != "") + return defaultBrowser; + defaultBrowser = getDefaultOutOfRegistry("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\http\\UserChoice") + if (defaultBrowser != "") + return defaultBrowser; + defaultBrowser = getDefaultOutOfRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\microsoft-edge\\shell\\open\\command"); + if (defaultBrowser != "") + return defaultBrowser; + defaultBrowser = getDefaultOutOfRegistry("HKEY_CLASSES_ROOT\\http\\shell\\open\\command"); + if (defaultBrowser != "") + return defaultBrowser; } return ""; } + public static String getDefaultOutOfRegistry(String hkeyquery){ + try { + // Get registry where we find the default browser + Process process = Runtime.getRuntime().exec("REG QUERY " + hkeyquery); + Scanner kb = new Scanner(process.getInputStream()); + while (kb.hasNextLine()) { + String line = kb.nextLine(); + if (line.contains("(Default")){ + String[] splitLine = line.split(" "); + kb.close(); + return splitLine[splitLine.length-1].replace("%1", "").replaceAll("\\s+$", ""); + } + } + // Match wasn't found, still need to close Scanner + kb.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + //"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations\(http|https)\UserChoice" public static String findUnsafeBrowserAnywhere() { return getDefaultWindowsBrowser(); }