Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 //#include "DispatcherBase.h" | 5 //#include "DispatcherBase.h" |
| 6 //#include "Parser.h" | 6 //#include "Parser.h" |
| 7 | 7 |
| 8 {% for namespace in config.protocol.namespace %} | 8 {% for namespace in config.protocol.namespace %} |
| 9 namespace {{namespace}} { | 9 namespace {{namespace}} { |
| 10 {% endfor %} | 10 {% endfor %} |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 128 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
| 129 if (!object) | 129 if (!object) |
| 130 return false; | 130 return false; |
| 131 | 131 |
| 132 if (!object->getString("method", result)) | 132 if (!object->getString("method", result)) |
| 133 return false; | 133 return false; |
| 134 | 134 |
| 135 return true; | 135 return true; |
| 136 } | 136 } |
| 137 | 137 |
| 138 namespace { | |
| 139 | |
| 140 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.
| |
| 141 public: | |
| 142 static std::unique_ptr<SerializableResponse> create(int callId, std::unique_ ptr<protocol::DictionaryValue> result) | |
| 143 { | |
| 144 return std::unique_ptr<SerializableResponse>(new SerializableResponse(ca llId, std::move(result))); | |
| 145 } | |
| 146 | |
| 147 std::unique_ptr<protocol::Value> serializeValue() const override | |
| 148 { | |
| 149 std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryV alue::create(); | |
| 150 responseMessage->setInteger("id", m_callId); | |
| 151 responseMessage->setObject("result", std::move(m_result)); | |
| 152 return responseMessage; | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 SerializableResponse(int callId, std::unique_ptr<protocol::DictionaryValue> result): | |
| 157 m_callId(callId), | |
| 158 m_result(std::move(result)) | |
| 159 { | |
| 160 } | |
| 161 | |
| 162 int m_callId; | |
| 163 mutable std::unique_ptr<protocol::DictionaryValue> m_result; | |
| 164 }; | |
| 165 | |
| 166 } // namespace | |
| 167 | |
| 138 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result) | 168 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response, std::unique_ptr<protocol::DictionaryValue> result) |
| 139 { | 169 { |
| 170 if (!m_frontendChannel) | |
| 171 return; | |
| 140 if (response.status() == DispatchResponse::kError) { | 172 if (response.status() == DispatchResponse::kError) { |
| 141 reportProtocolError(callId, response.errorCode(), response.errorMessage( ), nullptr); | 173 reportProtocolError(callId, response.errorCode(), response.errorMessage( ), nullptr); |
| 142 return; | 174 return; |
| 143 } | 175 } |
| 144 | 176 m_frontendChannel->sendProtocolResponse(callId, SerializableResponse::create (callId, std::move(result))); |
| 145 std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue ::create(); | |
| 146 responseMessage->setInteger("id", callId); | |
| 147 responseMessage->setObject("result", std::move(result)); | |
| 148 if (m_frontendChannel) | |
| 149 m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONS tring()); | |
| 150 } | 177 } |
| 151 | 178 |
| 152 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) | 179 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
| 153 { | 180 { |
| 154 sendResponse(callId, response, DictionaryValue::create()); | 181 sendResponse(callId, response, DictionaryValue::create()); |
| 155 } | 182 } |
| 156 | 183 |
| 184 namespace { | |
| 185 | |
| 186 class ProtocolError : public Serializable { | |
| 187 public: | |
| 188 static std::unique_ptr<ProtocolError> createErrorResponse(int callId, Dispat chResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) | |
| 189 { | |
| 190 fprintf(stderr, "callId: %d\n", callId); | |
| 191 std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, err orMessage)); | |
| 192 protocolError->setCallId(callId); | |
| 193 if (errors && errors->hasErrors()) | |
| 194 protocolError->setData(errors->errors()); | |
| 195 return protocolError; | |
| 196 } | |
| 197 | |
| 198 static std::unique_ptr<ProtocolError> createErrorNotification(DispatchRespon se::ErrorCode code, const String& errorMessage) | |
| 199 { | |
| 200 return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessa ge)); | |
| 201 } | |
| 202 | |
| 203 std::unique_ptr<protocol::Value> serializeValue() const override | |
| 204 { | |
| 205 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::crea te(); | |
| 206 error->setInteger("code", m_code); | |
| 207 error->setString("message", m_errorMessage); | |
| 208 if (m_data.length()) | |
| 209 error->setString("data", m_data); | |
| 210 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::cr eate(); | |
| 211 message->setObject("error", std::move(error)); | |
| 212 if (m_hasCallId) | |
| 213 message->setInteger("id", m_callId); | |
| 214 return message; | |
| 215 } | |
| 216 | |
| 217 private: | |
| 218 ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage) | |
| 219 : m_code(code) | |
| 220 , m_errorMessage(errorMessage) | |
| 221 { | |
| 222 } | |
| 223 | |
| 224 void setCallId(int callId) | |
| 225 { | |
| 226 m_callId = callId; | |
| 227 m_hasCallId = true; | |
| 228 } | |
| 229 void setData(const String& data) { m_data = data; } | |
| 230 | |
| 231 DispatchResponse::ErrorCode m_code; | |
| 232 String m_errorMessage; | |
| 233 String m_data; | |
| 234 int m_callId = 0; | |
| 235 bool m_hasCallId = false; | |
| 236 }; | |
| 237 | |
| 238 } // namespace | |
| 239 | |
| 157 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro rs) | 240 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro rs) |
| 158 { | 241 { |
| 159 if (!frontendChannel) | 242 if (frontendChannel) |
| 160 return; | 243 frontendChannel->sendProtocolResponse(callId, ProtocolError::createError Response(callId, code, errorMessage, errors)); |
| 161 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create() ; | |
| 162 error->setInteger("code", code); | |
| 163 error->setString("message", errorMessage); | |
| 164 if (errors && errors->hasErrors()) | |
| 165 error->setString("data", errors->errors()); | |
| 166 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create (); | |
| 167 message->setObject("error", std::move(error)); | |
| 168 message->setInteger("id", callId); | |
| 169 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); | |
| 170 } | 244 } |
| 171 | 245 |
| 172 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp onse::ErrorCode code, const String& errorMessage) | 246 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp onse::ErrorCode code, const String& errorMessage) |
| 173 { | 247 { |
| 174 if (!frontendChannel) | 248 if (frontendChannel) |
| 175 return; | 249 frontendChannel->sendProtocolNotification(ProtocolError::createErrorNoti fication(code, errorMessage)); |
| 176 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create() ; | |
| 177 error->setInteger("code", code); | |
| 178 error->setString("message", errorMessage); | |
| 179 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create (); | |
| 180 message->setObject("error", std::move(error)); | |
| 181 frontendChannel->sendProtocolNotification(message->toJSONString()); | |
| 182 } | 250 } |
| 183 | 251 |
| 184 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) | 252 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
| 185 { | 253 { |
| 186 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors) ; | 254 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors) ; |
| 187 } | 255 } |
| 188 | 256 |
| 189 void DispatcherBase::clearFrontend() | 257 void DispatcherBase::clearFrontend() |
| 190 { | 258 { |
| 191 m_frontendChannel = nullptr; | 259 m_frontendChannel = nullptr; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 return DispatchResponse::kError; | 327 return DispatchResponse::kError; |
| 260 } | 328 } |
| 261 return it->second->dispatch(callId, method, std::move(messageObject)); | 329 return it->second->dispatch(callId, method, std::move(messageObject)); |
| 262 } | 330 } |
| 263 | 331 |
| 264 UberDispatcher::~UberDispatcher() = default; | 332 UberDispatcher::~UberDispatcher() = default; |
| 265 | 333 |
| 266 {% for namespace in config.protocol.namespace %} | 334 {% for namespace in config.protocol.namespace %} |
| 267 } // namespace {{namespace}} | 335 } // namespace {{namespace}} |
| 268 {% endfor %} | 336 {% endfor %} |
| OLD | NEW |