11cb0ef41Sopenharmony_ci// This file is generated by DispatcherBase_h.template.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved.
41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci// found in the LICENSE file.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#ifndef {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
81cb0ef41Sopenharmony_ci#define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci//#include "Forward.h"
111cb0ef41Sopenharmony_ci//#include "ErrorSupport.h"
121cb0ef41Sopenharmony_ci//#include "Values.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
151cb0ef41Sopenharmony_cinamespace {{namespace}} {
161cb0ef41Sopenharmony_ci{% endfor %}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciclass WeakPtr;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} DispatchResponse {
211cb0ef41Sopenharmony_cipublic:
221cb0ef41Sopenharmony_ci    enum Status {
231cb0ef41Sopenharmony_ci        kSuccess = 0,
241cb0ef41Sopenharmony_ci        kError = 1,
251cb0ef41Sopenharmony_ci        kFallThrough = 2,
261cb0ef41Sopenharmony_ci    };
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    enum ErrorCode {
291cb0ef41Sopenharmony_ci        kParseError = -32700,
301cb0ef41Sopenharmony_ci        kInvalidRequest = -32600,
311cb0ef41Sopenharmony_ci        kMethodNotFound = -32601,
321cb0ef41Sopenharmony_ci        kInvalidParams = -32602,
331cb0ef41Sopenharmony_ci        kInternalError = -32603,
341cb0ef41Sopenharmony_ci        kServerError = -32000,
351cb0ef41Sopenharmony_ci    };
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    Status status() const { return m_status; }
381cb0ef41Sopenharmony_ci    const String& errorMessage() const { return m_errorMessage; }
391cb0ef41Sopenharmony_ci    ErrorCode errorCode() const { return m_errorCode; }
401cb0ef41Sopenharmony_ci    bool isSuccess() const { return m_status == kSuccess; }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    static DispatchResponse OK();
431cb0ef41Sopenharmony_ci    static DispatchResponse Error(const String&);
441cb0ef41Sopenharmony_ci    static DispatchResponse InternalError();
451cb0ef41Sopenharmony_ci    static DispatchResponse InvalidParams(const String&);
461cb0ef41Sopenharmony_ci    static DispatchResponse FallThrough();
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciprivate:
491cb0ef41Sopenharmony_ci    Status m_status;
501cb0ef41Sopenharmony_ci    String m_errorMessage;
511cb0ef41Sopenharmony_ci    ErrorCode m_errorCode;
521cb0ef41Sopenharmony_ci};
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} DispatcherBase {
551cb0ef41Sopenharmony_ci    PROTOCOL_DISALLOW_COPY(DispatcherBase);
561cb0ef41Sopenharmony_cipublic:
571cb0ef41Sopenharmony_ci    static const char kInvalidParamsString[];
581cb0ef41Sopenharmony_ci    class {{config.lib.export_macro}} WeakPtr {
591cb0ef41Sopenharmony_ci    public:
601cb0ef41Sopenharmony_ci        explicit WeakPtr(DispatcherBase*);
611cb0ef41Sopenharmony_ci        ~WeakPtr();
621cb0ef41Sopenharmony_ci        DispatcherBase* get() { return m_dispatcher; }
631cb0ef41Sopenharmony_ci        void dispose() { m_dispatcher = nullptr; }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    private:
661cb0ef41Sopenharmony_ci        DispatcherBase* m_dispatcher;
671cb0ef41Sopenharmony_ci    };
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    class {{config.lib.export_macro}} Callback {
701cb0ef41Sopenharmony_ci    public:
711cb0ef41Sopenharmony_ci        Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message);
721cb0ef41Sopenharmony_ci        virtual ~Callback();
731cb0ef41Sopenharmony_ci        void dispose();
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    protected:
761cb0ef41Sopenharmony_ci        void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
771cb0ef41Sopenharmony_ci        void fallThroughIfActive();
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    private:
801cb0ef41Sopenharmony_ci        std::unique_ptr<WeakPtr> m_backendImpl;
811cb0ef41Sopenharmony_ci        int m_callId;
821cb0ef41Sopenharmony_ci        String m_method;
831cb0ef41Sopenharmony_ci        ProtocolMessage m_message;
841cb0ef41Sopenharmony_ci    };
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    explicit DispatcherBase(FrontendChannel*);
871cb0ef41Sopenharmony_ci    virtual ~DispatcherBase();
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    virtual bool canDispatch(const String& method) = 0;
901cb0ef41Sopenharmony_ci    virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
911cb0ef41Sopenharmony_ci    FrontendChannel* channel() { return m_frontendChannel; }
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
941cb0ef41Sopenharmony_ci    void sendResponse(int callId, const DispatchResponse&);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
971cb0ef41Sopenharmony_ci    void clearFrontend();
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    std::unique_ptr<WeakPtr> weakPtr();
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciprivate:
1021cb0ef41Sopenharmony_ci    FrontendChannel* m_frontendChannel;
1031cb0ef41Sopenharmony_ci    std::unordered_set<WeakPtr*> m_weakPtrs;
1041cb0ef41Sopenharmony_ci};
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciclass {{config.lib.export_macro}} UberDispatcher {
1071cb0ef41Sopenharmony_ci    PROTOCOL_DISALLOW_COPY(UberDispatcher);
1081cb0ef41Sopenharmony_cipublic:
1091cb0ef41Sopenharmony_ci    explicit UberDispatcher(FrontendChannel*);
1101cb0ef41Sopenharmony_ci    void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
1111cb0ef41Sopenharmony_ci    void setupRedirects(const std::unordered_map<String, String>&);
1121cb0ef41Sopenharmony_ci    bool parseCommand(Value* message, int* callId, String* method);
1131cb0ef41Sopenharmony_ci    bool canDispatch(const String& method);
1141cb0ef41Sopenharmony_ci    void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage);
1151cb0ef41Sopenharmony_ci    FrontendChannel* channel() { return m_frontendChannel; }
1161cb0ef41Sopenharmony_ci    virtual ~UberDispatcher();
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ciprivate:
1191cb0ef41Sopenharmony_ci    protocol::DispatcherBase* findDispatcher(const String& method);
1201cb0ef41Sopenharmony_ci    FrontendChannel* m_frontendChannel;
1211cb0ef41Sopenharmony_ci    std::unordered_map<String, String> m_redirects;
1221cb0ef41Sopenharmony_ci    std::unordered_map<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
1231cb0ef41Sopenharmony_ci};
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciclass InternalResponse : public Serializable {
1261cb0ef41Sopenharmony_ci    PROTOCOL_DISALLOW_COPY(InternalResponse);
1271cb0ef41Sopenharmony_cipublic:
1281cb0ef41Sopenharmony_ci    static std::unique_ptr<InternalResponse> createResponse(int callId, std::unique_ptr<Serializable> params);
1291cb0ef41Sopenharmony_ci    static std::unique_ptr<InternalResponse> createNotification(const String& notification, std::unique_ptr<Serializable> params = nullptr);
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci    String serializeToJSON() override;
1321cb0ef41Sopenharmony_ci    std::vector<uint8_t> serializeToBinary() override;
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci    ~InternalResponse() override {}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciprivate:
1371cb0ef41Sopenharmony_ci    InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params);
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci    int m_callId;
1401cb0ef41Sopenharmony_ci    String m_notification;
1411cb0ef41Sopenharmony_ci    std::unique_ptr<Serializable> m_params;
1421cb0ef41Sopenharmony_ci};
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ciclass InternalRawNotification : public Serializable {
1451cb0ef41Sopenharmony_cipublic:
1461cb0ef41Sopenharmony_ci    static std::unique_ptr<InternalRawNotification> fromJSON(String notification)
1471cb0ef41Sopenharmony_ci    {
1481cb0ef41Sopenharmony_ci        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
1491cb0ef41Sopenharmony_ci    }
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci    static std::unique_ptr<InternalRawNotification> fromBinary(std::vector<uint8_t> notification)
1521cb0ef41Sopenharmony_ci    {
1531cb0ef41Sopenharmony_ci        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
1541cb0ef41Sopenharmony_ci    }
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci    ~InternalRawNotification() override {}
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci    String serializeToJSON() override
1591cb0ef41Sopenharmony_ci    {
1601cb0ef41Sopenharmony_ci        return std::move(m_jsonNotification);
1611cb0ef41Sopenharmony_ci    }
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci    std::vector<uint8_t> serializeToBinary() override
1641cb0ef41Sopenharmony_ci    {
1651cb0ef41Sopenharmony_ci        return std::move(m_binaryNotification);
1661cb0ef41Sopenharmony_ci    }
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ciprivate:
1691cb0ef41Sopenharmony_ci  explicit InternalRawNotification(String notification)
1701cb0ef41Sopenharmony_ci    : m_jsonNotification(std::move(notification)) { }
1711cb0ef41Sopenharmony_ci  explicit InternalRawNotification(std::vector<uint8_t> notification)
1721cb0ef41Sopenharmony_ci    : m_binaryNotification(std::move(notification)) { }
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  String m_jsonNotification;
1751cb0ef41Sopenharmony_ci  std::vector<uint8_t> m_binaryNotification;
1761cb0ef41Sopenharmony_ci};
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
1791cb0ef41Sopenharmony_ci} // namespace {{namespace}}
1801cb0ef41Sopenharmony_ci{% endfor %}
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci#endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h)
183