Skip to content

Commit 439b35a

Browse files
author
Eugene Ostroukhov
committed
inspector: remove AgentImpl
AgentImpl was introduce so inspector-agent.h does not leak libuv and inspector implementation details. Inspector had been reworked since and new classes provide this isolation and AgentImpl became unnecessary level of indirection. This change removes that class to streamline the code. PR-URL: #12576 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 95ab006 commit 439b35a

2 files changed

Lines changed: 68 additions & 156 deletions

File tree

‎src/inspector_agent.cc‎

Lines changed: 44 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
4444
returnStringBuffer::create(StringView(*buffer, buffer.length()));
4545
}
4646

47+
// Called from the main thread.
48+
voidStartInspectorIoThreadAsyncCallback(uv_async_t* handle) {
49+
static_cast<Agent*>(handle->data)->StartIoThread();
50+
}
51+
4752
#ifdef __POSIX__
4853
staticvoidEnableInspectorIOThreadSignalHandler(int signo) {
4954
uv_sem_post(&inspector_io_thread_semaphore);
@@ -156,61 +161,6 @@ static int RegisterDebugSignalHandler() {
156161
constintNANOS_PER_MSEC = 1000000;
157162
constintCONTEXT_GROUP_ID = 1;
158163

159-
classNodeInspectorClient;
160-
161-
classAgentImpl {
162-
public:
163-
explicitAgentImpl(node::Environment* env);
164-
165-
boolStart(v8::Platform* platform, constchar* path,
166-
const DebugOptions& options);
167-
voidStop();
168-
boolIsStarted();
169-
boolIsConnected();
170-
voidWaitForDisconnect();
171-
172-
voidFatalException(Local<Value> error,
173-
Local<v8::Message> message);
174-
175-
voidSchedulePauseOnNextStatement(const std::string& reason);
176-
177-
voidConnect(InspectorSessionDelegate* session);
178-
179-
NodeInspectorClient* client() {
180-
return inspector_.get();
181-
}
182-
183-
private:
184-
template <typename Action>
185-
using MessageQueue =
186-
std::vector<std::tuple<Action, int, std::unique_ptr<StringBuffer>>>;
187-
enumclassState { kNew, kAccepting, kConnected, kDone, kError };
188-
189-
staticvoidThreadCbIO(void* agent);
190-
staticvoidWriteCbIO(uv_async_t* async);
191-
staticvoidCallAndPauseOnStart(const v8::FunctionCallbackInfo<v8::Value>&);
192-
193-
voidWorkerRunIO();
194-
voidSetConnected(bool connected);
195-
voidWaitForFrontendMessage();
196-
voidNotifyMessageReceived();
197-
boolStartIoThread();
198-
staticvoidInspectorWrapConsoleCall(
199-
const v8::FunctionCallbackInfo<Value>& args);
200-
staticvoidInspectorConsoleCall(
201-
const v8::FunctionCallbackInfo<Value>& info);
202-
staticvoidStartInspectorIoThreadAsyncCallback(uv_async_t* handle);
203-
State ToState(State state);
204-
205-
node::Environment* parent_env_;
206-
std::unique_ptr<NodeInspectorClient> inspector_;
207-
std::unique_ptr<InspectorIo> io_;
208-
v8::Platform* platform_;
209-
bool inspector_console_;
210-
std::string path_;
211-
DebugOptions debug_options_;
212-
};
213-
214164
classChannelImplfinal : public v8_inspector::V8Inspector::Channel {
215165
public:
216166
explicitChannelImpl(V8Inspector* inspector,
@@ -367,14 +317,18 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
367317
std::unique_ptr<ChannelImpl> channel_;
368318
};
369319

370-
AgentImpl::AgentImpl(Environment* env) : parent_env_(env),
371-
inspector_(nullptr),
372-
platform_(nullptr),
373-
inspector_console_(false) {}
320+
Agent::Agent(Environment* env) : parent_env_(env),
321+
inspector_(nullptr),
322+
platform_(nullptr),
323+
inspector_console_(false) {}
324+
325+
// Header has unique_ptr to some incomplete types - this definition tells
326+
// the compiler to figure out destruction here, were those types are complete
327+
Agent::~Agent() {
328+
}
374329

375330
// static
376-
voidAgentImpl::InspectorConsoleCall(
377-
const v8::FunctionCallbackInfo<Value>& info) {
331+
voidAgent::InspectorConsoleCall(const v8::FunctionCallbackInfo<Value>& info) {
378332
Isolate* isolate = info.GetIsolate();
379333
Local<Context> context = isolate->GetCurrentContext();
380334

@@ -388,7 +342,7 @@ void AgentImpl::InspectorConsoleCall(
388342
}
389343

390344
Environment* env = Environment::GetCurrent(isolate);
391-
if (env->inspector_agent()->impl->inspector_console_) {
345+
if (env->inspector_agent()->inspector_console_) {
392346
Local<Value> inspector_method = args->Get(context, 0).ToLocalChecked();
393347
CHECK(inspector_method->IsFunction());
394348
Local<Value> config_value = args->Get(context, 2).ToLocalChecked();
@@ -417,33 +371,25 @@ void AgentImpl::InspectorConsoleCall(
417371
}
418372

419373
// static
420-
voidAgentImpl::InspectorWrapConsoleCall(
421-
const FunctionCallbackInfo<Value>& args) {
422-
Environment* env = Environment::GetCurrent(args);
423-
424-
if (args.Length() != 3 || !args[0]->IsFunction() ||
425-
!args[1]->IsFunction() || !args[2]->IsObject()) {
374+
voidAgent::InspectorWrapConsoleCall(const FunctionCallbackInfo<Value>& info) {
375+
Environment* env = Environment::GetCurrent(info);
376+
if (info.Length() != 3 || !info[0]->IsFunction() ||
377+
!info[1]->IsFunction() || !info[2]->IsObject()) {
426378
return env->ThrowError("inspector.wrapConsoleCall takes exactly 3 "
427379
"arguments: two functions and an object.");
428380
}
429381

430-
Local<v8::Array> array = v8::Array::New(env->isolate(), args.Length());
431-
CHECK(array->Set(env->context(), 0, args[0]).FromJust());
432-
CHECK(array->Set(env->context(), 1, args[1]).FromJust());
433-
CHECK(array->Set(env->context(), 2, args[2]).FromJust());
434-
args.GetReturnValue().Set(Function::New(env->context(),
382+
Local<v8::Array> array = v8::Array::New(env->isolate(), info.Length());
383+
CHECK(array->Set(env->context(), 0, info[0]).FromJust());
384+
CHECK(array->Set(env->context(), 1, info[1]).FromJust());
385+
CHECK(array->Set(env->context(), 2, info[2]).FromJust());
386+
info.GetReturnValue().Set(Function::New(env->context(),
435387
InspectorConsoleCall,
436388
array).ToLocalChecked());
437389
}
438390

439-
// Called from the main thread.
440-
// static
441-
voidAgentImpl::StartInspectorIoThreadAsyncCallback(uv_async_t* handle) {
442-
reinterpret_cast<AgentImpl*>(handle->data)->StartIoThread();
443-
}
444-
445-
boolAgentImpl::Start(v8::Platform* platform, constchar* path,
446-
const DebugOptions& options) {
391+
boolAgent::Start(v8::Platform* platform, constchar* path,
392+
const DebugOptions& options) {
447393
path_ = path == nullptr ? "" : path;
448394
debug_options_ = options;
449395
inspector_console_ = false;
@@ -479,7 +425,7 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
479425
}
480426
}
481427

482-
boolAgentImpl::StartIoThread() {
428+
boolAgent::StartIoThread() {
483429
if (io_ != nullptr)
484430
returntrue;
485431

@@ -517,27 +463,27 @@ bool AgentImpl::StartIoThread() {
517463
returntrue;
518464
}
519465

520-
voidAgentImpl::Stop() {
466+
voidAgent::Stop() {
521467
if (io_ != nullptr)
522468
io_->Stop();
523469
}
524470

525-
voidAgentImpl::Connect(InspectorSessionDelegate* delegate) {
471+
voidAgent::Connect(InspectorSessionDelegate* delegate) {
526472
inspector_console_ = true;
527473
inspector_->connectFrontend(delegate);
528474
}
529475

530-
boolAgentImpl::IsConnected() {
476+
boolAgent::IsConnected() {
531477
return io_ && io_->IsConnected();
532478
}
533479

534-
boolAgentImpl::IsStarted() {
480+
boolAgent::IsStarted() {
535481
return !!inspector_;
536482
}
537483

538484
// static
539-
voidAgentImpl::CallAndPauseOnStart(
540-
const v8::FunctionCallbackInfo<v8::Value>& args) {
485+
voidAgent::CallAndPauseOnStart(
486+
const v8::FunctionCallbackInfo<v8::Value>& args) {
541487
Environment* env = Environment::GetCurrent(args);
542488
CHECK_GT(args.Length(), 1);
543489
CHECK(args[0]->IsFunction());
@@ -546,91 +492,41 @@ void AgentImpl::CallAndPauseOnStart(
546492
call_args.push_back(args[i]);
547493
}
548494

549-
env->inspector_agent()->SchedulePauseOnNextStatement("Break on start");
495+
Agent* agent = env->inspector_agent();
496+
agent->inspector_->schedulePauseOnNextStatement("Break on start");
550497

551498
v8::MaybeLocal<v8::Value> retval =
552499
args[0].As<v8::Function>()->Call(env->context(), args[1],
553500
call_args.size(), call_args.data());
554501
args.GetReturnValue().Set(retval.ToLocalChecked());
555502
}
556503

557-
voidAgentImpl::WaitForDisconnect() {
504+
voidAgent::WaitForDisconnect() {
558505
if (io_ != nullptr) {
559506
io_->WaitForDisconnect();
560507
}
561508
}
562509

563-
voidAgentImpl::FatalException(Local<Value> error,
564-
Local<v8::Message> message) {
510+
voidAgent::FatalException(Local<Value> error, Local<v8::Message> message) {
565511
if (!IsStarted())
566512
return;
567513
inspector_->FatalException(error, message);
568514
WaitForDisconnect();
569515
}
570516

571-
voidAgentImpl::SchedulePauseOnNextStatement(const std::string& reason) {
572-
inspector_->schedulePauseOnNextStatement(reason);
573-
}
574-
575-
// Exported class Agent
576-
Agent::Agent(node::Environment* env) : impl(new AgentImpl(env)) {}
577-
578-
Agent::~Agent() {
579-
delete impl;
580-
}
581-
582-
boolAgent::Start(v8::Platform* platform, constchar* path,
583-
const DebugOptions& options) {
584-
return impl->Start(platform, path, options);
585-
}
586-
587-
voidAgent::Stop() {
588-
impl->Stop();
589-
}
590-
591-
boolAgent::IsStarted() {
592-
return impl->IsStarted();
593-
}
594-
595-
boolAgent::IsConnected() {
596-
return impl->IsConnected();
597-
}
598-
599-
voidAgent::WaitForDisconnect() {
600-
impl->WaitForDisconnect();
601-
}
602-
603-
voidAgent::FatalException(Local<Value> error,
604-
Local<v8::Message> message) {
605-
impl->FatalException(error, message);
606-
}
607-
608-
voidAgent::SchedulePauseOnNextStatement(const std::string& reason) {
609-
impl->SchedulePauseOnNextStatement(reason);
610-
}
611-
612-
voidAgent::Connect(InspectorSessionDelegate* delegate) {
613-
impl->Connect(delegate);
614-
}
615-
616517
voidAgent::Dispatch(const StringView& message) {
617-
CHECK_NE(impl->client(), nullptr);
618-
impl->client()->dispatchMessageFromFrontend(message);
518+
CHECK_NE(inspector_, nullptr);
519+
inspector_->dispatchMessageFromFrontend(message);
619520
}
620521

621522
voidAgent::Disconnect() {
622-
CHECK_NE(impl->client(), nullptr);
623-
impl->client()->disconnectFrontend();
624-
}
625-
626-
InspectorSessionDelegate* Agent::delegate() {
627-
CHECK_NE(impl->client(), nullptr);
628-
return impl->client()->delegate();
523+
CHECK_NE(inspector_, nullptr);
524+
inspector_->disconnectFrontend();
629525
}
630526

631527
voidAgent::RunMessageLoop() {
632-
CHECK_NE(impl->client(), nullptr);
633-
impl->client()->runMessageLoopOnPause(CONTEXT_GROUP_ID);
528+
CHECK_NE(inspector_, nullptr);
529+
inspector_->runMessageLoopOnPause(CONTEXT_GROUP_ID);
634530
}
635531

636532
} // namespace inspector

‎src/inspector_agent.h‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef SRC_INSPECTOR_AGENT_H_
22
#defineSRC_INSPECTOR_AGENT_H_
33

4+
#include<memory>
5+
46
#include<stddef.h>
57

68
#if !HAVE_INSPECTOR
@@ -15,11 +17,13 @@ class Environment;
1517
} // namespace node
1618

1719
namespacev8 {
18-
classPlatform;
20+
template <typename V>
21+
classFunctionCallbackInfo;
1922
template<typename T>
2023
classLocal;
21-
classValue;
2224
classMessage;
25+
classPlatform;
26+
classValue;
2327
} // namespace v8
2428

2529
namespacev8_inspector {
@@ -29,37 +33,49 @@ class StringView;
2933
namespacenode {
3034
namespaceinspector {
3135

32-
classAgentImpl;
33-
3436
classInspectorSessionDelegate {
3537
public:
3638
virtualboolWaitForFrontendMessage() = 0;
3739
virtualvoidOnMessage(const v8_inspector::StringView& message) = 0;
3840
};
3941

42+
classInspectorIo;
43+
classNodeInspectorClient;
44+
4045
classAgent {
4146
public:
4247
explicitAgent(node::Environment* env);
4348
~Agent();
4449

4550
boolStart(v8::Platform* platform, constchar* path,
4651
const DebugOptions& options);
52+
boolStartIoThread();
4753
voidStop();
4854

4955
boolIsStarted();
5056
boolIsConnected();
5157
voidWaitForDisconnect();
5258
voidFatalException(v8::Local<v8::Value> error,
5359
v8::Local<v8::Message> message);
54-
voidSchedulePauseOnNextStatement(const std::string& reason);
5560
voidConnect(InspectorSessionDelegate* delegate);
5661
voidDisconnect();
5762
voidDispatch(const v8_inspector::StringView& message);
58-
InspectorSessionDelegate* delegate();
5963
voidRunMessageLoop();
64+
6065
private:
61-
AgentImpl* impl;
62-
friendclassAgentImpl;
66+
staticvoidCallAndPauseOnStart(const v8::FunctionCallbackInfo<v8::Value>&);
67+
staticvoidInspectorConsoleCall(
68+
const v8::FunctionCallbackInfo<v8::Value>& info);
69+
staticvoidInspectorWrapConsoleCall(
70+
const v8::FunctionCallbackInfo<v8::Value>& info);
71+
72+
node::Environment* parent_env_;
73+
std::unique_ptr<NodeInspectorClient> inspector_;
74+
std::unique_ptr<InspectorIo> io_;
75+
v8::Platform* platform_;
76+
bool inspector_console_;
77+
std::string path_;
78+
DebugOptions debug_options_;
6379
};
6480

6581
} // namespace inspector

0 commit comments

Comments
 (0)