Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Network.loadNetworkResource etc in C++ #44845

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -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
Loading