|
14 | 14 | #include "JsV8InspectorClient.h" |
15 | 15 | #include "RuntimeConfig.h" |
16 | 16 | #include "include/libplatform/libplatform.h" |
| 17 | +#include "third_party/json.hpp" |
17 | 18 | #include "utils.h" |
18 | 19 |
|
19 | 20 | using namespace v8; |
| 21 | +using json = nlohmann::json; |
20 | 22 |
|
21 | 23 | namespace v8_inspector { |
22 | 24 |
|
|
264 | 266 | Local<Context> context = tns::Caches::Get(isolate)->GetContext(); |
265 | 267 | bool success; |
266 | 268 |
|
| 269 | + auto json_message = json::parse(message); |
| 270 | + std::string method = json_message["method"]; |
| 271 | + |
267 | 272 | // livesync uses the inspector socket for HMR/LiveSync... |
268 | | - if (message.find("Page.reload") != std::string::npos) { |
| 273 | + if (method == "Page.reload") { |
269 | 274 | success = tns::LiveSync(this->isolate_); |
270 | 275 | if (!success) { |
271 | 276 | NSLog(@"LiveSync failed"); |
272 | 277 | } |
273 | 278 | // todo: should we return here, or is it OK to pass onto a possible Page.reload domain handler? |
274 | 279 | } |
275 | 280 |
|
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); |
278 | 301 |
|
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()); |
282 | 307 | return; |
283 | 308 | } |
284 | 309 |
|
285 | | - if (message.find("Tracing.end") != std::string::npos) { |
| 310 | + if (method == "Tracing.end") { |
286 | 311 | tracing_agent_->end(); |
287 | 312 | for (const auto& traceMessage : tracing_agent_->getLastTrace()) { |
288 | 313 | notify(traceMessage); |
|
329 | 354 |
|
330 | 355 | // if no handler handled the message successfully, fall-through to the default V8 implementation |
331 | 356 | 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()); |
332 | 368 |
|
333 | 369 | // TODO: check why this is needed (it should trigger automatically when script depth is 0) |
334 | 370 | isolate->PerformMicrotaskCheckpoint(); |
|
0 commit comments