Skip to content

Commit

Permalink
Implement Network.loadNetworkResource etc in C++ (#44845)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44845

## Design
 - `NetworkIO` is an object owned by the `HostAgent`, created by `HostTarget` where it is given a scoped executor.
 - `HostAgent` passes most handling of CDP `Network.loadNetworkResource` through `NetworkIO`.
 - `NetworkIO.loadNetworkResource` creates and holds a shared_ptr to a `Stream` representing a single resource load, and owning received headers and data. A reference is held in a map `streams_` until an error or it is closed with `IO.close`. `delegate.networkRequest` is called with the `stream`, which it retains for the lifetime of the request, and uses its methods to call back with headers, data and errors.

 - Callbacks for `IO.read` requests are held by the `Stream` until the incoming data is complete or enough data is available to fill the request (an implementation choice to optimise for fewest round trips). Any incoming data or error causes any pending requests to be rechecked.

## Unimplemented platforms
 - Platforms may optionally implement `HostTargetDelegate.networkRequest` (as of this diff, none do). If they don't we report a CDP "not implemented" error, similar to the status quo where it was unimplemented by the C++ agent.

Differential Revision: D54309633
  • Loading branch information
robhogan authored and facebook-github-bot committed Jun 9, 2024
1 parent 6937c70 commit 59688d5
Show file tree
Hide file tree
Showing 9 changed files with 909 additions and 11 deletions.
139 changes: 135 additions & 4 deletions packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ HostAgent::HostAgent(
: frontendChannel_(frontendChannel),
targetController_(targetController),
sessionMetadata_(std::move(sessionMetadata)),
networkIO_(targetController.createNetworkHandler()),
sessionState_(sessionState) {}

void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
Expand Down Expand Up @@ -151,6 +152,15 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
folly::dynamic::object("dataLossOccurred", false)));
shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
} else if (req.method == "Network.loadNetworkResource") {
handleLoadNetworkResource(req);
return;
} else if (req.method == "IO.read") {
handleIoRead(req);
return;
} else if (req.method == "IO.close") {
handleIoClose(req);
return;
}

if (!isFinishedHandlingRequest && instanceAgent_ &&
Expand All @@ -163,10 +173,131 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
return;
}

frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::MethodNotFound,
req.method + " not implemented yet"));
throw NotImplementedException(req.method);
}

void HostAgent::handleLoadNetworkResource(const cdp::PreparsedRequest& req) {
long long requestId = req.id;
auto res = folly::dynamic::object("id", requestId);
if (!req.params.isObject()) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InvalidParams,
"Invalid params: not an object."));
return;
}
if ((req.params.count("url") == 0u) || !req.params.at("url").isString()) {
frontendChannel_(cdp::jsonError(
requestId,
cdp::ErrorCode::InvalidParams,
"Invalid params: url is missing or not a string."));
return;
}

networkIO_->loadNetworkResource(
{.url = req.params.at("url").asString()},
targetController_.getDelegate(),
// This callback is always called, with resource.success=false on failure.
[requestId,
frontendChannel = frontendChannel_](NetworkResource resource) {
auto dynamicResource =
folly::dynamic::object("success", resource.success);

if (resource.stream) {
dynamicResource("stream", *resource.stream);
}

if (resource.netErrorName) {
dynamicResource("netErrorName", *resource.netErrorName);
}

if (resource.httpStatusCode) {
dynamicResource("httpStatusCode", *resource.httpStatusCode);
}

if (resource.headers) {
auto dynamicHeaders = folly::dynamic::object();
for (const auto& pair : *resource.headers) {
dynamicHeaders(pair.first, pair.second);
}
dynamicResource("headers", std::move(dynamicHeaders));
}

frontendChannel(cdp::jsonResult(
requestId,
folly::dynamic::object("resource", std::move(dynamicResource))));
});
}

