plugins/org.symbian.tools.tmw.debug/src/org/symbian/tools/wrttools/debug/internal/launch/DebugConnectionJob.java
changeset 471 06589bf52fa7
parent 470 d4809db37847
child 472 bd9f2d7c64a6
equal deleted inserted replaced
470:d4809db37847 471:06589bf52fa7
     1 /*******************************************************************************
       
     2  * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  *******************************************************************************/
       
    19 package org.symbian.tools.wrttools.debug.internal.launch;
       
    20 
       
    21 import java.net.URI;
       
    22 
       
    23 import org.chromium.debug.core.model.DebugTargetImpl;
       
    24 import org.chromium.debug.core.model.Destructable;
       
    25 import org.chromium.debug.core.model.DestructingGuard;
       
    26 import org.chromium.debug.core.model.JavascriptVmEmbedder;
       
    27 import org.chromium.debug.core.model.JavascriptVmEmbedder.ConnectionToRemote;
       
    28 import org.chromium.debug.core.model.JavascriptVmEmbedderFactory;
       
    29 import org.chromium.debug.core.model.NamedConnectionLoggerFactory;
       
    30 import org.chromium.debug.core.model.WorkspaceBridge;
       
    31 import org.chromium.sdk.ConnectionLogger;
       
    32 import org.eclipse.core.resources.IProject;
       
    33 import org.eclipse.core.resources.ResourcesPlugin;
       
    34 import org.eclipse.core.runtime.CoreException;
       
    35 import org.eclipse.core.runtime.NullProgressMonitor;
       
    36 import org.eclipse.debug.core.DebugPlugin;
       
    37 import org.eclipse.debug.core.ILaunch;
       
    38 import org.symbian.tools.tmw.previewer.http.IPreviewStartupListener;
       
    39 import org.symbian.tools.wrttools.debug.internal.Activator;
       
    40 
       
    41 public class DebugConnectionJob implements IPreviewStartupListener {
       
    42 	static final NamedConnectionLoggerFactory NO_CONNECTION_LOGGER_FACTORY = new NamedConnectionLoggerFactory() {
       
    43 		public ConnectionLogger createLogger(String title) {
       
    44 			return null;
       
    45 		}
       
    46 	};
       
    47 	private final int port;
       
    48 	private final ILaunch launch;
       
    49 	private final IProject project;
       
    50 
       
    51 	public DebugConnectionJob(IProject project, final int port,
       
    52 			final ILaunch launch) {
       
    53 		if (Activator.DEBUG_CONNECTION) {
       
    54 			System.out.println("Debugging " + project.getName() + " on port "
       
    55 					+ port + ", launch: " + launch.getLaunchConfiguration());
       
    56 		}
       
    57 		this.project = project;
       
    58 		this.port = port;
       
    59 		this.launch = launch;
       
    60 	}
       
    61 
       
    62 	protected ConnectionToRemote createConnectionToRemote(int port,
       
    63 			ILaunch launch, URI uri) throws CoreException {
       
    64 		return JavascriptVmEmbedderFactory.connectToChromeDevTools(port,
       
    65 				NO_CONNECTION_LOGGER_FACTORY, new WidgetTabSelector(uri));
       
    66 	}
       
    67 
       
    68 	private static void terminateTarget(DebugTargetImpl target) {
       
    69 		target.setDisconnected(true);
       
    70 		target.fireTerminateEvent();
       
    71 	}
       
    72 
       
    73     public boolean browserRunning(URI uri, String sId) throws CoreException {
       
    74 		if (Activator.DEBUG_CONNECTION) {
       
    75 			System.out.println("Browser running, connecting @" + hashCode());
       
    76 		}
       
    77 		DestructingGuard destructingGuard = new DestructingGuard();
       
    78 		try {
       
    79 			JavascriptVmEmbedder.ConnectionToRemote remoteServer = createConnectionToRemote(
       
    80 					port, launch, uri);
       
    81 			Destructable lauchDestructor = new Destructable() {
       
    82 				public void destruct() {
       
    83 					if (!launch.hasChildren()) {
       
    84 						DebugPlugin.getDefault().getLaunchManager()
       
    85 								.removeLaunch(launch);
       
    86 					}
       
    87 				}
       
    88 			};
       
    89 			if (Activator.DEBUG_CONNECTION) {
       
    90 				System.out.println("Setting up 1@" + hashCode());
       
    91 			}
       
    92 			destructingGuard.addValue(lauchDestructor);
       
    93 
       
    94 			WorkspaceBridge.Factory bridgeFactory = new WRTProjectWorkspaceBridge.Factory(
       
    95 					project);
       
    96 			final DebugTargetImpl target = new DebugTargetImpl(launch,
       
    97 					bridgeFactory);
       
    98 			if (Activator.DEBUG_CONNECTION) {
       
    99 				System.out.println("Setting up 2@" + hashCode());
       
   100 			}
       
   101 
       
   102 			Destructable targetDestructor = new Destructable() {
       
   103 				public void destruct() {
       
   104 					terminateTarget(target);
       
   105 				}
       
   106 			};
       
   107 			destructingGuard.addValue(targetDestructor);
       
   108 
       
   109 			if (Activator.DEBUG_CONNECTION) {
       
   110 				System.out.println("Setting up 3@" + hashCode());
       
   111 			}
       
   112 			boolean attached = target.attach(remoteServer, destructingGuard,
       
   113 					null, new NullProgressMonitor());
       
   114 			if (Activator.DEBUG_CONNECTION) {
       
   115 				System.out.printf("Is attached: %b, @%d\n", attached, hashCode());
       
   116 			}
       
   117 			if (!attached) {
       
   118 				// Error
       
   119 				return false;
       
   120 			}
       
   121 
       
   122 			launch.addDebugTarget(target);
       
   123             launch.setAttribute("http.service.id", sId);
       
   124 			// All OK
       
   125 			destructingGuard.discharge();
       
   126             addResourceListenerIfNotInstalled();
       
   127 		} catch (CoreException e) {
       
   128 			DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
       
   129 			throw e;
       
   130 		} finally {
       
   131 			destructingGuard.doFinally();
       
   132 		}
       
   133 		return true;
       
   134 	}
       
   135 
       
   136     private static boolean listenerAdded = false;
       
   137 
       
   138     private void addResourceListenerIfNotInstalled() {
       
   139         synchronized (DebugConnectionJob.class) {
       
   140             if (!listenerAdded) {
       
   141                 ResourcesPlugin.getWorkspace().addResourceChangeListener(new ResourcesChangeListener());
       
   142                 listenerAdded = true;
       
   143             }
       
   144         }
       
   145     }
       
   146 }