]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/InspectorBackendDispatcher.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / InspectorBackendDispatcher.cpp
1 /*
2 * Copyright (C) 2013 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 "InspectorFrontendChannel.h"
31 #include "InspectorValues.h"
32 #include <wtf/text/CString.h>
33 #include <wtf/text/WTFString.h>
34
35 namespace Inspector {
36
37 BackendDispatcher::CallbackBase::CallbackBase(Ref<BackendDispatcher>&& backendDispatcher, int id)
38 : m_backendDispatcher(WTF::move(backendDispatcher))
39 , m_id(id)
40 , m_alreadySent(false)
41 {
42 }
43
44 bool BackendDispatcher::CallbackBase::isActive() const
45 {
46 return !m_alreadySent && m_backendDispatcher->isActive();
47 }
48
49 void BackendDispatcher::CallbackBase::sendFailure(const ErrorString& error)
50 {
51 ASSERT(error.length());
52 sendIfActive(nullptr, error);
53 }
54
55 void BackendDispatcher::CallbackBase::sendIfActive(RefPtr<InspectorObject>&& partialMessage, const ErrorString& invocationError)
56 {
57 if (m_alreadySent)
58 return;
59
60 m_backendDispatcher->sendResponse(m_id, WTF::move(partialMessage), invocationError);
61 m_alreadySent = true;
62 }
63
64 Ref<BackendDispatcher> BackendDispatcher::create(FrontendChannel* frontendChannel)
65 {
66 return adoptRef(*new BackendDispatcher(frontendChannel));
67 }
68
69 void BackendDispatcher::registerDispatcherForDomain(const String& domain, SupplementalBackendDispatcher* dispatcher)
70 {
71 auto result = m_dispatchers.add(domain, dispatcher);
72 ASSERT_UNUSED(result, result.isNewEntry);
73 }
74
75 void BackendDispatcher::dispatch(const String& message)
76 {
77 Ref<BackendDispatcher> protect(*this);
78
79 RefPtr<InspectorValue> parsedMessage;
80 if (!InspectorValue::parseJSON(message, parsedMessage)) {
81 reportProtocolError(nullptr, ParseError, ASCIILiteral("Message must be in JSON format"));
82 return;
83 }
84
85 RefPtr<InspectorObject> messageObject;
86 if (!parsedMessage->asObject(messageObject)) {
87 reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("Message must be a JSONified object"));
88 return;
89 }
90
91 RefPtr<InspectorValue> callIdValue;
92 if (!messageObject->getValue(ASCIILiteral("id"), callIdValue)) {
93 reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("'id' property was not found"));
94 return;
95 }
96
97 long callId = 0;
98 if (!callIdValue->asInteger(callId)) {
99 reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("The type of 'id' property must be integer"));
100 return;
101 }
102
103 RefPtr<InspectorValue> methodValue;
104 if (!messageObject->getValue(ASCIILiteral("method"), methodValue)) {
105 reportProtocolError(&callId, InvalidRequest, ASCIILiteral("'method' property wasn't found"));
106 return;
107 }
108
109 String method;
110 if (!methodValue->asString(method)) {
111 reportProtocolError(&callId, InvalidRequest, ASCIILiteral("The type of 'method' property must be string"));
112 return;
113 }
114
115 size_t position = method.find('.');
116 if (position == WTF::notFound) {
117 reportProtocolError(&callId, InvalidRequest, ASCIILiteral("The 'method' property was formatted incorrectly. It should be 'Domain.method'"));
118 return;
119 }
120
121 String domain = method.substring(0, position);
122 SupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain);
123 if (!domainDispatcher) {
124 reportProtocolError(&callId, MethodNotFound, "'" + domain + "' domain was not found");
125 return;
126 }
127
128 String domainMethod = method.substring(position + 1);
129 domainDispatcher->dispatch(callId, domainMethod, messageObject.releaseNonNull());
130 }
131
132 void BackendDispatcher::sendResponse(long callId, RefPtr<InspectorObject>&& result, const ErrorString& invocationError)
133 {
134 if (!m_frontendChannel)
135 return;
136
137 if (invocationError.length()) {
138 reportProtocolError(&callId, ServerError, invocationError);
139 return;
140 }
141
142 Ref<InspectorObject> responseMessage = InspectorObject::create();
143 responseMessage->setObject(ASCIILiteral("result"), result);
144 responseMessage->setInteger(ASCIILiteral("id"), callId);
145 m_frontendChannel->sendMessageToFrontend(responseMessage->toJSONString());
146 }
147
148 void BackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage) const
149 {
150 reportProtocolError(callId, errorCode, errorMessage, nullptr);
151 }
152
153 void BackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage, RefPtr<Inspector::Protocol::Array<String>>&& data) const
154 {
155 static const int errorCodes[] = {
156 -32700, // ParseError
157 -32600, // InvalidRequest
158 -32601, // MethodNotFound
159 -32602, // InvalidParams
160 -32603, // InternalError
161 -32000, // ServerError
162 };
163
164 ASSERT_ARG(errorCode, errorCode >= 0);
165 ASSERT_ARG(errorCode, (unsigned)errorCode < WTF_ARRAY_LENGTH(errorCodes));
166 ASSERT_ARG(errorCode, errorCodes[errorCode]);
167
168 if (!m_frontendChannel)
169 return;
170
171 Ref<InspectorObject> error = InspectorObject::create();
172 error->setInteger(ASCIILiteral("code"), errorCodes[errorCode]);
173 error->setString(ASCIILiteral("message"), errorMessage);
174 if (data)
175 error->setArray(ASCIILiteral("data"), WTF::move(data));
176
177 Ref<InspectorObject> message = InspectorObject::create();
178 message->setObject(ASCIILiteral("error"), WTF::move(error));
179 if (callId)
180 message->setInteger(ASCIILiteral("id"), *callId);
181 else
182 message->setValue(ASCIILiteral("id"), InspectorValue::null());
183
184 m_frontendChannel->sendMessageToFrontend(message->toJSONString());
185 }
186
187 template<typename ReturnValueType, typename ValueType, typename DefaultValueType>
188 static ReturnValueType getPropertyValue(InspectorObject* object, const String& name, bool* out_optionalValueFound, Inspector::Protocol::Array<String>& protocolErrors, DefaultValueType defaultValue, bool (*asMethod)(InspectorValue&, ValueType&), const char* typeName)
189 {
190 ValueType result = defaultValue;
191 // out_optionalValueFound signals to the caller whether an optional property was found.
192 // if out_optionalValueFound == nullptr, then this is a required property.
193 if (out_optionalValueFound)
194 *out_optionalValueFound = false;
195
196 if (!object) {
197 if (!out_optionalValueFound)
198 protocolErrors.addItem(String::format("'params' object must contain required parameter '%s' with type '%s'.", name.utf8().data(), typeName));
199 return result;
200 }
201
202 auto findResult = object->find(name);
203 if (findResult == object->end()) {
204 if (!out_optionalValueFound)
205 protocolErrors.addItem(String::format("Parameter '%s' with type '%s' was not found.", name.utf8().data(), typeName));
206 return result;
207 }
208
209 if (!asMethod(*findResult->value, result)) {
210 protocolErrors.addItem(String::format("Parameter '%s' has wrong type. It must be '%s'.", name.utf8().data(), typeName));
211 return result;
212 }
213
214 if (out_optionalValueFound)
215 *out_optionalValueFound = true;
216
217 return result;
218 }
219
220 struct AsMethodBridges {
221 static bool asInteger(InspectorValue& value, int& output) { return value.asInteger(output); }
222 static bool asDouble(InspectorValue& value, double& output) { return value.asDouble(output); }
223 static bool asString(InspectorValue& value, String& output) { return value.asString(output); }
224 static bool asBoolean(InspectorValue& value, bool& output) { return value.asBoolean(output); }
225 static bool asObject(InspectorValue& value, RefPtr<InspectorObject>& output) { return value.asObject(output); }
226 static bool asArray(InspectorValue& value, RefPtr<InspectorArray>& output) { return value.asArray(output); }
227 static bool asValue(InspectorValue& value, RefPtr<InspectorValue>& output) { return value.asValue(output); }
228 };
229
230 int BackendDispatcher::getInteger(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
231 {
232 return getPropertyValue<int, int, int>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asInteger, "Integer");
233 }
234
235 double BackendDispatcher::getDouble(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
236 {
237 return getPropertyValue<double, double, double>(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asDouble, "Number");
238 }
239
240 String BackendDispatcher::getString(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
241 {
242 return getPropertyValue<String, String, String>(object, name, valueFound, protocolErrors, "", AsMethodBridges::asString, "String");
243 }
244
245 bool BackendDispatcher::getBoolean(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
246 {
247 return getPropertyValue<bool, bool, bool>(object, name, valueFound, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean");
248 }
249
250 RefPtr<InspectorObject> BackendDispatcher::getObject(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
251 {
252 return getPropertyValue<RefPtr<InspectorObject>, RefPtr<InspectorObject>, InspectorObject*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asObject, "Object");
253 }
254
255 RefPtr<InspectorArray> BackendDispatcher::getArray(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
256 {
257 return getPropertyValue<RefPtr<InspectorArray>, RefPtr<InspectorArray>, InspectorArray*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asArray, "Array");
258 }
259
260 RefPtr<InspectorValue> BackendDispatcher::getValue(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors)
261 {
262 return getPropertyValue<RefPtr<InspectorValue>, RefPtr<InspectorValue>, InspectorValue*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asValue, "Value");
263 }
264
265 } // namespace Inspector