File: NodeJSDevToolsProtocol.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (221 lines) | stat: -rw-r--r-- 9,257 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "NodeDebugger.h"
#include "NodeFileManager.h"
#include "NodeJSDevToolsProtocol.h"
#include "NodeJSEvents.h"
#include "RemoteObject.h"
#include "SocketAPI/clSocketBase.h"
#include "clDebugRemoteObjectEvent.h"
#include "cl_command_event.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "JSON.h"

class RemoteObject;
NodeJSDevToolsProtocol::NodeJSDevToolsProtocol() {}

NodeJSDevToolsProtocol::~NodeJSDevToolsProtocol() {}

NodeJSDevToolsProtocol& NodeJSDevToolsProtocol::Get()
{
    static NodeJSDevToolsProtocol protocol;
    return protocol;
}

void NodeJSDevToolsProtocol::SendSimpleCommand(clWebSocketClient& socket, const wxString& command,
                                               const JSONItem& params)
{
    try {
        JSON root(cJSON_Object);
        JSONItem e = root.toElement();
        e.addProperty("id", ++message_id);
        e.addProperty("method", command);
        if(params.isOk()) { e.append(params); }
        wxString command = e.format(false);
        clDEBUG() << "-->" << command;
        socket.Send(command);
    } catch(clSocketException& e) {
        clWARNING() << e.what();
    }
}

void NodeJSDevToolsProtocol::ProcessMessage(const wxString& msg, clWebSocketClient& socket)
{
    JSON root(msg);
    if(!root.isOk()) { return; }
    JSONItem rootElement = root.toElement();
    wxString method = rootElement.namedObject("method").toString();
    long replyId = rootElement.namedObject("id").toInt(wxNOT_FOUND);
    if(!method.IsEmpty()) {
        // Debugger event arrived
        NodeMessageBase::Ptr_t handler = m_handlers.GetHandler(method);
        if(handler) {
            clDEBUG() << "Invoking handler:" << handler->GetEventName();
            handler->Process(socket, rootElement.namedObject("params"));
        }
    } else if(replyId != wxNOT_FOUND) {
        if(rootElement.hasNamedObject("result")) {
            // Invoke the action for this reply
            if(m_waitingReplyCommands.count(replyId)) {
                CommandHandler& handler = m_waitingReplyCommands[replyId];
                if(handler.action) { handler.action(rootElement.namedObject("result")); }
                // Delete the handler
                m_waitingReplyCommands.erase(replyId);
            }
        }
    }
}

void NodeJSDevToolsProtocol::SendStartCommands(clWebSocketClient& socket)
{
    SendSimpleCommand(socket, "Runtime.enable");
    SendSimpleCommand(socket, "Debugger.enable");
    {
        JSONItem params = JSONItem::createObject("params");
        params.addProperty("state", "uncaught");
        SendSimpleCommand(socket, "Debugger.setPauseOnExceptions", params);
    }
    SendSimpleCommand(socket, "Runtime.runIfWaitingForDebugger");
}

void NodeJSDevToolsProtocol::Next(clWebSocketClient& socket) { SendSimpleCommand(socket, "Debugger.stepOver"); }

void NodeJSDevToolsProtocol::StepIn(clWebSocketClient& socket) { SendSimpleCommand(socket, "Debugger.stepInto"); }

void NodeJSDevToolsProtocol::StepOut(clWebSocketClient& socket) { SendSimpleCommand(socket, "Debugger.stepOut"); }

void NodeJSDevToolsProtocol::Continue(clWebSocketClient& socket) { SendSimpleCommand(socket, "Debugger.resume"); }

void NodeJSDevToolsProtocol::Clear() { m_waitingReplyCommands.clear(); }

void NodeJSDevToolsProtocol::SetBreakpoint(clWebSocketClient& socket, const NodeJSBreakpoint& bp)
{
    try {
        JSONItem params = bp.ToJSON("params");
        // Nodejs is using 0 based line numbers, while the editor starts from 1
        params.removeProperty("lineNumber");
        params.addProperty("lineNumber", bp.GetLine() - 1);
        // Send the command
        SendSimpleCommand(socket, "Debugger.setBreakpointByUrl", params);

        // Register a handler to handle this command when it returns
        CommandHandler handler(message_id, [=](const JSONItem& result) {
            wxString breakpointId = result.namedObject("breakpointId").toString();
            NodeJSBreakpoint& b = m_debugger->GetBreakpointsMgr()->GetBreakpoint(bp.GetFilename(), bp.GetLine());
            if(b.IsOk()) {
                b.SetNodeBpID(breakpointId);
                clDebugEvent bpEvent(wxEVT_NODEJS_DEBUGGER_UPDATE_BREAKPOINTS_VIEW);
                EventNotifier::Get()->AddPendingEvent(bpEvent);
            }
        });
        m_waitingReplyCommands.insert({ handler.m_commandID, handler });
    } catch(clSocketException& e) {
        clWARNING() << e.what();
    }
}

