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..9dbd64378bbd9b96f997ad77cbf77460178fc383 100644 |
| --- a/third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| +++ b/third_party/inspector_protocol/lib/DispatcherBase_cpp.template |
| @@ -135,18 +135,45 @@ bool DispatcherBase::getCommandName(const String& message, String* result) |
| return true; |
| } |
| +namespace { |
| + |
| +class SerializableResponse : public Serializable { |
|
dgozman
2016/11/21 22:29:16
Can we combine this with Notification class?
kozy
2016/11/22 01:25:38
Done.
|
| +public: |
| + static std::unique_ptr<SerializableResponse> create(int callId, std::unique_ptr<protocol::DictionaryValue> result) |
| + { |
| + return std::unique_ptr<SerializableResponse>(new SerializableResponse(callId, std::move(result))); |
| + } |
| + |
| + std::unique_ptr<protocol::Value> serializeValue() const override |
| + { |
| + std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue::create(); |
| + responseMessage->setInteger("id", m_callId); |
| + responseMessage->setObject("result", std::move(m_result)); |
| + return responseMessage; |
| + } |
| + |
| +private: |
| + SerializableResponse(int callId, std::unique_ptr<protocol::DictionaryValue> result): |
| + m_callId(callId), |
| + m_result(std::move(result)) |
| + { |
| + } |
| + |
| + int m_callId; |
| + mutable std::unique_ptr<protocol::DictionaryValue> m_result; |
| +}; |
| + |
| +} // namespace |
| + |
| 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, SerializableResponse::create(callId, std::move(result))); |
| } |
| void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
| @@ -154,31 +181,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); |
| + 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)); |
| + } |
| + |
| + std::unique_ptr<protocol::Value> serializeValue() const 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; |
| + } |
| + |
| +private: |
| + ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage) |
| + : m_code(code) |
| + , m_errorMessage(errorMessage) |
| + { |
| + } |
| + |
| + void setCallId(int callId) |
| + { |
| + m_callId = callId; |
| + m_hasCallId = true; |
| + } |
| + void setData(const String& data) { m_data = data; } |
| + |
| + 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) |