InspectorBackendDispatcher.h [plain text]
#pragma once
#include "InspectorFrontendRouter.h"
#include "InspectorProtocolTypes.h"
#include <functional>
#include <wtf/Optional.h>
#include <wtf/RefCounted.h>
#include <wtf/text/WTFString.h>
namespace Inspector {
class BackendDispatcher;
typedef String ErrorString;
class JS_EXPORT_PRIVATE SupplementalBackendDispatcher : public RefCounted<SupplementalBackendDispatcher> {
public:
SupplementalBackendDispatcher(BackendDispatcher&);
virtual ~SupplementalBackendDispatcher();
virtual void dispatch(long requestId, const String& method, Ref<JSON::Object>&& message) = 0;
protected:
Ref<BackendDispatcher> m_backendDispatcher;
};
class JS_EXPORT_PRIVATE BackendDispatcher : public RefCounted<BackendDispatcher> {
public:
static Ref<BackendDispatcher> create(Ref<FrontendRouter>&&);
class JS_EXPORT_PRIVATE CallbackBase : public RefCounted<CallbackBase> {
public:
CallbackBase(Ref<BackendDispatcher>&&, long requestId);
bool isActive() const;
void disable() { m_alreadySent = true; }
void sendSuccess(RefPtr<JSON::Object>&&);
void sendFailure(const ErrorString&);
private:
Ref<BackendDispatcher> m_backendDispatcher;
long m_requestId;
bool m_alreadySent { false };
};
bool isActive() const;
bool hasProtocolErrors() const { return m_protocolErrors.size() > 0; }
enum CommonErrorCode {
ParseError = 0,
InvalidRequest,
MethodNotFound,
InvalidParams,
InternalError,
ServerError
};
void registerDispatcherForDomain(const String& domain, SupplementalBackendDispatcher*);
void dispatch(const String& message);
void sendResponse(long requestId, RefPtr<JSON::Object>&& result, bool unused);
void sendResponse(long requestId, RefPtr<JSON::Object>&& result);
void sendPendingErrors();
void reportProtocolError(CommonErrorCode, const String& errorMessage);
void reportProtocolError(Optional<long> relatedRequestId, CommonErrorCode, const String& errorMessage);
template<typename T>
WTF_INTERNAL
T getPropertyValue(JSON::Object*, const String& name, bool* out_optionalValueFound, T defaultValue, std::function<bool(JSON::Value&, T&)>, const char* typeName);
int getInteger(JSON::Object*, const String& name, bool* valueFound);
double getDouble(JSON::Object*, const String& name, bool* valueFound);
String getString(JSON::Object*, const String& name, bool* valueFound);
bool getBoolean(JSON::Object*, const String& name, bool* valueFound);
RefPtr<JSON::Value> getValue(JSON::Object*, const String& name, bool* valueFound);
RefPtr<JSON::Object> getObject(JSON::Object*, const String& name, bool* valueFound);
RefPtr<JSON::Array> getArray(JSON::Object*, const String& name, bool* valueFound);
private:
BackendDispatcher(Ref<FrontendRouter>&&);
Ref<FrontendRouter> m_frontendRouter;
HashMap<String, SupplementalBackendDispatcher*> m_dispatchers;
Vector<std::tuple<CommonErrorCode, String>> m_protocolErrors;
Optional<long> m_currentRequestId { WTF::nullopt };
};
}