diff options
Diffstat (limited to 'tools/inspector_protocol/templates/TypeBuilder_cpp.template')
-rw-r--r-- | tools/inspector_protocol/templates/TypeBuilder_cpp.template | 92 |
1 files changed, 56 insertions, 36 deletions
diff --git a/tools/inspector_protocol/templates/TypeBuilder_cpp.template b/tools/inspector_protocol/templates/TypeBuilder_cpp.template index 026c1cdb8d..4ef60a6ea2 100644 --- a/tools/inspector_protocol/templates/TypeBuilder_cpp.template +++ b/tools/inspector_protocol/templates/TypeBuilder_cpp.template @@ -1,10 +1,10 @@ -// This file is generated +// This file is generated by TypeBuilder_cpp.template. // Copyright (c) 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 {{format_include(config.protocol.package, domain.domain)}} +#include {{format_domain_include(config.protocol.package, domain.domain)}} #include {{format_include(config.protocol.package, "Protocol")}} @@ -24,7 +24,7 @@ const char Metainfo::version[] = "{{domain.version}}"; namespace {{type.id}}Enum { {% for literal in type.enum %} -const char* {{ literal | dash_to_camelcase}} = "{{literal}}"; +const char {{ literal | dash_to_camelcase}}[] = "{{literal}}"; {% endfor %} } // namespace {{type.id}}Enum {% if protocol.is_exported(domain.domain, type.id) %} @@ -101,10 +101,15 @@ std::unique_ptr<{{type.id}}> {{type.id}}::clone() const {{config.exported.string_out}} {{type.id}}::toJSONString() const { - String json = toValue()->serialize(); + String json = toValue()->serializeToJSON(); return {{config.exported.to_string_out % "json"}}; } +void {{type.id}}::writeBinary(std::vector<uint8_t>* out) const +{ + toValue()->writeBinary(out); +} + // static std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{config.exported.string_in}}& json) { @@ -114,6 +119,17 @@ std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromJSONString(const {{confi return nullptr; return protocol::{{domain.domain}}::{{type.id}}::fromValue(value.get(), &errors); } + +// static +std::unique_ptr<API::{{type.id}}> API::{{type.id}}::fromBinary(const uint8_t* data, size_t length) +{ + ErrorSupport errors; + std::unique_ptr<Value> value = Value::parseBinary(data, length); + if (!value) + return nullptr; + return protocol::{{domain.domain}}::{{type.id}}::fromValue(value.get(), &errors); +} + {% endif %} {% endfor %} @@ -187,19 +203,23 @@ void Frontend::flush() m_frontendChannel->flushProtocolNotifications(); } -void Frontend::sendRawNotification(const String& notification) +void Frontend::sendRawNotification(String notification) +{ + m_frontendChannel->sendProtocolNotification(InternalRawNotification::fromJSON(std::move(notification))); +} + +void Frontend::sendRawNotification(std::vector<uint8_t> notification) { - m_frontendChannel->sendProtocolNotification(InternalRawNotification::create(notification)); + m_frontendChannel->sendProtocolNotification(InternalRawNotification::fromBinary(std::move(notification))); } // --------------------- Dispatcher. class DispatcherImpl : public protocol::DispatcherBase { public: - DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend, bool fallThroughForNotFound) + DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend) : DispatcherBase(frontendChannel) - , m_backend(backend) - , m_fallThroughForNotFound(fallThroughForNotFound) { + , m_backend(backend) { {% for command in domain.commands %} {% if "redirect" in command %} m_redirects["{{domain.domain}}.{{command.name}}"] = "{{command.redirect}}.{{command.name}}"; @@ -210,37 +230,35 @@ public: {% endfor %} } ~DispatcherImpl() override { } - DispatchResponse::Status dispatch(int callId, const String& method, std::unique_ptr<protocol::DictionaryValue> messageObject) override; - HashMap<String, String>& redirects() { return m_redirects; } + bool canDispatch(const String& method) override; + void dispatch(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<protocol::DictionaryValue> messageObject) override; + std::unordered_map<String, String>& redirects() { return m_redirects; } protected: - using CallHandler = DispatchResponse::Status (DispatcherImpl::*)(int callId, std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors); - using DispatchMap = protocol::HashMap<String, CallHandler>; + using CallHandler = void (DispatcherImpl::*)(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors); + using DispatchMap = std::unordered_map<String, CallHandler>; DispatchMap m_dispatchMap; - HashMap<String, String> m_redirects; + std::unordered_map<String, String> m_redirects; {% for command in domain.commands %} {% if "redirect" in command %}{% continue %}{% endif %} {% if not protocol.generate_command(domain.domain, command.name) %}{% continue %}{% endif %} - DispatchResponse::Status {{command.name}}(int callId, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport*); + void {{command.name}}(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport*); {% endfor %} Backend* m_backend; - bool m_fallThroughForNotFound; }; -DispatchResponse::Status DispatcherImpl::dispatch(int callId, const String& method, std::unique_ptr<protocol::DictionaryValue> messageObject) -{ - protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method); - if (it == m_dispatchMap.end()) { - if (m_fallThroughForNotFound) - return DispatchResponse::kFallThrough; - reportProtocolError(callId, DispatchResponse::kMethodNotFound, "'" + method + "' wasn't found", nullptr); - return DispatchResponse::kError; - } +bool DispatcherImpl::canDispatch(const String& method) { + return m_dispatchMap.find(method) != m_dispatchMap.end(); +} +void DispatcherImpl::dispatch(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<protocol::DictionaryValue> messageObject) +{ + std::unordered_map<String, CallHandler>::iterator it = m_dispatchMap.find(method); + DCHECK(it != m_dispatchMap.end()); protocol::ErrorSupport errors; - return (this->*(it->second))(callId, std::move(messageObject), &errors); + (this->*(it->second))(callId, method, message, std::move(messageObject), &errors); } {% for command in domain.commands %} @@ -251,8 +269,8 @@ DispatchResponse::Status DispatcherImpl::dispatch(int callId, const String& meth class {{command_name_title}}CallbackImpl : public Backend::{{command_name_title}}Callback, public DispatcherBase::Callback { public: - {{command_name_title}}CallbackImpl(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, int callbackId) - : DispatcherBase::Callback(std::move(backendImpl), callId, callbackId) { } + {{command_name_title}}CallbackImpl(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message) + : DispatcherBase::Callback(std::move(backendImpl), callId, method, message) { } void sendSuccess( {%- for parameter in command.returns -%} @@ -289,7 +307,7 @@ public: }; {% endif %} -DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors) +void DispatcherImpl::{{command.name}}(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors) { {% if "parameters" in command %} // Prepare input parameters. @@ -312,7 +330,7 @@ DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu errors->pop(); if (errors->hasErrors()) { reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidParamsString, errors); - return DispatchResponse::kError; + return; } {% endif %} {% if "returns" in command and not protocol.is_async_command(domain.domain, command.name) %} @@ -343,8 +361,10 @@ DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu &out_{{parameter.name}} {%- endfor %} {% endif %}); - if (response.status() == DispatchResponse::kFallThrough) - return response.status(); + if (response.status() == DispatchResponse::kFallThrough) { + channel()->fallThrough(callId, method, message); + return; + } {% if "returns" in command %} std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create(); if (response.status() == DispatchResponse::kSuccess) { @@ -363,10 +383,10 @@ DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu if (weak->get()) weak->get()->sendResponse(callId, response); {% endif %} - return response.status(); + return; {% else %} std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr(); - std::unique_ptr<{{command_name_title}}CallbackImpl> callback(new {{command.name | to_title_case}}CallbackImpl(weakPtr(), callId, nextCallbackId())); + std::unique_ptr<{{command_name_title}}CallbackImpl> callback(new {{command.name | to_title_case}}CallbackImpl(weakPtr(), callId, method, message)); m_backend->{{command.name | to_method_case}}( {%- for property in command.parameters -%} {%- if not loop.first -%}, {% endif -%} @@ -378,7 +398,7 @@ DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu {%- endfor -%} {%- if command.parameters -%}, {% endif -%} std::move(callback)); - return (weak->get() && weak->get()->lastCallbackFallThrough()) ? DispatchResponse::kFallThrough : DispatchResponse::kAsync; + return; {% endif %} } {% endfor %} @@ -386,7 +406,7 @@ DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu // static void Dispatcher::wire(UberDispatcher* uber, Backend* backend) { - std::unique_ptr<DispatcherImpl> dispatcher(new DispatcherImpl(uber->channel(), backend, uber->fallThroughForNotFound())); + std::unique_ptr<DispatcherImpl> dispatcher(new DispatcherImpl(uber->channel(), backend)); uber->setupRedirects(dispatcher->redirects()); uber->registerBackend("{{domain.domain}}", std::move(dispatcher)); } |