Skip to content

Commit

Permalink
Add JNA code to retrieve default browser from Windows registry
Browse files Browse the repository at this point in the history
update testGetDefaultOpenCommand to use new JNA based method

update testGetDefaultOpenCommand to use new JNA based method

update testGetDefaultOpenCommand to use new JNA based method

Add JNA code to retrieve default browser from Windows registry

add JNA dependency

Add JNA code to retrieve default browser from Windows registry

correct ID10t errors made by me

Add JNA code to retrieve default browser from Windows registry

fix syntax errors
  • Loading branch information
dragonsKnight5 committed Nov 4, 2023
1 parent 20ee034 commit bfc2731
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ide/extbrowser/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@
<specification-version>8.0</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.libs.jna.platform</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>2</release-version>
<specification-version>2.17</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
<test-dependencies>
<test-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

package org.netbeans.modules.extbrowser;

import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import java.util.Locale

import java.awt.EventQueue;
import java.net.*;

Expand Down Expand Up @@ -61,6 +65,9 @@
* here</a>.
*
* @author Radim Kubacki
*
* @author James Pollard
* year: 2023
*/
public class NbDdeBrowserImpl extends ExtBrowserImpl {

Expand Down Expand Up @@ -127,6 +134,59 @@ public NbDdeBrowserImpl (ExtWebBrowser extBrowserFactory) {
*/
public static native String getDefaultOpenCommand() throws NbBrowserException;

/**
* Get the default browser name using Java JNA library
* @return String
*/
private static String getDefaultWindowsBrowser() {
String userChoice = Advapi32Util
.registryGetStringValue(
WinReg.HKEY_CURRENT_USER,
"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\https\\UserChoice",
"ProgId"
)
.toUpperCase(Locale.ROOT);

// done this way so that values like FirefoxURL-308046B0AF4A39CB can be handled
if (userChoice == null || userChoice.trim().isEmpty()) {
return ExtWebBrowser.IEXPLORE;
}
else if (userChoice.contains(ExtWebBrowser.FIREFOX)) {
return ExtWebBrowser.FIREFOX;
}
else if (user.Choicecontains(ExtWebBrowser.CHROME)) {
return ExtWebBrowser.CHROME;
} else if (userChoice.contains(ExtWebBrowser.CHROMIUM)) {
return ExtWebBrowser.CHROMIUM;
} else if (userChoice.contains(ExtWebBrowser.MOZILLA)) {
return ExtWebBrowser.MOZILLA;
} else {
//may not be needed
return ExtWebBrowser.IEXPLORE;
}
}

/**
* Retrieves the browser execution path from the registry using the java JNA library
* @param browser
* @return String
*/
private static String getDefaultWindowsOpenCommandPath(String browser) {
String executionCommand = Advapi32Util
.registryGetStringValue(
WinReg.HKEY_CLASSES_ROOT,
"Applications\\" + browser.toLowerCase() + ".exe\\shell\\open\\command",
""
);


return executionCommand;
}

public static String getDefaultWindowsOpenCommand() {
return getDefaultWindowsOpenCommandPath(getDefaultWindowsBrowser());
}

/** Sets current URL.
*
* @param url URL to show in the browser.
Expand Down Expand Up @@ -212,7 +272,15 @@ private String realDDEServer () {
}

try {
String cmd = getDefaultOpenCommand ();
String cmd = getDefaultWindowsOpenCommand();

/** if not found with getDefaultWindowsOpenCommand function
* fallback to previous method
*/
if (cmd == null || cmd.trim().isEmpty()) {
cmd = getDefaultOpenCommand();
}

if (cmd != null) {
cmd = cmd.toUpperCase();
if (cmd.contains(ExtWebBrowser.IEXPLORE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
/**
*
* @author rk109395
*
* @author James
* year: 2023
*/
public class NbDdeBrowserImplTest extends NbTestCase {

Expand All @@ -52,7 +55,15 @@ public void testGetBrowserPath () throws NbBrowserException {
public void testGetDefaultOpenCommand () throws NbBrowserException {
if (!org.openide.util.Utilities.isWindows ())
return;
getDDEBrowserImpl().getDefaultOpenCommand ();
getDDEBrowserImpl().getDefaultWindowsOpenCommand();

/** if not found with getDefaultWindowsOpenCommand function
* fallback to previous method
*/
if (getDDEBrowserImpl().getDefaultWindowsOpenCommand().isEmpty())
{
getDDEBrowserImpl().getDefaultOpenCommand();
}
}

/** Test of backward method, of class org.netbeans.modules.extbrowser.NbDdeBrowserImpl. */
Expand Down

0 comments on commit bfc2731

Please sign in to comment.