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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters"; | 51 const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters"; |
52 | 52 |
53 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } | 53 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } |
54 | 54 |
55 DispatcherBase::WeakPtr::~WeakPtr() | 55 DispatcherBase::WeakPtr::~WeakPtr() |
56 { | 56 { |
57 if (m_dispatcher) | 57 if (m_dispatcher) |
58 m_dispatcher->m_weakPtrs.erase(this); | 58 m_dispatcher->m_weakPtrs.erase(this); |
59 } | 59 } |
60 | 60 |
61 DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> back
endImpl, int callId) | 61 DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> back
endImpl, int callId, int callbackId) |
62 : m_backendImpl(std::move(backendImpl)) | 62 : m_backendImpl(std::move(backendImpl)) |
63 , m_callId(callId) { } | 63 , m_callId(callId) |
| 64 , m_callbackId(callbackId) { } |
64 | 65 |
65 DispatcherBase::Callback::~Callback() = default; | 66 DispatcherBase::Callback::~Callback() = default; |
66 | 67 |
67 void DispatcherBase::Callback::dispose() | 68 void DispatcherBase::Callback::dispose() |
68 { | 69 { |
69 m_backendImpl = nullptr; | 70 m_backendImpl = nullptr; |
70 } | 71 } |
71 | 72 |
72 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const DispatchResponse& response) | 73 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const DispatchResponse& response) |
73 { | 74 { |
74 if (!m_backendImpl || !m_backendImpl->get()) | 75 if (!m_backendImpl || !m_backendImpl->get()) |
75 return; | 76 return; |
76 m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMess
age)); | 77 m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMess
age)); |
77 m_backendImpl = nullptr; | 78 m_backendImpl = nullptr; |
78 } | 79 } |
79 | 80 |
| 81 void DispatcherBase::Callback::fallThroughIfActive() |
| 82 { |
| 83 if (!m_backendImpl || !m_backendImpl->get()) |
| 84 return; |
| 85 m_backendImpl->get()->markFallThrough(m_callbackId); |
| 86 m_backendImpl = nullptr; |
| 87 } |
| 88 |
80 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) | 89 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) |
81 : m_frontendChannel(frontendChannel) { } | 90 : m_frontendChannel(frontendChannel) |
| 91 , m_lastCallbackId(0) |
| 92 , m_lastCallbackFallThrough(false) { } |
82 | 93 |
83 DispatcherBase::~DispatcherBase() | 94 DispatcherBase::~DispatcherBase() |
84 { | 95 { |
85 clearFrontend(); | 96 clearFrontend(); |
86 } | 97 } |
87 | 98 |
| 99 int DispatcherBase::nextCallbackId() |
| 100 { |
| 101 m_lastCallbackFallThrough = false; |
| 102 return ++m_lastCallbackId; |
| 103 } |
| 104 |
| 105 void DispatcherBase::markFallThrough(int callbackId) |
| 106 { |
| 107 if (callbackId == m_lastCallbackId) |
| 108 m_lastCallbackFallThrough = true; |
| 109 } |
| 110 |
88 // static | 111 // static |
89 bool DispatcherBase::getCommandName(const String& message, String* result) | 112 bool DispatcherBase::getCommandName(const String& message, String* result) |
90 { | 113 { |
91 std::unique_ptr<protocol::Value> value = StringUtil::parseJSON(message); | 114 std::unique_ptr<protocol::Value> value = StringUtil::parseJSON(message); |
92 if (!value) | 115 if (!value) |
93 return false; | 116 return false; |
94 | 117 |
95 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 118 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
96 if (!object) | 119 if (!object) |
97 return false; | 120 return false; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() | 187 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() |
165 { | 188 { |
166 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); | 189 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); |
167 m_weakPtrs.insert(weak.get()); | 190 m_weakPtrs.insert(weak.get()); |
168 return weak; | 191 return weak; |
169 } | 192 } |
170 | 193 |
171 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) | 194 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) |
172 : m_frontendChannel(frontendChannel) { } | 195 : m_frontendChannel(frontendChannel) { } |
173 | 196 |
| 197 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel, bool fallThroug
hForNotFound) |
| 198 : m_frontendChannel(frontendChannel) |
| 199 , m_fallThroughForNotFound(fallThroughForNotFound) { } |
| 200 |
174 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) | 201 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) |
175 { | 202 { |
176 m_dispatchers[name] = std::move(dispatcher); | 203 m_dispatchers[name] = std::move(dispatcher); |
177 } | 204 } |
178 | 205 |
179 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) | 206 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) |
180 { | 207 { |
181 if (!parsedMessage) { | 208 if (!parsedMessage) { |
182 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); | 209 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); |
183 return DispatchResponse::kError; | 210 return DispatchResponse::kError; |
(...skipping 15 matching lines...) Expand all Loading... |
199 protocol::Value* methodValue = messageObject->get("method"); | 226 protocol::Value* methodValue = messageObject->get("method"); |
200 String method; | 227 String method; |
201 success = methodValue && methodValue->asString(&method); | 228 success = methodValue && methodValue->asString(&method); |
202 if (!success) { | 229 if (!success) { |
203 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); | 230 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); |
204 return DispatchResponse::kError; | 231 return DispatchResponse::kError; |
205 } | 232 } |
206 | 233 |
207 size_t dotIndex = method.find("."); | 234 size_t dotIndex = method.find("."); |
208 if (dotIndex == StringUtil::kNotFound) { | 235 if (dotIndex == StringUtil::kNotFound) { |
| 236 if (m_fallThroughForNotFound) |
| 237 return DispatchResponse::kFallThrough; |
209 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 238 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
210 return DispatchResponse::kError; | 239 return DispatchResponse::kError; |
211 } | 240 } |
212 String domain = StringUtil::substring(method, 0, dotIndex); | 241 String domain = StringUtil::substring(method, 0, dotIndex); |
213 auto it = m_dispatchers.find(domain); | 242 auto it = m_dispatchers.find(domain); |
214 if (it == m_dispatchers.end()) { | 243 if (it == m_dispatchers.end()) { |
| 244 if (m_fallThroughForNotFound) |
| 245 return DispatchResponse::kFallThrough; |
215 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); | 246 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
216 return DispatchResponse::kError; | 247 return DispatchResponse::kError; |
217 } | 248 } |
218 return it->second->dispatch(callId, method, std::move(messageObject)); | 249 return it->second->dispatch(callId, method, std::move(messageObject)); |
219 } | 250 } |
220 | 251 |
221 UberDispatcher::~UberDispatcher() = default; | 252 UberDispatcher::~UberDispatcher() = default; |
222 | 253 |
223 {% for namespace in config.protocol.namespace %} | 254 {% for namespace in config.protocol.namespace %} |
224 } // namespace {{namespace}} | 255 } // namespace {{namespace}} |
225 {% endfor %} | 256 {% endfor %} |
OLD | NEW |