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