Expose a send method with a string params

Summary:
For C++, folly::dynamic is used throughout.

On iOS and Android though, Flipper goes through multiple conversions to get to a folly::dynamic only to ultimately obtain a JSON string from it.

Let's take a look at Android:

There are multiple types like FlipperObject, FlipperArray that wrap around a JSONObject.

When data needs to be sent:
1. The JSONObject is asked for its string representation.
2. The string representation is then parsed by folly to construct the folly::dynamic instance.
3. The step above involves an extra boundary cross through JNI.
4. Ultimately, a socket or ws connection does not understand folly::dynamic so we then get a JSON string representation from it.
5. Data is sent.

As described above, for big enough objects, this represents an issue.

So, the idea of this change, is to allow plugins to send a JSON string instead. This will remove a few serialisation/deserialisation steps from the process.

*Note: this API is not currently used by anything so there's no impact to existing plugins.*

Changelog: expose a send method that accept a string as params

Reviewed By: LukeDefeo

Differential Revision: D38741582

fbshipit-source-id: 78e0acd80fc8c97378ee986cbaf377078996ed60
This commit is contained in:
Lorenzo Blasa
2022-08-17 09:18:20 -07:00
committed by Facebook GitHub Bot
parent 2833958488
commit c2ed2484d9
11 changed files with 89 additions and 1 deletions

View File

@@ -514,6 +514,7 @@ class JFlipperConnectionImpl
registerHybrid({
makeNativeMethod("sendObject", JFlipperConnectionImpl::sendObject),
makeNativeMethod("sendArray", JFlipperConnectionImpl::sendArray),
makeNativeMethod("sendString", JFlipperConnectionImpl::sendString),
makeNativeMethod("reportError", JFlipperConnectionImpl::reportError),
makeNativeMethod(
"reportErrorWithMetadata",
@@ -522,6 +523,10 @@ class JFlipperConnectionImpl
});
}
void sendString(const std::string method, const std::string params) {
_connection->send(std::move(method), std::move(params));
}
void sendObject(
const std::string method,
jni::alias_ref<JFlipperObject> json) {

View File

@@ -40,10 +40,17 @@ class FlipperConnectionImpl implements FlipperConnection {
sendArray(method, params);
}
@Override
public void send(String method, String params) {
sendString(method, params);
}
public native void sendObject(String method, FlipperObject params);
public native void sendArray(String method, FlipperArray params);
public native void sendString(String method, String params);
@Override
public native void reportErrorWithMetadata(String reason, String stackTrace);

View File

@@ -26,6 +26,12 @@ public interface FlipperConnection {
*/
void send(String method, FlipperArray params);
/**
* Call a remote method on the Flipper desktop application, passing an optional JSON string as a
* parameter.
*/
void send(String method, String message);
/** Report client error with reason and stacktrace as an argument */
void reportErrorWithMetadata(String reason, String stackTrace);

View File

@@ -47,6 +47,19 @@ public class FlipperConnectionMock implements FlipperConnection {
paramList.add(params);
}
@Override
public void send(String method, String params) {
final List<Object> paramList;
if (sent.containsKey(method)) {
paramList = sent.get(method);
} else {
paramList = new ArrayList<>();
sent.put(method, paramList);
}
paramList.add(params);
}
@Override
public void reportErrorWithMetadata(String reason, String stackTrace) {
errors.add(new Throwable(reason));