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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response,
std::unique_ptr<protocol::DictionaryValue> result) | 138 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response,
std::unique_ptr<protocol::DictionaryValue> result) |
139 { | 139 { |
| 140 if (!m_frontendChannel) |
| 141 return; |
140 if (response.status() == DispatchResponse::kError) { | 142 if (response.status() == DispatchResponse::kError) { |
141 reportProtocolError(callId, response.errorCode(), response.errorMessage(
), nullptr); | 143 reportProtocolError(callId, response.errorCode(), response.errorMessage(
), nullptr); |
142 return; | 144 return; |
143 } | 145 } |
144 | 146 m_frontendChannel->sendProtocolResponse(callId, InternalResponse::createResp
onse(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 } | 147 } |
151 | 148 |
152 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) | 149 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
153 { | 150 { |
154 sendResponse(callId, response, DictionaryValue::create()); | 151 sendResponse(callId, response, DictionaryValue::create()); |
155 } | 152 } |
156 | 153 |
| 154 namespace { |
| 155 |
| 156 class ProtocolError : public Serializable { |
| 157 public: |
| 158 static std::unique_ptr<ProtocolError> createErrorResponse(int callId, Dispat
chResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
| 159 { |
| 160 std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, err
orMessage)); |
| 161 protocolError->m_callId = callId; |
| 162 protocolError->m_hasCallId = true; |
| 163 if (errors && errors->hasErrors()) |
| 164 protocolError->m_data = errors->errors(); |
| 165 return protocolError; |
| 166 } |
| 167 |
| 168 static std::unique_ptr<ProtocolError> createErrorNotification(DispatchRespon
se::ErrorCode code, const String& errorMessage) |
| 169 { |
| 170 return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessa
ge)); |
| 171 } |
| 172 |
| 173 String serialize() override |
| 174 { |
| 175 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::crea
te(); |
| 176 error->setInteger("code", m_code); |
| 177 error->setString("message", m_errorMessage); |
| 178 if (m_data.length()) |
| 179 error->setString("data", m_data); |
| 180 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::cr
eate(); |
| 181 message->setObject("error", std::move(error)); |
| 182 if (m_hasCallId) |
| 183 message->setInteger("id", m_callId); |
| 184 return message->serialize(); |
| 185 } |
| 186 |
| 187 ~ProtocolError() override {} |
| 188 |
| 189 private: |
| 190 ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage) |
| 191 : m_code(code) |
| 192 , m_errorMessage(errorMessage) |
| 193 { |
| 194 } |
| 195 |
| 196 DispatchResponse::ErrorCode m_code; |
| 197 String m_errorMessage; |
| 198 String m_data; |
| 199 int m_callId = 0; |
| 200 bool m_hasCallId = false; |
| 201 }; |
| 202 |
| 203 } // namespace |
| 204 |
157 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro
rs) | 205 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro
rs) |
158 { | 206 { |
159 if (!frontendChannel) | 207 if (frontendChannel) |
160 return; | 208 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 } | 209 } |
171 | 210 |
172 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp
onse::ErrorCode code, const String& errorMessage) | 211 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp
onse::ErrorCode code, const String& errorMessage) |
173 { | 212 { |
174 if (!frontendChannel) | 213 if (frontendChannel) |
175 return; | 214 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 } | 215 } |
183 | 216 |
184 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode
code, const String& errorMessage, ErrorSupport* errors) | 217 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode
code, const String& errorMessage, ErrorSupport* errors) |
185 { | 218 { |
186 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors)
; | 219 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors)
; |
187 } | 220 } |
188 | 221 |
189 void DispatcherBase::clearFrontend() | 222 void DispatcherBase::clearFrontend() |
190 { | 223 { |
191 m_frontendChannel = nullptr; | 224 m_frontendChannel = nullptr; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if (m_fallThroughForNotFound) | 289 if (m_fallThroughForNotFound) |
257 return DispatchResponse::kFallThrough; | 290 return DispatchResponse::kFallThrough; |
258 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 291 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
259 return DispatchResponse::kError; | 292 return DispatchResponse::kError; |
260 } | 293 } |
261 return it->second->dispatch(callId, method, std::move(messageObject)); | 294 return it->second->dispatch(callId, method, std::move(messageObject)); |
262 } | 295 } |
263 | 296 |
264 UberDispatcher::~UberDispatcher() = default; | 297 UberDispatcher::~UberDispatcher() = default; |
265 | 298 |
| 299 // static |
| 300 std::unique_ptr<InternalResponse> InternalResponse::createResponse(int callId, s
td::unique_ptr<Serializable> params) |
| 301 { |
| 302 return std::unique_ptr<InternalResponse>(new InternalResponse(callId, String
(), std::move(params))); |
| 303 } |
| 304 |
| 305 // static |
| 306 std::unique_ptr<InternalResponse> InternalResponse::createNotification(const Str
ing& notification, std::unique_ptr<Serializable> params) |
| 307 { |
| 308 return std::unique_ptr<InternalResponse>(new InternalResponse(0, notificatio
n, std::move(params))); |
| 309 } |
| 310 |
| 311 String InternalResponse::serialize() |
| 312 { |
| 313 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); |
| 314 std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : Dictio
naryValue::create()); |
| 315 if (m_notification.length()) { |
| 316 result->setString("method", m_notification); |
| 317 result->setValue("params", SerializedValue::create(params->serialize()))
; |
| 318 } else { |
| 319 result->setInteger("id", m_callId); |
| 320 result->setValue("result", SerializedValue::create(params->serialize()))
; |
| 321 } |
| 322 return result->serialize(); |
| 323 } |
| 324 |
| 325 InternalResponse::InternalResponse(int callId, const String& notification, std::
unique_ptr<Serializable> params) |
| 326 : m_callId(callId) |
| 327 , m_notification(notification) |
| 328 , m_params(params ? std::move(params) : nullptr) |
| 329 { |
| 330 } |
| 331 |
266 {% for namespace in config.protocol.namespace %} | 332 {% for namespace in config.protocol.namespace %} |
267 } // namespace {{namespace}} | 333 } // namespace {{namespace}} |
268 {% endfor %} | 334 {% endfor %} |
OLD | NEW |