Search in sources :

Example 1 with Event

use of org.dartlang.vm.service.element.Event in project flutter-intellij by flutter.

the class ObservatoryActionGroup method debugActiveHelper.

private void debugActiveHelper(@NotNull FlutterApp app, @Nullable InspectorService inspectorService) {
    if (FlutterSettings.getInstance().isOpenInspectorOnAppLaunch()) {
        autoActivateToolWindow();
    }
    final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
    if (!(toolWindowManager instanceof ToolWindowManagerEx)) {
        return;
    }
    final ToolWindow toolWindow = toolWindowManager.getToolWindow(FlutterView.TOOL_WINDOW_ID);
    if (toolWindow == null) {
        return;
    }
    if (isDisplayingEmptyContent()) {
        removeEmptyContent(toolWindow);
    }
    listenForRenderTreeActivations(toolWindow);
    addInspector(app, inspectorService, toolWindow);
    app.getVmService().addVmServiceListener(new VmServiceListenerAdapter() {

        @Override
        public void connectionOpened() {
            onAppChanged(app);
        }

        @Override
        public void received(String streamId, Event event) {
            if (StringUtil.equals(streamId, VmService.EXTENSION_STREAM_ID)) {
                if (StringUtil.equals("Flutter.Frame", event.getExtensionKind())) {
                    handleFlutterFrame(app);
                }
            }
        }

        @Override
        public void connectionClosed() {
            ApplicationManager.getApplication().invokeLater(() -> {
                final ContentManager contentManager = toolWindow.getContentManager();
                onAppChanged(app);
                final PerAppState state = perAppViewState.remove(app);
                if (state != null && state.content != null) {
                    contentManager.removeContent(state.content, true);
                }
                if (perAppViewState.isEmpty()) {
                    // No more applications are running.
                    displayEmptyContent(toolWindow);
                }
            });
        }
    });
    onAppChanged(app);
    app.addStateListener(new FlutterApp.FlutterAppListener() {

        public void notifyAppRestarted() {
            // When we get a restart finishes, queue up a notification to the flutter view
            // actions. We don't notify right away because the new isolate can take a little
            // while to start up. We wait until we get the first frame event, which is
            // enough of an indication that the isolate and flutter framework are initialized
            // enough to receive service calls (for example, calls to restore various framework
            // debugging settings).
            final PerAppState state = getStateForApp(app);
            if (state != null) {
                state.sendRestartNotificationOnNextFrame = true;
            }
        }
    });
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) FlutterApp(io.flutter.run.daemon.FlutterApp) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) ContentManager(com.intellij.ui.content.ContentManager) Event(org.dartlang.vm.service.element.Event) ContentManagerEvent(com.intellij.ui.content.ContentManagerEvent) VmServiceListenerAdapter(io.flutter.utils.VmServiceListenerAdapter) ToolWindowManagerEx(com.intellij.openapi.wm.ex.ToolWindowManagerEx)

Example 2 with Event

use of org.dartlang.vm.service.element.Event in project intellij-plugins by JetBrains.

the class VmServiceBase method processResponse.

/**
   * Process the response from the VM service and forward that response to the consumer associated
   * with the response id.
   */
void processResponse(String jsonText) {
    if (jsonText == null || jsonText.isEmpty()) {
        return;
    }
    // Decode the JSON
    JsonObject json;
    try {
        json = (JsonObject) new JsonParser().parse(jsonText);
    } catch (Exception e) {
        Logging.getLogger().logError("Parse response failed: " + jsonText, e);
        return;
    }
    // Forward events
    JsonElement idElem = json.get(ID);
    if (idElem == null) {
        String method;
        try {
            method = json.get(METHOD).getAsString();
        } catch (Exception e) {
            Logging.getLogger().logError("Event missing " + METHOD, e);
            return;
        }
        if (!"streamNotify".equals(method)) {
            Logging.getLogger().logError("Unknown event " + METHOD + ": " + method);
            return;
        }
        JsonObject params;
        try {
            params = json.get(PARAMS).getAsJsonObject();
        } catch (Exception e) {
            Logging.getLogger().logError("Event missing " + PARAMS, e);
            return;
        }
        String streamId;
        try {
            streamId = params.get(STREAM_ID).getAsString();
        } catch (Exception e) {
            Logging.getLogger().logError("Event missing " + STREAM_ID, e);
            return;
        }
        Event event;
        try {
            event = new Event(params.get(EVENT).getAsJsonObject());
        } catch (Exception e) {
            Logging.getLogger().logError("Event missing " + EVENT, e);
            return;
        }
        forwardEvent(streamId, event);
        return;
    }
    // Get the consumer associated with this response
    String id;
    try {
        id = idElem.getAsString();
    } catch (Exception e) {
        Logging.getLogger().logError("Response missing " + ID, e);
        return;
    }
    Consumer consumer = consumerMap.remove(id);
    if (consumer == null) {
        Logging.getLogger().logError("No consumer associated with " + ID + ": " + id);
        return;
    }
    // Forward the response if the request was successfully executed
    JsonElement resultElem = json.get(RESULT);
    if (resultElem != null) {
        JsonObject result;
        try {
            result = resultElem.getAsJsonObject();
        } catch (Exception e) {
            Logging.getLogger().logError("Response has invalid " + RESULT, e);
            return;
        }
        String responseType;
        try {
            responseType = result.get(TYPE).getAsString();
        } catch (Exception e) {
            Logging.getLogger().logError("Response missing " + TYPE, e);
            return;
        }
        forwardResponse(consumer, responseType, result);
        return;
    }
    // Forward an error if the request failed
    resultElem = json.get(ERROR);
    if (resultElem != null) {
        JsonObject error;
        try {
            error = resultElem.getAsJsonObject();
        } catch (Exception e) {
            Logging.getLogger().logError("Response has invalid " + RESULT, e);
            return;
        }
        consumer.onError(new RPCError(error));
        return;
    }
    Logging.getLogger().logError("Response missing " + RESULT + " and " + ERROR);
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Event(org.dartlang.vm.service.element.Event) RPCError(org.dartlang.vm.service.element.RPCError) URISyntaxException(java.net.URISyntaxException) WebSocketException(de.roderick.weberknecht.WebSocketException) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Aggregations

Event (org.dartlang.vm.service.element.Event)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 ToolWindow (com.intellij.openapi.wm.ToolWindow)1 ToolWindowManager (com.intellij.openapi.wm.ToolWindowManager)1 ToolWindowManagerEx (com.intellij.openapi.wm.ex.ToolWindowManagerEx)1 ContentManager (com.intellij.ui.content.ContentManager)1 ContentManagerEvent (com.intellij.ui.content.ContentManagerEvent)1 WebSocketException (de.roderick.weberknecht.WebSocketException)1 FlutterApp (io.flutter.run.daemon.FlutterApp)1 VmServiceListenerAdapter (io.flutter.utils.VmServiceListenerAdapter)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 RPCError (org.dartlang.vm.service.element.RPCError)1