Made flipper plugins a little more robust

Summary: Added some assertions and string casts to make plugins a bit more robust

Reviewed By: passy

Differential Revision: D19427909

fbshipit-source-id: 46a3138805db865b538f745fae25ce1897e35736
This commit is contained in:
Michel Weststrate
2020-01-16 04:45:03 -08:00
committed by Facebook Github Bot
parent db9c41303d
commit 28fd95589f
3 changed files with 40 additions and 12 deletions

View File

@@ -72,10 +72,14 @@ public class FlipperReactNativeJavaScriptPluginManager {
}
public void send(String pluginId, String method, String data) {
FlipperReactNativeJavaScriptPlugin plugin = getPlugin(pluginId);
if (data == null) {
plugin.getConnection().send(method, (FlipperObject) null);
return;
}
// Optimization: throwing raw strings around to the desktop would probably avoid some double
// parsing...
Object parsedData = parseJSON(data);
FlipperReactNativeJavaScriptPlugin plugin = getPlugin(pluginId);
if (parsedData instanceof FlipperArray) {
plugin.getConnection().send(method, (FlipperArray) parsedData);
} else {
@@ -134,14 +138,17 @@ public class FlipperReactNativeJavaScriptPluginManager {
}
private static Object /* FlipperArray | FlipperObject */ parseJSON(String json) {
if (json == null) {
return null;
}
// returns either a FlipperObject or Flipper array, pending the data
try {
JSONTokener tokener = new JSONTokener(json);
if (tokener.nextClean() == '[') {
tokener.back();
char firstChar = tokener.nextClean();
tokener.back();
if (firstChar == '[') {
return new FlipperArray(new JSONArray(tokener));
} else {
tokener.back();
return new FlipperObject(new JSONObject(tokener));
}
} catch (JSONException e) {

View File

@@ -32,12 +32,14 @@ public class FlipperReactNativeJavaScriptReceiver implements FlipperReceiver {
@Override
public void onReceive(FlipperObject params, FlipperResponder responder) throws Exception {
String responderId = manager.createResponderId(responder);
WritableMap eventData = Arguments.createMap();
eventData.putString("plugin", plugin);
eventData.putString("method", method);
eventData.putString("params", params.toJsonString());
eventData.putString("responderId", responderId);
if (responder != null) {
String responderId = manager.createResponderId(responder);
eventData.putString("responderId", responderId);
}
module.sendJSEvent("react-native-flipper-receive-event", eventData);
}
}