<commit_before>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.firefox;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.internal.RunningInstanceConnection;

import java.io.IOException;
import java.net.ConnectException;

public class FirefoxLauncher {
  private final FirefoxBinary binary;

  public FirefoxLauncher(FirefoxBinary binary) {
      this.binary = binary;
  }

  public static void main(String[] args) {
    FirefoxBinary binary = new FirefoxBinary();
    FirefoxLauncher launcher = new FirefoxLauncher(new FirefoxBinary());
    ProfileManager profileManager = ProfileManager.getInstance();

    String profileName = "WebDriver";
    int port = FirefoxDriver.DEFAULT_PORT;
    if (args.length >= 2) {
      port = Integer.parseInt(args[1]);
    }
    
    if (args.length >= 1) {
      profileName = args[0];
    }
    
    // If there's a browser already running, connect and kill it.
    launcher.connectAndKill(port);

    // Ensure the profile is created, and initialize it.
    launcher.createBaseWebDriverProfile(binary, profileName, port);
    
    // Connect until it works.
    launcher.repeatedlyConnectUntilFirefoxAppearsStable(port);
  }
    
  public FirefoxBinary startProfile(FirefoxProfile profile, int port) throws IOException {
    FirefoxBinary binaryToUse = binary;
    if (binary == null) {
      binaryToUse = new FirefoxBinary();
    }

    FirefoxProfile profileToUse = profile.createCopy(port);
    binaryToUse.clean(profileToUse);
    binaryToUse.startProfile(profileToUse);
    return binaryToUse;
  }
  
  @Deprecated
  public void createBaseWebDriverProfile(FirefoxBinary binary, String profileName, int port) {
    // If there's a browser already running
    connectAndKill(port);

    System.out.println(String.format("Creating %s", profileName));
    try {
        binary.createProfile(profileName);
        System.out.println("Profile created");
        binary.waitFor();
    } catch (IOException e) {
        throw new WebDriverException("Unable to create base webdriver profile", e);
    } catch (InterruptedException e) {
        throw new WebDriverException(e);
    }
    
    ProfileManager.getInstance().createProfile(binary, profileName, port);
  }

  @Deprecated
  protected void connectAndKill(int port) {
    try {
        ExtensionConnection connection = new RunningInstanceConnection("localhost", port, 5000);
        connection.quit();
    } catch (ConnectException e) {
        // This is fine. It just means that Firefox isn't running with the webdriver extension installed already
    } catch (IOException e) {
        throw new WebDriverException(e);
    }
  }

  private void repeatedlyConnectUntilFirefoxAppearsStable(int port) {
    ExtensionConnection connection;
    <commit_msg>JiayaoYu:Fix an uncaught exception in rake install_firefox<commit_after>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.firefox;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.NotConnectedException;
import org.openqa.selenium.firefox.internal.RunningInstanceConnection;

import java.io.IOException;
import java.net.ConnectException;

public class FirefoxLauncher {
  private final FirefoxBinary binary;

  public FirefoxLauncher(FirefoxBinary binary) {
      this.binary = binary;
  }

  public static void main(String[] args) {
    FirefoxBinary binary = new FirefoxBinary();
    FirefoxLauncher launcher = new FirefoxLauncher(new FirefoxBinary());
    ProfileManager profileManager = ProfileManager.getInstance();

    String profileName = "WebDriver";
    int port = FirefoxDriver.DEFAULT_PORT;
    if (args.length >= 2) {
      port = Integer.parseInt(args[1]);
    }
    
    if (args.length >= 1) {
      profileName = args[0];
    }
    
    // If there's a browser already running, connect and kill it.
    launcher.connectAndKill(port);
    
    // Ensure the profile is created, and initialize it.
    launcher.createBaseWebDriverProfile(binary, profileName, port);

    // Connect until it works.
    launcher.repeatedlyConnectUntilFirefoxAppearsStable(port);
  }
    
  public FirefoxBinary startProfile(FirefoxProfile profile, int port) throws IOException {
    FirefoxBinary binaryToUse = binary;
    if (binary == null) {
      binaryToUse = new FirefoxBinary();
    }

    FirefoxProfile profileToUse = profile.createCopy(port);
    binaryToUse.clean(profileToUse);
    binaryToUse.startProfile(profileToUse);
    return binaryToUse;
  }
  
  @Deprecated
  public void createBaseWebDriverProfile(FirefoxBinary binary, String profileName, int port) {
    // If there's a browser already running
    connectAndKill(port);

    System.out.println(String.format("Creating %s", profileName));
    try {
        binary.createProfile(profileName);
        System.out.println("Profile created");
        binary.waitFor();
    } catch (IOException e) {
        throw new WebDriverException("Unable to create base webdriver profile", e);
    } catch (InterruptedException e) {
        throw new WebDriverException(e);
    }
    
    ProfileManager.getInstance().createProfile(binary, profileName, port);
  }

  @Deprecated
  protected void connectAndKill(int port) {
    try {
        ExtensionConnection connection = new RunningInstanceConnection("localhost", port, 5000);
        connection.quit();
    } catch (ConnectException e) {
        // This is fine. It just means that Firefox isn't running with the webdriver extension installed already
    } catch (NotConnectedException e) {
        // This is fine. It just means that Firefox isn't running with the webdriver extension installed already
    } catch (IOException e) {
        throw new WebDriverException(e);
    }
  }

  private void repeatedlyConnectUntilFirefoxAppearsStable(int port) {
    ExtensionConnection connection;
    
{'fraction_non_alphanumeric': 0.08185460789925587, 'fraction_numerical': 0.008013737836290785, 'mean_word_length': 3.7095687331536387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6540884', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1825, 'n_words': 591}