SystemMonitor: Convert most widgets to a failable factory

I didn't convert widgets that don't do any failable tasks currently
or are lazy-initialized.
This commit is contained in:
Karol Kosek 2023-05-21 16:47:34 +02:00 committed by Andreas Kling
parent a5936864d9
commit 412630637a
Notes: sideshowbarker 2024-07-17 07:06:47 +09:00
10 changed files with 96 additions and 71 deletions

View file

@ -15,34 +15,37 @@ REGISTER_WIDGET(SystemMonitor, ProcessFileDescriptorMapWidget)
namespace SystemMonitor {
ProcessFileDescriptorMapWidget::ProcessFileDescriptorMapWidget()
ErrorOr<NonnullRefPtr<ProcessFileDescriptorMapWidget>> ProcessFileDescriptorMapWidget::try_create()
{
set_layout<GUI::VerticalBoxLayout>(4);
m_table_view = add<GUI::TableView>();
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessFileDescriptorMapWidget()));
TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
widget->m_table_view = TRY(widget->try_add<GUI::TableView>());
Vector<GUI::JsonArrayModel::FieldSpec> pid_fds_fields;
pid_fds_fields.empend("fd", "FD"_short_string, Gfx::TextAlignment::CenterRight);
pid_fds_fields.empend("class", "Class"_short_string, Gfx::TextAlignment::CenterLeft);
pid_fds_fields.empend("offset", "Offset"_short_string, Gfx::TextAlignment::CenterRight);
pid_fds_fields.empend("absolute_path", "Path"_short_string, Gfx::TextAlignment::CenterLeft);
pid_fds_fields.empend("Access"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
TRY(pid_fds_fields.try_empend("fd", "FD"_short_string, Gfx::TextAlignment::CenterRight));
TRY(pid_fds_fields.try_empend("class", "Class"_short_string, Gfx::TextAlignment::CenterLeft));
TRY(pid_fds_fields.try_empend("offset", "Offset"_short_string, Gfx::TextAlignment::CenterRight));
TRY(pid_fds_fields.try_empend("absolute_path", "Path"_short_string, Gfx::TextAlignment::CenterLeft));
TRY(pid_fds_fields.try_empend("Access"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
return object.get_bool("seekable"sv).value_or(false) ? "Seekable" : "Sequential";
});
pid_fds_fields.empend("Blocking"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_fds_fields.try_empend(TRY("Blocking"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
return object.get_bool("blocking"sv).value_or(false) ? "Blocking" : "Nonblocking";
});
pid_fds_fields.empend("On exec"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_fds_fields.try_empend("On exec"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
return object.get_bool("cloexec"sv).value_or(false) ? "Close" : "Keep";
});
pid_fds_fields.empend("Can read"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_fds_fields.try_empend(TRY("Can read"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
return object.get_bool("can_read"sv).value_or(false) ? "Yes" : "No";
});
pid_fds_fields.empend("Can write"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_fds_fields.try_empend(TRY("Can write"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
return object.get_bool("can_write"sv).value_or(false) ? "Yes" : "No";
});
}));
m_model = GUI::JsonArrayModel::create({}, move(pid_fds_fields));
m_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_model)));
widget->m_model = GUI::JsonArrayModel::create({}, move(pid_fds_fields));
widget->m_table_view->set_model(TRY(GUI::SortingProxyModel::create(*widget->m_model)));
return widget;
}
void ProcessFileDescriptorMapWidget::set_pid(pid_t pid)

View file

