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 21 matching lines...) Expand all Loading... |
32 DispatchResponse DispatchResponse::InternalError() | 32 DispatchResponse DispatchResponse::InternalError() |
33 { | 33 { |
34 DispatchResponse result; | 34 DispatchResponse result; |
35 result.m_status = kError; | 35 result.m_status = kError; |
36 result.m_errorCode = kInternalError; | 36 result.m_errorCode = kInternalError; |
37 result.m_errorMessage = "Internal error"; | 37 result.m_errorMessage = "Internal error"; |
38 return result; | 38 return result; |
39 } | 39 } |
40 | 40 |
41 // static | 41 // static |
| 42 DispatchResponse DispatchResponse::InvalidParams(const String& error) |
| 43 { |
| 44 DispatchResponse result; |
| 45 result.m_status = kError; |
| 46 result.m_errorCode = kInvalidParams; |
| 47 result.m_errorMessage = error; |
| 48 return result; |
| 49 } |
| 50 |
| 51 // static |
42 DispatchResponse DispatchResponse::FallThrough() | 52 DispatchResponse DispatchResponse::FallThrough() |
43 { | 53 { |
44 DispatchResponse result; | 54 DispatchResponse result; |
45 result.m_status = kFallThrough; | 55 result.m_status = kFallThrough; |
46 result.m_errorCode = kParseError; | 56 result.m_errorCode = kParseError; |
47 return result; | 57 return result; |
48 } | 58 } |
49 | 59 |
50 // static | 60 // static |
51 const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters"; | 61 const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters"; |
52 | 62 |
53 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } | 63 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } |
54 | 64 |
55 DispatcherBase::WeakPtr::~WeakPtr() | 65 DispatcherBase::WeakPtr::~WeakPtr() |
56 { | 66 { |
57 if (m_dispatcher) | 67 if (m_dispatcher) |
58 m_dispatcher->m_weakPtrs.erase(this); | 68 m_dispatcher->m_weakPtrs.erase(this); |
59 } | 69 } |
60 | 70 |
61 DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> back
endImpl, int callId) | 71 DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> back
endImpl, int callId, int callbackId) |
62 : m_backendImpl(std::move(backendImpl)) | 72 : m_backendImpl(std::move(backendImpl)) |
63 , m_callId(callId) { } | 73 , m_callId(callId) |
| 74 , m_callbackId(callbackId) { } |
64 | 75 |
65 DispatcherBase::Callback::~Callback() = default; | 76 DispatcherBase::Callback::~Callback() = default; |
66 | 77 |
67 void DispatcherBase::Callback::dispose() | 78 void DispatcherBase::Callback::dispose() |
68 { | 79 { |
69 m_backendImpl = nullptr; | 80 m_backendImpl = nullptr; |
70 } | 81 } |
71 | 82 |
72 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const DispatchResponse& response) | 83 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const DispatchResponse& response) |
73 { | 84 { |
74 if (!m_backendImpl || !m_backendImpl->get()) | 85 if (!m_backendImpl || !m_backendImpl->get()) |
75 return; | 86 return; |
76 m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMess
age)); | 87 m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMess
age)); |
77 m_backendImpl = nullptr; | 88 m_backendImpl = nullptr; |
78 } | 89 } |
79 | 90 |
| 91 void DispatcherBase::Callback::fallThroughIfActive() |
| 92 { |
| 93 if (!m_backendImpl || !m_backendImpl->get()) |
| 94 return; |
| 95 m_backendImpl->get()->markFallThrough(m_callbackId); |
| 96 m_backendImpl = nullptr; |
| 97 } |
| 98 |
80 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) | 99 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) |
81 : m_frontendChannel(frontendChannel) { } | 100 : m_frontendChannel(frontendChannel) |
| 101 , m_lastCallbackId(0) |
| 102 , m_lastCallbackFallThrough(false) { } |
82 | 103 |
83 DispatcherBase::~DispatcherBase() | 104 DispatcherBase::~DispatcherBase() |
84 { | 105 { |
85 clearFrontend(); | 106 clearFrontend(); |
86 } | 107 } |
87 | 108 |
| 109 int DispatcherBase::nextCallbackId() |
| 110 { |
| 111 m_lastCallbackFallThrough = false; |
| 112 return ++m_lastCallbackId; |
| 113 } |
| 114 |
| 115 void DispatcherBase::markFallThrough(int callbackId) |
| 116 { |
| 117 DCHECK(callbackId == m_lastCallbackId); |
| 118 m_lastCallbackFallThrough = true; |
| 119 } |
| 120 |
88 // static | 121 // static |
89 bool DispatcherBase::getCommandName(const String& message, String* result) | 122 bool DispatcherBase::getCommandName(const String& message, String* result) |
90 { | 123 { |
91 std::unique_ptr<protocol::Value> value = parseJSON(message); | 124 std::unique_ptr<protocol::Value> value = StringUtil::parseJSON(message); |
92 if (!value) | 125 if (!value) |
93 return false; | 126 return false; |
94 | 127 |
95 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 128 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
96 if (!object) | 129 if (!object) |
97 return false; | 130 return false; |
98 | 131 |
99 if (!object->getString("method", result)) | 132 if (!object->getString("method", result)) |
100 return false; | 133 return false; |
101 | 134 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 195 } |
163 | 196 |
164 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() | 197 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() |
165 { | 198 { |
166 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); | 199 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); |
167 m_weakPtrs.insert(weak.get()); | 200 m_weakPtrs.insert(weak.get()); |
168 return weak; | 201 return weak; |
169 } | 202 } |
170 | 203 |
171 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) | 204 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) |
172 : m_frontendChannel(frontendChannel) { } | 205 : m_frontendChannel(frontendChannel) |
| 206 , m_fallThroughForNotFound(false) { } |
| 207 |
| 208 void UberDispatcher::setFallThroughForNotFound(bool fallThroughForNotFound) |
| 209 { |
| 210 m_fallThroughForNotFound = fallThroughForNotFound; |
| 211 } |
173 | 212 |
174 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) | 213 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) |
175 { | 214 { |
176 m_dispatchers[name] = std::move(dispatcher); | 215 m_dispatchers[name] = std::move(dispatcher); |
177 } | 216 } |
178 | 217 |
179 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) | 218 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) |
180 { | 219 { |
181 if (!parsedMessage) { | 220 if (!parsedMessage) { |
182 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); | 221 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); |
(...skipping 16 matching lines...) Expand all Loading... |
199 protocol::Value* methodValue = messageObject->get("method"); | 238 protocol::Value* methodValue = messageObject->get("method"); |
200 String method; | 239 String method; |
201 success = methodValue && methodValue->asString(&method); | 240 success = methodValue && methodValue->asString(&method); |
202 if (!success) { | 241 if (!success) { |
203 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); | 242 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); |
204 return DispatchResponse::kError; | 243 return DispatchResponse::kError; |
205 } | 244 } |
206 | 245 |
207 size_t dotIndex = method.find("."); | 246 size_t dotIndex = method.find("."); |
208 if (dotIndex == StringUtil::kNotFound) { | 247 if (dotIndex == StringUtil::kNotFound) { |
| 248 if (m_fallThroughForNotFound) |
| 249 return DispatchResponse::kFallThrough; |
209 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 250 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
210 return DispatchResponse::kError; | 251 return DispatchResponse::kError; |
211 } | 252 } |
212 String domain = StringUtil::substring(method, 0, dotIndex); | 253 String domain = StringUtil::substring(method, 0, dotIndex); |
213 auto it = m_dispatchers.find(domain); | 254 auto it = m_dispatchers.find(domain); |
214 if (it == m_dispatchers.end()) { | 255 if (it == m_dispatchers.end()) { |
| 256 if (m_fallThroughForNotFound) |
| 257 return DispatchResponse::kFallThrough; |
215 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 258 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
216 return DispatchResponse::kError; | 259 return DispatchResponse::kError; |
217 } | 260 } |
218 return it->second->dispatch(callId, method, std::move(messageObject)); | 261 return it->second->dispatch(callId, method, std::move(messageObject)); |
219 } | 262 } |
220 | 263 |
221 UberDispatcher::~UberDispatcher() = default; | 264 UberDispatcher::~UberDispatcher() = default; |
222 | 265 |
223 {% for namespace in config.protocol.namespace %} | 266 {% for namespace in config.protocol.namespace %} |
224 } // namespace {{namespace}} | 267 } // namespace {{namespace}} |
225 {% endfor %} | 268 {% endfor %} |
OLD | NEW |