2019-02-08 23:50:24 +00:00
|
|
|
// This file is generated by DispatcherBase_cpp.template.
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
//#include "DispatcherBase.h"
|
|
|
|
//#include "Parser.h"
|
|
|
|
|
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
namespace {{namespace}} {
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
// static
|
2016-11-01 22:33:05 +00:00
|
|
|
DispatchResponse DispatchResponse::OK()
|
|
|
|
{
|
|
|
|
DispatchResponse result;
|
|
|
|
result.m_status = kSuccess;
|
|
|
|
result.m_errorCode = kParseError;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
DispatchResponse DispatchResponse::Error(const String& error)
|
|
|
|
{
|
|
|
|
DispatchResponse result;
|
|
|
|
result.m_status = kError;
|
|
|
|
result.m_errorCode = kServerError;
|
|
|
|
result.m_errorMessage = error;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
DispatchResponse DispatchResponse::InternalError()
|
|
|
|
{
|
|
|
|
DispatchResponse result;
|
|
|
|
result.m_status = kError;
|
|
|
|
result.m_errorCode = kInternalError;
|
|
|
|
result.m_errorMessage = "Internal error";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:57:03 +00:00
|
|
|
// static
|
|
|
|
DispatchResponse DispatchResponse::InvalidParams(const String& error)
|
|
|
|
{
|
|
|
|
DispatchResponse result;
|
|
|
|
result.m_status = kError;
|
|
|
|
result.m_errorCode = kInvalidParams;
|
|
|
|
result.m_errorMessage = error;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
// static
|
|
|
|
DispatchResponse DispatchResponse::FallThrough()
|
|
|
|
{
|
|
|
|
DispatchResponse result;
|
|
|
|
result.m_status = kFallThrough;
|
|
|
|
result.m_errorCode = kParseError;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters";
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(dispatcher) { }
|
|
|
|
|
|
|
|
DispatcherBase::WeakPtr::~WeakPtr()
|
|
|
|
{
|
|
|
|
if (m_dispatcher)
|
|
|
|
m_dispatcher->m_weakPtrs.erase(this);
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message)
|
2016-10-28 18:20:08 +00:00
|
|
|
: m_backendImpl(std::move(backendImpl))
|
2016-11-22 18:57:03 +00:00
|
|
|
, m_callId(callId)
|
2018-07-25 00:17:11 +00:00
|
|
|
, m_method(method)
|
|
|
|
, m_message(message) { }
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
DispatcherBase::Callback::~Callback() = default;
|
|
|
|
|
|
|
|
void DispatcherBase::Callback::dispose()
|
|
|
|
{
|
|
|
|
m_backendImpl = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
if (!m_backendImpl || !m_backendImpl->get())
|
|
|
|
return;
|
2016-11-01 22:33:05 +00:00
|
|
|
m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMessage));
|
2016-10-28 18:20:08 +00:00
|
|
|
m_backendImpl = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:57:03 +00:00
|
|
|
void DispatcherBase::Callback::fallThroughIfActive()
|
|
|
|
{
|
|
|
|
if (!m_backendImpl || !m_backendImpl->get())
|
|
|
|
return;
|
2018-07-25 00:17:11 +00:00
|
|
|
m_backendImpl->get()->channel()->fallThrough(m_callId, m_method, m_message);
|
2016-11-22 18:57:03 +00:00
|
|
|
m_backendImpl = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel)
|
2018-07-25 00:17:11 +00:00
|
|
|
: m_frontendChannel(frontendChannel) { }
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
DispatcherBase::~DispatcherBase()
|
|
|
|
{
|
|
|
|
clearFrontend();
|
|
|
|
}
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
void DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
if (!m_frontendChannel)
|
|
|
|
return;
|
2016-11-01 22:33:05 +00:00
|
|
|
if (response.status() == DispatchResponse::kError) {
|
|
|
|
reportProtocolError(callId, response.errorCode(), response.errorMessage(), nullptr);
|
2016-10-28 18:20:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-23 00:45:30 +00:00
|
|
|
m_frontendChannel->sendProtocolResponse(callId, InternalResponse::createResponse(callId, std::move(result)));
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
void DispatcherBase::sendResponse(int callId, const DispatchResponse& response)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-01 22:33:05 +00:00
|
|
|
sendResponse(callId, response, DictionaryValue::create());
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ProtocolError : public Serializable {
|
|
|
|
public:
|
|
|
|
static std::unique_ptr<ProtocolError> createErrorResponse(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
|
|
|
|
{
|
|
|
|
std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, errorMessage));
|
|
|
|
protocolError->m_callId = callId;
|
|
|
|
protocolError->m_hasCallId = true;
|
|
|
|
if (errors && errors->hasErrors())
|
|
|
|
protocolError->m_data = errors->errors();
|
|
|
|
return protocolError;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<ProtocolError> createErrorNotification(DispatchResponse::ErrorCode code, const String& errorMessage)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessage));
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
String serializeToJSON() override
|
2016-11-23 00:45:30 +00:00
|
|
|
{
|
2019-02-12 19:00:13 +00:00
|
|
|
return serialize()->serializeToJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> serializeToBinary() override
|
|
|
|
{
|
|
|
|
return serialize()->serializeToBinary();
|
2016-11-23 00:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ProtocolError() override {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage)
|
|
|
|
: m_code(code)
|
|
|
|
, m_errorMessage(errorMessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
std::unique_ptr<DictionaryValue> serialize() {
|
|
|
|
std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create();
|
|
|
|
error->setInteger("code", m_code);
|
|
|
|
error->setString("message", m_errorMessage);
|
|
|
|
if (m_data.length())
|
|
|
|
error->setString("data", m_data);
|
|
|
|
std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create();
|
|
|
|
message->setObject("error", std::move(error));
|
|
|
|
if (m_hasCallId)
|
|
|
|
message->setInteger("id", m_callId);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
DispatchResponse::ErrorCode m_code;
|
|
|
|
String m_errorMessage;
|
|
|
|
String m_data;
|
|
|
|
int m_callId = 0;
|
|
|
|
bool m_hasCallId = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
if (frontendChannel)
|
|
|
|
frontendChannel->sendProtocolResponse(callId, ProtocolError::createErrorResponse(callId, code, errorMessage, errors));
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResponse::ErrorCode code, const String& errorMessage)
|
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
if (frontendChannel)
|
|
|
|
frontendChannel->sendProtocolNotification(ProtocolError::createErrorNotification(code, errorMessage));
|
2016-11-01 22:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DispatcherBase::clearFrontend()
|
|
|
|
{
|
|
|
|
m_frontendChannel = nullptr;
|
|
|
|
for (auto& weak : m_weakPtrs)
|
|
|
|
weak->dispose();
|
|
|
|
m_weakPtrs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr()
|
|
|
|
{
|
|
|
|
std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(this));
|
|
|
|
m_weakPtrs.insert(weak.get());
|
|
|
|
return weak;
|
|
|
|
}
|
|
|
|
|
|
|
|
UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel)
|
2018-07-25 00:17:11 +00:00
|
|
|
: m_frontendChannel(frontendChannel) { }
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase> dispatcher)
|
|
|
|
{
|
|
|
|
m_dispatchers[name] = std::move(dispatcher);
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:24:20 +00:00
|
|
|
void UberDispatcher::setupRedirects(const std::unordered_map<String, String>& redirects)
|
2017-10-11 21:57:46 +00:00
|
|
|
{
|
|
|
|
for (const auto& pair : redirects)
|
|
|
|
m_redirects[pair.first] = pair.second;
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:17:11 +00:00
|
|
|
bool UberDispatcher::parseCommand(Value* parsedMessage, int* outCallId, String* outMethod) {
|
2016-11-01 22:33:05 +00:00
|
|
|
if (!parsedMessage) {
|
|
|
|
reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError, "Message must be a valid JSON");
|
2018-07-25 00:17:11 +00:00
|
|
|
return false;
|
2016-11-01 22:33:05 +00:00
|
|
|
}
|
2018-07-25 00:17:11 +00:00
|
|
|
protocol::DictionaryValue* messageObject = DictionaryValue::cast(parsedMessage);
|
2016-11-01 22:33:05 +00:00
|
|
|
if (!messageObject) {
|
|
|
|
reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must be an object");
|
2018-07-25 00:17:11 +00:00
|
|
|
return false;
|
2016-11-01 22:33:05 +00:00
|
|
|
}
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
int callId = 0;
|
|
|
|
protocol::Value* callIdValue = messageObject->get("id");
|
|
|
|
bool success = callIdValue && callIdValue->asInteger(&callId);
|
2016-11-01 22:33:05 +00:00
|
|
|
if (!success) {
|
2017-12-06 17:30:15 +00:00
|
|
|
reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must have integer 'id' property");
|
2018-07-25 00:17:11 +00:00
|
|
|
return false;
|
2016-11-01 22:33:05 +00:00
|
|
|
}
|
2018-07-25 00:17:11 +00:00
|
|
|
if (outCallId)
|
|
|
|
*outCallId = callId;
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
protocol::Value* methodValue = messageObject->get("method");
|
|
|
|
String method;
|
|
|
|
success = methodValue && methodValue->asString(&method);
|
2016-11-01 22:33:05 +00:00
|
|
|
if (!success) {
|
2017-12-06 17:30:15 +00:00
|
|
|
reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInvalidRequest, "Message must have string 'method' property", nullptr);
|
2018-07-25 00:17:11 +00:00
|
|
|
return false;
|
2016-11-01 22:33:05 +00:00
|
|
|
}
|
2018-07-25 00:17:11 +00:00
|
|
|
if (outMethod)
|
|
|
|
*outMethod = method;
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-11 21:57:46 +00:00
|
|
|
|
2018-07-25 00:17:11 +00:00
|
|
|
protocol::DispatcherBase* UberDispatcher::findDispatcher(const String& method) {
|
2017-02-03 22:17:24 +00:00
|
|
|
size_t dotIndex = StringUtil::find(method, ".");
|
2018-07-25 00:17:11 +00:00
|
|
|
if (dotIndex == StringUtil::kNotFound)
|
|
|
|
return nullptr;
|
2016-10-28 18:20:08 +00:00
|
|
|
String domain = StringUtil::substring(method, 0, dotIndex);
|
|
|
|
auto it = m_dispatchers.find(domain);
|
2018-07-25 00:17:11 +00:00
|
|
|
if (it == m_dispatchers.end())
|
|
|
|
return nullptr;
|
|
|
|
if (!it->second->canDispatch(method))
|
|
|
|
return nullptr;
|
|
|
|
return it->second.get();
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 00:14:53 +00:00
|
|
|
bool UberDispatcher::canDispatch(const String& in_method)
|
2017-12-06 17:30:15 +00:00
|
|
|
{
|
2018-10-24 00:14:53 +00:00
|
|
|
String method = in_method;
|
|
|
|
auto redirectIt = m_redirects.find(method);
|
|
|
|
if (redirectIt != m_redirects.end())
|
|
|
|
method = redirectIt->second;
|
2018-07-25 00:17:11 +00:00
|
|
|
return !!findDispatcher(method);
|
|
|
|
}
|
2017-12-06 17:30:15 +00:00
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const ProtocolMessage& rawMessage)
|
2018-07-25 00:17:11 +00:00
|
|
|
{
|
2018-10-24 00:14:53 +00:00
|
|
|
String method = in_method;
|
|
|
|
auto redirectIt = m_redirects.find(method);
|
|
|
|
if (redirectIt != m_redirects.end())
|
|
|
|
method = redirectIt->second;
|
2018-07-25 00:17:11 +00:00
|
|
|
protocol::DispatcherBase* dispatcher = findDispatcher(method);
|
|
|
|
if (!dispatcher) {
|
|
|
|
reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr);
|
|
|
|
return;
|
2017-12-06 17:30:15 +00:00
|
|
|
}
|
2018-07-25 00:17:11 +00:00
|
|
|
std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(std::move(parsedMessage));
|
|
|
|
dispatcher->dispatch(callId, method, rawMessage, std::move(messageObject));
|
2017-12-06 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
UberDispatcher::~UberDispatcher() = default;
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
// static
|
|
|
|
std::unique_ptr<InternalResponse> InternalResponse::createResponse(int callId, std::unique_ptr<Serializable> params)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<InternalResponse>(new InternalResponse(callId, String(), std::move(params)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::unique_ptr<InternalResponse> InternalResponse::createNotification(const String& notification, std::unique_ptr<Serializable> params)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<InternalResponse>(new InternalResponse(0, notification, std::move(params)));
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
String InternalResponse::serializeToJSON()
|
|
|
|
{
|
|
|
|
std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
|
|
|
|
std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
|
|
|
|
if (m_notification.length()) {
|
|
|
|
result->setString("method", m_notification);
|
|
|
|
result->setValue("params", SerializedValue::fromJSON(params->serializeToJSON()));
|
|
|
|
} else {
|
|
|
|
result->setInteger("id", m_callId);
|
|
|
|
result->setValue("result", SerializedValue::fromJSON(params->serializeToJSON()));
|
|
|
|
}
|
|
|
|
return result->serializeToJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> InternalResponse::serializeToBinary()
|
2016-11-23 00:45:30 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
|
|
|
|
std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
|
|
|
|
if (m_notification.length()) {
|
|
|
|
result->setString("method", m_notification);
|
2019-02-12 19:00:13 +00:00
|
|
|
result->setValue("params", SerializedValue::fromBinary(params->serializeToBinary()));
|
2016-11-23 00:45:30 +00:00
|
|
|
} else {
|
|
|
|
result->setInteger("id", m_callId);
|
2019-02-12 19:00:13 +00:00
|
|
|
result->setValue("result", SerializedValue::fromBinary(params->serializeToBinary()));
|
2016-11-23 00:45:30 +00:00
|
|
|
}
|
2019-02-12 19:00:13 +00:00
|
|
|
return result->serializeToBinary();
|
2016-11-23 00:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InternalResponse::InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params)
|
|
|
|
: m_callId(callId)
|
|
|
|
, m_notification(notification)
|
|
|
|
, m_params(params ? std::move(params) : nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
} // namespace {{namespace}}
|
|
|
|
{% endfor %}
|