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