1// This file is generated by DispatcherBase_h.template.
2
3// Copyright 2016 The Chromium Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7#ifndef {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
8#define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
9
10//#include "Forward.h"
11//#include "ErrorSupport.h"
12//#include "Values.h"
13
14{% for namespace in config.protocol.namespace %}
15namespace {{namespace}} {
16{% endfor %}
17
18class WeakPtr;
19
20class {{config.lib.export_macro}} DispatchResponse {
21public:
22    enum Status {
23        kSuccess = 0,
24        kError = 1,
25        kFallThrough = 2,
26    };
27
28    enum ErrorCode {
29        kParseError = -32700,
30        kInvalidRequest = -32600,
31        kMethodNotFound = -32601,
32        kInvalidParams = -32602,
33        kInternalError = -32603,
34        kServerError = -32000,
35    };
36
37    Status status() const { return m_status; }
38    const String& errorMessage() const { return m_errorMessage; }
39    ErrorCode errorCode() const { return m_errorCode; }
40    bool isSuccess() const { return m_status == kSuccess; }
41
42    static DispatchResponse OK();
43    static DispatchResponse Error(const String&);
44    static DispatchResponse InternalError();
45    static DispatchResponse InvalidParams(const String&);
46    static DispatchResponse FallThrough();
47
48private:
49    Status m_status;
50    String m_errorMessage;
51    ErrorCode m_errorCode;
52};
53
54class {{config.lib.export_macro}} DispatcherBase {
55    PROTOCOL_DISALLOW_COPY(DispatcherBase);
56public:
57    static const char kInvalidParamsString[];
58    class {{config.lib.export_macro}} WeakPtr {
59    public:
60        explicit WeakPtr(DispatcherBase*);
61        ~WeakPtr();
62        DispatcherBase* get() { return m_dispatcher; }
63        void dispose() { m_dispatcher = nullptr; }
64
65    private:
66        DispatcherBase* m_dispatcher;
67    };
68
69    class {{config.lib.export_macro}} Callback {
70    public:
71        Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message);
72        virtual ~Callback();
73        void dispose();
74
75    protected:
76        void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
77        void fallThroughIfActive();
78
79    private:
80        std::unique_ptr<WeakPtr> m_backendImpl;
81        int m_callId;
82        String m_method;
83        ProtocolMessage m_message;
84    };
85
86    explicit DispatcherBase(FrontendChannel*);
87    virtual ~DispatcherBase();
88
89    virtual bool canDispatch(const String& method) = 0;
90    virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
91    FrontendChannel* channel() { return m_frontendChannel; }
92
93    void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
94    void sendResponse(int callId, const DispatchResponse&);
95
96    void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
97    void clearFrontend();
98
99    std::unique_ptr<WeakPtr> weakPtr();
100
101private:
102    FrontendChannel* m_frontendChannel;
103    std::unordered_set<WeakPtr*> m_weakPtrs;
104};
105
106class {{config.lib.export_macro}} UberDispatcher {
107    PROTOCOL_DISALLOW_COPY(UberDispatcher);
108public:
109    explicit UberDispatcher(FrontendChannel*);
110    void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
111    void setupRedirects(const std::unordered_map<String, String>&);
112    bool parseCommand(Value* message, int* callId, String* method);
113    bool canDispatch(const String& method);
114    void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage);
115    FrontendChannel* channel() { return m_frontendChannel; }
116    virtual ~UberDispatcher();
117
118private:
119    protocol::DispatcherBase* findDispatcher(const String& method);
120    FrontendChannel* m_frontendChannel;
121    std::unordered_map<String, String> m_redirects;
122    std::unordered_map<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
123};
124
125class InternalResponse : public Serializable {
126    PROTOCOL_DISALLOW_COPY(InternalResponse);
127public:
128    static std::unique_ptr<InternalResponse> createResponse(int callId, std::unique_ptr<Serializable> params);
129    static std::unique_ptr<InternalResponse> createNotification(const String& notification, std::unique_ptr<Serializable> params = nullptr);
130
131    String serializeToJSON() override;
132    std::vector<uint8_t> serializeToBinary() override;
133
134    ~InternalResponse() override {}
135
136private:
137    InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params);
138
139    int m_callId;
140    String m_notification;
141    std::unique_ptr<Serializable> m_params;
142};
143
144class InternalRawNotification : public Serializable {
145public:
146    static std::unique_ptr<InternalRawNotification> fromJSON(String notification)
147    {
148        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
149    }
150
151    static std::unique_ptr<InternalRawNotification> fromBinary(std::vector<uint8_t> notification)
152    {
153        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
154    }
155
156    ~InternalRawNotification() override {}
157
158    String serializeToJSON() override
159    {
160        return std::move(m_jsonNotification);
161    }
162
163    std::vector<uint8_t> serializeToBinary() override
164    {
165        return std::move(m_binaryNotification);
166    }
167
168private:
169  explicit InternalRawNotification(String notification)
170    : m_jsonNotification(std::move(notification)) { }
171  explicit InternalRawNotification(std::vector<uint8_t> notification)
172    : m_binaryNotification(std::move(notification)) { }
173
174  String m_jsonNotification;
175  std::vector<uint8_t> m_binaryNotification;
176};
177
178{% for namespace in config.protocol.namespace %}
179} // namespace {{namespace}}
180{% endfor %}
181
182#endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h)
183