Files
react-native/packages/react-native/ReactCommon/jsinspector-modern/InstanceAgent.cpp
T
Rob HoganandFacebook GitHub Bot 3ed0ff34b3 Add helpers for formatting JSON CDP responses (#43340)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43340

Adds convenience methods `jsonResult`, `jsonError` and `jsonNotification` for more ergonomic construction of CDP JSON responses. Note that CDP is *loosely* based on [JSON-RPC 2.0](https://www.jsonrpc.org/specification), but differs for example in the  omission of `"jsonrpc": "2.0"`.

Before:
```
frontendChannel_(folly::toJson(folly::dynamic::object("id", req.id)(
            "error",
            folly::dynamic::object("code", -32602)(
                "message",
                "executionContextName is mutually exclusive with executionContextId"))));
```

After:
```
frontendChannel_(cdp::jsonError(
            req.id,
            cdp::ErrorCode::InvalidParams,
            "executionContextName is mutually exclusive with executionContextId"));
```

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D54202854

fbshipit-source-id: 76a407ae39ff9c2ec79bcaddb6cd4d494afb7693
2024-03-06 09:27:50 -08:00

75 lines
2.3 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <jsinspector-modern/InstanceAgent.h>
#include "CdpJson.h"
#include "RuntimeTarget.h"
namespace facebook::react::jsinspector_modern {
InstanceAgent::InstanceAgent(
FrontendChannel frontendChannel,
InstanceTarget& target,
SessionState& sessionState)
: frontendChannel_(frontendChannel),
target_(target),
sessionState_(sessionState) {
(void)target_;
}
bool InstanceAgent::handleRequest(const cdp::PreparsedRequest& req) {
if (req.method == "Runtime.enable") {
maybeSendExecutionContextCreatedNotification();
// Fall through
}
if (runtimeAgent_ && runtimeAgent_->handleRequest(req)) {
return true;
}
return false;
}
void InstanceAgent::setCurrentRuntime(RuntimeTarget* runtimeTarget) {
auto previousRuntimeAgent = std::move(runtimeAgent_);
if (runtimeTarget) {
runtimeAgent_ = runtimeTarget->createAgent(frontendChannel_, sessionState_);
} else {
runtimeAgent_.reset();
}
if (!sessionState_.isRuntimeDomainEnabled) {
return;
}
if (previousRuntimeAgent != nullptr) {
auto& previousContext =
previousRuntimeAgent->getExecutionContextDescription();
folly::dynamic params =
folly::dynamic::object("executionContextId", previousContext.id);
if (previousContext.uniqueId.has_value()) {
params["executionContextUniqueId"] = *previousContext.uniqueId;
}
frontendChannel_(
cdp::jsonNotification("Runtime.executionContextDestroyed", params));
}
maybeSendExecutionContextCreatedNotification();
}
void InstanceAgent::maybeSendExecutionContextCreatedNotification() {
if (runtimeAgent_ != nullptr) {
auto& newContext = runtimeAgent_->getExecutionContextDescription();
folly::dynamic params = folly::dynamic::object(
"context",
folly::dynamic::object("id", newContext.id)(
"origin", newContext.origin)("name", newContext.name));
if (newContext.uniqueId.has_value()) {
params["uniqueId"] = *newContext.uniqueId;
}
frontendChannel_(
cdp::jsonNotification("Runtime.executionContextCreated", params));
}
}
} // namespace facebook::react::jsinspector_modern