Search in sources :

Example 1 with JsonRpcRequest

use of com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcRequest in project stetho by facebook.

the class JsonRpcPeer method invokeMethod.

public void invokeMethod(String method, Object paramsObject, @Nullable PendingRequestCallback callback) throws NotYetConnectedException {
    Util.throwIfNull(method);
    Long requestId = (callback != null) ? preparePendingRequest(callback) : null;
    // magic, can basically convert anything for some amount of runtime overhead...
    JSONObject params = mObjectMapper.convertValue(paramsObject, JSONObject.class);
    JsonRpcRequest message = new JsonRpcRequest(requestId, method, params);
    String requestString;
    JSONObject jsonObject = mObjectMapper.convertValue(message, JSONObject.class);
    requestString = jsonObject.toString();
    mPeer.sendText(requestString);
}
Also used : JSONObject(org.json.JSONObject) JsonRpcRequest(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcRequest)

Example 2 with JsonRpcRequest

use of com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcRequest in project stetho by facebook.

the class ChromeDevtoolsServer method handleRemoteRequest.

private void handleRemoteRequest(JsonRpcPeer peer, JSONObject requestNode) throws MessageHandlingException {
    JsonRpcRequest request;
    request = mObjectMapper.convertValue(requestNode, JsonRpcRequest.class);
    JSONObject result = null;
    JSONObject error = null;
    try {
        result = mMethodDispatcher.dispatch(peer, request.method, request.params);
    } catch (JsonRpcException e) {
        logDispatchException(e);
        error = mObjectMapper.convertValue(e.getErrorMessage(), JSONObject.class);
    }
    if (request.id != null) {
        JsonRpcResponse response = new JsonRpcResponse();
        response.id = request.id;
        response.result = result;
        response.error = error;
        JSONObject jsonObject = mObjectMapper.convertValue(response, JSONObject.class);
        String responseString;
        try {
            responseString = jsonObject.toString();
        } catch (OutOfMemoryError e) {
            // JSONStringer can cause an OOM when the Json to handle is too big.
            response.result = null;
            response.error = mObjectMapper.convertValue(e.getMessage(), JSONObject.class);
            jsonObject = mObjectMapper.convertValue(response, JSONObject.class);
            responseString = jsonObject.toString();
        }
        peer.getWebSocket().sendText(responseString);
    }
}
Also used : JsonRpcResponse(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcResponse) JsonRpcRequest(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcRequest) JSONObject(org.json.JSONObject) JsonRpcException(com.facebook.stetho.inspector.jsonrpc.JsonRpcException)

Aggregations

JsonRpcRequest (com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcRequest)2 JSONObject (org.json.JSONObject)2 JsonRpcException (com.facebook.stetho.inspector.jsonrpc.JsonRpcException)1 JsonRpcResponse (com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcResponse)1