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