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 fprintf(stderr, "callId: %d\n", callId); | |
dgozman
2016/11/22 19:39:24
fprintf!
kozy
2016/11/22 21:01:27
Done.
| |
161 std::unique_ptr<ProtocolError> protocolError(new ProtocolError(code, err orMessage)); | |
162 protocolError->setCallId(callId); | |
163 if (errors && errors->hasErrors()) | |
164 protocolError->setData(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() | |
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->toJSONString(); | |
185 } | |
186 | |
187 private: | |
188 ProtocolError(DispatchResponse::ErrorCode code, const String& errorMessage) | |
189 : m_code(code) | |
190 , m_errorMessage(errorMessage) | |
191 { | |
192 } | |
193 | |
194 void setCallId(int callId) | |
dgozman
2016/11/22 19:39:24
Inline this.
kozy
2016/11/22 21:01:27
Done.
| |
195 { | |
196 m_callId = callId; | |
197 m_hasCallId = true; | |
198 } | |
199 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.
| |
200 | |
201 DispatchResponse::ErrorCode m_code; | |
202 String m_errorMessage; | |
203 String m_data; | |
204 int m_callId = 0; | |
205 bool m_hasCallId = false; | |
206 }; | |
207 | |
208 } // namespace | |
209 | |
157 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro rs) | 210 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro rs) |
158 { | 211 { |
159 if (!frontendChannel) | 212 if (frontendChannel) |
160 return; | 213 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 } | 214 } |
171 | 215 |
172 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp onse::ErrorCode code, const String& errorMessage) | 216 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp onse::ErrorCode code, const String& errorMessage) |
173 { | 217 { |
174 if (!frontendChannel) | 218 if (frontendChannel) |
175 return; | 219 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 } | 220 } |
183 | 221 |
184 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) | 222 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* errors) |
185 { | 223 { |
186 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors) ; | 224 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors) ; |
187 } | 225 } |
188 | 226 |
189 void DispatcherBase::clearFrontend() | 227 void DispatcherBase::clearFrontend() |
190 { | 228 { |
191 m_frontendChannel = nullptr; | 229 m_frontendChannel = nullptr; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 if (m_fallThroughForNotFound) | 294 if (m_fallThroughForNotFound) |
257 return DispatchResponse::kFallThrough; | 295 return DispatchResponse::kFallThrough; |
258 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth odNotFound, "'" + method + "' wasn't found", nullptr); | 296 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth odNotFound, "'" + method + "' wasn't found", nullptr); |
259 return DispatchResponse::kError; | 297 return DispatchResponse::kError; |
260 } | 298 } |
261 return it->second->dispatch(callId, method, std::move(messageObject)); | 299 return it->second->dispatch(callId, method, std::move(messageObject)); |
262 } | 300 } |
263 | 301 |
264 UberDispatcher::~UberDispatcher() = default; | 302 UberDispatcher::~UberDispatcher() = default; |
265 | 303 |
304 // static | |
305 std::unique_ptr<InternalResponse> InternalResponse::createResponse(int callId, s td::unique_ptr<Serializable> params) | |
306 { | |
307 return std::unique_ptr<InternalResponse>(new InternalResponse(callId, String (), std::move(params))); | |
308 } | |
309 | |
310 // static | |
311 std::unique_ptr<InternalResponse> InternalResponse::createNotification(const Str ing& method, std::unique_ptr<Serializable> params) | |
312 { | |
313 return std::unique_ptr<InternalResponse>(new InternalResponse(0, method, std ::move(params))); | |
314 } | |
315 | |
316 String InternalResponse::serialize() | |
317 { | |
318 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); | |
319 if (m_type.length()) { | |
320 result->setString("method", m_type); | |
321 result->setValue("params", SerializedValue::create(m_params->serialize() )); | |
322 } else { | |
323 result->setInteger("id", m_callId); | |
324 result->setValue("result", SerializedValue::create(m_params->serialize() )); | |
325 } | |
326 return result->toJSONString(); | |
dgozman
2016/11/22 19:39:24
m_params.reset()
kozy
2016/11/22 21:01:26
Done.
| |
327 } | |
328 | |
329 InternalResponse::InternalResponse(int callId, const String& type, std::unique_p tr<Serializable> params) | |
330 : m_callId(callId) | |
331 , m_type(type) | |
332 , 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.
| |
333 { | |
334 } | |
335 | |
266 {% for namespace in config.protocol.namespace %} | 336 {% for namespace in config.protocol.namespace %} |
267 } // namespace {{namespace}} | 337 } // namespace {{namespace}} |
268 {% endfor %} | 338 {% endfor %} |
OLD | NEW |