Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JNA code to retrieve default browser from Windows registry #6626

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +142 to +148
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will throw a NPE if registryGetStringValue(...) returns null

change to:

        String userChoice = Advapi32Util
                .registryGetStringValue(
                        WinReg.HKEY_CURRENT_USER,
                        "SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\https\\UserChoice",
                        "ProgId"
                );

        if (userChoice == null || userChoice.trim().isEmpty()) {
            return ExtWebBrowser.IEXPLORE;
        } else {
            userChoice = userChoice.toUpperCase(Locale.ROOT);
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need import java.util.Locale


// 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 (userChoice.contains(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