Skip to content

Commit 35796f6

Browse files
authored
Register VM service with DTD (#8436)
Addresses #8261
1 parent 81509f8 commit 35796f6

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/io/flutter/run/daemon/FlutterApp.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.common.base.Stopwatch;
99
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonPrimitive;
1011
import com.intellij.execution.ExecutionException;
1112
import com.intellij.execution.configurations.GeneralCommandLine;
1213
import com.intellij.execution.process.ProcessAdapter;
@@ -27,13 +28,17 @@
2728
import com.intellij.openapi.util.text.StringUtil;
2829
import com.intellij.util.EventDispatcher;
2930
import com.intellij.util.concurrency.AppExecutorUtil;
31+
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
32+
import de.roderick.weberknecht.WebSocketException;
3033
import io.flutter.FlutterMessages;
3134
import io.flutter.FlutterUtils;
3235
import io.flutter.ObservatoryConnector;
3336
import io.flutter.bazel.Workspace;
3437
import io.flutter.bazel.WorkspaceCache;
38+
import io.flutter.dart.DtdUtils;
3539
import io.flutter.dart.FlutterDartAnalysisServer;
3640
import io.flutter.logging.FlutterConsoleLogManager;
41+
import io.flutter.logging.PluginLogger;
3742
import io.flutter.run.FlutterDebugProcess;
3843
import io.flutter.run.FlutterDevice;
3944
import io.flutter.run.FlutterLaunchMode;
@@ -677,10 +682,13 @@ public void dispose() {
677682
* Listens for events while running or debugging an app.
678683
*/
679684
class FlutterAppDaemonEventListener implements DaemonEvent.Listener {
680-
private static final @NotNull Logger LOG = Logger.getInstance(FlutterAppDaemonEventListener.class);
685+
private static final @NotNull Logger LOG = PluginLogger.createLogger(FlutterAppDaemonEventListener.class);
681686

682687
private final @NotNull FlutterApp app;
683688
private final @NotNull ProgressHelper progress;
689+
private String appVmServiceUri;
690+
private final DtdUtils dtdUtils = new DtdUtils();
691+
684692

685693
FlutterAppDaemonEventListener(@NotNull FlutterApp app, @NotNull Project project) {
686694
this.app = app;
@@ -738,6 +746,12 @@ public void onAppStarting(DaemonEvent.AppStarting event) {
738746
@Override
739747
public void onAppDebugPort(@NotNull DaemonEvent.AppDebugPort debugInfo) {
740748
app.setWsUrl(debugInfo.wsUri);
749+
this.appVmServiceUri = debugInfo.wsUri;
750+
751+
final JsonObject params = new JsonObject();
752+
params.addProperty("uri", debugInfo.wsUri);
753+
params.addProperty("name", debugInfo.appId);
754+
sendDtdRequest("ConnectedApp.registerVmService", params);
741755

742756
// Print the conneciton info to the console.
743757
final ConsoleView console = app.getConsole();
@@ -804,7 +818,36 @@ public void onAppStopped(@NotNull DaemonEvent.AppStopped stopped) {
804818
if (stopped.error != null && app.getConsole() != null) {
805819
app.getConsole().print("Finished with error: " + stopped.error + "\n", ConsoleViewContentType.ERROR_OUTPUT);
806820
}
821+
822+
final JsonObject params = new JsonObject();
823+
params.addProperty("uri", appVmServiceUri);
824+
sendDtdRequest("ConnectedApp.unregisterVmService", params);
825+
807826
progress.cancel();
808827
app.getProcessHandler().destroyProcess();
809828
}
829+
830+
private void sendDtdRequest(@NotNull String requestName, @NotNull JsonObject params) {
831+
try {
832+
final DartToolingDaemonService dtdService = dtdUtils.readyDtdService(app.getProject()).get();
833+
if (dtdService == null) {
834+
return;
835+
}
836+
// This removes secret from params when we print out after receiving response.
837+
JsonObject initialParams = params.deepCopy();
838+
dtdService.sendRequest(requestName, params, true, object -> {
839+
final JsonObject result = object.getAsJsonObject("result");
840+
final JsonPrimitive type = result != null ? result.getAsJsonPrimitive("type") : null;
841+
if (type != null && "Success".equals(type.getAsString())) {
842+
LOG.info("Successful request " + requestName + " to DTD with params: " + initialParams);
843+
} else {
844+
LOG.warn("Failed request " + requestName + "to DTD with params: " + initialParams);
845+
LOG.warn("Result: " + result);
846+
}
847+
});
848+
}
849+
catch (InterruptedException | java.util.concurrent.ExecutionException | WebSocketException e) {
850+
LOG.error("Exception while sending DTD request", e);
851+
}
852+
}
810853
}

0 commit comments

Comments
 (0)