| 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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 void UberDispatcher::setFallThroughForNotFound(bool fallThroughForNotFound) | 241 void UberDispatcher::setFallThroughForNotFound(bool fallThroughForNotFound) |
| 242 { | 242 { |
| 243 m_fallThroughForNotFound = fallThroughForNotFound; | 243 m_fallThroughForNotFound = fallThroughForNotFound; |
| 244 } | 244 } |
| 245 | 245 |
| 246 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) | 246 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) |
| 247 { | 247 { |
| 248 m_dispatchers[name] = std::move(dispatcher); | 248 m_dispatchers[name] = std::move(dispatcher); |
| 249 } | 249 } |
| 250 | 250 |
| 251 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) | 251 void UberDispatcher::setupRedirects(const HashMap<String, String>& redirects) |
| 252 { |
| 253 for (const auto& pair : redirects) |
| 254 m_redirects[pair.first] = pair.second; |
| 255 } |
| 256 |
| 257 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage, int* outCallId, String* outMethod) |
| 252 { | 258 { |
| 253 if (!parsedMessage) { | 259 if (!parsedMessage) { |
| 254 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); | 260 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); |
| 255 return DispatchResponse::kError; | 261 return DispatchResponse::kError; |
| 256 } | 262 } |
| 257 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); | 263 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); |
| 258 if (!messageObject) { | 264 if (!messageObject) { |
| 259 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must be an object"); | 265 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must be an object"); |
| 260 return DispatchResponse::kError; | 266 return DispatchResponse::kError; |
| 261 } | 267 } |
| 262 | 268 |
| 263 int callId = 0; | 269 int callId = 0; |
| 264 protocol::Value* callIdValue = messageObject->get("id"); | 270 protocol::Value* callIdValue = messageObject->get("id"); |
| 265 bool success = callIdValue && callIdValue->asInteger(&callId); | 271 bool success = callIdValue && callIdValue->asInteger(&callId); |
| 272 if (outCallId) |
| 273 *outCallId = callId; |
| 266 if (!success) { | 274 if (!success) { |
| 267 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must have integer 'id' porperty"); | 275 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must have integer 'id' porperty"); |
| 268 return DispatchResponse::kError; | 276 return DispatchResponse::kError; |
| 269 } | 277 } |
| 270 | 278 |
| 271 protocol::Value* methodValue = messageObject->get("method"); | 279 protocol::Value* methodValue = messageObject->get("method"); |
| 272 String method; | 280 String method; |
| 273 success = methodValue && methodValue->asString(&method); | 281 success = methodValue && methodValue->asString(&method); |
| 282 if (outMethod) |
| 283 *outMethod = method; |
| 274 if (!success) { | 284 if (!success) { |
| 275 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); | 285 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); |
| 276 return DispatchResponse::kError; | 286 return DispatchResponse::kError; |
| 277 } | 287 } |
| 278 | 288 |
| 289 HashMap<String, String>::iterator redirectIt = m_redirects.find(method); |
| 290 if (redirectIt != m_redirects.end()) |
| 291 method = redirectIt->second; |
| 292 |
| 279 size_t dotIndex = method.find("."); | 293 size_t dotIndex = method.find("."); |
| 280 if (dotIndex == StringUtil::kNotFound) { | 294 if (dotIndex == StringUtil::kNotFound) { |
| 281 if (m_fallThroughForNotFound) | 295 if (m_fallThroughForNotFound) |
| 282 return DispatchResponse::kFallThrough; | 296 return DispatchResponse::kFallThrough; |
| 283 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 297 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
| 284 return DispatchResponse::kError; | 298 return DispatchResponse::kError; |
| 285 } | 299 } |
| 286 String domain = StringUtil::substring(method, 0, dotIndex); | 300 String domain = StringUtil::substring(method, 0, dotIndex); |
| 287 auto it = m_dispatchers.find(domain); | 301 auto it = m_dispatchers.find(domain); |
| 288 if (it == m_dispatchers.end()) { | 302 if (it == m_dispatchers.end()) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 InternalResponse::InternalResponse(int callId, const String& notification, std::
unique_ptr<Serializable> params) | 339 InternalResponse::InternalResponse(int callId, const String& notification, std::
unique_ptr<Serializable> params) |
| 326 : m_callId(callId) | 340 : m_callId(callId) |
| 327 , m_notification(notification) | 341 , m_notification(notification) |
| 328 , m_params(params ? std::move(params) : nullptr) | 342 , m_params(params ? std::move(params) : nullptr) |
| 329 { | 343 { |
| 330 } | 344 } |
| 331 | 345 |
| 332 {% for namespace in config.protocol.namespace %} | 346 {% for namespace in config.protocol.namespace %} |
| 333 } // namespace {{namespace}} | 347 } // namespace {{namespace}} |
| 334 {% endfor %} | 348 {% endfor %} |
| OLD | NEW |