protected String resolveLaunchUrl(
      IResourceResolver resourceResolver, SDBGLaunchConfigWrapper launchConfig)
      throws CoreException {
    String url;

    if (launchConfig.getShouldLaunchFile()) {
      IResource resource = launchConfig.getApplicationResource();
      if (resource == null) {
        throw new CoreException(
            new Status(
                IStatus.ERROR, SDBGDebugCorePlugin.PLUGIN_ID, "HTML file could not be found"));
      }

      if (resource instanceof IFile) {
        url = resourceResolver.getUrlForResource(resource);
      } else {
        url = resource.getLocationURI().toString();
      }
    } else {
      url = launchConfig.getUrl();
    }

    return launchConfig.appendQueryParams(url);
  }
Ejemplo n.º 2
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;
    }
  }