X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/81345200c95645a1b0d2635520f96ad55dfde63f..refs/heads/master:/inspector/InspectorBackendDispatcher.cpp diff --git a/inspector/InspectorBackendDispatcher.cpp b/inspector/InspectorBackendDispatcher.cpp index 30a66f7..f88cfbd 100644 --- a/inspector/InspectorBackendDispatcher.cpp +++ b/inspector/InspectorBackendDispatcher.cpp @@ -27,8 +27,6 @@ #include "config.h" #include "InspectorBackendDispatcher.h" -#if ENABLE(INSPECTOR) - #include "InspectorFrontendChannel.h" #include "InspectorValues.h" #include @@ -36,80 +34,80 @@ namespace Inspector { -InspectorBackendDispatcher::CallbackBase::CallbackBase(PassRefPtr backendDispatcher, int id) - : m_backendDispatcher(backendDispatcher) +BackendDispatcher::CallbackBase::CallbackBase(Ref&& backendDispatcher, int id) + : m_backendDispatcher(WTF::move(backendDispatcher)) , m_id(id) , m_alreadySent(false) { } -bool InspectorBackendDispatcher::CallbackBase::isActive() const +bool BackendDispatcher::CallbackBase::isActive() const { return !m_alreadySent && m_backendDispatcher->isActive(); } -void InspectorBackendDispatcher::CallbackBase::sendFailure(const ErrorString& error) +void BackendDispatcher::CallbackBase::sendFailure(const ErrorString& error) { ASSERT(error.length()); sendIfActive(nullptr, error); } -void InspectorBackendDispatcher::CallbackBase::sendIfActive(PassRefPtr partialMessage, const ErrorString& invocationError) +void BackendDispatcher::CallbackBase::sendIfActive(RefPtr&& partialMessage, const ErrorString& invocationError) { if (m_alreadySent) return; - m_backendDispatcher->sendResponse(m_id, partialMessage, invocationError); + m_backendDispatcher->sendResponse(m_id, WTF::move(partialMessage), invocationError); m_alreadySent = true; } -PassRefPtr InspectorBackendDispatcher::create(InspectorFrontendChannel* inspectorFrontendChannel) +Ref BackendDispatcher::create(FrontendChannel* frontendChannel) { - return adoptRef(new InspectorBackendDispatcher(inspectorFrontendChannel)); + return adoptRef(*new BackendDispatcher(frontendChannel)); } -void InspectorBackendDispatcher::registerDispatcherForDomain(const String& domain, InspectorSupplementalBackendDispatcher* dispatcher) +void BackendDispatcher::registerDispatcherForDomain(const String& domain, SupplementalBackendDispatcher* dispatcher) { auto result = m_dispatchers.add(domain, dispatcher); ASSERT_UNUSED(result, result.isNewEntry); } -void InspectorBackendDispatcher::dispatch(const String& message) +void BackendDispatcher::dispatch(const String& message) { - Ref protect(*this); + Ref protect(*this); - RefPtr parsedMessage = InspectorValue::parseJSON(message); - if (!parsedMessage) { + RefPtr parsedMessage; + if (!InspectorValue::parseJSON(message, parsedMessage)) { reportProtocolError(nullptr, ParseError, ASCIILiteral("Message must be in JSON format")); return; } - RefPtr messageObject = parsedMessage->asObject(); - if (!messageObject) { + RefPtr messageObject; + if (!parsedMessage->asObject(messageObject)) { reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("Message must be a JSONified object")); return; } - RefPtr callIdValue = messageObject->get("id"); - if (!callIdValue) { + RefPtr callIdValue; + if (!messageObject->getValue(ASCIILiteral("id"), callIdValue)) { reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("'id' property was not found")); return; } long callId = 0; - if (!callIdValue->asNumber(&callId)) { - reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("The type of 'id' property must be number")); + if (!callIdValue->asInteger(callId)) { + reportProtocolError(nullptr, InvalidRequest, ASCIILiteral("The type of 'id' property must be integer")); return; } - RefPtr methodValue = messageObject->get("method"); - if (!methodValue) { + RefPtr methodValue; + if (!messageObject->getValue(ASCIILiteral("method"), methodValue)) { reportProtocolError(&callId, InvalidRequest, ASCIILiteral("'method' property wasn't found")); return; } String method; - if (!methodValue->asString(&method)) { + if (!methodValue->asString(method)) { reportProtocolError(&callId, InvalidRequest, ASCIILiteral("The type of 'method' property must be string")); return; } @@ -121,19 +119,19 @@ void InspectorBackendDispatcher::dispatch(const String& message) } String domain = method.substring(0, position); - InspectorSupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain); + SupplementalBackendDispatcher* domainDispatcher = m_dispatchers.get(domain); if (!domainDispatcher) { reportProtocolError(&callId, MethodNotFound, "'" + domain + "' domain was not found"); return; } String domainMethod = method.substring(position + 1); - domainDispatcher->dispatch(callId, domainMethod, messageObject.release()); + domainDispatcher->dispatch(callId, domainMethod, messageObject.releaseNonNull()); } -void InspectorBackendDispatcher::sendResponse(long callId, PassRefPtr result, const ErrorString& invocationError) +void BackendDispatcher::sendResponse(long callId, RefPtr&& result, const ErrorString& invocationError) { - if (!m_inspectorFrontendChannel) + if (!m_frontendChannel) return; if (invocationError.length()) { @@ -141,18 +139,18 @@ void InspectorBackendDispatcher::sendResponse(long callId, PassRefPtr responseMessage = InspectorObject::create(); + Ref responseMessage = InspectorObject::create(); responseMessage->setObject(ASCIILiteral("result"), result); - responseMessage->setNumber(ASCIILiteral("id"), callId); - m_inspectorFrontendChannel->sendMessageToFrontend(responseMessage->toJSONString()); + responseMessage->setInteger(ASCIILiteral("id"), callId); + m_frontendChannel->sendMessageToFrontend(responseMessage->toJSONString()); } -void InspectorBackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage) const +void BackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage) const { reportProtocolError(callId, errorCode, errorMessage, nullptr); } -void InspectorBackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage, PassRefPtr data) const +void BackendDispatcher::reportProtocolError(const long* const callId, CommonErrorCode errorCode, const String& errorMessage, RefPtr>&& data) const { static const int errorCodes[] = { -32700, // ParseError @@ -163,102 +161,105 @@ void InspectorBackendDispatcher::reportProtocolError(const long* const callId, C -32000, // ServerError }; - ASSERT(errorCode >= 0); - ASSERT((unsigned)errorCode < WTF_ARRAY_LENGTH(errorCodes)); - ASSERT(errorCodes[errorCode]); + ASSERT_ARG(errorCode, errorCode >= 0); + ASSERT_ARG(errorCode, (unsigned)errorCode < WTF_ARRAY_LENGTH(errorCodes)); + ASSERT_ARG(errorCode, errorCodes[errorCode]); - if (!m_inspectorFrontendChannel) + if (!m_frontendChannel) return; - RefPtr error = InspectorObject::create(); - error->setNumber(ASCIILiteral("code"), errorCodes[errorCode]); + Ref error = InspectorObject::create(); + error->setInteger(ASCIILiteral("code"), errorCodes[errorCode]); error->setString(ASCIILiteral("message"), errorMessage); if (data) - error->setArray(ASCIILiteral("data"), data); + error->setArray(ASCIILiteral("data"), WTF::move(data)); - RefPtr message = InspectorObject::create(); - message->setObject(ASCIILiteral("error"), error.release()); + Ref message = InspectorObject::create(); + message->setObject(ASCIILiteral("error"), WTF::move(error)); if (callId) - message->setNumber(ASCIILiteral("id"), *callId); + message->setInteger(ASCIILiteral("id"), *callId); else message->setValue(ASCIILiteral("id"), InspectorValue::null()); - m_inspectorFrontendChannel->sendMessageToFrontend(message->toJSONString()); + m_frontendChannel->sendMessageToFrontend(message->toJSONString()); } template -static ReturnValueType getPropertyValue(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors, DefaultValueType defaultValue, bool (*asMethod)(InspectorValue*, ValueType*), const char* typeName) +static ReturnValueType getPropertyValue(InspectorObject* object, const String& name, bool* out_optionalValueFound, Inspector::Protocol::Array& protocolErrors, DefaultValueType defaultValue, bool (*asMethod)(InspectorValue&, ValueType&), const char* typeName) { - ASSERT(protocolErrors); - - ValueType value = defaultValue; - if (valueFound) - *valueFound = false; + ValueType 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 (!valueFound) - protocolErrors->pushString(String::format("'params' object must contain required parameter '%s' with type '%s'.", name.utf8().data(), typeName)); - return value; + if (!out_optionalValueFound) + protocolErrors.addItem(String::format("'params' object must contain required parameter '%s' with type '%s'.", name.utf8().data(), typeName)); + return result; } - InspectorObject::const_iterator end = object->end(); - InspectorObject::const_iterator valueIterator = object->find(name); - if (valueIterator == end) { - if (!valueFound) - protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name.utf8().data(), typeName)); - return value; + auto findResult = object->find(name); + if (findResult == object->end()) { + if (!out_optionalValueFound) + protocolErrors.addItem(String::format("Parameter '%s' with type '%s' was not found.", name.utf8().data(), typeName)); + return result; } - if (!asMethod(valueIterator->value.get(), &value)) { - protocolErrors->pushString(String::format("Parameter '%s' has wrong type. It must be '%s'.", name.utf8().data(), typeName)); - return value; + if (!asMethod(*findResult->value, result)) { + protocolErrors.addItem(String::format("Parameter '%s' has wrong type. It must be '%s'.", name.utf8().data(), typeName)); + return result; } - if (valueFound) - *valueFound = true; + if (out_optionalValueFound) + *out_optionalValueFound = true; - return value; + return result; } struct AsMethodBridges { - static bool asInt(InspectorValue* value, int* output) { return value->asNumber(output); } - static bool asDouble(InspectorValue* value, double* output) { return value->asNumber(output); } - static bool asString(InspectorValue* value, String* output) { return value->asString(output); } - static bool asBoolean(InspectorValue* value, bool* output) { return value->asBoolean(output); } - static bool asObject(InspectorValue* value, RefPtr* output) { return value->asObject(output); } - static bool asArray(InspectorValue* value, RefPtr* output) { return value->asArray(output); } + static bool asInteger(InspectorValue& value, int& output) { return value.asInteger(output); } + static bool asDouble(InspectorValue& value, double& output) { return value.asDouble(output); } + static bool asString(InspectorValue& value, String& output) { return value.asString(output); } + static bool asBoolean(InspectorValue& value, bool& output) { return value.asBoolean(output); } + static bool asObject(InspectorValue& value, RefPtr& output) { return value.asObject(output); } + static bool asArray(InspectorValue& value, RefPtr& output) { return value.asArray(output); } + static bool asValue(InspectorValue& value, RefPtr& output) { return value.asValue(output); } }; -int InspectorBackendDispatcher::getInt(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +int BackendDispatcher::getInteger(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { - return getPropertyValue(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asInt, "Number"); + return getPropertyValue(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asInteger, "Integer"); } -double InspectorBackendDispatcher::getDouble(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +double BackendDispatcher::getDouble(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { return getPropertyValue(object, name, valueFound, protocolErrors, 0, AsMethodBridges::asDouble, "Number"); } -String InspectorBackendDispatcher::getString(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +String BackendDispatcher::getString(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { return getPropertyValue(object, name, valueFound, protocolErrors, "", AsMethodBridges::asString, "String"); } -bool InspectorBackendDispatcher::getBoolean(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +bool BackendDispatcher::getBoolean(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { return getPropertyValue(object, name, valueFound, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean"); } -PassRefPtr InspectorBackendDispatcher::getObject(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +RefPtr BackendDispatcher::getObject(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { - return getPropertyValue, RefPtr, InspectorObject*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asObject, "Object"); + return getPropertyValue, RefPtr, InspectorObject*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asObject, "Object"); } -PassRefPtr InspectorBackendDispatcher::getArray(InspectorObject* object, const String& name, bool* valueFound, InspectorArray* protocolErrors) +RefPtr BackendDispatcher::getArray(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) { - return getPropertyValue, RefPtr, InspectorArray*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asArray, "Array"); + return getPropertyValue, RefPtr, InspectorArray*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asArray, "Array"); } -} // namespace Inspector +RefPtr BackendDispatcher::getValue(InspectorObject* object, const String& name, bool* valueFound, Inspector::Protocol::Array& protocolErrors) +{ + return getPropertyValue, RefPtr, InspectorValue*>(object, name, valueFound, protocolErrors, nullptr, AsMethodBridges::asValue, "Value"); +} -#endif // ENABLE(INSPECTOR) +} // namespace Inspector