mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Support limiting CDP bindings to specific contexts (#43089)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43089 Changelog: [Internal] Completes React Native's support for the [`Runtime.addBinding`](https://cdpstatus.reactnative.dev/devtools-protocol/tot/Runtime#method-addBinding) and [`Runtime.removeBinding`](https://cdpstatus.reactnative.dev/devtools-protocol/tot/Runtime#method-removeBinding) methods (in the modern CDP backend) by handling the optional `executionContextId` and `executionContextName` params. Reviewed By: huntie Differential Revision: D53760393 fbshipit-source-id: 1679fcde64c1125eb3f0f780432fedd6e3c2efc1
This commit is contained in:
1 parent
918b75f3f4
commit
1863805164
5 files changed
+271
-11
No files matched your search
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 "ExecutionContext.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
namespace {
|
||||
|
||||
template <class>
|
||||
inline constexpr bool always_false_v = false;
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ExecutionContextSelector::matches(
|
||||
const ExecutionContextDescription& context) const noexcept {
|
||||
// Exhaustiveness checking based on the example in
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit.
|
||||
return std::visit(
|
||||
[&context](auto&& arg) {
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_same_v<T, AllContexts>) {
|
||||
return true;
|
||||
} else if constexpr (std::is_same_v<T, ContextId>) {
|
||||
return context.id == arg;
|
||||
} else if constexpr (std::is_same_v<T, ContextName>) {
|
||||
return context.name == arg;
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "non-exhaustive visitor");
|
||||
}
|
||||
},
|
||||
value_);
|
||||
|
||||
// Prevent the compiler from thinking always_false_v is unused when the
|
||||
// visitor is (correctly) exhaustive.
|
||||
(void)always_false_v<void>;
|
||||
}
|
||||
|
||||
ExecutionContextSelector ExecutionContextSelector::byId(int32_t id) {
|
||||
return ExecutionContextSelector{id};
|
||||
}
|
||||
|
||||
ExecutionContextSelector ExecutionContextSelector::byName(std::string name) {
|
||||
return ExecutionContextSelector{std::move(name)};
|
||||
}
|
||||
|
||||
ExecutionContextSelector ExecutionContextSelector::all() {
|
||||
return ExecutionContextSelector{AllContexts{}};
|
||||
}
|
||||
|
||||
bool matchesAny(
|
||||
const ExecutionContextDescription& context,
|
||||
const ExecutionContextSelectorSet& selectors) {
|
||||
for (const auto& selector : selectors) {
|
||||
if (selector.matches(context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -7,9 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "UniqueMonostate.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <variant>
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
@@ -20,4 +24,86 @@ struct ExecutionContextDescription {
|
||||
std::optional<std::string> uniqueId;
|
||||
};
|
||||
|
||||
/**
|
||||
* A type-safe selector for execution contexts.
|
||||
*/
|
||||
class ExecutionContextSelector {
|
||||
public:
|
||||
/**
|
||||
* Returns true iff this selector matches \c context.
|
||||
*/
|
||||
bool matches(const ExecutionContextDescription& context) const noexcept;
|
||||
|
||||
/**
|
||||
* Returns a new selector that matches only the given execution context ID.
|
||||
*/
|
||||
static ExecutionContextSelector byId(int32_t id);
|
||||
|
||||
/**
|
||||
* Returns a new selector that matches only the given execution context name.
|
||||
*/
|
||||
static ExecutionContextSelector byName(std::string name);
|
||||
|
||||
/**
|
||||
* Returns a new selector that matches any execution context.
|
||||
*/
|
||||
static ExecutionContextSelector all();
|
||||
|
||||
ExecutionContextSelector() = delete;
|
||||
ExecutionContextSelector(const ExecutionContextSelector& other) = default;
|
||||
ExecutionContextSelector(ExecutionContextSelector&& other) noexcept = default;
|
||||
ExecutionContextSelector& operator=(const ExecutionContextSelector& other) =
|
||||
default;
|
||||
ExecutionContextSelector& operator=(
|
||||
ExecutionContextSelector&& other) noexcept = default;
|
||||
~ExecutionContextSelector() = default;
|
||||
|
||||
inline bool operator==(const ExecutionContextSelector& other) const noexcept {
|
||||
return value_ == other.value_;
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* Marker type used to represent "all execution contexts".
|
||||
*
|
||||
* Q: What is a UniqueMonostate?
|
||||
* A: std::monostate, but it's distinct from other UniqueMonostate<...>s, so
|
||||
* you can use multiple of them in the same variant without ambiguity.
|
||||
*/
|
||||
using AllContexts = UniqueMonostate<0>;
|
||||
using ContextId = int32_t;
|
||||
using ContextName = std::string;
|
||||
|
||||
using Representation = std::variant<AllContexts, ContextId, ContextName>;
|
||||
|
||||
explicit inline ExecutionContextSelector(Representation&& r) : value_(r) {}
|
||||
|
||||
Representation value_;
|
||||
|
||||
friend struct std::hash<
|
||||
facebook::react::jsinspector_modern::ExecutionContextSelector>;
|
||||
};
|
||||
|
||||
using ExecutionContextSelectorSet =
|
||||
std::unordered_set<ExecutionContextSelector>;
|
||||
|
||||
bool matchesAny(
|
||||
const ExecutionContextDescription& context,
|
||||
const ExecutionContextSelectorSet& selectors);
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<::facebook::react::jsinspector_modern::ExecutionContextSelector> {
|
||||
size_t operator()(
|
||||
const ::facebook::react::jsinspector_modern::ExecutionContextSelector&
|
||||
selector) const {
|
||||
return hash<::facebook::react::jsinspector_modern::
|
||||
ExecutionContextSelector::Representation>{}(
|
||||
selector.value_);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
@@ -20,19 +20,52 @@ RuntimeAgent::RuntimeAgent(
|
||||
sessionState_(sessionState),
|
||||
delegate_(std::move(delegate)),
|
||||
executionContextDescription_(executionContextDescription) {
|
||||
for (auto& name : sessionState_.subscribedBindingNames) {
|
||||
targetController_.installBindingHandler(name);
|
||||
for (auto& [name, contextSelectors] : sessionState_.subscribedBindings) {
|
||||
if (matchesAny(executionContextDescription_, contextSelectors)) {
|
||||
targetController_.installBindingHandler(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
|
||||
if (req.method == "Runtime.addBinding") {
|
||||
std::string bindingName = req.params["name"].getString();
|
||||
// TODO: Respect @cdp Runtime.addBinding's executionContextId and
|
||||
// executionContextName params.
|
||||
sessionState_.subscribedBindingNames.emplace(bindingName);
|
||||
|
||||
targetController_.installBindingHandler(bindingName);
|
||||
ExecutionContextSelector contextSelector = ExecutionContextSelector::all();
|
||||
|
||||
// TODO: Eventually, move execution context targeting out of RuntimeAgent.
|
||||
// Right now, there's only ever one context (Runtime) in a Page, so we can
|
||||
// handle it here for simplicity, and use session state to propagate
|
||||
// bindings to the next RuntimeAgent.
|
||||
if (req.params.count("executionContextId")) {
|
||||
auto executionContextId = req.params["executionContextId"].getInt();
|
||||
if (executionContextId < (int64_t)std::numeric_limits<int32_t>::min() ||
|
||||
executionContextId > (int64_t)std::numeric_limits<int32_t>::max()) {
|
||||
frontendChannel_(folly::toJson(folly::dynamic::object("id", req.id)(
|
||||
"error",
|
||||
folly::dynamic::object("code", -32602)(
|
||||
"message", "Invalid execution context id"))));
|
||||
return true;
|
||||
}
|
||||
contextSelector =
|
||||
ExecutionContextSelector::byId((int32_t)executionContextId);
|
||||
|
||||
if (req.params.count("executionContextName")) {
|
||||
frontendChannel_(folly::toJson(folly::dynamic::object("id", req.id)(
|
||||
"error",
|
||||
folly::dynamic::object("code", -32602)(
|
||||
"message",
|
||||
"executionContextName is mutually exclusive with executionContextId"))));
|
||||
return true;
|
||||
}
|
||||
} else if (req.params.count("executionContextName")) {
|
||||
contextSelector = ExecutionContextSelector::byName(
|
||||
req.params["executionContextName"].getString());
|
||||
}
|
||||
if (contextSelector.matches(executionContextDescription_)) {
|
||||
targetController_.installBindingHandler(bindingName);
|
||||
}
|
||||
sessionState_.subscribedBindings[bindingName].insert(contextSelector);
|
||||
|
||||
folly::dynamic res = folly::dynamic::object("id", req.id)(
|
||||
"result", folly::dynamic::object());
|
||||
@@ -42,7 +75,12 @@ bool RuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
|
||||
return true;
|
||||
}
|
||||
if (req.method == "Runtime.removeBinding") {
|
||||
sessionState_.subscribedBindingNames.erase(req.params["name"].getString());
|
||||
// @cdp Runtime.removeBinding has no targeting by execution context. We
|
||||
// interpret it to mean "unsubscribe, and stop installing the binding on
|
||||
// all new contexts". This diverges slightly from V8, which continues
|
||||
// to install the binding on new contexts after it's "removed", but *only*
|
||||
// if the subscription is targeted by context name.
|
||||
sessionState_.subscribedBindings.erase(req.params["name"].getString());
|
||||
|
||||
folly::dynamic res = folly::dynamic::object("id", req.id)(
|
||||
"result", folly::dynamic::object());
|
||||
@@ -60,7 +98,17 @@ bool RuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
|
||||
void RuntimeAgent::notifyBindingCalled(
|
||||
const std::string& bindingName,
|
||||
const std::string& payload) {
|
||||
if (!sessionState_.subscribedBindingNames.count(bindingName)) {
|
||||
// NOTE: When dispatching @cdp Runtime.bindingCalled notifications, we don't
|
||||
// re-check whether the session is expecting notifications from the current
|
||||
// context - only that it's subscribed to that binding name.
|
||||
// Theoretically, this can result in over-sending notifications from contexts
|
||||
// that the client no longer cares about, or never cared about to begin with
|
||||
// (e.g. if the binding handler was installed by a previous session).
|
||||
//
|
||||
// React Native intentionally replicates this behavior for the sake of
|
||||
// bug-for-bug compatibility with Chrome, but clients should probably not rely
|
||||
// on it.
|
||||
if (!sessionState_.subscribedBindings.count(bindingName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ExecutionContext.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
@@ -20,13 +23,16 @@ struct SessionState {
|
||||
bool isRuntimeDomainEnabled{false};
|
||||
|
||||
/**
|
||||
* The set of bindings registered during this session using @cdp
|
||||
* Runtime.addBinding. Even though bindings get added to the global scope as
|
||||
* A map from binding names (registered during this session using @cdp
|
||||
* Runtime.addBinding) to execution context selectors.
|
||||
*
|
||||
* Even though bindings get added to the global scope as
|
||||
* functions that can outlive a session, they are treated as session state,
|
||||
* matching Chrome's behaviour (a binding not added by the current session
|
||||
* will not emit events on it).
|
||||
*/
|
||||
std::unordered_set<std::string> subscribedBindingNames;
|
||||
std::unordered_map<std::string, ExecutionContextSelectorSet>
|
||||
subscribedBindings;
|
||||
|
||||
// Here, we will eventually allow RuntimeAgents to store their own arbitrary
|
||||
// state (e.g. some sort of K/V storage of folly::dynamic?)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
/**
|
||||
* A template for easily creating empty types that are distinct from one
|
||||
* another. Useful if you need to generate marker types for use in an
|
||||
* std::variant, and a single std::monostate won't do.
|
||||
*/
|
||||
template <size_t key>
|
||||
struct UniqueMonostate {
|
||||
constexpr bool operator==(const UniqueMonostate<key>&) const noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool operator!=(const UniqueMonostate<key>&) const noexcept {
|
||||
return false;
|
||||
}
|
||||
constexpr bool operator<(const UniqueMonostate<key>&) const noexcept {
|
||||
return false;
|
||||
}
|
||||
constexpr bool operator>(const UniqueMonostate<key>&) const noexcept {
|
||||
return false;
|
||||
}
|
||||
constexpr bool operator<=(const UniqueMonostate<key>&) const noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool operator>=(const UniqueMonostate<key>&) const noexcept {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
|
||||
namespace std {
|
||||
|
||||
template <size_t key>
|
||||
struct hash<::facebook::react::jsinspector_modern::UniqueMonostate<key>> {
|
||||
size_t operator()(
|
||||
const ::facebook::react::jsinspector_modern::UniqueMonostate<key>&)
|
||||
const noexcept {
|
||||
return key;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
Reference in new issue
Block a user