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

src: allow preventing InitializeInspector in env #35025

Merged
merged 1 commit into from Feb 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/api/environment.cc
Expand Up @@ -344,12 +344,14 @@ Environment* CreateEnvironment(
Environment* env = new Environment(
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
#if HAVE_INSPECTOR
if (inspector_parent_handle) {
env->InitializeInspector(
std::move(static_cast<InspectorParentHandleImpl*>(
inspector_parent_handle.get())->impl));
} else {
env->InitializeInspector({});
if (env->should_create_inspector()) {
if (inspector_parent_handle) {
env->InitializeInspector(
std::move(static_cast<InspectorParentHandleImpl*>(
inspector_parent_handle.get())->impl));
} else {
env->InitializeInspector({});
}
}
#endif

Expand Down
4 changes: 4 additions & 0 deletions src/env-inl.h
Expand Up @@ -869,6 +869,10 @@ inline bool Environment::owns_inspector() const {
return flags_ & EnvironmentFlags::kOwnsInspector;
}

inline bool Environment::should_create_inspector() const {
return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
}

inline bool Environment::tracks_unmanaged_fds() const {
return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
}
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Expand Up @@ -1210,6 +1210,7 @@ class Environment : public MemoryRetainer {
inline bool is_main_thread() const;
inline bool no_native_addons() const;
inline bool should_not_register_esm_loader() const;
inline bool should_create_inspector() const;
inline bool owns_process_state() const;
inline bool owns_inspector() const;
inline bool tracks_unmanaged_fds() const;
Expand Down
47 changes: 47 additions & 0 deletions src/inspector_agent.cc
Expand Up @@ -368,6 +368,16 @@ bool IsFilePath(const std::string& path) {
}
#endif // __POSIX__

void ThrowUninitializedInspectorError(Environment* env) {
HandleScope scope(env->isolate());

const char* msg = "This Environment was initialized without a V8::Inspector";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nicer if this error is defined in node_errors.h and documented in errors.md, though I understand that this would not normally show up when you use the Node.js official build so it's more like something that belongs to the doc of the embedder..

Local<Value> exception =
v8::String::NewFromUtf8(env->isolate(), msg).ToLocalChecked();

env->isolate()->ThrowException(exception);
}

} // namespace

class NodeInspectorClient : public V8InspectorClient {
Expand Down Expand Up @@ -728,6 +738,11 @@ bool Agent::StartIoThread() {
if (io_ != nullptr)
return true;

if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return false;
}

CHECK_NOT_NULL(client_);

io_ = InspectorIo::Start(client_->getThreadHandle(),
Expand All @@ -748,7 +763,13 @@ void Agent::Stop() {
std::unique_ptr<InspectorSession> Agent::Connect(
std::unique_ptr<InspectorSessionDelegate> delegate,
bool prevent_shutdown) {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return std::unique_ptr<InspectorSession>{};
}

CHECK_NOT_NULL(client_);

int session_id = client_->connectFrontend(std::move(delegate),
prevent_shutdown);
return std::unique_ptr<InspectorSession>(
Expand All @@ -758,6 +779,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
std::unique_ptr<InspectorSessionDelegate> delegate,
bool prevent_shutdown) {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return std::unique_ptr<InspectorSession>{};
}

CHECK_NOT_NULL(parent_handle_);
CHECK_NOT_NULL(client_);
auto thread_safe_delegate =
Expand All @@ -767,6 +793,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
}

void Agent::WaitForDisconnect() {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return;
}

CHECK_NOT_NULL(client_);
bool is_worker = parent_handle_ != nullptr;
parent_handle_.reset();
Expand Down Expand Up @@ -912,6 +943,12 @@ void Agent::SetParentHandle(

std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
uint64_t thread_id, const std::string& url) {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return std::unique_ptr<ParentInspectorHandle>{};
}

CHECK_NOT_NULL(client_);
if (!parent_handle_) {
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
} else {
Expand All @@ -920,11 +957,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
}

void Agent::WaitForConnect() {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return;
}

CHECK_NOT_NULL(client_);
client_->waitForFrontend();
}

std::shared_ptr<WorkerManager> Agent::GetWorkerManager() {
if (!parent_env_->should_create_inspector() && !client_) {
ThrowUninitializedInspectorError(parent_env_);
return std::unique_ptr<WorkerManager>{};
}

CHECK_NOT_NULL(client_);
return client_->getWorkerManager();
}
Expand Down
5 changes: 5 additions & 0 deletions src/node.h
Expand Up @@ -442,6 +442,11 @@ enum Flags : uint64_t {
kNoGlobalSearchPaths = 1 << 7,
// Do not export browser globals like setTimeout, console, etc.
kNoBrowserGlobals = 1 << 8,
// Controls whether or not the Environment should call V8Inspector::create().
// This control is needed by embedders who may not want to initialize the V8
// inspector in situations where one has already been created,
// e.g. Blink's in Chromium.
kNoCreateInspector = 1 << 9
};
} // namespace EnvironmentFlags

Expand Down