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
100 lines
2.7 KiB
C++
100 lines
2.7 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <map>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include "FlipperConnection.h"
|
|
#include "FlipperConnectionManager.h"
|
|
#include "Log.h"
|
|
|
|
namespace facebook {
|
|
namespace flipper {
|
|
|
|
class FlipperConnectionImpl : public FlipperConnection {
|
|
public:
|
|
FlipperConnectionImpl(
|
|
FlipperConnectionManager* socket,
|
|
const std::string& name)
|
|
: socket_(socket), name_(name) {}
|
|
|
|
void call(
|
|
const std::string& method,
|
|
const folly::dynamic& params,
|
|
std::shared_ptr<FlipperResponder> responder) {
|
|
if (receivers_.find(method) == receivers_.end()) {
|
|
std::string errorMessage = "Receiver " + method + " not found.";
|
|
log("Error: " + errorMessage);
|
|
responder->error(folly::dynamic::object("message", errorMessage));
|
|
return;
|
|
}
|
|
try {
|
|
receivers_.at(method)(params, responder);
|
|
} catch (const std::exception& ex) {
|
|
std::string errorMessage = "Receiver " + method + " failed with error. ";
|
|
std::string reason = ex.what();
|
|
errorMessage += "Error: '" + reason + "'.";
|
|
log("Error: " + errorMessage);
|
|
responder->error(folly::dynamic::object("message", errorMessage));
|
|
}
|
|
}
|
|
|
|
void send(const std::string& method, const folly::dynamic& params) override {
|
|
folly::dynamic message = folly::dynamic::object("method", "execute")(
|
|
"params",
|
|
folly::dynamic::object("api", name_)("method", method)(
|
|
"params", params));
|
|
socket_->sendMessage(message);
|
|
}
|
|
|
|
void send(const std::string& method, const std::string& params) override {
|
|
std::stringstream ss;
|
|
ss << "{"
|
|
"\"method\": \"execute\","
|
|
"\"params\": {"
|
|
"\"api\": \""
|
|
<< name_
|
|
<< "\","
|
|
"\"method\": \""
|
|
<< method
|
|
<< "\","
|
|
"\"params\":"
|
|
<< params << "}}";
|
|
|
|
socket_->sendMessage(ss.str());
|
|
}
|
|
|
|
void error(const std::string& message, const std::string& stacktrace)
|
|
override {
|
|
socket_->sendMessage(folly::dynamic::object(
|
|
"error",
|
|
folly::dynamic::object("message", message)("stacktrace", stacktrace)));
|
|
}
|
|
|
|
void receive(const std::string& method, const FlipperReceiver& receiver)
|
|
override {
|
|
receivers_[method] = receiver;
|
|
}
|
|
|
|
/**
|
|
Runtime check which receivers are supported for this app
|
|
*/
|
|
bool hasReceiver(const std::string& method) {
|
|
return receivers_.find(method) != receivers_.end();
|
|
}
|
|
|
|
private:
|
|
FlipperConnectionManager* socket_;
|
|
std::string name_;
|
|
std::map<std::string, FlipperReceiver> receivers_;
|
|
};
|
|
|
|
} // namespace flipper
|
|
} // namespace facebook
|