1 | /* |
2 | * Copyright (C) 2013-2017 Apple Inc. All Rights Reserved. |
3 | * Copyright (C) 2011 The Chromium Authors. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "InspectorBackendDispatcher.h" |
29 | |
30 | #include "InspectorFrontendRouter.h" |
31 | #include <wtf/JSONValues.h> |
32 | #include <wtf/SetForScope.h> |
33 | #include <wtf/text/CString.h> |
34 | #include <wtf/text/WTFString.h> |
35 | |
36 | namespace Inspector { |
37 | |
38 | SupplementalBackendDispatcher::SupplementalBackendDispatcher(BackendDispatcher& backendDispatcher) |
39 | : m_backendDispatcher(backendDispatcher) |
40 | { |
41 | } |
42 | |
43 | SupplementalBackendDispatcher::~SupplementalBackendDispatcher() |
44 | { |
45 | } |
46 | |
47 | BackendDispatcher::CallbackBase::CallbackBase(Ref<BackendDispatcher>&& backendDispatcher, long requestId) |
48 | : m_backendDispatcher(WTFMove(backendDispatcher)) |
49 | , m_requestId(requestId) |
50 | { |
51 | } |
52 | |
53 | bool BackendDispatcher::CallbackBase::isActive() const |
54 | { |
55 | return !m_alreadySent && m_backendDispatcher->isActive(); |
56 | } |
57 | |
58 | void BackendDispatcher::CallbackBase::sendFailure(const ErrorString& error) |
59 | { |
60 | ASSERT(error.length()); |
61 | |
62 | if (m_alreadySent) |
63 | return; |
64 | |
65 | m_alreadySent = true; |
66 | |
67 | // Immediately send an error message since this is an async response with a single error. |
68 | m_backendDispatcher->reportProtocolError(m_requestId, ServerError, error); |
69 | m_backendDispatcher->sendPendingErrors(); |
70 | } |
71 | |
72 | void BackendDispatcher::CallbackBase::sendSuccess(RefPtr<JSON::Object>&& partialMessage) |
73 | { |
74 | if (m_alreadySent) |
75 | return; |
76 | |
77 | m_alreadySent = true; |
78 | m_backendDispatcher->sendResponse(m_requestId, WTFMove(partialMessage), false); |
79 | } |
80 | |
81 | BackendDispatcher::BackendDispatcher(Ref<FrontendRouter>&& router) |
82 | : m_frontendRouter(WTFMove(router)) |
83 | { |
84 | } |
85 | |
86 | Ref<BackendDispatcher> BackendDispatcher::create(Ref<FrontendRouter>&& router) |
87 | { |
88 | return adoptRef(*new BackendDispatcher(WTFMove(router))); |
89 | } |
90 | |
91 | bool BackendDispatcher::isActive() const |
92 | { |
93 | return m_frontendRouter->hasFrontends(); |
94 | } |
95 | |
96 | void BackendDispatcher::registerDispatcherForDomain(const String& domain, SupplementalBackendDispatcher* dispatcher) |
97 | { |
98 | ASSERT_ARG(dispatcher, dispatcher); |
99 | |
100 | // FIXME: <https://webkit.org/b/148492> Agents should only register with the backend once, |
101 | // and we should re-add the assertion that only one dispatcher is registered per domain. |
102 | m_dispatchers.set(domain, dispatcher); |
103 | } |
104 | |
105 | void BackendDispatcher::dispatch(const String& message) |
106 | { |
107 | Ref<BackendDispatcher> protect(*this); |
108 | |
109 | ASSERT(!m_protocolErrors.size()); |
110 | |
111 | long requestId = 0; |
112 | RefPtr<JSON::Object> messageObject; |
113 | |
114 | { |
115 | // In case this is a re-entrant call from a nested run loop, we don't want to lose |
116 | // the outer request's id just because the inner request is bogus. |
117 | SetForScope<Optional<long>> scopedRequestId(m_currentRequestId, WTF::nullopt); |
118 | |
119 | RefPtr<JSON::Value> parsedMessage; |
120 | if (!JSON::Value::parseJSON(message, parsedMessage)) { |
121 | reportProtocolError(ParseError, "Message must be in JSON format"_s ); |
122 | sendPendingErrors(); |
123 | return; |
124 | } |
125 | |
126 | if (!parsedMessage->asObject(messageObject)) { |
127 | reportProtocolError(InvalidRequest, "Message must be a JSONified object"_s ); |
128 | sendPendingErrors(); |
129 | return; |
130 | } |
131 | |
132 | RefPtr<JSON::Value> requestIdValue; |
133 | if (!messageObject->getValue("id"_s , requestIdValue)) { |
134 | reportProtocolError(InvalidRequest, "'id' property was not found"_s ); |
135 | sendPendingErrors(); |
136 | return; |
137 | } |
138 | |
139 | if (!requestIdValue->asInteger(requestId)) { |
140 | reportProtocolError(InvalidRequest, "The type of 'id' property must be integer"_s ); |
141 | sendPendingErrors(); |
142 | return; |
143 | } |
144 | } |
145 | |
146 | { |
147 | // We could be called re-entrantly from a nested run loop, so restore the previous id. |
148 | SetForScope<Optional<long>> scopedRequestId(m_currentRequestId, requestId); |
149 | |
150 | RefPtr<JSON::Value> methodValue; |
151 | if (!messageObject->getValue("method"_s , methodValue)) { |
152 | reportProtocolError(InvalidRequest, "'method' property wasn't found"_s ); |
153 | sendPendingErrors(); |
154 | return; |
155 | } |
156 | |
157 | String methodString; |
158 | if (!methodValue->asString(methodString)) { |
159 | reportProtocolError(InvalidRequest, "The type of 'method' property must be string"_s ); |
160 | sendPendingErrors(); |
161 | return; |
162 | } |
163 | |
164 | Vector<String> domainAndMethod = methodString.splitAllowingEmptyEntries('.'); |
165 | if (domainAndMethod.size() != 2 || !domainAndMethod[0].length() || !domainAndMethod[1].length()) { |
166 | reportProtocolError(InvalidRequest, "The 'method' property was formatted incorrectly. It should be 'Domain.method'"_s ); |
167 | sendPendingErrors(); |
168 | return; |
169 | } |
170 | |
171 | String domain = domainAndMethod[0]; |
172 | SupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain); |
173 | if (!domainDispatcher) { |
174 | reportProtocolError(MethodNotFound, "'" + domain + "' domain was not found" ); |
175 | sendPendingErrors(); |
176 | return; |
177 | } |
178 | |
179 | String method = domainAndMethod[1]; |
180 | domainDispatcher->dispatch(requestId, method, messageObject.releaseNonNull()); |
181 | |
182 | if (m_protocolErrors.size()) |
183 | sendPendingErrors(); |
184 | } |
185 | } |
186 | |
187 | // FIXME: remove this function when legacy InspectorObject symbols are no longer needed <http://webkit.org/b/179847>. |
188 | void BackendDispatcher::sendResponse(long requestId, RefPtr<JSON::Object>&& result) |
189 | { |
190 | sendResponse(requestId, WTFMove(result), false); |
191 | } |
192 | |
193 | void BackendDispatcher::sendResponse(long requestId, RefPtr<JSON::Object>&& result, bool) |
194 | { |
195 | ASSERT(!m_protocolErrors.size()); |
196 | |
197 | // The JSON-RPC 2.0 specification requires that the "error" member have the value 'null' |
198 | // if no error occurred during an invocation, but we do not include it at all. |
199 | Ref<JSON::Object> responseMessage = JSON::Object::create(); |
200 | responseMessage->setObject("result"_s , WTFMove(result)); |
201 | responseMessage->setInteger("id"_s , requestId); |
202 | m_frontendRouter->sendResponse(responseMessage->toJSONString()); |
203 | } |
204 | |
205 | void BackendDispatcher::sendPendingErrors() |
206 | { |
207 | // These error codes are specified in JSON-RPC 2.0, Section 5.1. |
208 | static const int errorCodes[] = { |
209 | -32700, // ParseError |
210 | -32600, // InvalidRequest |
211 | -32601, // MethodNotFound |
212 | -32602, // InvalidParams |
213 | -32603, // InternalError |
214 | -32000, // ServerError |
215 | }; |
216 | |
217 | // To construct the error object, only use the last error's code and message. |
218 | // Per JSON-RPC 2.0, Section 5.1, the 'data' member may contain nested errors, |
219 | // but only one top-level Error object should be sent per request. |
220 | CommonErrorCode errorCode = InternalError; |
221 | String errorMessage; |
222 | Ref<JSON::Array> payload = JSON::Array::create(); |
223 | |
224 | for (auto& data : m_protocolErrors) { |
225 | errorCode = std::get<0>(data); |
226 | errorMessage = std::get<1>(data); |
227 | |
228 | ASSERT_ARG(errorCode, (unsigned)errorCode < WTF_ARRAY_LENGTH(errorCodes)); |
229 | ASSERT_ARG(errorCode, errorCodes[errorCode]); |
230 | |
231 | Ref<JSON::Object> error = JSON::Object::create(); |
232 | error->setInteger("code"_s , errorCodes[errorCode]); |
233 | error->setString("message"_s , errorMessage); |
234 | payload->pushObject(WTFMove(error)); |
235 | } |
236 | |
237 | Ref<JSON::Object> topLevelError = JSON::Object::create(); |
238 | topLevelError->setInteger("code"_s , errorCodes[errorCode]); |
239 | topLevelError->setString("message"_s , errorMessage); |
240 | topLevelError->setArray("data"_s , WTFMove(payload)); |
241 | |
242 | Ref<JSON::Object> message = JSON::Object::create(); |
243 | message->setObject("error"_s , WTFMove(topLevelError)); |
244 | if (m_currentRequestId) |
245 | message->setInteger("id"_s , m_currentRequestId.value()); |
246 | else { |
247 | // The 'null' value for an unknown id is specified in JSON-RPC 2.0, Section 5. |
248 | message->setValue("id"_s , JSON::Value::null()); |
249 | } |
250 | |
251 | m_frontendRouter->sendResponse(message->toJSONString()); |
252 | |
253 | m_protocolErrors.clear(); |
254 | m_currentRequestId = WTF::nullopt; |
255 | } |
256 | |
257 | void BackendDispatcher::reportProtocolError(CommonErrorCode errorCode, const String& errorMessage) |
258 | { |
259 | reportProtocolError(m_currentRequestId, errorCode, errorMessage); |
260 | } |
261 | |
262 | void BackendDispatcher::reportProtocolError(Optional<long> relatedRequestId, CommonErrorCode errorCode, const String& errorMessage) |
263 | { |
264 | ASSERT_ARG(errorCode, errorCode >= 0); |
265 | |
266 | // If the error was reported from an async callback, then no request id will be registered yet. |
267 | if (!m_currentRequestId) |
268 | m_currentRequestId = relatedRequestId; |
269 | |
270 | m_protocolErrors.append(std::tuple<CommonErrorCode, String>(errorCode, errorMessage)); |
271 | } |
272 | |
273 | template<typename T> |
274 | T BackendDispatcher::getPropertyValue(JSON::Object* object, const String& name, bool* out_optionalValueFound, T defaultValue, std::function<bool(JSON::Value&, T&)> asMethod, const char* typeName) |
275 | { |
276 | T result(defaultValue); |
277 | // out_optionalValueFound signals to the caller whether an optional property was found. |
278 | // if out_optionalValueFound == nullptr, then this is a required property. |
279 | if (out_optionalValueFound) |
280 | *out_optionalValueFound = false; |
281 | |
282 | if (!object) { |
283 | if (!out_optionalValueFound) |
284 | reportProtocolError(BackendDispatcher::InvalidParams, makeString("'params' object must contain required parameter '" , name, "' with type '" , typeName, "'." )); |
285 | return result; |
286 | } |
287 | |
288 | auto findResult = object->find(name); |
289 | if (findResult == object->end()) { |
290 | if (!out_optionalValueFound) |
291 | reportProtocolError(BackendDispatcher::InvalidParams, makeString("Parameter '" , name, "' with type '" , typeName, "' was not found." )); |
292 | return result; |
293 | } |
294 | |
295 | if (!asMethod(*findResult->value, result)) { |
296 | reportProtocolError(BackendDispatcher::InvalidParams, makeString("Parameter '" , name, "' has wrong type. It must be '" , typeName, "'." )); |
297 | return result; |
298 | } |
299 | |
300 | if (out_optionalValueFound) |
301 | *out_optionalValueFound = true; |
302 | |
303 | return result; |
304 | } |
305 | |
306 | static bool castToInteger(JSON::Value& value, int& result) { return value.asInteger(result); } |
307 | static bool castToNumber(JSON::Value& value, double& result) { return value.asDouble(result); } |
308 | |
309 | int BackendDispatcher::getInteger(JSON::Object* object, const String& name, bool* valueFound) |
310 | { |
311 | return getPropertyValue<int>(object, name, valueFound, 0, &castToInteger, "Integer" ); |
312 | } |
313 | |
314 | double BackendDispatcher::getDouble(JSON::Object* object, const String& name, bool* valueFound) |
315 | { |
316 | return getPropertyValue<double>(object, name, valueFound, 0, &castToNumber, "Number" ); |
317 | } |
318 | |
319 | String BackendDispatcher::getString(JSON::Object* object, const String& name, bool* valueFound) |
320 | { |
321 | return getPropertyValue<String>(object, name, valueFound, "" , &JSON::Value::asString, "String" ); |
322 | } |
323 | |
324 | bool BackendDispatcher::getBoolean(JSON::Object* object, const String& name, bool* valueFound) |
325 | { |
326 | return getPropertyValue<bool>(object, name, valueFound, false, &JSON::Value::asBoolean, "Boolean" ); |
327 | } |
328 | |
329 | RefPtr<JSON::Object> BackendDispatcher::getObject(JSON::Object* object, const String& name, bool* valueFound) |
330 | { |
331 | return getPropertyValue<RefPtr<JSON::Object>>(object, name, valueFound, nullptr, &JSON::Value::asObject, "Object" ); |
332 | } |
333 | |
334 | RefPtr<JSON::Array> BackendDispatcher::getArray(JSON::Object* object, const String& name, bool* valueFound) |
335 | { |
336 | return getPropertyValue<RefPtr<JSON::Array>>(object, name, valueFound, nullptr, &JSON::Value::asArray, "Array" ); |
337 | } |
338 | |
339 | RefPtr<JSON::Value> BackendDispatcher::getValue(JSON::Object* object, const String& name, bool* valueFound) |
340 | { |
341 | return getPropertyValue<RefPtr<JSON::Value>>(object, name, valueFound, nullptr, &JSON::Value::asValue, "Value" ); |
342 | } |
343 | |
344 | } // namespace Inspector |
345 | |