void HostAgent::handleIoRead(const cdp::PreparsedRequest& req) {
long long requestId = req.id;
if (!req.params.isObject()) {
frontendChannel_(cdp::jsonError(
requestId,
cdp::ErrorCode::InvalidParams,
"Invalid params: not an object."));
return;
}
if ((req.params.count("handle") == 0u) ||
!req.params.at("handle").isString()) {
frontendChannel_(cdp::jsonError(
requestId,
cdp::ErrorCode::InvalidParams,
"Invalid params: handle is missing or not a string."));
return;
}
std::optional<unsigned long> size = std::nullopt;
if ((req.params.count("size") != 0u) && req.params.at("size").isInt()) {
size = req.params.at("size").asInt();
}
networkIO_->readStream(
{.handle = req.params.at("handle").asString(), .size = size},
[requestId, frontendChannel = frontendChannel_](
std::variant<IOReadError, IOReadResult> resultOrError) {
if (std::holds_alternative<IOReadError>(resultOrError)) {
frontendChannel(cdp::jsonError(
requestId,
cdp::ErrorCode::InternalError,
std::get<IOReadError>(resultOrError)));
} else {
const auto& result = std::get<IOReadResult>(resultOrError);
auto stringResult = cdp::jsonResult(
requestId,
folly::dynamic::object("data", result.data)("eof", result.eof)(
"base64Encoded", result.base64Encoded));
frontendChannel(stringResult);
}
});
}

void HostAgent::handleIoClose(const cdp::PreparsedRequest& req) {
long long requestId = req.id;
if (!req.params.isObject()) {
frontendChannel_(cdp::jsonError(
requestId,
cdp::ErrorCode::InvalidParams,
"Invalid params: not an object."));
return;
}
if ((req.params.count("handle") == 0u) ||
!req.params.at("handle").isString()) {
frontendChannel_(cdp::jsonError(
requestId,
cdp::ErrorCode::InvalidParams,
"Invalid params: handle is missing or not a string."));
return;
}
networkIO_->closeStream(
req.params.at("handle").asString(),
[requestId, frontendChannel = frontendChannel_](
std::optional<std::string> maybeError) {
if (maybeError) {
frontendChannel(cdp::jsonError(
requestId, cdp::ErrorCode::InternalError, *maybeError));
} else {
frontendChannel(cdp::jsonResult(requestId));
}
});
}

HostAgent::~HostAgent() {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-native/ReactCommon/jsinspector-modern/HostAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#pragma once

#include "CdpJson.h"
#include "HostTarget.h"
#include "NetworkIO.h"
#include "SessionState.h"

#include <jsinspector-modern/InspectorInterfaces.h>
Expand Down Expand Up @@ -98,12 +100,20 @@ class HostAgent final {
std::shared_ptr<InstanceAgent> instanceAgent_;
FuseboxClientType fuseboxClientType_{FuseboxClientType::Unknown};
bool isPausedInDebuggerOverlayVisible_{false};
std::shared_ptr<NetworkIO> networkIO_;

/**
* A shared reference to the session's state. This is only safe to access
* during handleRequest and other method calls on the same thread.
*/
SessionState& sessionState_;

/** Handle a Network.loadNetworkResource CDP request. */
void handleLoadNetworkResource(const cdp::PreparsedRequest& req);
/** Handle an IO.read CDP request. */
void handleIoRead(const cdp::PreparsedRequest& req);
/** Handle an IO.close CDP request. */
void handleIoClose(const cdp::PreparsedRequest& req);
};

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ class HostTargetSession {
return;
}

// Catch exceptions that may arise from accessing dynamic params during
// request handling.
try {
hostAgent_.handleRequest(request);
} catch (const cdp::TypeError& e) {
}
// Catch exceptions that may arise from accessing dynamic params during
// request handling.
catch (const cdp::TypeError& e) {
frontendChannel_(
cdp::jsonError(request.id, cdp::ErrorCode::InvalidRequest, e.what()));
return;
}
// Catch exceptions for unrecognised or partially implemented CDP methods.
catch (const NotImplementedException& e) {
frontendChannel_(
cdp::jsonError(request.id, cdp::ErrorCode::MethodNotFound, e.what()));
return;
}
}