@ -12,15 +12,17 @@
namespace SystemMonitor {
class ProcessFileDescriptorMapWidget final : public GUI::Widget {
C_OBJECT(ProcessFileDescriptorMapWidget);
C_OBJECT_ABSTRACT(ProcessFileDescriptorMapWidget)
public:
virtual ~ProcessFileDescriptorMapWidget() override = default;
static ErrorOr<NonnullRefPtr<ProcessFileDescriptorMapWidget>> try_create();
void set_pid(pid_t);
private:
ProcessFileDescriptorMapWidget();
ProcessFileDescriptorMapWidget() = default;
RefPtr<GUI::TableView> m_table_view;
RefPtr<GUI::JsonArrayModel> m_model;

View file

@ -49,19 +49,21 @@ public:
}
};
ProcessMemoryMapWidget::ProcessMemoryMapWidget()
ErrorOr<NonnullRefPtr<ProcessMemoryMapWidget>> ProcessMemoryMapWidget::try_create()
{
set_layout<GUI::VerticalBoxLayout>(4);
m_table_view = add<GUI::TableView>();
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessMemoryMapWidget()));
TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
widget->m_table_view = TRY(widget->try_add<GUI::TableView>());
Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields;
pid_vm_fields.empend(
TRY(pid_vm_fields.try_empend(
"Address"_short_string, Gfx::TextAlignment::CenterLeft,
[](auto& object) { return DeprecatedString::formatted("{:p}", object.get_u64("address"sv).value_or(0)); },
[](auto& object) { return object.get_u64("address"sv).value_or(0); });
pid_vm_fields.empend("size", "Size"_short_string, Gfx::TextAlignment::CenterRight);
pid_vm_fields.empend("amount_resident", "Resident"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterRight);
pid_vm_fields.empend("amount_dirty", "Dirty"_short_string, Gfx::TextAlignment::CenterRight);
pid_vm_fields.empend("Access"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
[](auto& object) { return object.get_u64("address"sv).value_or(0); }));
TRY(pid_vm_fields.try_empend("size", "Size"_short_string, Gfx::TextAlignment::CenterRight));
TRY(pid_vm_fields.try_empend("amount_resident", TRY("Resident"_string), Gfx::TextAlignment::CenterRight));
TRY(pid_vm_fields.try_empend("amount_dirty", "Dirty"_short_string, Gfx::TextAlignment::CenterRight));
TRY(pid_vm_fields.try_empend("Access"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
StringBuilder builder;
if (object.get_bool("readable"sv).value_or(false))
builder.append('R');
@ -76,20 +78,20 @@ ProcessMemoryMapWidget::ProcessMemoryMapWidget()
if (object.get_bool("stack"sv).value_or(false))
builder.append('T');
return builder.to_deprecated_string();
});
pid_vm_fields.empend("VMObject type"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_vm_fields.try_empend(TRY("VMObject type"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
auto type = object.get_deprecated_string("vmobject"sv).value_or({});
if (type.ends_with("VMObject"sv))
type = type.substring(0, type.length() - 8);
return type;
});
pid_vm_fields.empend("Purgeable"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft, [](auto& object) {
}));
TRY(pid_vm_fields.try_empend(TRY("Purgeable"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
if (object.get_bool("volatile"sv).value_or(false))
return "Volatile";
return "Non-volatile";
});
pid_vm_fields.empend(
"Page map"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft,
}));
TRY(pid_vm_fields.try_empend(
TRY("Page map"_string), Gfx::TextAlignment::CenterLeft,
[](auto&) {
return GUI::Variant();
},
@ -99,17 +101,19 @@ ProcessMemoryMapWidget::ProcessMemoryMapWidget()
[](JsonObject const& object) {
auto pagemap = object.get_deprecated_string("pagemap"sv).value_or({});
return pagemap;
});
pid_vm_fields.empend("cow_pages", "# CoW"_short_string, Gfx::TextAlignment::CenterRight);
pid_vm_fields.empend("name", "Name"_short_string, Gfx::TextAlignment::CenterLeft);
m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
m_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_json_model)));
}));
TRY(pid_vm_fields.try_empend("cow_pages", "# CoW"_short_string, Gfx::TextAlignment::CenterRight));
TRY(pid_vm_fields.try_empend("name", "Name"_short_string, Gfx::TextAlignment::CenterLeft));
widget->m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
widget->m_table_view->set_model(TRY(GUI::SortingProxyModel::create(*widget->m_json_model)));
m_table_view->set_column_painting_delegate(7, make<PagemapPaintingDelegate>());
widget->m_table_view->set_column_painting_delegate(7, TRY(try_make<PagemapPaintingDelegate>()));
m_table_view->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
m_timer = add<Core::Timer>(1000, [this] { refresh(); });
m_timer->start();
widget->m_table_view->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
widget->m_timer = TRY(widget->try_add<Core::Timer>(1000, [widget] { widget->refresh(); }));
widget->m_timer->start();
return widget;
}
void ProcessMemoryMapWidget::set_pid(pid_t pid)

View file

@ -12,16 +12,19 @@
namespace SystemMonitor {
class ProcessMemoryMapWidget final : public GUI::Widget {
C_OBJECT(ProcessMemoryMapWidget);
C_OBJECT_ABSTRACT(ProcessMemoryMapWidget);
public:
virtual ~ProcessMemoryMapWidget() override = default;
static ErrorOr<NonnullRefPtr<ProcessMemoryMapWidget>> try_create();
void set_pid(pid_t);
void refresh();
private:
ProcessMemoryMapWidget();
ProcessMemoryMapWidget() = default;
RefPtr<GUI::TableView> m_table_view;
RefPtr<GUI::JsonArrayModel> m_json_model;
pid_t m_pid { -1 };

View file

@ -93,13 +93,15 @@ private:
pid_t m_pid { -1 };
};
ProcessStateWidget::ProcessStateWidget()
ErrorOr<NonnullRefPtr<ProcessStateWidget>> ProcessStateWidget::try_create()
{
set_layout<GUI::VerticalBoxLayout>(4);
m_table_view = add<GUI::TableView>();
m_table_view->set_model(adopt_ref(*new ProcessStateModel(ProcessModel::the(), 0)));
m_table_view->column_header().set_visible(false);
m_table_view->column_header().set_section_size(0, 90);
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessStateWidget()));
TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
widget->m_table_view = TRY(widget->try_add<GUI::TableView>());
widget->m_table_view->set_model(TRY(try_make_ref_counted<ProcessStateModel>(ProcessModel::the(), 0)));
widget->m_table_view->column_header().set_visible(false);
widget->m_table_view->column_header().set_section_size(0, 90);
return widget;
}
void ProcessStateWidget::set_pid(pid_t pid)

