2019-02-08 23:50:24 +00:00
|
|
|
// This file is generated by DispatcherBase_h.template.
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
|
|
|
|
#define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
|
|
|
|
|
|
|
|
//#include "Forward.h"
|
2018-07-17 17:24:20 +00:00
|
|
|
//#include "ErrorSupport.h"
|
2016-10-28 18:20:08 +00:00
|
|
|
//#include "Values.h"
|
|
|
|
|
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
namespace {{namespace}} {
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
class WeakPtr;
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
class {{config.lib.export_macro}} DispatchResponse {
|
|
|
|
public:
|
|
|
|
enum Status {
|
|
|
|
kSuccess = 0,
|
|
|
|
kError = 1,
|
2018-08-21 14:31:54 +00:00
|
|
|
kFallThrough = 2,
|
2016-11-01 22:33:05 +00:00
|
|
|
};
|
|
|
|
|
2019-06-07 01:25:03 +00:00
|
|
|
// For historical reasons, these error codes correspond to commonly used
|
|
|
|
// XMLRPC codes (e.g. see METHOD_NOT_FOUND in
|
|
|
|
// https://github.com/python/cpython/blob/master/Lib/xmlrpc/client.py).
|
2016-11-01 22:33:05 +00:00
|
|
|
enum ErrorCode {
|
|
|
|
kParseError = -32700,
|
|
|
|
kInvalidRequest = -32600,
|
|
|
|
kMethodNotFound = -32601,
|
|
|
|
kInvalidParams = -32602,
|
|
|
|
kInternalError = -32603,
|
|
|
|
kServerError = -32000,
|
|
|
|
};
|
|
|
|
|
|
|
|
Status status() const { return m_status; }
|
|
|
|
const String& errorMessage() const { return m_errorMessage; }
|
|
|
|
ErrorCode errorCode() const { return m_errorCode; }
|
|
|
|
bool isSuccess() const { return m_status == kSuccess; }
|
|
|
|
|
|
|
|
static DispatchResponse OK();
|
|
|
|
static DispatchResponse Error(const String&);
|
|
|
|
static DispatchResponse InternalError();
|
2016-11-22 18:57:03 +00:00
|
|
|
static DispatchResponse InvalidParams(const String&);
|
2016-11-01 22:33:05 +00:00
|
|
|
static DispatchResponse FallThrough();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Status m_status;
|
|
|
|
String m_errorMessage;
|
|
|
|
ErrorCode m_errorCode;
|
|
|
|
};
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
class {{config.lib.export_macro}} DispatcherBase {
|
|
|
|
PROTOCOL_DISALLOW_COPY(DispatcherBase);
|
|
|
|
public:
|
2016-11-01 22:33:05 +00:00
|
|
|
static const char kInvalidParamsString[];
|
2016-10-28 18:20:08 +00:00
|
|
|
class {{config.lib.export_macro}} WeakPtr {
|
|
|
|
public:
|
|
|
|
explicit WeakPtr(DispatcherBase*);
|
|
|
|
~WeakPtr();
|
|
|
|
DispatcherBase* get() { return m_dispatcher; }
|
|
|
|
void dispose() { m_dispatcher = nullptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DispatcherBase* m_dispatcher;
|
|
|
|
};
|
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
class {{config.lib.export_macro}} Callback {
|
2016-10-28 18:20:08 +00:00
|
|
|
public:
|
2019-02-12 19:00:13 +00:00
|
|
|
Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message);
|
2016-10-28 18:20:08 +00:00
|
|
|
virtual ~Callback();
|
|
|
|
void dispose();
|
|
|
|
|
|
|
|
protected:
|
2016-11-01 22:33:05 +00:00
|
|
|
void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
|
2016-11-22 18:57:03 +00:00
|
|
|
void fallThroughIfActive();
|
2016-10-28 18:20:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<WeakPtr> m_backendImpl;
|
|
|
|
int m_callId;
|
2018-07-25 00:17:11 +00:00
|
|
|
String m_method;
|
2019-02-12 19:00:13 +00:00
|
|
|
ProtocolMessage m_message;
|
2016-10-28 18:20:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit DispatcherBase(FrontendChannel*);
|
|
|
|
virtual ~DispatcherBase();
|
|
|
|
|
2018-07-25 00:17:11 +00:00
|
|
|
virtual bool canDispatch(const String& method) = 0;
|
2019-02-12 19:00:13 +00:00
|
|
|
virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
|
2018-07-25 00:17:11 +00:00
|
|
|
FrontendChannel* channel() { return m_frontendChannel; }
|
2016-10-28 18:20:08 +00:00
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
|
|
|
|
void sendResponse(int callId, const DispatchResponse&);
|
2016-10-28 18:20:08 +00:00
|
|
|
|
2016-11-01 22:33:05 +00:00
|
|
|
void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
|
2016-10-28 18:20:08 +00:00
|
|
|
void clearFrontend();
|
|
|
|
|
|
|
|
std::unique_ptr<WeakPtr> weakPtr();
|
|
|
|
|
|
|
|
private:
|
|
|
|
FrontendChannel* m_frontendChannel;
|
2018-07-17 17:24:20 +00:00
|
|
|
std::unordered_set<WeakPtr*> m_weakPtrs;
|
2016-10-28 18:20:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class {{config.lib.export_macro}} UberDispatcher {
|
|
|
|
PROTOCOL_DISALLOW_COPY(UberDispatcher);
|
|
|
|
public:
|
|
|
|
explicit UberDispatcher(FrontendChannel*);
|
|
|
|
void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
|
2018-07-17 17:24:20 +00:00
|
|
|
void setupRedirects(const std::unordered_map<String, String>&);
|
2018-07-25 00:17:11 +00:00
|
|
|
bool parseCommand(Value* message, int* callId, String* method);
|
|
|
|
bool canDispatch(const String& method);
|
2019-02-12 19:00:13 +00:00
|
|
|
void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage);
|
2016-10-28 18:20:08 +00:00
|
|
|
FrontendChannel* channel() { return m_frontendChannel; }
|
|
|
|
virtual ~UberDispatcher();
|
|
|
|
|
|
|
|
private:
|
2018-07-25 00:17:11 +00:00
|
|
|
protocol::DispatcherBase* findDispatcher(const String& method);
|
2016-10-28 18:20:08 +00:00
|
|
|
FrontendChannel* m_frontendChannel;
|
2018-07-17 17:24:20 +00:00
|
|
|
std::unordered_map<String, String> m_redirects;
|
|
|
|
std::unordered_map<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
|
2016-10-28 18:20:08 +00:00
|
|
|
};
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
class InternalResponse : public Serializable {
|
|
|
|
PROTOCOL_DISALLOW_COPY(InternalResponse);
|
|
|
|
public:
|
2019-10-10 23:47:49 +00:00
|
|
|
static std::unique_ptr<Serializable> createResponse(int callId, std::unique_ptr<Serializable> params);
|
2019-10-28 22:47:09 +00:00
|
|
|
static std::unique_ptr<Serializable> createNotification(const char* method, std::unique_ptr<Serializable> params = nullptr);
|
2019-10-10 23:47:49 +00:00
|
|
|
static std::unique_ptr<Serializable> createErrorResponse(int callId, DispatchResponse::ErrorCode code, const String& message);
|
2016-11-23 00:45:30 +00:00
|
|
|
|
2019-10-28 22:47:09 +00:00
|
|
|
void AppendSerialized(std::vector<uint8_t>* out) const override;
|
2016-11-23 00:45:30 +00:00
|
|
|
|
|
|
|
~InternalResponse() override {}
|
|
|
|
|
|
|
|
private:
|
2019-10-28 22:47:09 +00:00
|
|
|
InternalResponse(int callId, const char* method, std::unique_ptr<Serializable> params);
|
2016-11-23 00:45:30 +00:00
|
|
|
|
|
|
|
int m_callId;
|
2019-10-28 22:47:09 +00:00
|
|
|
const char* m_method = nullptr;
|
2016-11-23 00:45:30 +00:00
|
|
|
std::unique_ptr<Serializable> m_params;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InternalRawNotification : public Serializable {
|
|
|
|
public:
|
2019-02-12 19:00:13 +00:00
|
|
|
static std::unique_ptr<InternalRawNotification> fromBinary(std::vector<uint8_t> notification)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
~InternalRawNotification() override {}
|
|
|
|
|
2019-10-28 22:47:09 +00:00
|
|
|
std::vector<uint8_t> TakeSerialized() && override {
|
2019-02-12 19:00:13 +00:00
|
|
|
return std::move(m_binaryNotification);
|
2016-11-23 00:45:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 22:47:09 +00:00
|
|
|
void AppendSerialized(std::vector<uint8_t>* out) const override
|
|
|
|
{
|
|
|
|
out->insert(out->end(), m_binaryNotification.begin(), m_binaryNotification.end());
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
private:
|
2019-02-12 19:00:13 +00:00
|
|
|
explicit InternalRawNotification(std::vector<uint8_t> notification)
|
|
|
|
: m_binaryNotification(std::move(notification)) { }
|
2016-11-23 00:45:30 +00:00
|
|
|
|
2019-02-12 19:00:13 +00:00
|
|
|
std::vector<uint8_t> m_binaryNotification;
|
2016-11-23 00:45:30 +00:00
|
|
|
};
|
|
|
|
|
2016-10-28 18:20:08 +00:00
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
} // namespace {{namespace}}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
#endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h)
|