Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmake/TransmuteCore.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
file(GLOB TR_CORE_SOURCE
file(GLOB_RECURSE TR_CORE_SOURCE
"src/analytics/*.cpp"
"src/math/*.cpp"
"src/runtime/*.cpp"
"src/renderer/*.cpp"
"src/renderer/gles/*.cpp"
"src/xr/*.cpp"
)

if(NOT TR_ENABLE_INSPECTOR)
list(FILTER TR_CORE_SOURCE EXCLUDE REGEX ".*/src/runtime/inspector\\.cpp$")
list(FILTER TR_CORE_SOURCE EXCLUDE REGEX ".*/src/runtime/inspector_[^/]*\\.cpp$")
list(FILTER TR_CORE_SOURCE EXCLUDE REGEX ".*/src/runtime/inspector/.*\\.cpp$")
endif()

if(APPLE)
Expand Down
238 changes: 238 additions & 0 deletions docs/internals/cdp_support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
# Chrome DevTools Protocol (CDP) Support

This document describes the CDP support added to the JSAR Runtime inspector.

## Overview

The JSAR Runtime inspector now supports basic Chrome DevTools Protocol (CDP) functionality, enabling CDP clients (such as Chrome DevTools) to connect via WebSocket and interact with the runtime.

## Architecture

### Core Components

- **`CdpHandler`** - Main coordinator for CDP message processing
- **`CdpMessage`** - CDP message parsing with JSON validation
- **`CdpResponse`** - Response builder for success/error/event responses
- **`CdpDomainHandler`** - Base class for domain implementations

### Domain Implementations

- **`CdpRuntimeDomain`** - Runtime domain with JavaScript runtime methods
- **`CdpMyExampleDomain`** - Example domain for testing connectivity

## Supported Domains

### Runtime Domain

The Runtime domain exposes JavaScript runtime functionality.

#### Methods

- **`Runtime.enable`** - Enables runtime domain
- **`Runtime.disable`** - Disables runtime domain
- **`Runtime.getVersion`** - Returns JavaScript runtime version information

#### Example Usage

```javascript
// Enable runtime domain
{
"id": 1,
"method": "Runtime.enable",
"params": {}
}

// Response
{
"id": 1,
"result": {}
}
```

```javascript
// Get version information
{
"id": 2,
"method": "Runtime.getVersion",
"params": {}
}

// Response
{
"id": 2,
"result": {
"product": "JSAR",
"revision": "0.8.2",
"userAgent": "JSAR/0.8.2",
"jsVersion": "ES2021"
}
}
```

### MyExample Domain

Sample domain for testing CDP connectivity and method invocation.

#### Methods

- **`MyExample.ping`** - Simple ping command that responds with pong
- **`MyExample.echo`** - Echoes back the provided parameters
- **`MyExample.getInfo`** - Returns information about this domain

#### Example Usage

```javascript
// Ping test
{
"id": 3,
"method": "MyExample.ping",
"params": {}
}

// Response
{
"id": 3,
"result": {
"message": "pong",
"timestamp": 1234567890
}
}
```

```javascript
// Echo test
{
"id": 4,
"method": "MyExample.echo",
"params": {
"message": "Hello JSAR!",
"data": { "test": true }
}
}

// Response
{
"id": 4,
"result": {
"echoed": {
"message": "Hello JSAR!",
"data": { "test": true }
}
}
}
```

## Protocol Discovery

The inspector provides protocol discovery via the `/json/protocol` endpoint:

```bash
curl http://localhost:9423/json/protocol
```

This returns the complete protocol description including all supported domains and their methods.

## Connection

### WebSocket Endpoint

```
ws://localhost:9423/devtools/inspector/{content_id}
```

### Example Connection

```javascript
const ws = new WebSocket('ws://localhost:9423/devtools/inspector/1');

ws.onopen = () => {
// Send CDP message
ws.send(JSON.stringify({
id: 1,
method: 'Runtime.enable',
params: {}
}));
};

ws.onmessage = (event) => {
const response = JSON.parse(event.data);
console.log('CDP Response:', response);
};
```

## Error Handling

The implementation follows JSON-RPC 2.0 error codes:

- **-32700** - Parse error (invalid JSON)
- **-32601** - Method not found (unknown domain or method)
- **-32603** - Internal error (exception during processing)

### Example Error Response

```javascript
{
"id": 5,
"error": {
"code": -32601,
"message": "Method not found"
}
}
```

## Extensibility

### Adding New Domains

1. Create a new domain handler class extending `CdpDomainHandler`
2. Implement the `handleMethod` function
3. Register the domain in `TrInspector::initialize()`

#### Example

```cpp
class CdpCustomDomain : public CdpDomainHandler {
public:
std::string handleMethod(const std::string& method, const CdpMessage& message) override {
if (method == "customMethod") {
rapidjson::Document result;
result.SetObject();
// ... build result
return CdpResponse::success(message.id, result);
}
return CdpResponse::error(message.id, -32601, "Custom." + method + " is not supported");
}
};

// Register in TrInspector::initialize()
cdpHandler_->registerDomain("Custom", std::make_unique<CdpCustomDomain>());
```

## Logging

The implementation includes comprehensive logging for debugging:

- Connection events
- Message parsing
- Method invocation
- Error handling

Logs use the `LOG_TAG_INSPECTOR` tag and can be viewed in the inspector logs.

## Testing

Use the provided test clients:

- **Node.js Client**: `fixtures/inspector-client/inspector_websocket_client.js`
- **Browser Test**: `fixtures/inspector-client/inspector_websocket_test.html`

Or connect with Chrome DevTools using the DevTools frontend URL from `/json/list`.

## Compatibility

The implementation is compatible with:

- Chrome DevTools Protocol v1.3
- Standard CDP clients
- WebSocket-based debugging tools
- JSON-RPC 2.0 message format
Loading