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

Fixed flaw in safe code that made it unsafe. #924

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
64 changes: 37 additions & 27 deletions mbhd-swing/src/main/java/org/multibit/hd/ui/utils/SafeDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,62 @@ public static boolean browse(final URI uri) {

try {
Desktop desktop = Desktop.getDesktop();
if (desktop != null) {
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(uri);
// Assume success
return true;
} else {
// Consider using xdg
return tryAltBrowse(uri);
}

// Consider using xdg

} catch (RuntimeException | IOException e) {
// Probably a weird setup - just stop now
log.warn("Failed to open browser.", e);
}


} else {
// GNOME libraries are probably not available check OS
if (!OSUtils.isLinux()) {
// This is Windows/OS X so probably a weird setup - just stop now
log.warn("No default browser configured.");
return false;
}
return tryAltBrowse(uri);
}

// Might have a chance with KDE libraries since we're on Linux
log.warn("Attempting to open browser with xdg-open");
try {
new ProcessBuilder(
"xdg-open",
uri.toString()
).start();
// Must have failed to be here
return false;

}

/**
* <p>A method to try and use alternative ways to use the browse action on platforms where java standard code gives problems.</p>
* @param uri The URI to pass to the browser
* @return True if the browser opened successfully (i.e. no visible error to the JVM)
*/
private static boolean tryAltBrowse(URI uri) {
// GNOME libraries are probably not available check OS
if (!OSUtils.isLinux()) {
// This is Windows/OS X so probably a weird setup - just stop now
log.warn("No default browser configured.");
return false;
}

// Need to include a call to waitFor() and examine the exit code to
// accurately determine success. This requires wrapping the code in
// a timeout executor which is more complex than it's worth at present
// Might have a chance with KDE libraries since we're on Linux
log.warn("Attempting to open browser with xdg-open");
try {
new ProcessBuilder(
"xdg-open",
uri.toString()
).start();

// Assume success
return true;
// Need to include a call to waitFor() and examine the exit code to
// accurately determine success. This requires wrapping the code in
// a timeout executor which is more complex than it's worth at present

} catch (IOException e) {
log.warn("Failed to open external browser through 'xdg-open {}'", uri.toString(), e);
}
// Assume success
return true;

} catch (IOException e) {
log.warn("Failed to open external browser through 'xdg-open {}'", uri.toString(), e);
}

// Must have failed to be here
return false;

}

}