-
Notifications
You must be signed in to change notification settings - Fork 874
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
dragonsKnight5
wants to merge
1
commit into
apache:master
Choose a base branch
from
dragonsKnight5:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
|
||
|
@@ -61,6 +65,9 @@ | |
* here</a>. | ||
* | ||
* @author Radim Kubacki | ||
* | ||
* @author James Pollard | ||
* year: 2023 | ||
*/ | ||
public class NbDdeBrowserImpl extends ExtBrowserImpl { | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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)) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 nullchange to: