1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:37:35 +00:00

Notification: Use RefPtr for connection

With this RefPtr, we can initialize the connection to the
NotificationServer upon showing the notification. With this, we can
prevent double shows and updates or closes before showing.
This commit is contained in:
Nick Johnson 2021-03-12 11:53:51 -06:00 committed by Andreas Kling
parent e17752fc91
commit c7f00717c3
2 changed files with 11 additions and 47 deletions

View file

@ -54,7 +54,6 @@ private:
}; };
Notification::Notification() Notification::Notification()
: m_connection(NotificationServerConnection::construct())
{ {
} }
@ -64,73 +63,40 @@ Notification::~Notification()
void Notification::show() void Notification::show()
{ {
VERIFY(!m_showing); VERIFY(!m_connection);
VERIFY(!m_disposed);
if (!m_connection->is_connected()) {
// This would imply that the NotificationServer crashed before we could send it any data.
VERIFY_NOT_REACHED();
}
auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap(); auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
m_connection = NotificationServerConnection::construct();
m_connection->send_sync<Messages::NotificationServer::ShowNotification>(m_text, m_title, icon); m_connection->send_sync<Messages::NotificationServer::ShowNotification>(m_text, m_title, icon);
m_showing = true;
} }
void Notification::close() void Notification::close()
{ {
VERIFY(m_showing); VERIFY(m_connection);
if (m_connection->is_connected()) { if (!m_connection->is_connected()) {
m_connection->send_sync<Messages::NotificationServer::CloseNotification>(); return;
} }
m_connection->send_sync<Messages::NotificationServer::CloseNotification>();
m_showing = false;
m_disposed = true;
} }
bool Notification::update() bool Notification::update()
{ {
VERIFY(m_showing); VERIFY(m_connection);
VERIFY(!m_disposed);
if (!m_connection->is_connected()) { if (!m_connection->is_connected()) {
m_showing = false;
m_disposed = true;
return false; return false;
} }
bool is_checked = false;
if (m_text_dirty || m_title_dirty) { if (m_text_dirty || m_title_dirty) {
auto response = m_connection->send_sync<Messages::NotificationServer::UpdateNotificationText>(m_text, m_title); m_connection->send_sync<Messages::NotificationServer::UpdateNotificationText>(m_text, m_title);
m_text_dirty = false; m_text_dirty = false;
m_title_dirty = false; m_title_dirty = false;
is_checked = true;
if (!response->still_showing()) {
m_showing = false;
m_disposed = true;
return false;
}
} }
if (m_icon_dirty) { if (m_icon_dirty) {
auto response = m_connection->send_sync<Messages::NotificationServer::UpdateNotificationIcon>(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap()); m_connection->send_sync<Messages::NotificationServer::UpdateNotificationIcon>(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
m_icon_dirty = false; m_icon_dirty = false;
is_checked = true;
if (!response->still_showing()) {
m_showing = false;
m_disposed = true;
return false;
}
} }
if (!is_checked) { return true;
auto response = m_connection->send_sync<Messages::NotificationServer::IsShowing>();
m_showing = response->still_showing();
if (!m_showing) {
m_disposed = true;
}
}
return m_showing;
} }
} }

View file

@ -74,9 +74,7 @@ private:
RefPtr<Gfx::Bitmap> m_icon; RefPtr<Gfx::Bitmap> m_icon;
bool m_icon_dirty; bool m_icon_dirty;
NonnullRefPtr<NotificationServerConnection> m_connection; RefPtr<NotificationServerConnection> m_connection;
bool m_showing { false };
bool m_disposed { false };
}; };
} }