/**
Expand Down Expand Up @@ -198,13 +205,23 @@ void HostTarget::sendCommand(HostCommand command) {
});
}

std::shared_ptr<NetworkIO> HostTarget::createNetworkHandler() {
auto networkIO = std::make_shared<NetworkIO>();
networkIO->setExecutor(executorFromThis());
return networkIO;
}

HostTargetController::HostTargetController(HostTarget& target)
: target_(target) {}

HostTargetDelegate& HostTargetController::getDelegate() {
return target_.getDelegate();
}

std::shared_ptr<NetworkIO> HostTargetController::createNetworkHandler() {
return target_.createNetworkHandler();
};

bool HostTargetController::hasInstance() const {
return target_.hasInstance();
}
Expand Down
33 changes: 30 additions & 3 deletions packages/react-native/ReactCommon/jsinspector-modern/HostTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "HostCommand.h"
#include "InspectorInterfaces.h"
#include "InstanceTarget.h"
#include "NetworkIO.h"
#include "ScopedExecutor.h"
#include "WeakList.h"

Expand Down Expand Up @@ -41,13 +42,13 @@ class HostTarget;
* React Native platform needs to implement in order to integrate with the
* debugging stack.
*/
class HostTargetDelegate {
class HostTargetDelegate : public NetworkRequestDelegate {
public:
HostTargetDelegate() = default;
HostTargetDelegate(const HostTargetDelegate&) = delete;
HostTargetDelegate(HostTargetDelegate&&) = default;
HostTargetDelegate(HostTargetDelegate&&) = delete;
HostTargetDelegate& operator=(const HostTargetDelegate&) = delete;
HostTargetDelegate& operator=(HostTargetDelegate&&) = default;
HostTargetDelegate& operator=(HostTargetDelegate&&) = delete;

// TODO(moti): This is 1:1 the shape of the corresponding CDP message -
// consider reusing typed/generated CDP interfaces when we have those.
Expand Down Expand Up @@ -104,6 +105,19 @@ class HostTargetDelegate {
*/
virtual void onSetPausedInDebuggerMessage(
const OverlaySetPausedInDebuggerMessageRequest& request) = 0;

/**
* Called by NetworkIO on handling a `Network.loadNetworkResource` CDP
* request. Platform implementations should override this to perform a
* network request of the given URL, and use listener's callbacks on receipt
* of headers, data chunks, and errors.
*/
void networkRequest(
const std::string& /*url*/,
std::shared_ptr<NetworkRequestListener> /*listener*/) override {
throw NotImplementedException(
"NetworkRequestDelegate.networkRequest is not implemented by this host target delegate.");
}
};

/**
Expand All @@ -116,6 +130,13 @@ class HostTargetController final {

HostTargetDelegate& getDelegate();

/**
* Instantiate a new NetworkIO with a scoped executor derived from the
* HostTarget's executor. Neither HostTarget nor HostTargetController
* retain a reference to the shared_ptr.
*/
std::shared_ptr<NetworkIO> createNetworkHandler();

bool hasInstance() const;

/**
Expand Down Expand Up @@ -213,6 +234,12 @@ class JSINSPECTOR_EXPORT HostTarget
*/
void sendCommand(HostCommand command);

/**
* Instantiate a new NetworkIO with a scoped executor derived from the
* HostTarget's executor. HostTarget does not retain a reference.
*/
std::shared_ptr<NetworkIO> createNetworkHandler();

private:
/**
* Constructs a new HostTarget.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class IDestructible {

struct InspectorTargetCapabilities {
bool nativePageReloads = false;
bool nativeSourceCodeFetching = false;
bool nativeSourceCodeFetching = true;
bool prefersFuseboxFrontend = false;
};

Expand Down Expand Up @@ -130,6 +130,19 @@ class JSINSPECTOR_EXPORT IInspector : public IDestructible {
std::weak_ptr<IPageStatusListener> listener) = 0;
};

class NotImplementedException : public std::exception {
public:
explicit NotImplementedException(std::string message)
: msg_(std::move(message)) {}

const char* what() const noexcept override {
return msg_.c_str();
}

private:
std::string msg_;
};

/// getInspectorInstance retrieves the singleton inspector that tracks all
/// debuggable pages in this process.
extern IInspector& getInspectorInstance();
Expand Down
Loading

0 comments on commit 59688d5

Please sign in to comment.