Chromium Code Reviews| Index: third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| diff --git a/third_party/inspector_protocol/lib/DispatcherBase_cpp.template b/third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| index b08947aba43f32c1da32a290560dcbd32471e306..457381eeaca6cbd4081e0cf77a20f0e91bc48786 100644 |
| --- a/third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| +++ b/third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| @@ -137,16 +137,13 @@ bool DispatcherBase::getCommandName(const String& message, String* result) |
| void DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result) |
| { |
| + if (!m_frontendChannel) |
| + return; |
| if (response.status() == DispatchResponse::kError) { |
| reportProtocolError(callId, response.errorCode(), response.errorMessage(), nullptr); |
| return; |
| } |
| - |
| - std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue::create(); |
| - responseMessage->setInteger("id", callId); |
| - responseMessage->setObject("result", std::move(result)); |
| - if (m_frontendChannel) |
| - m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONString()); |
| + m_frontendChannel->sendProtocolResponse(callId, InternalResponse::createResponse(callId, std::move(result))); |
| } |
| void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
| @@ -154,31 +151,72 @@ void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
| sendResponse(callId, response, DictionaryValue::create()); |
| } |
| +namespace { |
| + |
| +class ProtocolError : public Serializable { |
| +public: |
| + static std::unique_ptr<ProtocolError> createErrorResponse(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
| + { |
| + fprintf(stderr, "callId: %d\n", callId); |
|
dgozman
2016/11/22 19:39:24
fprintf!
kozy
2016/11/22 21:01:27
Done.
|
| + std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, errorMessage)); |
| + protocolError->setCallId(callId); |
| + if (errors && errors->hasErrors()) |
| + protocolError->setData(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)); |
| + } |
| + |
| + String 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->toJSONString(); |
| + } |
| + |
| +private: |
| + ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage) |
| + : m_code(code) |
| + , m_errorMessage(errorMessage) |
| + { |
| + } |
| + |
| + void setCallId(int callId) |
|
dgozman
2016/11/22 19:39:24
Inline this.
kozy
2016/11/22 21:01:27
Done.
|
| + { |
| + m_callId = callId; |
| + m_hasCallId = true; |
| + } |
| + void setData(const String& data) { m_data = data; } |
|
dgozman
2016/11/22 19:39:24
Inline this.
kozy
2016/11/22 21:01:26
Done.
|
| + |
| + DispatchResponse::ErrorCode m_code; |
| + String m_errorMessage; |
| + String m_data; |
| + int m_callId = 0; |
| + bool m_hasCallId = false; |
| +}; |
| + |
| +} // namespace |
| + |
| static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
| { |
| - if (!frontendChannel) |
| - return; |
| - std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create(); |
| - error->setInteger("code", code); |
| - error->setString("message", errorMessage); |
| - if (errors && errors->hasErrors()) |
| - error->setString("data", errors->errors()); |
| - std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create(); |
| - message->setObject("error", std::move(error)); |
| - message->setInteger("id", callId); |
| - frontendChannel->sendProtocolResponse(callId, message->toJSONString()); |
| + if (frontendChannel) |
| + frontendChannel->sendProtocolResponse(callId, ProtocolError::createErrorResponse(callId, code, errorMessage, errors)); |
| } |
| static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResponse::ErrorCode code, const String& errorMessage) |
| { |
| - if (!frontendChannel) |
| - return; |
| - std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create(); |
| - error->setInteger("code", code); |
| - error->setString("message", errorMessage); |
| - std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create(); |
| - message->setObject("error", std::move(error)); |
| - frontendChannel->sendProtocolNotification(message->toJSONString()); |
| + if (frontendChannel) |
| + frontendChannel->sendProtocolNotification(ProtocolError::createErrorNotification(code, errorMessage)); |
| } |
| void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
| @@ -263,6 +301,38 @@ DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM |
| UberDispatcher::~UberDispatcher() = default; |
| +// 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& method, std::unique_ptr<Serializable> params) |
| +{ |
| + return std::unique_ptr<InternalResponse>(new InternalResponse(0, method, std::move(params))); |
| +} |
| + |
| +String InternalResponse::serialize() |
| +{ |
| + std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); |
| + if (m_type.length()) { |
| + result->setString("method", m_type); |
| + result->setValue("params", SerializedValue::create(m_params->serialize())); |
| + } else { |
| + result->setInteger("id", m_callId); |
| + result->setValue("result", SerializedValue::create(m_params->serialize())); |
| + } |
| + return result->toJSONString(); |
|
dgozman
2016/11/22 19:39:24
m_params.reset()
kozy
2016/11/22 21:01:26
Done.
|
| +} |
| + |
| +InternalResponse::InternalResponse(int callId, const String& type, std::unique_ptr<Serializable> params) |
| + : m_callId(callId) |
| + , m_type(type) |
| + , m_params(params ? std::move(params) : DictionaryValue::create()) |
|
dgozman
2016/11/22 19:39:24
Let's make it nullable.
kozy
2016/11/22 21:01:26
Done.
|
| +{ |
| +} |
| + |
| {% for namespace in config.protocol.namespace %} |
| } // namespace {{namespace}} |
| {% endfor %} |