11cb0ef41Sopenharmony_ci// This file is generated by DispatcherBase_cpp.template.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved.
41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci// found in the LICENSE file.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci//#include "DispatcherBase.h"
81cb0ef41Sopenharmony_ci//#include "Parser.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
111cb0ef41Sopenharmony_cinamespace {{namespace}} {
121cb0ef41Sopenharmony_ci{% endfor %}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// static
151cb0ef41Sopenharmony_ciDispatchResponse DispatchResponse::OK()
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci    DispatchResponse result;
181cb0ef41Sopenharmony_ci    result.m_status = kSuccess;
191cb0ef41Sopenharmony_ci    result.m_errorCode = kParseError;
201cb0ef41Sopenharmony_ci    return result;
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// static
241cb0ef41Sopenharmony_ciDispatchResponse DispatchResponse::Error(const String& error)
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci    DispatchResponse result;
271cb0ef41Sopenharmony_ci    result.m_status = kError;
281cb0ef41Sopenharmony_ci    result.m_errorCode = kServerError;
291cb0ef41Sopenharmony_ci    result.m_errorMessage = error;
301cb0ef41Sopenharmony_ci    return result;
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// static
341cb0ef41Sopenharmony_ciDispatchResponse DispatchResponse::InternalError()
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci    DispatchResponse result;
371cb0ef41Sopenharmony_ci    result.m_status = kError;
381cb0ef41Sopenharmony_ci    result.m_errorCode = kInternalError;
391cb0ef41Sopenharmony_ci    result.m_errorMessage = "Internal error";
401cb0ef41Sopenharmony_ci    return result;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// static
441cb0ef41Sopenharmony_ciDispatchResponse DispatchResponse::InvalidParams(const String& error)
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci    DispatchResponse result;
471cb0ef41Sopenharmony_ci    result.m_status = kError;
481cb0ef41Sopenharmony_ci    result.m_errorCode = kInvalidParams;
491cb0ef41Sopenharmony_ci    result.m_errorMessage = error;
501cb0ef41Sopenharmony_ci    return result;
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// static
541cb0ef41Sopenharmony_ciDispatchResponse DispatchResponse::FallThrough()
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci    DispatchResponse result;
571cb0ef41Sopenharmony_ci    result.m_status = kFallThrough;
581cb0ef41Sopenharmony_ci    result.m_errorCode = kParseError;
591cb0ef41Sopenharmony_ci    return result;
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// static
631cb0ef41Sopenharmony_ciconst char DispatcherBase::kInvalidParamsString[] = "Invalid parameters";
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciDispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(dispatcher) { }
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciDispatcherBase::WeakPtr::~WeakPtr()
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci    if (m_dispatcher)
701cb0ef41Sopenharmony_ci        m_dispatcher->m_weakPtrs.erase(this);
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciDispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message)
741cb0ef41Sopenharmony_ci    : m_backendImpl(std::move(backendImpl))
751cb0ef41Sopenharmony_ci    , m_callId(callId)
761cb0ef41Sopenharmony_ci    , m_method(method)
771cb0ef41Sopenharmony_ci    , m_message(message) { }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciDispatcherBase::Callback::~Callback() = default;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_civoid DispatcherBase::Callback::dispose()
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci    m_backendImpl = nullptr;
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_civoid DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response)
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci    if (!m_backendImpl || !m_backendImpl->get())
891cb0ef41Sopenharmony_ci        return;
901cb0ef41Sopenharmony_ci    m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMessage));
911cb0ef41Sopenharmony_ci    m_backendImpl = nullptr;
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_civoid DispatcherBase::Callback::fallThroughIfActive()
951cb0ef41Sopenharmony_ci{
961cb0ef41Sopenharmony_ci    if (!m_backendImpl || !m_backendImpl->get())
971cb0ef41Sopenharmony_ci        return;
981cb0ef41Sopenharmony_ci    m_backendImpl->get()->channel()->fallThrough(m_callId, m_method, m_message);
991cb0ef41Sopenharmony_ci    m_backendImpl = nullptr;
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciDispatcherBase::DispatcherBase(FrontendChannel* frontendChannel)
1031cb0ef41Sopenharmony_ci    : m_frontendChannel(frontendChannel) { }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciDispatcherBase::~DispatcherBase()
1061cb0ef41Sopenharmony_ci{
1071cb0ef41Sopenharmony_ci    clearFrontend();
1081cb0ef41Sopenharmony_ci}
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_civoid DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result)
1111cb0ef41Sopenharmony_ci{
1121cb0ef41Sopenharmony_ci    if (!m_frontendChannel)
1131cb0ef41Sopenharmony_ci        return;
1141cb0ef41Sopenharmony_ci    if (response.status() == DispatchResponse::kError) {
1151cb0ef41Sopenharmony_ci        reportProtocolError(callId, response.errorCode(), response.errorMessage(), nullptr);
1161cb0ef41Sopenharmony_ci        return;
1171cb0ef41Sopenharmony_ci    }
1181cb0ef41Sopenharmony_ci    m_frontendChannel->sendProtocolResponse(callId, InternalResponse::createResponse(callId, std::move(result)));
1191cb0ef41Sopenharmony_ci}
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_civoid DispatcherBase::sendResponse(int callId, const DispatchResponse& response)
1221cb0ef41Sopenharmony_ci{
1231cb0ef41Sopenharmony_ci    sendResponse(callId, response, DictionaryValue::create());
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_cinamespace {
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciclass ProtocolError : public Serializable {
1291cb0ef41Sopenharmony_cipublic:
1301cb0ef41Sopenharmony_ci    static std::unique_ptr<ProtocolError> createErrorResponse(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
1311cb0ef41Sopenharmony_ci    {
1321cb0ef41Sopenharmony_ci        std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, errorMessage));
1331cb0ef41Sopenharmony_ci        protocolError->m_callId = callId;
1341cb0ef41Sopenharmony_ci        protocolError->m_hasCallId = true;
1351cb0ef41Sopenharmony_ci        if (errors && errors->hasErrors())
1361cb0ef41Sopenharmony_ci            protocolError->m_data = errors->errors();
1371cb0ef41Sopenharmony_ci        return protocolError;
1381cb0ef41Sopenharmony_ci    }
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    static std::unique_ptr<ProtocolError> createErrorNotification(DispatchResponse::ErrorCode code, const String& errorMessage)
1411cb0ef41Sopenharmony_ci    {
1421cb0ef41Sopenharmony_ci        return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessage));
1431cb0ef41Sopenharmony_ci    }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci    String serializeToJSON() override
1461cb0ef41Sopenharmony_ci    {
1471cb0ef41Sopenharmony_ci        return serialize()->serializeToJSON();
1481cb0ef41Sopenharmony_ci    }
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci    std::vector<uint8_t> serializeToBinary() override
1511cb0ef41Sopenharmony_ci    {
1521cb0ef41Sopenharmony_ci        return serialize()->serializeToBinary();
1531cb0ef41Sopenharmony_ci    }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci    ~ProtocolError() override {}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ciprivate:
1581cb0ef41Sopenharmony_ci    ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage)
1591cb0ef41Sopenharmony_ci        : m_code(code)
1601cb0ef41Sopenharmony_ci        , m_errorMessage(errorMessage)
1611cb0ef41Sopenharmony_ci    {
1621cb0ef41Sopenharmony_ci    }
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci    std::unique_ptr<DictionaryValue> serialize() {
1651cb0ef41Sopenharmony_ci        std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create();
1661cb0ef41Sopenharmony_ci        error->setInteger("code", m_code);
1671cb0ef41Sopenharmony_ci        error->setString("message", m_errorMessage);
1681cb0ef41Sopenharmony_ci        if (m_data.length())
1691cb0ef41Sopenharmony_ci            error->setString("data", m_data);
1701cb0ef41Sopenharmony_ci        std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create();
1711cb0ef41Sopenharmony_ci        message->setObject("error", std::move(error));
1721cb0ef41Sopenharmony_ci        if (m_hasCallId)
1731cb0ef41Sopenharmony_ci            message->setInteger("id", m_callId);
1741cb0ef41Sopenharmony_ci        return message;
1751cb0ef41Sopenharmony_ci    }
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci    DispatchResponse::ErrorCode m_code;
1781cb0ef41Sopenharmony_ci    String m_errorMessage;
1791cb0ef41Sopenharmony_ci    String m_data;
1801cb0ef41Sopenharmony_ci    int m_callId = 0;
1811cb0ef41Sopenharmony_ci    bool m_hasCallId = false;
1821cb0ef41Sopenharmony_ci};
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci} // namespace
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_cistatic void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
1871cb0ef41Sopenharmony_ci{
1881cb0ef41Sopenharmony_ci    if (frontendChannel)
1891cb0ef41Sopenharmony_ci        frontendChannel->sendProtocolResponse(callId, ProtocolError::createErrorResponse(callId, code, errorMessage, errors));
1901cb0ef41Sopenharmony_ci}
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_cistatic void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResponse::ErrorCode code, const String& errorMessage)
1931cb0ef41Sopenharmony_ci{
1941cb0ef41Sopenharmony_ci    if (frontendChannel)
1951cb0ef41Sopenharmony_ci        frontendChannel->sendProtocolNotification(ProtocolError::createErrorNotification(code, errorMessage));
1961cb0ef41Sopenharmony_ci}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_civoid DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors)
1991cb0ef41Sopenharmony_ci{
2001cb0ef41Sopenharmony_ci    reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors);
2011cb0ef41Sopenharmony_ci}
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_civoid DispatcherBase::clearFrontend()
2041cb0ef41Sopenharmony_ci{
2051cb0ef41Sopenharmony_ci    m_frontendChannel = nullptr;
2061cb0ef41Sopenharmony_ci    for (auto& weak : m_weakPtrs)
2071cb0ef41Sopenharmony_ci        weak->dispose();
2081cb0ef41Sopenharmony_ci    m_weakPtrs.clear();
2091cb0ef41Sopenharmony_ci}
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_cistd::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr()
2121cb0ef41Sopenharmony_ci{
2131cb0ef41Sopenharmony_ci    std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(this));
2141cb0ef41Sopenharmony_ci    m_weakPtrs.insert(weak.get());
2151cb0ef41Sopenharmony_ci    return weak;
2161cb0ef41Sopenharmony_ci}
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ciUberDispatcher::UberDispatcher(FrontendChannel* frontendChannel)
2191cb0ef41Sopenharmony_ci    : m_frontendChannel(frontendChannel) { }
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_civoid UberDispatcher::registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase> dispatcher)
2221cb0ef41Sopenharmony_ci{
2231cb0ef41Sopenharmony_ci    m_dispatchers[name] = std::move(dispatcher);
2241cb0ef41Sopenharmony_ci}
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_civoid UberDispatcher::setupRedirects(const std::unordered_map<String, String>& redirects)
2271cb0ef41Sopenharmony_ci{
2281cb0ef41Sopenharmony_ci    for (const auto& pair : redirects)
2291cb0ef41Sopenharmony_ci        m_redirects[pair.first] = pair.second;
2301cb0ef41Sopenharmony_ci}
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_cibool UberDispatcher::parseCommand(Value* parsedMessage, int* outCallId, String* outMethod) {
2331cb0ef41Sopenharmony_ci    if (!parsedMessage) {
2341cb0ef41Sopenharmony_ci        reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError, "Message must be a valid JSON");
2351cb0ef41Sopenharmony_ci        return false;
2361cb0ef41Sopenharmony_ci    }
2371cb0ef41Sopenharmony_ci    protocol::DictionaryValue* messageObject = DictionaryValue::cast(parsedMessage);
2381cb0ef41Sopenharmony_ci    if (!messageObject) {
2391cb0ef41Sopenharmony_ci        reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must be an object");
2401cb0ef41Sopenharmony_ci        return false;
2411cb0ef41Sopenharmony_ci    }
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci    int callId = 0;
2441cb0ef41Sopenharmony_ci    protocol::Value* callIdValue = messageObject->get("id");
2451cb0ef41Sopenharmony_ci    bool success = callIdValue && callIdValue->asInteger(&callId);
2461cb0ef41Sopenharmony_ci    if (!success) {
2471cb0ef41Sopenharmony_ci        reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must have integer 'id' property");
2481cb0ef41Sopenharmony_ci        return false;
2491cb0ef41Sopenharmony_ci    }
2501cb0ef41Sopenharmony_ci    if (outCallId)
2511cb0ef41Sopenharmony_ci      *outCallId = callId;
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ci    protocol::Value* methodValue = messageObject->get("method");
2541cb0ef41Sopenharmony_ci    String method;
2551cb0ef41Sopenharmony_ci    success = methodValue && methodValue->asString(&method);
2561cb0ef41Sopenharmony_ci    if (!success) {
2571cb0ef41Sopenharmony_ci        reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInvalidRequest, "Message must have string 'method' property", nullptr);
2581cb0ef41Sopenharmony_ci        return false;
2591cb0ef41Sopenharmony_ci    }
2601cb0ef41Sopenharmony_ci    if (outMethod)
2611cb0ef41Sopenharmony_ci      *outMethod = method;
2621cb0ef41Sopenharmony_ci    return true;
2631cb0ef41Sopenharmony_ci}
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ciprotocol::DispatcherBase* UberDispatcher::findDispatcher(const String& method) {
2661cb0ef41Sopenharmony_ci    size_t dotIndex = StringUtil::find(method, ".");
2671cb0ef41Sopenharmony_ci    if (dotIndex == StringUtil::kNotFound)
2681cb0ef41Sopenharmony_ci        return nullptr;
2691cb0ef41Sopenharmony_ci    String domain = StringUtil::substring(method, 0, dotIndex);
2701cb0ef41Sopenharmony_ci    auto it = m_dispatchers.find(domain);
2711cb0ef41Sopenharmony_ci    if (it == m_dispatchers.end())
2721cb0ef41Sopenharmony_ci        return nullptr;
2731cb0ef41Sopenharmony_ci    if (!it->second->canDispatch(method))
2741cb0ef41Sopenharmony_ci        return nullptr;
2751cb0ef41Sopenharmony_ci    return it->second.get();
2761cb0ef41Sopenharmony_ci}
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_cibool UberDispatcher::canDispatch(const String& in_method)
2791cb0ef41Sopenharmony_ci{
2801cb0ef41Sopenharmony_ci    String method = in_method;
2811cb0ef41Sopenharmony_ci    auto redirectIt = m_redirects.find(method);
2821cb0ef41Sopenharmony_ci    if (redirectIt != m_redirects.end())
2831cb0ef41Sopenharmony_ci        method = redirectIt->second;
2841cb0ef41Sopenharmony_ci    return !!findDispatcher(method);
2851cb0ef41Sopenharmony_ci}
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_civoid UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const ProtocolMessage& rawMessage)
2881cb0ef41Sopenharmony_ci{
2891cb0ef41Sopenharmony_ci    String method = in_method;
2901cb0ef41Sopenharmony_ci    auto redirectIt = m_redirects.find(method);
2911cb0ef41Sopenharmony_ci    if (redirectIt != m_redirects.end())
2921cb0ef41Sopenharmony_ci        method = redirectIt->second;
2931cb0ef41Sopenharmony_ci    protocol::DispatcherBase* dispatcher = findDispatcher(method);
2941cb0ef41Sopenharmony_ci    if (!dispatcher) {
2951cb0ef41Sopenharmony_ci        reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr);
2961cb0ef41Sopenharmony_ci        return;
2971cb0ef41Sopenharmony_ci    }
2981cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(std::move(parsedMessage));
2991cb0ef41Sopenharmony_ci    dispatcher->dispatch(callId, method, rawMessage, std::move(messageObject));
3001cb0ef41Sopenharmony_ci}
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ciUberDispatcher::~UberDispatcher() = default;
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci// static
3051cb0ef41Sopenharmony_cistd::unique_ptr<InternalResponse> InternalResponse::createResponse(int callId, std::unique_ptr<Serializable> params)
3061cb0ef41Sopenharmony_ci{
3071cb0ef41Sopenharmony_ci    return std::unique_ptr<InternalResponse>(new InternalResponse(callId, String(), std::move(params)));
3081cb0ef41Sopenharmony_ci}
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci// static
3111cb0ef41Sopenharmony_cistd::unique_ptr<InternalResponse> InternalResponse::createNotification(const String& notification, std::unique_ptr<Serializable> params)
3121cb0ef41Sopenharmony_ci{
3131cb0ef41Sopenharmony_ci    return std::unique_ptr<InternalResponse>(new InternalResponse(0, notification, std::move(params)));
3141cb0ef41Sopenharmony_ci}
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ciString InternalResponse::serializeToJSON()
3171cb0ef41Sopenharmony_ci{
3181cb0ef41Sopenharmony_ci    std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
3191cb0ef41Sopenharmony_ci    std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
3201cb0ef41Sopenharmony_ci    if (m_notification.length()) {
3211cb0ef41Sopenharmony_ci        result->setString("method", m_notification);
3221cb0ef41Sopenharmony_ci        result->setValue("params", SerializedValue::fromJSON(params->serializeToJSON()));
3231cb0ef41Sopenharmony_ci    } else {
3241cb0ef41Sopenharmony_ci        result->setInteger("id", m_callId);
3251cb0ef41Sopenharmony_ci        result->setValue("result", SerializedValue::fromJSON(params->serializeToJSON()));
3261cb0ef41Sopenharmony_ci    }
3271cb0ef41Sopenharmony_ci    return result->serializeToJSON();
3281cb0ef41Sopenharmony_ci}
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_cistd::vector<uint8_t> InternalResponse::serializeToBinary()
3311cb0ef41Sopenharmony_ci{
3321cb0ef41Sopenharmony_ci    std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
3331cb0ef41Sopenharmony_ci    std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
3341cb0ef41Sopenharmony_ci    if (m_notification.length()) {
3351cb0ef41Sopenharmony_ci        result->setString("method", m_notification);
3361cb0ef41Sopenharmony_ci        result->setValue("params", SerializedValue::fromBinary(params->serializeToBinary()));
3371cb0ef41Sopenharmony_ci    } else {
3381cb0ef41Sopenharmony_ci        result->setInteger("id", m_callId);
3391cb0ef41Sopenharmony_ci        result->setValue("result", SerializedValue::fromBinary(params->serializeToBinary()));
3401cb0ef41Sopenharmony_ci    }
3411cb0ef41Sopenharmony_ci    return result->serializeToBinary();
3421cb0ef41Sopenharmony_ci}
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ciInternalResponse::InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params)
3451cb0ef41Sopenharmony_ci    : m_callId(callId)
3461cb0ef41Sopenharmony_ci    , m_notification(notification)
3471cb0ef41Sopenharmony_ci    , m_params(params ? std::move(params) : nullptr)
3481cb0ef41Sopenharmony_ci{
3491cb0ef41Sopenharmony_ci}
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
3521cb0ef41Sopenharmony_ci} // namespace {{namespace}}
3531cb0ef41Sopenharmony_ci{% endfor %}
354