Skip to content

Commit 18f69c1

Browse files
committed
feat: add json parsing and better handling of categories
1 parent e848a01 commit 18f69c1

File tree

6 files changed

+25788
-14
lines changed

6 files changed

+25788
-14
lines changed

.clang-format-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ NativeScript/inspector/third_party/*
99
NativeScript/inspector/third_party/*/*
1010
NativeScript/inspector/third_party/*/*/*
1111
NativeScript/inspector/third_party/*/*/*/*
12+
NativeScript/third_party/*

NativeScript/inspector/JsV8InspectorClient.mm

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
#include "JsV8InspectorClient.h"
1515
#include "RuntimeConfig.h"
1616
#include "include/libplatform/libplatform.h"
17+
#include "third_party/json.hpp"
1718
#include "utils.h"
1819

1920
using namespace v8;
21+
using json = nlohmann::json;
2022

2123
namespace v8_inspector {
2224

@@ -264,25 +266,48 @@
264266
Local<Context> context = tns::Caches::Get(isolate)->GetContext();
265267
bool success;
266268

269+
auto json_message = json::parse(message);
270+
std::string method = json_message["method"];
271+
267272
// livesync uses the inspector socket for HMR/LiveSync...
268-
if (message.find("Page.reload") != std::string::npos) {
273+
if (method == "Page.reload") {
269274
success = tns::LiveSync(this->isolate_);
270275
if (!success) {
271276
NSLog(@"LiveSync failed");
272277
}
273278
// todo: should we return here, or is it OK to pass onto a possible Page.reload domain handler?
274279
}
275280

276-
if (message.find("Tracing.start") != std::string::npos) {
277-
tracing_agent_->start();
281+
if (method == "Tracing.start") {
282+
std::vector<std::string> categories;
283+
284+
// Support new traceConfig format
285+
if (json_message.contains("params") && json_message["params"].contains("traceConfig")) {
286+
auto traceConfig = json_message["params"]["traceConfig"];
287+
if (traceConfig.contains("includedCategories")) {
288+
for (const auto& category : traceConfig["includedCategories"]) {
289+
categories.push_back(category.get<std::string>());
290+
}
291+
}
292+
}
293+
// Fall back to deprecated categories format
294+
else if (json_message.contains("params") && json_message["params"].contains("categories")) {
295+
for (const auto& category : json_message["params"]["categories"]) {
296+
categories.push_back(category.get<std::string>());
297+
}
298+
}
299+
300+
tracing_agent_->start(categories);
278301

279-
// echo back the request to notify frontend the action was a success
280-
// todo: send an empty response for the incoming message id instead.
281-
this->sendNotification(StringBuffer::create(messageView));
302+
json json_response = {
303+
{"id", json_message["id"]},
304+
{"result", json::object()},
305+
};
306+
this->notify(json_response.dump());
282307
return;
283308
}
284309

285-
if (message.find("Tracing.end") != std::string::npos) {
310+
if (method == "Tracing.end") {
286311
tracing_agent_->end();
287312
for (const auto& traceMessage : tracing_agent_->getLastTrace()) {
288313
notify(traceMessage);
@@ -329,6 +354,17 @@
329354

330355
// if no handler handled the message successfully, fall-through to the default V8 implementation
331356
this->session_->dispatchProtocolMessage(messageView);
357+
// if needed to test, disable the default handling and use this
358+
// json error = {
359+
// {"id", json_message["id"]},
360+
// {"error", {
361+
// {"code", -32601},
362+
// {
363+
// "message", "Method not found: " + method
364+
// }
365+
// }}
366+
// };
367+
// notify(error.dump());
332368

333369
// TODO: check why this is needed (it should trigger automatically when script depth is 0)
334370
isolate->PerformMicrotaskCheckpoint();

NativeScript/inspector/ns-v8-tracing-agent-impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class NSInMemoryTraceWriter : public TraceWriter {
5050
class TracingAgentImpl {
5151
public:
5252
TracingAgentImpl();
53-
bool start();
53+
bool start(const std::vector<std::string>& categories = {});
5454
bool end();
5555
const std::vector<std::string>& getLastTrace() { return lastTrace_; }
5656

NativeScript/inspector/ns-v8-tracing-agent-impl.mm

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
reinterpret_cast<TracingController*>(tns::Runtime::GetPlatform()->GetTracingController());
7575
}
7676

77-
bool TracingAgentImpl::start() {
77+
bool TracingAgentImpl::start(const std::vector<std::string>& categories) {
7878
if (!tracing_) {
7979
tracing_ = true;
8080

@@ -84,8 +84,15 @@
8484
tracing_controller_->Initialize(TraceBuffer::CreateTraceBufferRingBuffer(
8585
TraceBuffer::kRingBufferChunks, current_trace_writer_));
8686
// todo: create TraceConfig based on params.
87-
TraceConfig* config = TraceConfig::CreateDefaultTraceConfig();
88-
config->AddIncludedCategory("disabled-by-default-v8.cpu_profiler");
87+
TraceConfig* config = new TraceConfig();
88+
if (categories.size() > 0) {
89+
for (const auto& category : categories) {
90+
config->AddIncludedCategory(category.c_str());
91+
}
92+
} else {
93+
config->AddIncludedCategory("v8");
94+
config->AddIncludedCategory("disabled-by-default-v8.cpu_profiler");
95+
}
8996
config->SetTraceRecordMode(TraceRecordMode::RECORD_CONTINUOUSLY);
9097
tracing_controller_->StartTracing(config);
9198
}

0 commit comments

Comments
 (0)