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 <[email protected]>
1 parent 95ab006 commit 439b35a

File tree

2 files changed

+68
-156
lines changed

2 files changed

+68
-156
lines changed

src/inspector_agent.cc

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

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

159-
class NodeInspectorClient;
160-
161-
class AgentImpl {
162-
public:
163-
explicit AgentImpl(node::Environment* env);
164-
165-
bool Start(v8::Platform* platform, const char* path,
166-
const DebugOptions& options);
167-
void Stop();
168-
bool IsStarted();
169-
bool IsConnected();
170-
void WaitForDisconnect();
171-
172-
void FatalException(Local<Value> error,
173-
Local<v8::Message> message);
174-
175-
void SchedulePauseOnNextStatement(const std::string& reason);
176-
177-
void Connect(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-
enum class State { kNew, kAccepting, kConnected, kDone, kError };
188-
189-
static void ThreadCbIO(void* agent);
190-
static void WriteCbIO(uv_async_t* async);
191-
static void CallAndPauseOnStart(const v8::FunctionCallbackInfo<v8::Value>&);
192-
193-
void WorkerRunIO();
194-
void SetConnected(bool connected);
195-
void WaitForFrontendMessage();
196-
void NotifyMessageReceived();
197-
bool StartIoThread();
198-
static void InspectorWrapConsoleCall(
199-
const v8::FunctionCallbackInfo<Value>& args);
200-
static void InspectorConsoleCall(
201-
const v8::FunctionCallbackInfo<Value>& info);
202-
static void StartInspectorIoThreadAsyncCallback(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
class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
215165
public:
216166
explicit ChannelImpl(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-
void AgentImpl::InspectorConsoleCall(
377-
const v8::FunctionCallbackInfo<Value>& info) {
331+
void Agent::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-
void AgentImpl::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+
void Agent::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-
void AgentImpl::StartInspectorIoThreadAsyncCallback(uv_async_t* handle) {
442-
reinterpret_cast<AgentImpl*>(handle->data)->StartIoThread();
443-
}
444-
445-
bool AgentImpl::Start(v8::Platform* platform, const char* path,
446-
const DebugOptions& options) {
391+
bool Agent::Start(v8::Platform* platform, const char* 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-
bool AgentImpl::StartIoThread() {
428+
bool Agent::StartIoThread() {
483429
if (io_ != nullptr)
484430
return true;
485431

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

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

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

530-
bool AgentImpl::IsConnected() {
476+
bool Agent::IsConnected() {
531477
return io_ && io_->IsConnected();
532478
}
533479

534-
bool AgentImpl::IsStarted() {
480+
bool Agent::IsStarted() {
535481
return !!inspector_;
536482
}
537483

538484
// static
539-
void AgentImpl::CallAndPauseOnStart(
540-
const v8::FunctionCallbackInfo<v8::Value>& args) {
485+
void Agent::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-
void AgentImpl::WaitForDisconnect() {
504+
void Agent::WaitForDisconnect() {
558505
if (io_ != nullptr) {
559506
io_->WaitForDisconnect();
560507
}
561508
}
562509

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

571-
void AgentImpl::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-
bool Agent::Start(v8::Platform* platform, const char* path,
583-
const DebugOptions& options) {
584-
return impl->Start(platform, path, options);
585-
}
586-
587-
void Agent::Stop() {
588-
impl->Stop();
589-
}
590-
591-
bool Agent::IsStarted() {
592-
return impl->IsStarted();
593-
}
594-
595-
bool Agent::IsConnected() {
596-
return impl->IsConnected();
597-
}
598-
599-
void Agent::WaitForDisconnect() {
600-
impl->WaitForDisconnect();
601-
}
602-
603-
void Agent::FatalException(Local<Value> error,
604-
Local<v8::Message> message) {
605-
impl->FatalException(error, message);
606-
}
607-
608-
void Agent::SchedulePauseOnNextStatement(const std::string& reason) {
609-
impl->SchedulePauseOnNextStatement(reason);
610-
}
611-
612-
void Agent::Connect(InspectorSessionDelegate* delegate) {
613-
impl->Connect(delegate);
614-
}
615-
616517
void Agent::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
void Agent::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
void Agent::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

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef SRC_INSPECTOR_AGENT_H_
22
#define SRC_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
namespace v8 {
18-
class Platform;
20+
template <typename V>
21+
class FunctionCallbackInfo;
1922
template<typename T>
2023
class Local;
21-
class Value;
2224
class Message;
25+
class Platform;
26+
class Value;
2327
} // namespace v8
2428

2529
namespace v8_inspector {
@@ -29,37 +33,49 @@ class StringView;
2933
namespace node {
3034
namespace inspector {
3135

32-
class AgentImpl;
33-
3436
class InspectorSessionDelegate {
3537
public:
3638
virtual bool WaitForFrontendMessage() = 0;
3739
virtual void OnMessage(const v8_inspector::StringView& message) = 0;
3840
};
3941

42+
class InspectorIo;
43+
class NodeInspectorClient;
44+
4045
class Agent {
4146
public:
4247
explicit Agent(node::Environment* env);
4348
~Agent();
4449

4550
bool Start(v8::Platform* platform, const char* path,
4651
const DebugOptions& options);
52+
bool StartIoThread();
4753
void Stop();
4854

4955
bool IsStarted();
5056
bool IsConnected();
5157
void WaitForDisconnect();
5258
void FatalException(v8::Local<v8::Value> error,
5359
v8::Local<v8::Message> message);
54-
void SchedulePauseOnNextStatement(const std::string& reason);
5560
void Connect(InspectorSessionDelegate* delegate);
5661
void Disconnect();
5762
void Dispatch(const v8_inspector::StringView& message);
58-
InspectorSessionDelegate* delegate();
5963
void RunMessageLoop();
64+
6065
private:
61-
AgentImpl* impl;
62-
friend class AgentImpl;
66+
static void CallAndPauseOnStart(const v8::FunctionCallbackInfo<v8::Value>&);
67+
static void InspectorConsoleCall(
68+
const v8::FunctionCallbackInfo<v8::Value>& info);
69+
static void InspectorWrapConsoleCall(
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)