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#include "debugger_service.h" 17e509ee18Sopenharmony_ci 18e509ee18Sopenharmony_ci#include "protocol_handler.h" 19e509ee18Sopenharmony_ci 20e509ee18Sopenharmony_ci#include "ecmascript/debugger/js_debugger_manager.h" 21e509ee18Sopenharmony_ci#include "ecmascript/ecma_vm.h" 22e509ee18Sopenharmony_ci 23e509ee18Sopenharmony_cinamespace panda::ecmascript::tooling { 24e509ee18Sopenharmony_civoid InitializeDebugger(::panda::ecmascript::EcmaVM *vm, 25e509ee18Sopenharmony_ci const std::function<void(const void *, const std::string &)> &onResponse) 26e509ee18Sopenharmony_ci{ 27e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 28e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 29e509ee18Sopenharmony_ci return; 30e509ee18Sopenharmony_ci } 31e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 32e509ee18Sopenharmony_ci if (handler != nullptr) { 33e509ee18Sopenharmony_ci LOG_DEBUGGER(ERROR) << "JS debugger was initialized"; 34e509ee18Sopenharmony_ci return; 35e509ee18Sopenharmony_ci } 36e509ee18Sopenharmony_ci vm->GetJsDebuggerManager()->SetDebuggerHandler(new ProtocolHandler(onResponse, vm)); 37e509ee18Sopenharmony_ci} 38e509ee18Sopenharmony_ci 39e509ee18Sopenharmony_civoid UninitializeDebugger(::panda::ecmascript::EcmaVM *vm) 40e509ee18Sopenharmony_ci{ 41e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 42e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 43e509ee18Sopenharmony_ci return; 44e509ee18Sopenharmony_ci } 45e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 46e509ee18Sopenharmony_ci delete handler; 47e509ee18Sopenharmony_ci vm->GetJsDebuggerManager()->SetDebuggerHandler(nullptr); 48e509ee18Sopenharmony_ci} 49e509ee18Sopenharmony_ci 50e509ee18Sopenharmony_civoid WaitForDebugger(const ::panda::ecmascript::EcmaVM *vm) 51e509ee18Sopenharmony_ci{ 52e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 53e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 54e509ee18Sopenharmony_ci return; 55e509ee18Sopenharmony_ci } 56e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 57e509ee18Sopenharmony_ci if (LIKELY(handler != nullptr)) { 58e509ee18Sopenharmony_ci handler->WaitForDebugger(); 59e509ee18Sopenharmony_ci } 60e509ee18Sopenharmony_ci} 61e509ee18Sopenharmony_ci 62e509ee18Sopenharmony_civoid OnMessage(const ::panda::ecmascript::EcmaVM *vm, std::string &&message) 63e509ee18Sopenharmony_ci{ 64e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 65e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 66e509ee18Sopenharmony_ci return; 67e509ee18Sopenharmony_ci } 68e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 69e509ee18Sopenharmony_ci if (LIKELY(handler != nullptr)) { 70e509ee18Sopenharmony_ci handler->DispatchCommand(std::move(message)); 71e509ee18Sopenharmony_ci } 72e509ee18Sopenharmony_ci} 73e509ee18Sopenharmony_ci 74e509ee18Sopenharmony_civoid ProcessMessage(const ::panda::ecmascript::EcmaVM *vm) 75e509ee18Sopenharmony_ci{ 76e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 77e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 78e509ee18Sopenharmony_ci return; 79e509ee18Sopenharmony_ci } 80e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 81e509ee18Sopenharmony_ci if (LIKELY(handler != nullptr)) { 82e509ee18Sopenharmony_ci handler->ProcessCommand(); 83e509ee18Sopenharmony_ci } 84e509ee18Sopenharmony_ci} 85e509ee18Sopenharmony_ci 86e509ee18Sopenharmony_ciint32_t GetDispatchStatus(const ::panda::ecmascript::EcmaVM *vm) 87e509ee18Sopenharmony_ci{ 88e509ee18Sopenharmony_ci if (vm == nullptr || vm->GetJsDebuggerManager() == nullptr) { 89e509ee18Sopenharmony_ci LOG_DEBUGGER(DEBUG) << "VM has already been destroyed"; 90e509ee18Sopenharmony_ci return ProtocolHandler::DispatchStatus::UNKNOWN; 91e509ee18Sopenharmony_ci } 92e509ee18Sopenharmony_ci ProtocolHandler *handler = vm->GetJsDebuggerManager()->GetDebuggerHandler(); 93e509ee18Sopenharmony_ci if (LIKELY(handler != nullptr)) { 94e509ee18Sopenharmony_ci return handler->GetDispatchStatus(); 95e509ee18Sopenharmony_ci } 96e509ee18Sopenharmony_ci return ProtocolHandler::DispatchStatus::UNKNOWN; 97e509ee18Sopenharmony_ci} 98e509ee18Sopenharmony_ci} // namespace panda::ecmascript::tooling