2018-05-31 22:31:03 +00:00
|
|
|
// Copyright (c) 2004-present, Facebook, Inc.
|
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include "MethodCall.h"
|
|
|
|
|
|
|
|
#include <folly/json.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
#define REQUEST_MODULE_IDS 0
|
|
|
|
#define REQUEST_METHOD_IDS 1
|
|
|
|
#define REQUEST_PARAMSS 2
|
|
|
|
#define REQUEST_CALLID 3
|
|
|
|
|
2017-08-29 10:03:10 +00:00
|
|
|
static const char *errorPrefix = "Malformed calls from JS: ";
|
|
|
|
|
2016-09-19 11:43:09 +00:00
|
|
|
std::vector<MethodCall> parseMethodCalls(folly::dynamic&& jsonData) throw(std::invalid_argument) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (jsonData.isNull()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jsonData.isArray()) {
|
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "input isn't array but ", jsonData.typeName()));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonData.size() < REQUEST_PARAMSS + 1) {
|
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "size == ", jsonData.size()));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 11:43:09 +00:00
|
|
|
auto& moduleIds = jsonData[REQUEST_MODULE_IDS];
|
|
|
|
auto& methodIds = jsonData[REQUEST_METHOD_IDS];
|
|
|
|
auto& params = jsonData[REQUEST_PARAMSS];
|
2016-05-04 02:29:58 +00:00
|
|
|
int callId = -1;
|
|
|
|
|
|
|
|
if (!moduleIds.isArray() || !methodIds.isArray() || !params.isArray()) {
|
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "not all fields are arrays.\n\n", folly::toJson(jsonData)));
|
2016-07-25 19:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (moduleIds.size() != methodIds.size() || moduleIds.size() != params.size()) {
|
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "field sizes are different.\n\n", folly::toJson(jsonData)));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonData.size() > REQUEST_CALLID) {
|
2017-08-18 21:43:28 +00:00
|
|
|
if (!jsonData[REQUEST_CALLID].isNumber()) {
|
2016-05-04 02:29:58 +00:00
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "invalid callId", jsonData[REQUEST_CALLID].typeName()));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2017-08-18 21:43:28 +00:00
|
|
|
callId = jsonData[REQUEST_CALLID].asInt();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MethodCall> methodCalls;
|
|
|
|
for (size_t i = 0; i < moduleIds.size(); i++) {
|
2016-09-19 11:43:09 +00:00
|
|
|
if (!params[i].isArray()) {
|
2016-05-04 02:29:58 +00:00
|
|
|
throw std::invalid_argument(
|
2017-08-29 10:03:10 +00:00
|
|
|
folly::to<std::string>(errorPrefix, "method arguments isn't array but ", params[i].typeName()));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
methodCalls.emplace_back(
|
2017-08-18 21:43:28 +00:00
|
|
|
moduleIds[i].asInt(),
|
|
|
|
methodIds[i].asInt(),
|
2016-09-19 11:43:09 +00:00
|
|
|
std::move(params[i]),
|
2016-05-04 02:29:58 +00:00
|
|
|
callId);
|
|
|
|
|
|
|
|
// only incremement callid if contains valid callid as callid is optional
|
|
|
|
callId += (callId != -1) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
|