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 %} |
11 | 11 |
12 // static | 12 // static |
13 const char DispatcherBase::kInvalidRequest[] = "Invalid request"; | 13 DispatchResponse DispatchResponse::OK() |
| 14 { |
| 15 DispatchResponse result; |
| 16 result.m_status = kSuccess; |
| 17 result.m_errorCode = kParseError; |
| 18 return result; |
| 19 } |
| 20 |
| 21 // static |
| 22 DispatchResponse DispatchResponse::Error(const String& error) |
| 23 { |
| 24 DispatchResponse result; |
| 25 result.m_status = kError; |
| 26 result.m_errorCode = kServerError; |
| 27 result.m_errorMessage = error; |
| 28 return result; |
| 29 } |
| 30 |
| 31 // static |
| 32 DispatchResponse DispatchResponse::InternalError() |
| 33 { |
| 34 DispatchResponse result; |
| 35 result.m_status = kError; |
| 36 result.m_errorCode = kInternalError; |
| 37 result.m_errorMessage = "Internal error"; |
| 38 return result; |
| 39 } |
| 40 |
| 41 // static |
| 42 DispatchResponse DispatchResponse::FallThrough() |
| 43 { |
| 44 DispatchResponse result; |
| 45 result.m_status = kFallThrough; |
| 46 result.m_errorCode = kParseError; |
| 47 return result; |
| 48 } |
| 49 |
| 50 // static |
| 51 const char DispatcherBase::kInvalidParamsString[] = "Invalid parameters"; |
14 | 52 |
15 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } | 53 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } |
16 | 54 |
17 DispatcherBase::WeakPtr::~WeakPtr() | 55 DispatcherBase::WeakPtr::~WeakPtr() |
18 { | 56 { |
19 if (m_dispatcher) | 57 if (m_dispatcher) |
20 m_dispatcher->m_weakPtrs.erase(this); | 58 m_dispatcher->m_weakPtrs.erase(this); |
21 } | 59 } |
22 | 60 |
23 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) |
24 : m_backendImpl(std::move(backendImpl)) | 62 : m_backendImpl(std::move(backendImpl)) |
25 , m_callId(callId) { } | 63 , m_callId(callId) { } |
26 | 64 |
27 DispatcherBase::Callback::~Callback() = default; | 65 DispatcherBase::Callback::~Callback() = default; |
28 | 66 |
29 void DispatcherBase::Callback::dispose() | 67 void DispatcherBase::Callback::dispose() |
30 { | 68 { |
31 m_backendImpl = nullptr; | 69 m_backendImpl = nullptr; |
32 } | 70 } |
33 | 71 |
34 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const ErrorString& invocationError) | 72 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const DispatchResponse& response) |
35 { | 73 { |
36 if (!m_backendImpl || !m_backendImpl->get()) | 74 if (!m_backendImpl || !m_backendImpl->get()) |
37 return; | 75 return; |
38 m_backendImpl->get()->sendResponse(m_callId, invocationError, nullptr, std::
move(partialMessage)); | 76 m_backendImpl->get()->sendResponse(m_callId, response, std::move(partialMess
age)); |
39 m_backendImpl = nullptr; | 77 m_backendImpl = nullptr; |
40 } | 78 } |
41 | 79 |
42 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) | 80 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) |
43 : m_frontendChannel(frontendChannel) { } | 81 : m_frontendChannel(frontendChannel) { } |
44 | 82 |
45 DispatcherBase::~DispatcherBase() | 83 DispatcherBase::~DispatcherBase() |
46 { | 84 { |
47 clearFrontend(); | 85 clearFrontend(); |
48 } | 86 } |
49 | 87 |
50 // static | 88 // static |
51 bool DispatcherBase::getCommandName(const String& message, String* result) | 89 bool DispatcherBase::getCommandName(const String& message, String* result) |
52 { | 90 { |
53 std::unique_ptr<protocol::Value> value = parseJSON(message); | 91 std::unique_ptr<protocol::Value> value = parseJSON(message); |
54 if (!value) | 92 if (!value) |
55 return false; | 93 return false; |
56 | 94 |
57 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 95 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
58 if (!object) | 96 if (!object) |
59 return false; | 97 return false; |
60 | 98 |
61 if (!object->getString("method", result)) | 99 if (!object->getString("method", result)) |
62 return false; | 100 return false; |
63 | 101 |
64 return true; | 102 return true; |
65 } | 103 } |
66 | 104 |
67 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, ErrorSupport* errors, std::unique_ptr<protocol::DictionaryValue> result) | 105 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response,
std::unique_ptr<protocol::DictionaryValue> result) |
68 { | 106 { |
69 if (invocationError.length() || (errors && errors->hasErrors())) { | 107 if (response.status() == DispatchResponse::kError) { |
70 reportProtocolError(callId, ServerError, invocationError, errors); | 108 reportProtocolError(callId, response.errorCode(), response.errorMessage(
), nullptr); |
71 return; | 109 return; |
72 } | 110 } |
73 | 111 |
74 std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue
::create(); | 112 std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue
::create(); |
75 responseMessage->setInteger("id", callId); | 113 responseMessage->setInteger("id", callId); |
76 responseMessage->setObject("result", std::move(result)); | 114 responseMessage->setObject("result", std::move(result)); |
77 if (m_frontendChannel) | 115 if (m_frontendChannel) |
78 m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONS
tring()); | 116 m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONS
tring()); |
79 } | 117 } |
80 | 118 |
81 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, std::unique_ptr<protocol::DictionaryValue> result) | 119 void DispatcherBase::sendResponse(int callId, const DispatchResponse& response) |
82 { | 120 { |
83 sendResponse(callId, invocationError, nullptr, std::move(result)); | 121 sendResponse(callId, response, DictionaryValue::create()); |
84 } | 122 } |
85 | 123 |
86 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) | 124 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatchResponse::ErrorCode code, const String& errorMessage, ErrorSupport* erro
rs) |
87 { | |
88 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); | |
89 } | |
90 | |
91 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatcherBase::CommonErrorCode code, const String& errorMessage, ErrorSupport*
errors) | |
92 { | 125 { |
93 if (!frontendChannel) | 126 if (!frontendChannel) |
94 return; | 127 return; |
95 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; | 128 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; |
96 error->setInteger("code", code); | 129 error->setInteger("code", code); |
97 error->setString("message", errorMessage); | 130 error->setString("message", errorMessage); |
98 DCHECK(error); | |
99 if (errors && errors->hasErrors()) | 131 if (errors && errors->hasErrors()) |
100 error->setString("data", errors->errors()); | 132 error->setString("data", errors->errors()); |
101 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); | 133 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); |
102 message->setObject("error", std::move(error)); | 134 message->setObject("error", std::move(error)); |
103 message->setInteger("id", callId); | 135 message->setInteger("id", callId); |
104 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); | 136 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); |
105 } | 137 } |
106 | 138 |
107 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String& errorMessage, ErrorSupport* errors) | 139 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, DispatchResp
onse::ErrorCode code, const String& errorMessage) |
| 140 { |
| 141 if (!frontendChannel) |
| 142 return; |
| 143 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; |
| 144 error->setInteger("code", code); |
| 145 error->setString("message", errorMessage); |
| 146 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); |
| 147 message->setObject("error", std::move(error)); |
| 148 frontendChannel->sendProtocolNotification(message->toJSONString()); |
| 149 } |
| 150 |
| 151 void DispatcherBase::reportProtocolError(int callId, DispatchResponse::ErrorCode
code, const String& errorMessage, ErrorSupport* errors) |
108 { | 152 { |
109 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors)
; | 153 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors)
; |
110 } | 154 } |
111 | 155 |
112 void DispatcherBase::clearFrontend() | 156 void DispatcherBase::clearFrontend() |
113 { | 157 { |
114 m_frontendChannel = nullptr; | 158 m_frontendChannel = nullptr; |
115 for (auto& weak : m_weakPtrs) | 159 for (auto& weak : m_weakPtrs) |
116 weak->dispose(); | 160 weak->dispose(); |
117 m_weakPtrs.clear(); | 161 m_weakPtrs.clear(); |
118 } | 162 } |
119 | 163 |
120 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() | 164 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() |
121 { | 165 { |
122 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); | 166 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); |
123 m_weakPtrs.insert(weak.get()); | 167 m_weakPtrs.insert(weak.get()); |
124 return weak; | 168 return weak; |
125 } | 169 } |
126 | 170 |
127 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) | 171 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) |
128 : m_frontendChannel(frontendChannel) { } | 172 : m_frontendChannel(frontendChannel) { } |
129 | 173 |
130 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) | 174 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) |
131 { | 175 { |
132 m_dispatchers[name] = std::move(dispatcher); | 176 m_dispatchers[name] = std::move(dispatcher); |
133 } | 177 } |
134 | 178 |
135 void UberDispatcher::dispatch(std::unique_ptr<Value> parsedMessage) | 179 DispatchResponse::Status UberDispatcher::dispatch(std::unique_ptr<Value> parsedM
essage) |
136 { | 180 { |
137 if (!parsedMessage) | 181 if (!parsedMessage) { |
138 return; | 182 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kParseError,
"Message must be a valid JSON"); |
| 183 return DispatchResponse::kError; |
| 184 } |
139 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); | 185 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); |
140 if (!messageObject) | 186 if (!messageObject) { |
141 return; | 187 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must be an object"); |
| 188 return DispatchResponse::kError; |
| 189 } |
142 | 190 |
143 int callId = 0; | 191 int callId = 0; |
144 protocol::Value* callIdValue = messageObject->get("id"); | 192 protocol::Value* callIdValue = messageObject->get("id"); |
145 bool success = callIdValue && callIdValue->asInteger(&callId); | 193 bool success = callIdValue && callIdValue->asInteger(&callId); |
146 if (!success) | 194 if (!success) { |
147 return; | 195 reportProtocolErrorTo(m_frontendChannel, DispatchResponse::kInvalidReque
st, "Message must have integer 'id' porperty"); |
| 196 return DispatchResponse::kError; |
| 197 } |
148 | 198 |
149 protocol::Value* methodValue = messageObject->get("method"); | 199 protocol::Value* methodValue = messageObject->get("method"); |
150 String method; | 200 String method; |
151 success = methodValue && methodValue->asString(&method); | 201 success = methodValue && methodValue->asString(&method); |
152 if (!success) | 202 if (!success) { |
153 return; | 203 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kInva
lidRequest, "Message must have string 'method' porperty", nullptr); |
| 204 return DispatchResponse::kError; |
| 205 } |
154 | 206 |
155 size_t dotIndex = method.find("."); | 207 size_t dotIndex = method.find("."); |
156 if (dotIndex == StringUtil::kNotFound) { | 208 if (dotIndex == StringUtil::kNotFound) { |
157 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); | 209 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
158 return; | 210 return DispatchResponse::kError; |
159 } | 211 } |
160 String domain = StringUtil::substring(method, 0, dotIndex); | 212 String domain = StringUtil::substring(method, 0, dotIndex); |
161 auto it = m_dispatchers.find(domain); | 213 auto it = m_dispatchers.find(domain); |
162 if (it == m_dispatchers.end()) { | 214 if (it == m_dispatchers.end()) { |
163 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); | 215 reportProtocolErrorTo(m_frontendChannel, callId, DispatchResponse::kMeth
odNotFound, "'" + method + "' wasn't found", nullptr); |
164 return; | 216 return DispatchResponse::kError; |
165 } | 217 } |
166 it->second->dispatch(callId, method, std::move(messageObject)); | 218 return it->second->dispatch(callId, method, std::move(messageObject)); |
167 } | 219 } |
168 | 220 |
169 UberDispatcher::~UberDispatcher() = default; | 221 UberDispatcher::~UberDispatcher() = default; |
170 | 222 |
171 {% for namespace in config.protocol.namespace %} | 223 {% for namespace in config.protocol.namespace %} |
172 } // namespace {{namespace}} | 224 } // namespace {{namespace}} |
173 {% endfor %} | 225 {% endfor %} |
OLD | NEW |