java.lang.Object
net.i2p.apps.systray.UrlLauncher
All Implemented Interfaces:
ClientApp

public class UrlLauncherextends Object implements ClientApp

A quick and simple multi-platform URL launcher. It attempts to launch the default browser for the host platform first, then popular third-party browsers if that was not successful.

Handles Galeon, Internet Explorer, Falkon, Konqueror, Links, Lynx, Mozilla, Mozilla Firefox, Netscape, Opera, and Safari.

  • Field Details

    • _shellCommand

      private final ShellCommand _shellCommand
      Helper running the browser command.
    • _state

      private volatile ClientAppState _state
      Current state of the launcher.
    • _context

      private final I2PAppContext _context
      The app context.
    • _mgr

      private final ClientAppManager _mgr
      The manager notified of the state changes.
    • _args

      private final String[] _args
      The command line arguments given.
    • _log

      private final Log _log
      Logger for the URL launcher.
    • WAIT_TIME

      private static final int WAIT_TIME
      Time between the console readiness checks.
      See Also:
    • MAX_WAIT_TIME

      private static final int MAX_WAIT_TIME
      Longest wait for the console to come up.
      See Also:
    • MAX_TRIES

      private static final int MAX_TRIES
      Most readiness checks before giving up.
      See Also:
    • REGISTERED_NAME

      private static final String REGISTERED_NAME
      Name the launcher registers with the manager.
      See Also:
    • PROP_BROWSER

      private static final String PROP_BROWSER
      Config key of the browser command.
      See Also:
    • IS_SERVICE

      private static final boolean IS_SERVICE
      True when running as a system service.
    • BROWSERS

      private static final String[] BROWSERS
      Browsers to try IN-ORDER.
  • Constructor Details

    • UrlLauncher

      public UrlLauncher(I2PAppContext context, ClientAppManager mgr, String[] args)
      ClientApp constructor used from clients.config.
      Parameters:
      context - the router context supplying the console URL used when args is empty
      mgr - null OK
      args - URL in args[0] or null args for router console
    • UrlLauncher

      public UrlLauncher()
      Constructor from SysTray.
  • Method Details

    • waitForServer

      private static boolean waitForServer(String urlString)
      Prevent bad user experience by waiting for the server to be there before launching the browser.
      Parameters:
      urlString - the URL to probe, host and port are used for the socket
      Returns:
      true once the socket connects, false if the retries run out or the URL cannot be parsed
    • getDefaultWindowsBrowser

      private String getDefaultWindowsBrowser(String url)
      Obtains the default browser for the Windows platform, which by now should be Edgium in the worst-case scenario but in case it isn't, we can use this function to figure it out. It can find:
      1. The current user's HTTPS default browser if they configured it to be non-default
      2. The current user's HTTP default browser if they configured it to be non-default
      3. Edgium if it's available
      4. iexplore if it's not
      and it will return the first one we find in exactly that order. Adapted from: https://stackoverflow.com/questions/15852885/me...
      Parameters:
      url - containing full scheme, i.e. http://127.0.0.1:7657
      Returns:
      path to command[0] and target URL[1] to the default browser ready for execution, non-null
    • registryQuery

      private String registryQuery(String hkeyquery, String key)
      Obtains a value matching a key contained in the windows registry at a path represented by hkeyquery.
      Parameters:
      hkeyquery - registry entry to ask for.
      key - key to retrieve value from
      Returns:
      either a registry "Default" value or null if one does not exist/is empty
    • followUserConfiguredBrowserToCommand

      private String followUserConfiguredBrowserToCommand(String hkeyquery)
      If following a query back to the Default value doesn't work, what we have is a "ProgID", registered under \HKEY_CLASSES_ROOT\%ProgId% with a \shell\open\command entry whose value contains the command it needs. Takes a registry query in the same format as getDefaultOutOfRegistry but follows the ProgID instead of the default entry.
      Parameters:
      hkeyquery - the registry query in the same format as getDefaultOutOfRegistry, whose ProgID entry is followed
      Returns:
      the command required to run the application referenced in hkeyquery, or null
    • followProgIdToCommand

      private String followProgIdToCommand(String progid)
      Cross-references a progId obtained by followUserConfiguredBrowserToCommand against HKEY_CLASSES_ROOT\%ProgId%\shell\open\command, which holds the value of the command which we need to run to launch the default browser.
      Parameters:
      progid - the ProgID obtained by followUserConfiguredBrowserToCommand, interpolated into the registry path
      Returns:
      the command required to run the application referenced in hkeyquery, or null
    • getDefaultOutOfRegistry

      private String getDefaultOutOfRegistry(String hkeyquery)
      Obtains a default browsing command out of the Windows registry.
      Parameters:
      hkeyquery - registry entry to ask for.
      Returns:
      either a registry "Default" value or null if one does not exist/is empty
    • openUrl

      public boolean openUrl(String url)
      Discovers the operating system the installer is running under and tries to launch the given URL using the default browser for that platform; if unsuccessful, an attempt is made to launch the URL using the most common browsers. As of 0.9.46, fails immediately if JVM is a Windows or Linux Service. BLOCKING. This repeatedly probes the server port at the given url until it is apparently ready.
      Parameters:
      url - The URL to open.
      Returns:
      true if the operation was successful, otherwise false.
    • openUrl

      public boolean openUrl(String url, String browser)
      Opens the given URL with the given browser. As of 0.9.38, the browser parameter will be parsed into arguments separated by spaces or tabs. %1, if present, will be replaced with the url. Arguments may be surrounded by single or double quotes if they contain spaces or tabs. There is no mechanism to escape quotes or other chars with backslashes. BLOCKING. However, this does NOT probe the server port to see if it is ready.
      Parameters:
      url - The URL to open.
      browser - The browser to use. See above for quoting rules.
      Returns:
      true if the operation was successful, otherwise false.
    • parseArgs

      private static String[] parseArgs(String args, String url)
      Parse args into arguments separated by spaces or tabs. %1, if present, will be replaced with the url, otherwise it will be added as the last argument. Arguments may be surrounded by single or double quotes if they contain spaces or tabs. There is no mechanism to escape quotes or other chars with backslashes. Adapted from i2ptunnel SSLHelper.
      Parameters:
      args - the command line template to parse, non-null
      url - the URL substituted for %1, or appended if no %1 is present
      Returns:
      the parsed argument array with the URL substituted
    • validateUrlFormat

      private static boolean validateUrlFormat(String urlString)
      Checks whether a URL is safe to open.
      Parameters:
      urlString - the URL to check
      Returns:
      true if the format is acceptable
    • startup

      public void startup()
      ClientApp interface As of 0.9.46, stops immediately if JVM is a Windows or Linux Service.
      Specified by:
      startup in interface ClientApp
    • getState

      public ClientAppState getState()
      ClientApp interface.
      Specified by:
      getState in interface ClientApp
      Returns:
      the tracked application state
    • getName

      public String getName()
      ClientApp interface.
      Specified by:
      getName in interface ClientApp
      Returns:
      the name this application registers with the router, always UrlLauncher
    • getDisplayName

      public String getDisplayName()
      ClientApp interface.
      Specified by:
      getDisplayName in interface ClientApp
      Returns:
      the display name
    • changeState

      private void changeState(ClientAppState state)
      React to a client app state change.
      Parameters:
      state - the new client app state
    • changeState

      private void changeState(ClientAppState state, Exception e)
      React to a state change with an error.
      Parameters:
      state - the new client app state
      e - the error that caused the change, null if none
    • shutdown

      public void shutdown(String[] args)
      ClientApp interface.
      Specified by:
      shutdown in interface ClientApp
      Parameters:
      args - generally null but could be stopArgs from clients.config
    • main

      public static void main(String[] args)
      Obsolete, now uses ClientApp interface.
      Parameters:
      args - URL(s) to open