<commit_before>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.ui.launcher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicBoolean;

import org.chromium.debug.core.ChromiumDebugPlugin;
import org.chromium.debug.core.model.ConnectionLoggerImpl;
import org.chromium.debug.core.model.ConsolePseudoProcess;
import org.chromium.debug.core.model.DebugTargetImpl;
import org.chromium.debug.core.model.Destructable;
import org.chromium.debug.core.model.DestructingGuard;
import org.chromium.debug.core.model.JavascriptVmEmbedder;
import org.chromium.debug.core.model.JavascriptVmEmbedderFactory;
import org.chromium.debug.core.model.NamedConnectionLoggerFactory;
import org.chromium.debug.ui.DialogBasedTabSelector;
import org.chromium.debug.ui.PluginUtil;
import org.chromium.sdk.ConnectionLogger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.internal.core.LaunchConfiguration;

/**
 * A launch configuration delegate for the JavaScript debugging.
 */
public abstract class LaunchType implements ILaunchConfigurationDelegate {

  public static class Chromium extends LaunchType {
    @Override
    protected JavascriptVmEmbedder.ConnectionToRemote createConnectionToRemote(int port,
        ILaunch launch, boolean addConsoleLogger) throws CoreException {
      NamedConnectionLoggerFactory consoleFactory =
        addConsoleLogger ? CONNECTION_LOGGER_FACTORY : NO_CONNECTION_LOGGER_FACTORY;
      return JavascriptVmEmbedderFactory.connectToChromeDevTools(port, consoleFactory,
          new DialogBasedTabSelector());
    }

    /**
     * This thing is responsible for creating a separate launch that holds
     * logger console pseudo-projects.
     * TODO(peter.rybin): these projects stay as zombies under the launch; fix it
     */
    private final static NamedConnectionLoggerFactory CONNECTION_LOGGER_FACTORY =
        new NamedConnectionLoggerFactory() {
      private final ILaunch commonLaunch;

      {
        // Let's create configuration first. We might need to create file that holds it.
        IPath configurationPath;
        try {
          configurationPath = initializeConfigurationFile();
        } catch (IOException e) {
          throw new RuntimeException("Failed to create configuration file", e); //$NON-NLS-1$
        }

        LaunchConfiguration configuration = new LaunchConfiguration(configurationPath) {
          @Override
          public boolean isLocal() {
            return true;
          }
        };
        commonLaunch = new Launch(configuration, ILaunchManager.DEBUG_MODE, null);
      }

      public ConnectionLogger createLogger(String title) {
        return LaunchType.createConsoleAndLogger(commonLaunch, true, title);
      }

      /**
       * Creates file so that configuration could be read from some location.
       */
      private IPath initializeConfigurationFile() throws IOException {
        IPath configurationPath = ChromiumDebugPlugin.getDefault().getStateLocation().append(
            Messages.LaunchType_LogConsoleLaunchName
            + "." + ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION); //$NON-NLS-1$
        String osPath = configurationPath.toOSString();
        File file = new File(osPath);
        synchronized (this) {
          Writer writer = new OutputStreamWriter(new FileOutputStream(file));
          writer.write(
              "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + //$NON-NLS-1$
              "<launchConfiguration " + //$NON-NLS-1$
              "type=\"org.chromium.debug.ui.ConsolePseudoConfigurationType\"/>"); //$NON-NLS-1$
          writer.close();
        }
        return configurationPath;
      }
    };
  }

