diff options
Diffstat (limited to 'tools/inspector_protocol/lib/DispatcherBase_cpp.template')
-rw-r--r-- | tools/inspector_protocol/lib/DispatcherBase_cpp.template | 173 |
1 files changed, 86 insertions, 87 deletions
diff --git a/tools/inspector_protocol/lib/DispatcherBase_cpp.template b/tools/inspector_protocol/lib/DispatcherBase_cpp.template index cecef743bf..11843f4330 100644 --- a/tools/inspector_protocol/lib/DispatcherBase_cpp.template +++ b/tools/inspector_protocol/lib/DispatcherBase_cpp.template @@ -1,3 +1,5 @@ +// This file is generated by DispatcherBase_cpp.template. + // 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. @@ -68,10 +70,11 @@ DispatcherBase::WeakPtr::~WeakPtr() m_dispatcher->m_weakPtrs.erase(this); } -DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, int callbackId) +DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message) : m_backendImpl(std::move(backendImpl)) , m_callId(callId) - , m_callbackId(callbackId) { } + , m_method(method) + , m_message(message) { } DispatcherBase::Callback::~Callback() = default; @@ -92,32 +95,18 @@ void DispatcherBase::Callback::fallThroughIfActive() { if (!m_backendImpl || !m_backendImpl->get()) return; - m_backendImpl->get()->markFallThrough(m_callbackId); + m_backendImpl->get()->channel()->fallThrough(m_callId, m_method, m_message); m_backendImpl = nullptr; } DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) - : m_frontendChannel(frontendChannel) - , m_lastCallbackId(0) - , m_lastCallbackFallThrough(false) { } + : m_frontendChannel(frontendChannel) { } DispatcherBase::~DispatcherBase() { clearFrontend(); } -int DispatcherBase::nextCallbackId() -{ - m_lastCallbackFallThrough = false; - return ++m_lastCallbackId; -} - -void DispatcherBase::markFallThrough(int callbackId) -{ - DCHECK(callbackId == m_lastCallbackId); - m_lastCallbackFallThrough = true; -} - void DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result) { if (!m_frontendChannel) @@ -153,18 +142,14 @@ public: return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessage)); } - String serialize() override + String serializeToJSON() override { - 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->serialize(); + return serialize()->serializeToJSON(); + } + + std::vector<uint8_t> serializeToBinary() override + { + return serialize()->serializeToBinary(); } ~ProtocolError() override {} @@ -176,6 +161,19 @@ private: { } + 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; + } + DispatchResponse::ErrorCode m_code; String m_errorMessage; String m_data; @@ -218,100 +216,87 @@ std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() } UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) - : m_frontendChannel(frontendChannel) - , m_fallThroughForNotFound(false) { } - -void UberDispatcher::setFallThroughForNotFound(bool fallThroughForNotFound) -{ - m_fallThroughForNotFound = fallThroughForNotFound; -} + : m_frontendChannel(frontendChannel) { } void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase> dispatcher) { m_dispatchers[name] = std::move(dispatcher); } -void UberDispatcher::setupRedirects(const HashMap<String, String>& redirects) +void UberDispatcher::setupRedirects(const std::unordered_map<String, String>& redirects) { for (const auto& pair : redirects) m_redirects[pair.first] = pair.second; } -DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedMessage, int* outCallId, String* outMethod) -{ +bool UberDispatcher::parseCommand(Value* parsedMessage, int* outCallId, String* outMethod) { if (!parsedMessage) { reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError, "Message must be a valid JSON"); - return DispatchResponse::kError; + return false; } - std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(std::move(parsedMessage)); + protocol::DictionaryValue* messageObject = DictionaryValue::cast(parsedMessage); if (!messageObject) { reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must be an object"); - return DispatchResponse::kError; + return false; } int callId = 0; protocol::Value* callIdValue = messageObject->get("id"); bool success = callIdValue && callIdValue->asInteger(&callId); - if (outCallId) - *outCallId = callId; if (!success) { reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must have integer 'id' property"); - return DispatchResponse::kError; + return false; } + if (outCallId) + *outCallId = callId; protocol::Value* methodValue = messageObject->get("method"); String method; success = methodValue && methodValue->asString(&method); - if (outMethod) - *outMethod = method; if (!success) { reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInvalidRequest, "Message must have string 'method' property", nullptr); - return DispatchResponse::kError; + return false; } + if (outMethod) + *outMethod = method; + return true; +} - HashMap<String, String>::iterator redirectIt = m_redirects.find(method); - if (redirectIt != m_redirects.end()) - method = redirectIt->second; - +protocol::DispatcherBase* UberDispatcher::findDispatcher(const String& method) { size_t dotIndex = StringUtil::find(method, "."); - if (dotIndex == StringUtil::kNotFound) { - if (m_fallThroughForNotFound) - return DispatchResponse::kFallThrough; - reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr); - return DispatchResponse::kError; - } + if (dotIndex == StringUtil::kNotFound) + return nullptr; String domain = StringUtil::substring(method, 0, dotIndex); auto it = m_dispatchers.find(domain); - if (it == m_dispatchers.end()) { - if (m_fallThroughForNotFound) - return DispatchResponse::kFallThrough; - reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr); - return DispatchResponse::kError; - } - return it->second->dispatch(callId, method, std::move(messageObject)); + if (it == m_dispatchers.end()) + return nullptr; + if (!it->second->canDispatch(method)) + return nullptr; + return it->second.get(); } -bool UberDispatcher::getCommandName(const String& message, String* method, std::unique_ptr<protocol::DictionaryValue>* parsedMessage) +bool UberDispatcher::canDispatch(const String& in_method) { - std::unique_ptr<protocol::Value> value = StringUtil::parseJSON(message); - if (!value) { - reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError, "Message must be a valid JSON"); - return false; - } - - protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); - if (!object) { - reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must be an object"); - return false; - } + String method = in_method; + auto redirectIt = m_redirects.find(method); + if (redirectIt != m_redirects.end()) + method = redirectIt->second; + return !!findDispatcher(method); +} - if (!object->getString("method", method)) { - reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidRequest, "Message must have string 'method' property"); - return false; +void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const ProtocolMessage& rawMessage) +{ + String method = in_method; + auto redirectIt = m_redirects.find(method); + if (redirectIt != m_redirects.end()) + method = redirectIt->second; + protocol::DispatcherBase* dispatcher = findDispatcher(method); + if (!dispatcher) { + reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr); + return; } - - parsedMessage->reset(DictionaryValue::cast(value.release())); - return true; + std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(std::move(parsedMessage)); + dispatcher->dispatch(callId, method, rawMessage, std::move(messageObject)); } UberDispatcher::~UberDispatcher() = default; @@ -328,18 +313,32 @@ std::unique_ptr<InternalResponse> InternalResponse::createNotification(const Str return std::unique_ptr<InternalResponse>(new InternalResponse(0, notification, std::move(params))); } -String InternalResponse::serialize() +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() { 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::create(params->serialize())); + result->setValue("params", SerializedValue::fromBinary(params->serializeToBinary())); } else { result->setInteger("id", m_callId); - result->setValue("result", SerializedValue::create(params->serialize())); + result->setValue("result", SerializedValue::fromBinary(params->serializeToBinary())); } - return result->serialize(); + return result->serializeToBinary(); } InternalResponse::InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params) |