<commit_before>package org.lantern;

import java.awt.Desktop;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.lantern.state.Model;
import org.lantern.state.StaticSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jna.platform.win32.Shell32Util;
import com.sun.jna.platform.win32.ShlObj;
import com.sun.jna.platform.win32.Win32Exception;

public class ChromeRunner {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Point location;
    
    private volatile Process process;
    private final int screenWidth;
    private final int screenHeight;
    private final Model model;
    
    public ChromeRunner(final int screenWidth, final int screenHeight, 
            final Model model) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.model = model;
        this.location = 
            LanternUtils.getScreenCenter(screenWidth, screenHeight);
    }


    private String determineExecutable() throws IOException {
        final String path = determineExecutablePath();
        if (path == null) {
            return null;
        }
        final File file = new File(path);
        if (!file.isFile()) {
            throw new IOException("Could not find chrome at:" + path);
        } else if (!file.canExecute()) {
            throw new IOException("Chrome not executable at:" + path);
        }
        return path;
    }

    private String determineExecutablePath() {
        if (SystemUtils.IS_OS_MAC_OSX) {
            final File path1 = new File("install/osx/Lantern.app/Contents/MacOS/Lantern");
            if (path1.isFile() && path1.canExecute()) return path1.getAbsolutePath();
            final File path2 = new File("Lantern.app/Contents/MacOS/Lantern");
            if (path2.isFile() && path2.canExecute()) return path2.getAbsolutePath();
            log.warn("Could not find LanternBrowser");
            return null;
            //chrome is broken on os x -- see #622
            //return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
        } else if (SystemUtils.IS_OS_LINUX) {
            final ArrayList<String> path_candidates = new ArrayList<String>();
            path_candidates.add("/usr/bin/google-chrome");
            path_candidates.add("/usr/bin/google-chrome-stable");
            path_candidates.add("/usr/bin/google-chrome-unstable");
            path_candidates.add("/usr/bin/chromium");
            path_candidates.add("/usr/bin/chromium-browser");
            path_candidates.add("/usr/bin/google-chrome-beta");
            for (String path: path_candidates) {
                final File opt = new File(path);
                if (opt.isFile() && opt.canExecute()) return path;
            }
            log.warn("Could not find chrome");
            return null;
        } else if (SystemUtils.IS_OS_WINDOWS) {
            return findWindowsExe();
        } else {
            throw new UnsupportedOperationException(
                    "Unsupported OS: "+SystemUtils.OS_NAME);
        }
    }
    
    private void openSystemDefaultBrowser(String uri) {
        log.debug("Opening system default browser to: {}", uri);
        try {
            Desktop.getDesktop().browse(new URI(uri));
        } catch (Exception e) {
            log.error("Unable to browse to uri: {}", uri, e);
        }
    }
    
    private String findWindowsExe() {//final String... opts) {
        final Map<String, Integer> opts = new HashMap<String, Integer>();
        opts.put("APPDATA", ShlObj.CSIDL_APPDATA);
        opts.put("LOCALAPPDATA", ShlObj.CSIDL_LOCAL_APPDATA);
        opts.put("PROGRAMFILES", ShlObj.CSIDL_PROGRAM_FILES);
        opts.put("ProgramW6432", ShlObj.CSIDL_PROGRAM_FILESX86);
        final String chromePath = "/Google/Chrome/Application/chrome.exe";
        final Collection<String> paths = new HashSet<String>();
        for (final Entry<String, Integer> entry : opts.entrySet()) {
            String base;
            final String envBase = System.getenv(entry.getKey());
            if (StringUtils.isBlank(envBase)) {
                try {
                    base = Shell32Util.getFolderPath(entry.getValue().intValue());
                } catch (Win32Exception we) {
                    // This means that we couldn't find the executable
                    base = null;
                }
            } else {
                base = envBase;
            }
            if (StringUtils.isBlank(base)) {
                log.error("Could not resolve env variable: {}", base);
                continue;
            }
            final String path = base + chromePath;
            paths.add(path);
            final File candidate = new File(path);
            if (candidate.isFile() && candidate.canExecute()) {
                log.debug("Running with path: {}", path);
                return path;
            }
        }
        
        final String msg = 
                "Could not find Chrome on Windows!! Searched paths:\n"+paths;
        log.warn(msg);
        return null;
    }

    public void open() throws IOException {
        open(StaticSettings.getApiPort(), StaticSettings.getPrefix());<commit_msg>Added ordering to search paths closes #1979<commit_after>package org.lantern;

import java.awt.Desktop;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.lantern.state.Model;
import org.lantern.state.StaticSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jna.platform.win32.Shell32Util;
import com.sun.jna.platform.win32.ShlObj;
import com.sun.jna.platform.win32.Win32Exception;

public class ChromeRunner {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Point location;
    
    private volatile Process process;
    private final int screenWidth;
    private final int screenHeight;
    private final Model model;
    
    public ChromeRunner(final int screenWidth, final int screenHeight, 
            final Model model) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.model = model;
        this.location = 
            LanternUtils.getScreenCenter(screenWidth, screenHeight);
    }


    private String determineExecutable() throws IOException {
        final String path = determineExecutablePath();
        if (path == null) {
            return null;
        }
        final File file = new File(path);
        if (!file.isFile()) {
            throw new IOException("Could not find chrome at:" + path);
        } else if (!file.canExecute()) {
            throw new IOException("Chrome not executable at:" + path);
        }
        return path;
    }

    private String determineExecutablePath() {
        if (SystemUtils.IS_OS_MAC_OSX) {
            final File path1 = new File("install/osx/Lantern.app/Contents/MacOS/Lantern");
            if (path1.isFile() && path1.canExecute()) return path1.getAbsolutePath();
            final File path2 = new File("Lantern.app/Contents/MacOS/Lantern");
            if (path2.isFile() && path2.canExecute()) return path2.getAbsolutePath();
            log.warn("Could not find LanternBrowser");
            return null;
            //chrome is broken on os x -- see #622
            //return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
        } else if (SystemUtils.IS_OS_LINUX) {
            final ArrayList<String> path_candidates = new ArrayList<String>();
            path_candidates.add("/usr/bin/google-chrome");
            path_candidates.add("/usr/bin/google-chrome-stable");
            path_candidates.add("/usr/bin/google-chrome-unstable");
            path_candidates.add("/usr/bin/chromium");
            path_candidates.add("/usr/bin/chromium-browser");
            path_candidates.add("/usr/bin/google-chrome-beta");
            for (String path: path_candidates) {
                final File opt = new File(path);
                if (opt.isFile() && opt.canExecute()) return path;
            }
            log.warn("Could not find chrome");
            return null;
        } else if (SystemUtils.IS_OS_WINDOWS) {
            return findWindowsExe();
        } else {
            throw new UnsupportedOperationException(
                    "Unsupported OS: "+SystemUtils.OS_NAME);
        }
    }
    
    private void openSystemDefaultBrowser(String uri) {
        log.debug("Opening system default browser to: {}", uri);
        try {
            Desktop.getDesktop().browse(new URI(uri));
        } catch (Exception e) {
            log.error("Unable to browse to uri: {}", uri, e);
        }
    }
    
    private String findWindowsExe() {//final String... opts) {
        final Map<String, Integer> opts = new LinkedHashMap<String, Integer>();
        opts.put("APPDATA", ShlObj.CSIDL_APPDATA);
        opts.put("LOCALAPPDATA", ShlObj.CSIDL_LOCAL_APPDATA);
        opts.put("PROGRAMFILES", ShlObj.CSIDL_PROGRAM_FILES);
        opts.put("ProgramW6432", ShlObj.CSIDL_PROGRAM_FILESX86);
        final String chromePath = "/Google/Chrome/Application/chrome.exe";
        final Collection<String> paths = new HashSet<String>();
        for (final Entry<String, Integer> entry : opts.entrySet()) {
            final String envvar = entry.getKey();
            String base;
            final String envBase = System.getenv(envvar);
            if (StringUtils.isBlank(envBase)) {
                try {
                    base = Shell32Util.getFolderPath(entry.getValue().intValue());
                } catch (Win32Exception we) {
                    // This means that we couldn't find the executable
                    base = null;
                }
            } else {
                base = envBase;
            }
            if (StringUtils.isBlank(base)) {
                log.error("Could not resolve env variable: {}", envvar);
                continue;
            }
            final String path = base + chromePath;
            paths.add(path);
            final File candidate = new File(path);
            if (candidate.isFile() && candidate.canExecute()) {
                log.debug("Running with path: {}", path);
                return path;
            }
        }
        
        final String msg = 
                "Could not find Chrome on Windows!! Searched paths:\n"+paths;
        log.warn(msg);
        return null;
    }

    public void open() throws IOException {
        open(StaticSettings.getApiPort(), StaticSettings.getPrefix());
{'fraction_non_alphanumeric': 0.11029411764705882, 'fraction_numerical': 0.006477591036414566, 'mean_word_length': 2.2402155416903007, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3215242', 'n_tokens_mistral': 3223, 'n_tokens_neox': 3114, 'n_words': 741}