  public static class StandaloneV8 extends LaunchType {
    @Override
    protected JavascriptVmEmbedder.ConnectionToRemote createConnectionToRemote(int port,
        final ILaunch launch, boolean addConsoleLogger) {
      NamedConnectionLoggerFactory consoleFactory;<commit_msg>Make org.chromium.debug.ui compilable in 3.5<commit_after>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.ui.launcher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicBoolean;

import org.chromium.debug.core.model.ConnectionLoggerImpl;
import org.chromium.debug.core.model.ConsolePseudoProcess;
import org.chromium.debug.core.model.DebugTargetImpl;
import org.chromium.debug.core.model.Destructable;
import org.chromium.debug.core.model.DestructingGuard;
import org.chromium.debug.core.model.JavascriptVmEmbedder;
import org.chromium.debug.core.model.JavascriptVmEmbedderFactory;
import org.chromium.debug.core.model.NamedConnectionLoggerFactory;
import org.chromium.debug.ui.ChromiumDebugUIPlugin;
import org.chromium.debug.ui.DialogBasedTabSelector;
import org.chromium.debug.ui.PluginUtil;
import org.chromium.sdk.ConnectionLogger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;

/**
 * A launch configuration delegate for the JavaScript debugging.
 */
public abstract class LaunchType implements ILaunchConfigurationDelegate {

  // TODO(peter.rybin): move this class out into a package
  public static class Chromium extends LaunchType {
    @Override
    protected JavascriptVmEmbedder.ConnectionToRemote createConnectionToRemote(int port,
        ILaunch launch, boolean addConsoleLogger) throws CoreException {
      NamedConnectionLoggerFactory consoleFactory =
          addConsoleLogger ? getLoggerFactory() : NO_CONNECTION_LOGGER_FACTORY;
      return JavascriptVmEmbedderFactory.connectToChromeDevTools(port, consoleFactory,
          new DialogBasedTabSelector());
    }

    private static NamedConnectionLoggerFactory loggerFactory = null;

    private synchronized static NamedConnectionLoggerFactory getLoggerFactory()
        throws CoreException {
      if (loggerFactory == null) {
        loggerFactory = new ConnectionLoggerFactoryImpl();
      }
      return loggerFactory;
    }

    /**
     * This thing is responsible for creating a separate launch that holds
     * logger console pseudo-projects.
     * TODO(peter.rybin): these projects stay as zombies under the launch; fix it
     */
    private static class ConnectionLoggerFactoryImpl implements NamedConnectionLoggerFactory {
      private final ILaunch commonLaunch;

      /**
       * Here we create launch configuration and launch, which will hold console pseudo-process.
       * This is a bit messy, because ILaunchManager mostly supports user creation of new
       * configurations in UI.
       */
      public ConnectionLoggerFactoryImpl() throws CoreException {
        String location = Messages.LaunchType_LogConsoleLaunchName + "." + //$NON-NLS-1$
            ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION;
        String memento =
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + //$NON-NLS-1$
            "<launchConfiguration " + //$NON-NLS-1$
            "local=\"true\" path=\"" + location + "\"/>"; //$NON-NLS-1$ //$NON-NLS-2$

        ILaunchConfiguration configuration =
            DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(memento);
        try {
          initializeConfigurationFile(configuration.getLocation());
        } catch (IOException e) {
          throw new CoreException(new Status(IStatus.ERROR, ChromiumDebugUIPlugin.PLUGIN_ID,
              "Failed to create launch configuration file", e)); //$NON-NLS-1$
        }
        commonLaunch = new Launch(configuration, ILaunchManager.DEBUG_MODE, null);
      }

      public ConnectionLogger createLogger(String title) {
        return LaunchType.createConsoleAndLogger(commonLaunch, true, title);
      }

      /**
       * Creates file so that configuration could be read from some location.
       */
      private void initializeConfigurationFile(IPath configurationPath) throws IOException {
        String osPath = configurationPath.toOSString();
        File file = new File(osPath);
        synchronized (this) {
          Writer writer = new OutputStreamWriter(new FileOutputStream(file));
          writer.write(
              "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + //$NON-NLS-1$
              "<launchConfiguration " + //$NON-NLS-1$
              "type=\"org.chromium.debug.ui.ConsolePseudoConfigurationType\"/>"); //$NON-NLS-1$
          writer.close();
        }
      }
    }
  }

  public static class StandaloneV8 extends LaunchType {
    @Override
    protected JavascriptVmEmbedder.ConnectionToRemote createConnectionToRemote(int port,
        final ILaunch launch, boolean addConsoleLogger) {
      NamedConnectionLoggerFactory consoleFactory;
{'fraction_non_alphanumeric': 0.09141940657578188, 'fraction_numerical': 0.003508420208500401, 'mean_word_length': 3.580808080808081, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 3, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '833258', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2631, 'n_words': 625}