void BackendDispatcher::dispatch(const String& message)
{
    Ref<BackendDispatcher> protect(*this);

    RefPtr<InspectorValue> parsedMessage;
    if (!InspectorValue::parseJSON(message, parsedMessage)) {
        reportProtocolError(nullptr, ParseError, ASCIILiteral("Message must be in JSON format"));
        return;
    }

    RefPtr<InspectorObject> messageObject;
    if (!parsedMessage->asObject(messageObject)) {
        reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("Message must be a JSONified object"));
        return;
    }

    RefPtr<InspectorValue> callIdValue;
    if (!messageObject->getValue(ASCIILiteral("id"), callIdValue)) {
        reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("'id' property was not found"));
        return;
    }

    long callId = 0;
    if (!callIdValue->asInteger(callId)) {
        reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("The type of 'id' property must be integer"));
        return;
    }

    RefPtr<InspectorValue> methodValue;
    if (!messageObject->getValue(ASCIILiteral("method"), methodValue)) {
        reportProtocolError(&callId, InvalidRequest, ASCIILiteral("'method' property wasn't found"));
        return;
    }

    String method;
    if (!methodValue->asString(method)) {
        reportProtocolError(&callId, InvalidRequest, ASCIILiteral("The type of 'method' property must be string"));
        return;
    }

    size_t position = method.find('.');
    if (position == WTF::notFound) {
        reportProtocolError(&callId, InvalidRequest, ASCIILiteral("The 'method' property was formatted incorrectly. It should be 'Domain.method'"));
        return;
    }

    String domain = method.substring(0, position);
    SupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain);
    if (!domainDispatcher) {
        reportProtocolError(&callId, MethodNotFound, "'" + domain + "' domain was not found");
        return;
    }

    String domainMethod = method.substring(position + 1);
    domainDispatcher->dispatch(callId, domainMethod, messageObject.releaseNonNull());
}
void BackendDispatcher::dispatch(const String& message)
{
    Ref<BackendDispatcher> protect(*this);

    ASSERT(!m_protocolErrors.size());

    long requestId = 0;
    RefPtr<JSON::Object> messageObject;

    {
        // In case this is a re-entrant call from a nested run loop, we don't want to lose
        // the outer request's id just because the inner request is bogus.
        SetForScope<std::optional<long>> scopedRequestId(m_currentRequestId, std::nullopt);

        RefPtr<JSON::Value> parsedMessage;
        if (!JSON::Value::parseJSON(message, parsedMessage)) {
            reportProtocolError(ParseError, ASCIILiteral("Message must be in JSON format"));
            sendPendingErrors();
            return;
        }

        if (!parsedMessage->asObject(messageObject)) {
            reportProtocolError(InvalidRequest, ASCIILiteral("Message must be a JSONified object"));
            sendPendingErrors();
            return;
        }

        RefPtr<JSON::Value> requestIdValue;
        if (!messageObject->getValue(ASCIILiteral("id"), requestIdValue)) {
            reportProtocolError(InvalidRequest, ASCIILiteral("'id' property was not found"));
            sendPendingErrors();
            return;
        }

        if (!requestIdValue->asInteger(requestId)) {
            reportProtocolError(InvalidRequest, ASCIILiteral("The type of 'id' property must be integer"));
            sendPendingErrors();
            return;
        }
    }

    {
        // We could be called re-entrantly from a nested run loop, so restore the previous id.
        SetForScope<std::optional<long>> scopedRequestId(m_currentRequestId, requestId);

        RefPtr<JSON::Value> methodValue;
        if (!messageObject->getValue(ASCIILiteral("method"), methodValue)) {
            reportProtocolError(InvalidRequest, ASCIILiteral("'method' property wasn't found"));
            sendPendingErrors();
            return;
        }

        String methodString;
        if (!methodValue->asString(methodString)) {
            reportProtocolError(InvalidRequest, ASCIILiteral("The type of 'method' property must be string"));
            sendPendingErrors();
            return;
        }

        Vector<String> domainAndMethod;
        methodString.split('.', true, domainAndMethod);
        if (domainAndMethod.size() != 2 || !domainAndMethod[0].length() || !domainAndMethod[1].length()) {
            reportProtocolError(InvalidRequest, ASCIILiteral("The 'method' property was formatted incorrectly. It should be 'Domain.method'"));
            sendPendingErrors();
            return;
        }

        String domain = domainAndMethod[0];
        SupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain);
        if (!domainDispatcher) {
            reportProtocolError(MethodNotFound, "'" + domain + "' domain was not found");
            sendPendingErrors();
            return;
        }

        String method = domainAndMethod[1];
        domainDispatcher->dispatch(requestId, method, messageObject.releaseNonNull());

        if (m_protocolErrors.size())
            sendPendingErrors();
    }
}