View file

@ -12,15 +12,18 @@
namespace SystemMonitor {
class ProcessStateWidget final : public GUI::Widget {
C_OBJECT(ProcessStateWidget);
C_OBJECT_ABSTRACT(ProcessStateWidget);
public:
virtual ~ProcessStateWidget() override = default;
static ErrorOr<NonnullRefPtr<ProcessStateWidget>> try_create();
void set_pid(pid_t);
private:
ProcessStateWidget();
ProcessStateWidget() = default;
RefPtr<GUI::TableView> m_table_view;
};

View file

@ -16,17 +16,19 @@ REGISTER_WIDGET(SystemMonitor, ProcessUnveiledPathsWidget)
namespace SystemMonitor {
ProcessUnveiledPathsWidget::ProcessUnveiledPathsWidget()
ErrorOr<NonnullRefPtr<ProcessUnveiledPathsWidget>> ProcessUnveiledPathsWidget::try_create()
{
set_layout<GUI::VerticalBoxLayout>(4);
m_table_view = add<GUI::TableView>();
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessUnveiledPathsWidget()));
TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
widget->m_table_view = TRY(widget->try_add<GUI::TableView>());
Vector<GUI::JsonArrayModel::FieldSpec> pid_unveil_fields;
pid_unveil_fields.empend("path", "Path"_short_string, Gfx::TextAlignment::CenterLeft);
pid_unveil_fields.empend("permissions", "Permissions"_string.release_value_but_fixme_should_propagate_errors(), Gfx::TextAlignment::CenterLeft);
TRY(pid_unveil_fields.try_empend("path", "Path"_short_string, Gfx::TextAlignment::CenterLeft));
TRY(pid_unveil_fields.try_empend("permissions", TRY("Permissions"_string), Gfx::TextAlignment::CenterLeft));
m_model = GUI::JsonArrayModel::create({}, move(pid_unveil_fields));
m_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_model)));
widget->m_model = GUI::JsonArrayModel::create({}, move(pid_unveil_fields));
widget->m_table_view->set_model(TRY(GUI::SortingProxyModel::create(*widget->m_model)));
return widget;
}
void ProcessUnveiledPathsWidget::set_pid(pid_t pid)

View file

@ -12,15 +12,17 @@
namespace SystemMonitor {
class ProcessUnveiledPathsWidget final : public GUI::Widget {
C_OBJECT(ProcessUnveiledPathsWidget);
C_OBJECT_ABSTRACT(ProcessUnveiledPathsWidget);
public:
virtual ~ProcessUnveiledPathsWidget() override = default;
static ErrorOr<NonnullRefPtr<ProcessUnveiledPathsWidget>> try_create();
void set_pid(pid_t);
private:
ProcessUnveiledPathsWidget();
ProcessUnveiledPathsWidget() = default;
RefPtr<GUI::TableView> m_table_view;
RefPtr<GUI::JsonArrayModel> m_model;

View file

@ -71,11 +71,13 @@ private:
Vector<Symbolication::Symbol> m_symbols;
};
ThreadStackWidget::ThreadStackWidget()
ErrorOr<NonnullRefPtr<ThreadStackWidget>> ThreadStackWidget::try_create()
{
set_layout<GUI::VerticalBoxLayout>(4);
m_stack_table = add<GUI::TableView>();
m_stack_table->set_model(adopt_ref(*new ThreadStackModel()));
auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ThreadStackWidget()));
TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
widget->m_stack_table = TRY(widget->try_add<GUI::TableView>());
widget->m_stack_table->set_model(TRY(try_make_ref_counted<ThreadStackModel>()));
return widget;
}
void ThreadStackWidget::show_event(GUI::ShowEvent&)

View file

@ -13,15 +13,17 @@
namespace SystemMonitor {
class ThreadStackWidget final : public GUI::Widget {
C_OBJECT(ThreadStackWidget)
C_OBJECT_ABSTRACT(ThreadStackWidget)
public:
virtual ~ThreadStackWidget() override = default;
static ErrorOr<NonnullRefPtr<ThreadStackWidget>> try_create();
void set_ids(pid_t pid, pid_t tid);
void refresh();
private:
ThreadStackWidget();
ThreadStackWidget() = default;
virtual void show_event(GUI::ShowEvent&) override;
virtual void hide_event(GUI::HideEvent&) override;