<commit_before>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Portions copyright 2007 ThoughtWorks, 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.internal;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.Command;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxLauncher;
import org.openqa.selenium.firefox.FirefoxProfile;

public class NewProfileExtensionConnection extends AbstractExtensionConnection {
  private FirefoxBinary process;
  private FirefoxProfile profile;
  private int bufferSize = 4096;

  public NewProfileExtensionConnection(Lock lock, FirefoxBinary binary, FirefoxProfile profile, String host) throws IOException {
    lock.lock(binary.getTimeout());
    try {
      int portToUse = determineNextFreePort(host, profile.getPort());

      binary.setOutputWatcher(new CircularOutputStream(bufferSize));
      process = new FirefoxLauncher(binary).startProfile(profile, portToUse);
      this.profile = process.getProfile();

      setAddress(host, portToUse);

      connectToBrowser(binary.getTimeout());
    } finally {
      lock.unlock();
    }
  }

  @Override
  protected void connectToBrowser(long timeToWaitInMilliSeconds) throws IOException {
    try {
      super.connectToBrowser(timeToWaitInMilliSeconds);
    } catch (IOException e) {
      throw new WebDriverException(
          String.format("Failed to connect to binary %s on port %d; process output follows: \n%s", 
              process.toString(), profile.getPort(), process.getConsoleOutput()), e);
    } catch (WebDriverException e) {
      throw new WebDriverException(
          String.format("Failed to connect to binary %s on port %d; process output follows: \n%s", 
              process.toString(), profile.getPort(), process.getConsoleOutput()), e);
    }
  }

  protected int determineNextFreePort(String host, int port) throws IOException {
    // Attempt to connect to the given port on the host
    // If we can't connect, then we're good to use it
    int newport;

    for (newport = port; newport < port + 200; newport++) {
      Socket socket = new Socket();
      InetSocketAddress address = new InetSocketAddress(host, newport);

      try {
        socket.bind(address);
        return newport;
      } catch (BindException e) {
        // Port is already bound. Skip it and continue
      } finally {
        socket.close();
      }
    }

    throw new WebDriverException(String.format("Cannot find free port in the range %d to %d ", port, newport));
  }

  public void quit() {
        try {
            sendMessageAndWaitForResponse(WebDriverException.class, new Command(null, "quit"));
        } catch (Exception e) {
            // this is expected
        }

        if (Platform.getCurrent().is(Platform.WINDOWS)) {
            quitOnWindows();
        } else {
            quitOnOtherPlatforms();
        }

        profile.clean();    
    }

  private void quitOnOtherPlatforms() {
    // Wait for process to die and return
    try {
      process.waitFor();
    } catch (InterruptedException e) {
      throw new WebDriverException(e);
    } catch (IOException e) {
      throw new WebDriverException(e);
    }<commit_msg>PatrickLightbody Code formatting before work gets done on this class<commit_after>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Portions copyright 2007 ThoughtWorks, 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.internal;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.Command;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxLauncher;
import org.openqa.selenium.firefox.FirefoxProfile;

public class NewProfileExtensionConnection extends AbstractExtensionConnection {
  private FirefoxBinary process;
  private FirefoxProfile profile;
  private int bufferSize = 4096;

  public NewProfileExtensionConnection(Lock lock, FirefoxBinary binary, FirefoxProfile profile, String host)
      throws IOException {
    lock.lock(binary.getTimeout());
    try {
      int portToUse = determineNextFreePort(host, profile.getPort());

      binary.setOutputWatcher(new CircularOutputStream(bufferSize));
      process = new FirefoxLauncher(binary).startProfile(profile, portToUse);
      this.profile = process.getProfile();

      setAddress(host, portToUse);

      connectToBrowser(binary.getTimeout());
    } finally {
      lock.unlock();
    }
  }

  @Override
  protected void connectToBrowser(long timeToWaitInMilliSeconds) throws IOException {
    try {
      super.connectToBrowser(timeToWaitInMilliSeconds);
    } catch (IOException e) {
      throw new WebDriverException(
          String.format("Failed to connect to binary %s on port %d; process output follows: \n%s",
              process.toString(), profile.getPort(), process.getConsoleOutput()), e);
    } catch (WebDriverException e) {
      throw new WebDriverException(
          String.format("Failed to connect to binary %s on port %d; process output follows: \n%s",
              process.toString(), profile.getPort(), process.getConsoleOutput()), e);
    }
  }

  protected int determineNextFreePort(String host, int port) throws IOException {
    // Attempt to connect to the given port on the host
    // If we can't connect, then we're good to use it
    int newport;

    for (newport = port; newport < port + 200; newport++) {
      Socket socket = new Socket();
      InetSocketAddress address = new InetSocketAddress(host, newport);

      try {
        socket.bind(address);
        return newport;
      } catch (BindException e) {
        // Port is already bound. Skip it and continue
      } finally {
        socket.close();
      }
    }

    throw new WebDriverException(
        String.format("Cannot find free port in the range %d to %d ", port, newport));
  }

  public void quit() {
    try {
      sendMessageAndWaitForResponse(WebDriverException.class, new Command(null, "quit"));
    } catch (Exception e) {
      // this is expected
    }

    if (Platform.getCurrent().is(Platform.WINDOWS)) {
      quitOnWindows();
    } else {
      quitOnOtherPlatforms();
    }

    profile.clean();
  }

  private void quitOnOtherPlatforms() {
    // Wait for process to die and return
    try {
      process.waitFor();
    } catch (InterruptedException e) {
      throw new WebDriverException(e);
    } catch (IOException e) {
      throw new WebDriverException(e);
    }
{'fraction_non_alphanumeric': 0.08970775095298603, 'fraction_numerical': 0.007878017789072427, 'mean_word_length': 3.3800779076238174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6038907', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2081, 'n_words': 680}