void NodeJSDevToolsProtocol::DeleteBreakpoint(clWebSocketClient& socket, const NodeJSBreakpoint& bp)
{
    try {
        JSONItem params = JSONItem::createObject("params");
        params.addProperty("breakpointId", bp.GetNodeBpID());
        // Send the command
        SendSimpleCommand(socket, "Debugger.removeBreakpoint", params);

        // Register a handler to handle this command when it returns
        CommandHandler handler(message_id, [=](const JSONItem& result) {
            clDebugEvent bpEvent(wxEVT_NODEJS_DEBUGGER_UPDATE_BREAKPOINTS_VIEW);
            EventNotifier::Get()->AddPendingEvent(bpEvent);
        });
        m_waitingReplyCommands.insert({ handler.m_commandID, handler });
    } catch(clSocketException& e) {
        clWARNING() << e.what();
    }
}

void NodeJSDevToolsProtocol::GetScriptSource(clWebSocketClient& socket, const wxString& scriptId)
{
    JSONItem params = JSONItem::createObject("params");
    params.addProperty("scriptId", scriptId);
    // Send the command
    SendSimpleCommand(socket, "Debugger.getScriptSource", params);
    // Register a handler to handle this command when it returns
    CommandHandler handler(message_id, [=](const JSONItem& result) {
        wxString fileContent = result.namedObject("scriptSource").toString();
        NodeFileManager::Get().CacheRemoteCopy(scriptId, fileContent);
    });
    m_waitingReplyCommands.insert({ handler.m_commandID, handler });
}

void NodeJSDevToolsProtocol::Eval(clWebSocketClient& socket, const wxString& expression, const wxString& frameId)
{
    JSONItem params = JSONItem::createObject("params");
    params.addProperty("callFrameId", frameId);
    params.addProperty("expression", expression);
    params.addProperty("generatePreview", true);
    SendSimpleCommand(socket, "Debugger.evaluateOnCallFrame", params);
    // Register a handler to handle this command when it returns
    CommandHandler handler(message_id, [=](const JSONItem& result) {
        if(result.hasNamedObject("result")) {
            nSerializableObject::Ptr_t ro(new RemoteObject());
            ro->To<RemoteObject>()->SetExpression(expression);
            ro->To<RemoteObject>()->FromJSON(result.namedObject("result"));
            clDebugRemoteObjectEvent evt(wxEVT_NODEJS_DEBUGGER_EVAL_RESULT);
            evt.SetRemoteObject(ro);
            EventNotifier::Get()->AddPendingEvent(evt);
        }
    });
    m_waitingReplyCommands.insert({ handler.m_commandID, handler });
}

void NodeJSDevToolsProtocol::GetObjectProperties(clWebSocketClient& socket, const wxString& objectId,
                                                 wxEventType eventType)
{
    JSONItem params = JSONItem::createObject("params");
    params.addProperty("objectId", objectId);
    SendSimpleCommand(socket, "Runtime.getProperties", params);
    // Register a handler to handle this command when it returns
    CommandHandler handler(message_id, [=](const JSONItem& result) {
        if(result.hasNamedObject("result")) {
            clDebugEvent evt(eventType);
            evt.SetString(result.namedObject("result").format(false));
            evt.SetStartupCommands(objectId);
            EventNotifier::Get()->AddPendingEvent(evt);
        }
    });
    m_waitingReplyCommands.insert({ handler.m_commandID, handler });
}

void NodeJSDevToolsProtocol::CreateObject(clWebSocketClient& socket, const wxString& expression,
                                          const wxString& frameId)
{
    // Same as eval but without the preview
    JSONItem params = JSONItem::createObject("params");
    params.addProperty("callFrameId", frameId);
    params.addProperty("expression", expression);
    params.addProperty("generatePreview", false);
    SendSimpleCommand(socket, "Debugger.evaluateOnCallFrame", params);
    // Register a handler to handle this command when it returns
    CommandHandler handler(message_id, [=](const JSONItem& result) {
        if(result.hasNamedObject("result")) {
            nSerializableObject::Ptr_t ro(new RemoteObject());
            ro->To<RemoteObject>()->SetExpression(expression);
            ro->To<RemoteObject>()->FromJSON(result.namedObject("result"));
            clDebugRemoteObjectEvent evt(wxEVT_NODEJS_DEBUGGER_CREATE_OBJECT);
            evt.SetRemoteObject(ro);
            EventNotifier::Get()->AddPendingEvent(evt);
        }
    });
    m_waitingReplyCommands.insert({ handler.m_commandID, handler });
}

void NodeJSDevToolsProtocol::DeleteBreakpointByID(clWebSocketClient& socket, const wxString& bpid)
{
    try {
        JSONItem params = JSONItem::createObject("params");
        params.addProperty("breakpointId", bpid);
        // Send the command
        SendSimpleCommand(socket, "Debugger.removeBreakpoint", params);
    } catch(clSocketException& e) {
        clWARNING() << e.what();
    }
}