Skip to content

Commit 842efe0

Browse files
committed
src: allow preventing InitializeInspector in env
1 parent c380ee6 commit 842efe0

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/api/environment.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,14 @@ Environment* CreateEnvironment(
341341
Environment* env = new Environment(
342342
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
343343
#if HAVE_INSPECTOR
344-
if (inspector_parent_handle) {
345-
env->InitializeInspector(
346-
std::move(static_cast<InspectorParentHandleImpl*>(
347-
inspector_parent_handle.get())->impl));
348-
} else {
349-
env->InitializeInspector({});
344+
if (env->should_create_inspector()) {
345+
if (inspector_parent_handle) {
346+
env->InitializeInspector(
347+
std::move(static_cast<InspectorParentHandleImpl*>(
348+
inspector_parent_handle.get())->impl));
349+
} else {
350+
env->InitializeInspector({});
351+
}
350352
}
351353
#endif
352354

src/env-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,10 @@ inline bool Environment::owns_inspector() const {
808808
return flags_ & EnvironmentFlags::kOwnsInspector;
809809
}
810810

811+
inline bool Environment::should_create_inspector() const {
812+
return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
813+
}
814+
811815
inline bool Environment::tracks_unmanaged_fds() const {
812816
return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
813817
}

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ class Environment : public MemoryRetainer {
11661166

11671167
inline bool is_main_thread() const;
11681168
inline bool should_not_register_esm_loader() const;
1169+
inline bool should_create_inspector() const;
11691170
inline bool owns_process_state() const;
11701171
inline bool owns_inspector() const;
11711172
inline bool tracks_unmanaged_fds() const;

src/inspector_agent.cc

+47
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ bool IsFilePath(const std::string& path) {
370370
}
371371
#endif // __POSIX__
372372

373+
void ThrowUninitializedInspectorError(Environment* env) {
374+
HandleScope scope(env->isolate());
375+
376+
const char* msg = "This Environment was initialized without a V8::Inspector";
377+
Local<Value> exception =
378+
v8::String::NewFromUtf8(env->isolate(), msg).ToLocalChecked();
379+
380+
env->isolate()->ThrowException(exception);
381+
}
382+
373383
} // namespace
374384

375385
class NodeInspectorClient : public V8InspectorClient {
@@ -730,6 +740,11 @@ bool Agent::StartIoThread() {
730740
if (io_ != nullptr)
731741
return true;
732742

743+
if (!parent_env_->should_create_inspector() && !client_) {
744+
ThrowUninitializedInspectorError(parent_env_);
745+
return false;
746+
}
747+
733748
CHECK_NOT_NULL(client_);
734749

735750
io_ = InspectorIo::Start(client_->getThreadHandle(),
@@ -750,7 +765,13 @@ void Agent::Stop() {
750765
std::unique_ptr<InspectorSession> Agent::Connect(
751766
std::unique_ptr<InspectorSessionDelegate> delegate,
752767
bool prevent_shutdown) {
768+
if (!parent_env_->should_create_inspector() && !client_) {
769+
ThrowUninitializedInspectorError(parent_env_);
770+
return std::unique_ptr<InspectorSession>{};
771+
}
772+
753773
CHECK_NOT_NULL(client_);
774+
754775
int session_id = client_->connectFrontend(std::move(delegate),
755776
prevent_shutdown);
756777
return std::unique_ptr<InspectorSession>(
@@ -760,6 +781,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
760781
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
761782
std::unique_ptr<InspectorSessionDelegate> delegate,
762783
bool prevent_shutdown) {
784+
if (!parent_env_->should_create_inspector() && !client_) {
785+
ThrowUninitializedInspectorError(parent_env_);
786+
return std::unique_ptr<InspectorSession>{};
787+
}
788+
763789
CHECK_NOT_NULL(parent_handle_);
764790
CHECK_NOT_NULL(client_);
765791
auto thread_safe_delegate =
@@ -769,6 +795,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
769795
}
770796

771797
void Agent::WaitForDisconnect() {
798+
if (!parent_env_->should_create_inspector() && !client_) {
799+
ThrowUninitializedInspectorError(parent_env_);
800+
return;
801+
}
802+
772803
CHECK_NOT_NULL(client_);
773804
bool is_worker = parent_handle_ != nullptr;
774805
parent_handle_.reset();
@@ -918,6 +949,12 @@ void Agent::SetParentHandle(
918949

919950
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
920951
int thread_id, const std::string& url) {
952+
if (!parent_env_->should_create_inspector() && !client_) {
953+
ThrowUninitializedInspectorError(parent_env_);
954+
return std::unique_ptr<ParentInspectorHandle>{};
955+
}
956+
957+
CHECK_NOT_NULL(client_);
921958
if (!parent_handle_) {
922959
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
923960
} else {
@@ -926,11 +963,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
926963
}
927964

928965
void Agent::WaitForConnect() {
966+
if (!parent_env_->should_create_inspector() && !client_) {
967+
ThrowUninitializedInspectorError(parent_env_);
968+
return;
969+
}
970+
929971
CHECK_NOT_NULL(client_);
930972
client_->waitForFrontend();
931973
}
932974

933975
std::shared_ptr<WorkerManager> Agent::GetWorkerManager() {
976+
if (!parent_env_->should_create_inspector() && !client_) {
977+
ThrowUninitializedInspectorError(parent_env_);
978+
return std::unique_ptr<WorkerManager>{};
979+
}
980+
934981
CHECK_NOT_NULL(client_);
935982
return client_->getWorkerManager();
936983
}

src/node.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,12 @@ enum Flags : uint64_t {
402402
kNoRegisterESMLoader = 1 << 3,
403403
// Set this flag to make Node.js track "raw" file descriptors, i.e. managed
404404
// by fs.open() and fs.close(), and close them during FreeEnvironment().
405-
kTrackUnmanagedFds = 1 << 4
405+
kTrackUnmanagedFds = 1 << 4,
406+
// Controls whether or not the Environment should call V8Inspector::create().
407+
// This control is needed by embedders who may not want to initialize the V8
408+
// inspector in situations where one has already been created,
409+
// e.g. Blink's in Chromium.
410+
kNoCreateInspector = 1 << 5
406411
};
407412
} // namespace EnvironmentFlags
408413

0 commit comments

Comments
 (0)