1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#ifndef ECMASCRIPT_TOOLING_PROTOCOL_HANDLER_H
17e509ee18Sopenharmony_ci#define ECMASCRIPT_TOOLING_PROTOCOL_HANDLER_H
18e509ee18Sopenharmony_ci
19e509ee18Sopenharmony_ci#include <condition_variable>
20e509ee18Sopenharmony_ci#include <functional>
21e509ee18Sopenharmony_ci#include <queue>
22e509ee18Sopenharmony_ci#include <memory>
23e509ee18Sopenharmony_ci
24e509ee18Sopenharmony_ci#include "protocol_channel.h"
25e509ee18Sopenharmony_ci
26e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling {
27e509ee18Sopenharmony_ciclass ProtocolHandler final : public ProtocolChannel {
28e509ee18Sopenharmony_cipublic:
29e509ee18Sopenharmony_ci    enum DispatchStatus : int32_t {
30e509ee18Sopenharmony_ci        UNKNOWN = 0,
31e509ee18Sopenharmony_ci        DISPATCHING,
32e509ee18Sopenharmony_ci        DISPATCHED
33e509ee18Sopenharmony_ci    };
34e509ee18Sopenharmony_ci
35e509ee18Sopenharmony_ci    ProtocolHandler(std::function<void(const void *, const std::string &)> callback, const EcmaVM *vm)
36e509ee18Sopenharmony_ci        : callback_(std::move(callback)), dispatcher_(vm, this), vm_(vm) {}
37e509ee18Sopenharmony_ci    ~ProtocolHandler() override = default;
38e509ee18Sopenharmony_ci
39e509ee18Sopenharmony_ci    void WaitForDebugger() override;
40e509ee18Sopenharmony_ci    void RunIfWaitingForDebugger() override;
41e509ee18Sopenharmony_ci    void ProcessCommand();
42e509ee18Sopenharmony_ci    void DispatchCommand(std::string &&msg);
43e509ee18Sopenharmony_ci    int32_t GetDispatchStatus();
44e509ee18Sopenharmony_ci
45e509ee18Sopenharmony_ci    void SendResponse(const DispatchRequest &request, const DispatchResponse &response,
46e509ee18Sopenharmony_ci                      const PtBaseReturns &result) override;
47e509ee18Sopenharmony_ci    void SendNotification(const PtBaseEvents &events) override;
48e509ee18Sopenharmony_ci
49e509ee18Sopenharmony_ci    std::unique_ptr<PtJson> CreateErrorReply(const DispatchResponse &response);
50e509ee18Sopenharmony_ci    void SendReply(const PtJson &reply);
51e509ee18Sopenharmony_ci
52e509ee18Sopenharmony_ciprivate:
53e509ee18Sopenharmony_ci    NO_MOVE_SEMANTIC(ProtocolHandler);
54e509ee18Sopenharmony_ci    NO_COPY_SEMANTIC(ProtocolHandler);
55e509ee18Sopenharmony_ci
56e509ee18Sopenharmony_ci    std::function<void(const void *, const std::string &)> callback_;
57e509ee18Sopenharmony_ci    Dispatcher dispatcher_;
58e509ee18Sopenharmony_ci
59e509ee18Sopenharmony_ci    bool waitingForDebugger_ {false};
60e509ee18Sopenharmony_ci    const EcmaVM *vm_ {nullptr};
61e509ee18Sopenharmony_ci
62e509ee18Sopenharmony_ci    std::condition_variable requestQueueCond_;
63e509ee18Sopenharmony_ci    std::queue<std::string> requestQueue_;
64e509ee18Sopenharmony_ci    std::mutex requestLock_;
65e509ee18Sopenharmony_ci    std::atomic<bool> isDispatchingMessage_ {false};
66e509ee18Sopenharmony_ci};
67e509ee18Sopenharmony_ci}  // namespace panda::ecmascript::tooling
68e509ee18Sopenharmony_ci
69e509ee18Sopenharmony_ci#endif
70