Example #1
0
  public WebkitDebugTarget connect(
      ILaunch launch,
      ILaunchConfiguration configuration,
      IResourceResolver resourceResolver,
      IDeviceChooser deviceChooser,
      IBrowserTabChooser browserTabChooser,
      IProgressMonitor monitor)
      throws CoreException {
    try {
      SDBGLaunchConfigWrapper launchConfig = new SDBGLaunchConfigWrapper(configuration);

      LogTimer timer = new LogTimer("Chrome debug connect");

      try {
        timer.startTask("connect");

        try {
          launchConfig.markAsLaunched();

          List<? extends IDeviceInfo> devices = adbManager.getDevices();
          if (devices.isEmpty()) {
            throw new DebugException(
                new Status(
                    IStatus.ERROR,
                    SDBGDebugCorePlugin.PLUGIN_ID,
                    "No USB-attached Android devices found.\n\nPlease make sure you have enabled USB debugging on your device and you have attached it to the PC via USB."));
          }

          IDeviceInfo device = deviceChooser.chooseDevice(devices);
          if (device != null) {
            int port = NetUtils.findUnusedPort(DEVTOOLS_PORT_NUMBER);
            MobileBrowserUtils.addChromiumForward(adbManager, device.getId(), port);

            try {
              final String url = launchConfig.getUrl();
              boolean launchTab =
                  launchConfig.isLaunchTabWithUrl() && url != null && url.length() > 0;

              if (launchTab) {
                MobileBrowserUtils.launchChromeBrowser(adbManager, device.getId());

                IBrowserTabInfo tab =
                    getChromiumTab(
                        null /*runtimeProcess*/,
                        new IBrowserTabChooser() {
                          @Override
                          public IBrowserTabInfo chooseTab(List<? extends IBrowserTabInfo> tabs)
                              throws CoreException {
                            for (IBrowserTabInfo tab : tabs) {
                              if (tab.getUrl() != null
                                  && tab.getUrl().toLowerCase().contains(url.toLowerCase())) {
                                return tab;
                              }
                            }

                            return null;
                          }
                        },
                        "127.0.0.1",
                        port,
                        10 * 1000L /*maxStartupDelay*/,
                        null /*output*/);

                if (tab == null) {
                  MobileBrowserUtils.launchChromeBrowser(
                      adbManager, device.getId(), launchConfig.getUrl());
                }
              }

              return connectToChromiumDebug(
                  "Mobile Chrome Remote Connection",
                  launch,
                  launchConfig,
                  null /*url*/,
                  monitor,
                  null /*runtimeProcess*/,
                  timer,
                  true /*enableBreakpoints*/,
                  "127.0.0.1",
                  port,
                  launchTab ? 10 * 1000L : 0L /*maxStartupDelay*/,
                  null /*browserOutput*/,
                  null /*processDescription*/,
                  resourceResolver,
                  browserTabChooser,
                  true /*remote*/);
            } catch (IOException e) {
              adbManager.removeAllForwards();
              throw new CoreException(
                  new Status(
                      IStatus.ERROR,
                      SDBGDebugCorePlugin.PLUGIN_ID,
                      "Unable to connect to debugger in Chrome: " + e.getMessage(),
                      e));
            } catch (CoreException e) {
              adbManager.removeAllForwards();
              throw e;
            }
          } else {
            throw new DebugException(
                new Status(
                    IStatus.INFO,
                    SDBGDebugCorePlugin.PLUGIN_ID,
                    "No Android device was chosen. Connection cancelled."));
          }
        } finally {
          timer.stopTask();
        }
      } finally {
        timer.stopTimer();
      }
    } catch (CoreException e) {
      DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
      throw e;
    }
  }
Example #2
0
  /**
   * @param launchConfig
   * @param url
   * @param monitor
   * @param enableDebugging
   * @param browserLocation
   * @param browserName
   * @throws CoreException
   */
  private ListeningStream startNewBrowserProcess(
      SDBGLaunchConfigWrapper launchConfig,
      String url,
      IProgressMonitor monitor,
      boolean enableDebugging,
      StringBuilder argDescription,
      List<String> extraArguments,
      int[] devToolsPortNumberHolder)
      throws CoreException {

    Process process = null;
    monitor.worked(1);

    ProcessBuilder builder = new ProcessBuilder();
    Map<String, String> env = builder.environment();

    // Due to differences in 32bit and 64 bit environments, dartium 32bit launch does not work on
    // linux with this property.
    env.remove("LD_LIBRARY_PATH");

    Map<String, String> wrapperEnv = launchConfig.getEnvironment();
    if (!wrapperEnv.isEmpty()) {
      for (String key : wrapperEnv.keySet()) {
        env.put(key, wrapperEnv.get(key));
      }
    }

    int devToolsPortNumber = -1;
    if (enableDebugging) {
      devToolsPortNumber = NetUtils.findUnusedPort(DEVTOOLS_PORT_NUMBER);

      if (devToolsPortNumber == -1) {
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                SDBGDebugCorePlugin.PLUGIN_ID,
                "Unable to locate an available port for the browser debugger"));
      }
    }

    devToolsPortNumberHolder[0] = devToolsPortNumber;
    List<String> arguments =
        buildArgumentsList(
            launchConfig,
            enableDebugging && url != null ? INITIAL_PAGE : url,
            devToolsPortNumber,
            extraArguments);
    builder.command(arguments);
    builder.redirectErrorStream(true);

    describe(arguments, argDescription);

    try {
      process = builder.start();
    } catch (IOException e) {
      SDBGDebugCorePlugin.logError("Exception while starting browser", e);

      throw new CoreException(
          new Status(
              IStatus.ERROR,
              SDBGDebugCorePlugin.PLUGIN_ID,
              "Could not launch browser: " + e.toString()));
    }

    browserProcess = process;

    return readFromProcessPipes(browserProcess.getInputStream());
  }