Skip to content

Commit 86248f1

Browse files
codebyteresxa
authored andcommitted
src: allow preventing InitializeInspector in env
PR-URL: #35025 Reviewed-By: David Carlier <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 96510b3 commit 86248f1

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
@@ -344,12 +344,14 @@ Environment* CreateEnvironment(
344344
Environment* env = new Environment(
345345
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
346346
#if HAVE_INSPECTOR
347-
if (inspector_parent_handle) {
348-
env->InitializeInspector(
349-
std::move(static_cast<InspectorParentHandleImpl*>(
350-
inspector_parent_handle.get())->impl));
351-
} else {
352-
env->InitializeInspector({});
347+
if (env->should_create_inspector()) {
348+
if (inspector_parent_handle) {
349+
env->InitializeInspector(
350+
std::move(static_cast<InspectorParentHandleImpl*>(
351+
inspector_parent_handle.get())->impl));
352+
} else {
353+
env->InitializeInspector({});
354+
}
353355
}
354356
#endif
355357

src/env-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,10 @@ inline bool Environment::owns_inspector() const {
869869
return flags_ & EnvironmentFlags::kOwnsInspector;
870870
}
871871

872+
inline bool Environment::should_create_inspector() const {
873+
return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
874+
}
875+
872876
inline bool Environment::tracks_unmanaged_fds() const {
873877
return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
874878
}

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ class Environment : public MemoryRetainer {
12101210
inline bool is_main_thread() const;
12111211
inline bool no_native_addons() const;
12121212
inline bool should_not_register_esm_loader() const;
1213+
inline bool should_create_inspector() const;
12131214
inline bool owns_process_state() const;
12141215
inline bool owns_inspector() const;
12151216
inline bool tracks_unmanaged_fds() const;

src/inspector_agent.cc

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

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

373383
class NodeInspectorClient : public V8InspectorClient {
@@ -728,6 +738,11 @@ bool Agent::StartIoThread() {
728738
if (io_ != nullptr)
729739
return true;
730740

741+
if (!parent_env_->should_create_inspector() && !client_) {
742+
ThrowUninitializedInspectorError(parent_env_);
743+
return false;
744+
}
745+
731746
CHECK_NOT_NULL(client_);
732747

733748
io_ = InspectorIo::Start(client_->getThreadHandle(),
@@ -748,7 +763,13 @@ void Agent::Stop() {
748763
std::unique_ptr<InspectorSession> Agent::Connect(
749764
std::unique_ptr<InspectorSessionDelegate> delegate,
750765
bool prevent_shutdown) {
766+
if (!parent_env_->should_create_inspector() && !client_) {
767+
ThrowUninitializedInspectorError(parent_env_);
768+
return std::unique_ptr<InspectorSession>{};
769+
}
770+
751771
CHECK_NOT_NULL(client_);
772+
752773
int session_id = client_->connectFrontend(std::move(delegate),
753774
prevent_shutdown);
754775
return std::unique_ptr<InspectorSession>(
@@ -758,6 +779,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
758779
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
759780
std::unique_ptr<InspectorSessionDelegate> delegate,
760781
bool prevent_shutdown) {
782+
if (!parent_env_->should_create_inspector() && !client_) {
783+
ThrowUninitializedInspectorError(parent_env_);
784+
return std::unique_ptr<InspectorSession>{};
785+
}
786+
761787
CHECK_NOT_NULL(parent_handle_);
762788
CHECK_NOT_NULL(client_);
763789
auto thread_safe_delegate =
@@ -767,6 +793,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
767793
}
768794

769795
void Agent::WaitForDisconnect() {
796+
if (!parent_env_->should_create_inspector() && !client_) {
797+
ThrowUninitializedInspectorError(parent_env_);
798+
return;
799+
}
800+
770801
CHECK_NOT_NULL(client_);
771802
bool is_worker = parent_handle_ != nullptr;
772803
parent_handle_.reset();
@@ -912,6 +943,12 @@ void Agent::SetParentHandle(
912943

913944
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
914945
uint64_t thread_id, const std::string& url) {
946+
if (!parent_env_->should_create_inspector() && !client_) {
947+
ThrowUninitializedInspectorError(parent_env_);
948+
return std::unique_ptr<ParentInspectorHandle>{};
949+
}
950+
951+
CHECK_NOT_NULL(client_);
915952
if (!parent_handle_) {
916953
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
917954
} else {
@@ -920,11 +957,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
920957
}
921958

922959
void Agent::WaitForConnect() {
960+
if (!parent_env_->should_create_inspector() && !client_) {
961+
ThrowUninitializedInspectorError(parent_env_);
962+
return;
963+
}
964+
923965
CHECK_NOT_NULL(client_);
924966
client_->waitForFrontend();
925967
}
926968

927969
std::shared_ptr<WorkerManager> Agent::GetWorkerManager() {
970+
if (!parent_env_->should_create_inspector() && !client_) {
971+
ThrowUninitializedInspectorError(parent_env_);
972+
return std::unique_ptr<WorkerManager>{};
973+
}
974+
928975
CHECK_NOT_NULL(client_);
929976
return client_->getWorkerManager();
930977
}

src/node.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,12 @@ enum Flags : uint64_t {
439439
// $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
440440
// do not expect to have their behaviors changed because of globally
441441
// installed modules.
442-
kNoGlobalSearchPaths = 1 << 7
442+
kNoGlobalSearchPaths = 1 << 7,
443+
// Controls whether or not the Environment should call V8Inspector::create().
444+
// This control is needed by embedders who may not want to initialize the V8
445+
// inspector in situations where one has already been created,
446+
// e.g. Blink's in Chromium.
447+
kNoCreateInspector = 1 << 9
443448
};
444449
} // namespace EnvironmentFlags
445450

0 commit comments

Comments
 (0)