org.chromium.debug.ui/src/org/chromium/debug/ui/launcher/ChromiumLaunchType.java
changeset 2 e4420d2515f1
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright 2009 Google Inc. All Rights Reserved.
       
     2 
       
     3 package org.chromium.debug.ui.launcher;
       
     4 
       
     5 import org.chromium.debug.core.model.JavascriptVmEmbedder;
       
     6 import org.chromium.debug.core.model.JavascriptVmEmbedderFactory;
       
     7 import org.chromium.debug.core.model.NamedConnectionLoggerFactory;
       
     8 import org.chromium.debug.ui.ChromiumDebugUIPlugin;
       
     9 import org.chromium.debug.ui.DialogBasedTabSelector;
       
    10 import org.chromium.sdk.ConnectionLogger;
       
    11 import org.eclipse.core.runtime.CoreException;
       
    12 import org.eclipse.core.runtime.IPath;
       
    13 import org.eclipse.core.runtime.IStatus;
       
    14 import org.eclipse.core.runtime.Status;
       
    15 import org.eclipse.debug.core.DebugPlugin;
       
    16 import org.eclipse.debug.core.ILaunch;
       
    17 import org.eclipse.debug.core.ILaunchConfiguration;
       
    18 import org.eclipse.debug.core.ILaunchManager;
       
    19 import org.eclipse.debug.core.Launch;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileOutputStream;
       
    23 import java.io.IOException;
       
    24 import java.io.OutputStreamWriter;
       
    25 import java.io.Writer;
       
    26 
       
    27 public class ChromiumLaunchType extends LaunchTypeBase {
       
    28   @Override
       
    29   protected JavascriptVmEmbedder.ConnectionToRemote createConnectionToRemote(int port,
       
    30       ILaunch launch, boolean addConsoleLogger) throws CoreException {
       
    31     NamedConnectionLoggerFactory consoleFactory =
       
    32         addConsoleLogger ? getLoggerFactory() : NO_CONNECTION_LOGGER_FACTORY;
       
    33     return JavascriptVmEmbedderFactory.connectToChromeDevTools(port, consoleFactory,
       
    34         new DialogBasedTabSelector());
       
    35   }
       
    36 
       
    37   private static NamedConnectionLoggerFactory loggerFactory = null;
       
    38 
       
    39   private static synchronized NamedConnectionLoggerFactory getLoggerFactory()
       
    40       throws CoreException {
       
    41     if (loggerFactory == null) {
       
    42       loggerFactory = new ConnectionLoggerFactoryImpl();
       
    43     }
       
    44     return loggerFactory;
       
    45   }
       
    46 
       
    47   /**
       
    48    * This thing is responsible for creating a separate launch that holds
       
    49    * logger console pseudo-projects.
       
    50    * TODO(peter.rybin): these projects stay as zombies under the launch; fix it
       
    51    */
       
    52   private static class ConnectionLoggerFactoryImpl implements NamedConnectionLoggerFactory {
       
    53     private static final String LAUNCH_CONFIGURATION_FILE_CONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + //$NON-NLS-1$
       
    54                 "<launchConfiguration " + //$NON-NLS-1$
       
    55                 "type=\"org.chromium.debug.ui.ConsolePseudoConfigurationType\"/>";
       
    56 
       
    57     private final ILaunch commonLaunch;
       
    58 
       
    59     /**
       
    60      * Here we create launch configuration and launch, which will hold console pseudo-process.
       
    61      * This is a bit messy, because ILaunchManager mostly supports user creation of new
       
    62      * configurations in UI.
       
    63      */
       
    64     public ConnectionLoggerFactoryImpl() throws CoreException {
       
    65       String location = Messages.LaunchType_LogConsoleLaunchName + "." + //$NON-NLS-1$
       
    66           ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION;
       
    67       String memento =
       
    68           "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + //$NON-NLS-1$
       
    69           "<launchConfiguration " + //$NON-NLS-1$
       
    70           "local=\"true\" path=\"" + location + "\"/>"; //$NON-NLS-1$ //$NON-NLS-2$
       
    71 
       
    72       ILaunchConfiguration configuration =
       
    73           DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(memento);
       
    74       try {
       
    75         initializeConfigurationFile(configuration.getLocation());
       
    76       } catch (IOException e) {
       
    77         throw new CoreException(new Status(IStatus.ERROR, ChromiumDebugUIPlugin.PLUGIN_ID,
       
    78             "Failed to create launch configuration file", e)); //$NON-NLS-1$
       
    79       }
       
    80       commonLaunch = new Launch(configuration, ILaunchManager.DEBUG_MODE, null);
       
    81     }
       
    82 
       
    83     public ConnectionLogger createLogger(String title) {
       
    84       return LaunchTypeBase.createConsoleAndLogger(commonLaunch, true, title);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Creates file so that configuration could be read from some location.
       
    89      */
       
    90     private void initializeConfigurationFile(IPath configurationPath) throws IOException {
       
    91       String osPath = configurationPath.toOSString();
       
    92       File file = new File(osPath);
       
    93       synchronized (this) {
       
    94         Writer writer = new OutputStreamWriter(new FileOutputStream(file));
       
    95         writer.write(
       
    96             LAUNCH_CONFIGURATION_FILE_CONTENT); //$NON-NLS-1$
       
    97         writer.close();
       
    98       }
       
    99     }
       
   100   }
       
   101 }