|
7 | 7 |
|
8 | 8 | import com.google.common.base.Stopwatch; |
9 | 9 | import com.google.gson.JsonObject; |
| 10 | +import com.google.gson.JsonPrimitive; |
10 | 11 | import com.intellij.execution.ExecutionException; |
11 | 12 | import com.intellij.execution.configurations.GeneralCommandLine; |
12 | 13 | import com.intellij.execution.process.ProcessAdapter; |
|
27 | 28 | import com.intellij.openapi.util.text.StringUtil; |
28 | 29 | import com.intellij.util.EventDispatcher; |
29 | 30 | import com.intellij.util.concurrency.AppExecutorUtil; |
| 31 | +import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService; |
| 32 | +import de.roderick.weberknecht.WebSocketException; |
30 | 33 | import io.flutter.FlutterMessages; |
31 | 34 | import io.flutter.FlutterUtils; |
32 | 35 | import io.flutter.ObservatoryConnector; |
33 | 36 | import io.flutter.bazel.Workspace; |
34 | 37 | import io.flutter.bazel.WorkspaceCache; |
| 38 | +import io.flutter.dart.DtdUtils; |
35 | 39 | import io.flutter.dart.FlutterDartAnalysisServer; |
36 | 40 | import io.flutter.logging.FlutterConsoleLogManager; |
| 41 | +import io.flutter.logging.PluginLogger; |
37 | 42 | import io.flutter.run.FlutterDebugProcess; |
38 | 43 | import io.flutter.run.FlutterDevice; |
39 | 44 | import io.flutter.run.FlutterLaunchMode; |
@@ -677,10 +682,13 @@ public void dispose() { |
677 | 682 | * Listens for events while running or debugging an app. |
678 | 683 | */ |
679 | 684 | 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); |
681 | 686 |
|
682 | 687 | private final @NotNull FlutterApp app; |
683 | 688 | private final @NotNull ProgressHelper progress; |
| 689 | + private String appVmServiceUri; |
| 690 | + private final DtdUtils dtdUtils = new DtdUtils(); |
| 691 | + |
684 | 692 |
|
685 | 693 | FlutterAppDaemonEventListener(@NotNull FlutterApp app, @NotNull Project project) { |
686 | 694 | this.app = app; |
@@ -738,6 +746,12 @@ public void onAppStarting(DaemonEvent.AppStarting event) { |
738 | 746 | @Override |
739 | 747 | public void onAppDebugPort(@NotNull DaemonEvent.AppDebugPort debugInfo) { |
740 | 748 | 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); |
741 | 755 |
|
742 | 756 | // Print the conneciton info to the console. |
743 | 757 | final ConsoleView console = app.getConsole(); |
@@ -804,7 +818,36 @@ public void onAppStopped(@NotNull DaemonEvent.AppStopped stopped) { |
804 | 818 | if (stopped.error != null && app.getConsole() != null) { |
805 | 819 | app.getConsole().print("Finished with error: " + stopped.error + "\n", ConsoleViewContentType.ERROR_OUTPUT); |
806 | 820 | } |
| 821 | + |
| 822 | + final JsonObject params = new JsonObject(); |
| 823 | + params.addProperty("uri", appVmServiceUri); |
| 824 | + sendDtdRequest("ConnectedApp.unregisterVmService", params); |
| 825 | + |
807 | 826 | progress.cancel(); |
808 | 827 | app.getProcessHandler().destroyProcess(); |
809 | 828 | } |
| 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 | + } |
810 | 853 | } |
0 commit comments