Skip to content

Commit

Permalink
Comments and minor refactorings #2043
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Dec 16, 2014
1 parent dcc8b34 commit 640b635
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/main/java/org/lantern/browser/BrowserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class BrowserUtils {

private BrowserUtils(){}

/**
* Adds the various default arguments to chrome.
*
* @param commands The list of commands to add arguments to.
* @param windowWidth The desired window width.
* @param windowHeight The desired window height.
*/
public static void addDefaultChromeArgs(final List<String> commands,
final int windowWidth, final int windowHeight) {

Expand All @@ -49,7 +56,14 @@ public static void addDefaultChromeArgs(final List<String> commands,
commands.add("--disable-extensions");
}

public static void openSystemDefaultBrowser(String uri) {
/**
* Opens the specified URL in the operating system's default browser.
* This does not return the process ID of the new window and therefore
* cannot be used if the caller needs to ever close the window.
*
* @param uri The URI to open.
*/
public static void openSystemDefaultBrowser(final String uri) {
LOG.debug("Opening system default browser to: {}", uri);
try {
Desktop.getDesktop().browse(new URI(uri));
Expand All @@ -58,6 +72,13 @@ public static void openSystemDefaultBrowser(String uri) {
}
}

/**
* Runs the process specified in the given list of commands.
*
* @param commands The list of commands to run.
* @return The process.
* @throws IOException If there's an error running the commands.
*/
public static Process runProcess(final List<String> commands) throws IOException {
final ProcessBuilder processBuilder = new ProcessBuilder(commands);

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/lantern/browser/LanternBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

import java.io.IOException;

/**
* Interface for operating-specific browsers that display the Lantern user
* interface.
*/
public interface LanternBrowser {

/**
* Opens the URI to the Lantern user interface.
*
* @param uri The URI to open.
* @return The {@link Process} for the new browser, allowing the caller
* to eventually close it, or <code>null</code> if a browser with an
* associated process could not be created.
* @throws IOException If there's an error opening the browser.
*/
Process open(String uri) throws IOException;
}
3 changes: 1 addition & 2 deletions src/main/java/org/lantern/browser/UbuntuBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public UbuntuBrowser(final int windowWidth, final int windowHeight) {
@Override
public Process open(final String uri) throws IOException {
log.info("Opening browser to: {}", uri);
final List<String> commands = new ArrayList<String>();

final Collection<String> pathCandidates = new ArrayList<String>();
pathCandidates.add("/usr/bin/google-chrome");
pathCandidates.add("/usr/bin/google-chrome-stable");
Expand All @@ -46,6 +44,7 @@ public Process open(final String uri) throws IOException {
final File opt = new File(path);
if (opt.isFile() && opt.canExecute()) {
// http://peter.sh/experiments/chromium-command-line-switches/
final List<String> commands = new ArrayList<String>();
commands.add(path);
BrowserUtils.addDefaultChromeArgs(commands, this.windowWidth, this.windowHeight);
commands.add("--app=" + uri);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/lantern/browser/WindowsBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public WindowsBrowser(final int windowWidth, final int windowHeight) {
}

public Process open(final String uri) throws IOException {
final List<String> commands = new ArrayList<String>();

final String executable = determineExecutable();
if (StringUtils.isBlank(executable)) {
// At this point we've effectively only searched for Chrome and
Expand All @@ -49,6 +47,7 @@ public Process open(final String uri) throws IOException {
throw new UnsupportedOperationException("Could not find Chrome!");
}
}
final List<String> commands = new ArrayList<String>();
commands.add(executable);
BrowserUtils.addDefaultChromeArgs(commands, this.windowWidth,
this.windowHeight);
Expand Down

0 comments on commit 640b635

Please sign in to comment.