Skip to content

Commit 24808b6

Browse files
committed
feat: enhance notification validation and cleanup
1. Added isValid() method to BubbleItem to check entity validity 2. Implemented clearInvalidBubbles() in BubbleModel to remove invalid notifications 3. Added entity validation before creating new bubbles in BubblePanel 4. Enhanced NotifyEntity validation to include timestamp check 5. Added validation in NotificationManager timeout processing 6. Added debug logging for invalid notification cases These changes improve notification system reliability by: - Preventing invalid notifications from being displayed - Cleaning up stale notifications automatically - Adding better error handling and logging - Preventing race conditions in timeout processing - Ensuring only valid entities are processed feat: 增强通知验证和清理功能 1. 在 BubbleItem 中添加 isValid() 方法检查实体有效性 2. 在 BubbleModel 中实现 clearInvalidBubbles() 清理无效通知 3. 在 BubblePanel 创建新气泡前添加实体验证 4. 增强 NotifyEntity 验证包含时间戳检查 5. 在 NotificationManager 超时处理中添加验证 6. 为无效通知情况添加调试日志 这些改进通过以下方式提升通知系统可靠性: - 防止显示无效通知 - 自动清理陈旧通知 - 添加更好的错误处理和日志记录 - 防止超时处理中的竞态条件 - 确保只处理有效实体 Issue: https://bbs.deepin.org/post/289141
1 parent 49420ce commit 24808b6

File tree

8 files changed

+45
-1
lines changed

8 files changed

+45
-1
lines changed

panels/notification/bubble/bubbleitem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ void BubbleItem::setEnablePreview(bool enable)
317317
m_enablePreview = enable;
318318
}
319319

320+
bool BubbleItem::isValid() const
321+
{
322+
return m_entity.isValid();
323+
}
324+
320325
QString BubbleItem::displayText() const
321326
{
322327
return m_enablePreview ? m_entity.body() : tr("1 new message");

panels/notification/bubble/bubbleitem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class BubbleItem : public QObject
4848
bool enablePreview() const;
4949
void setEnablePreview(bool enable);
5050

51+
bool isValid() const;
52+
5153
signals:
5254
void levelChanged();
5355
void timeTipChanged();

panels/notification/bubble/bubblemodel.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ void BubbleModel::setDelayRemovedBubble(qint64 newDelayRemovedBubble)
284284
emit delayRemovedBubbleChanged();
285285
}
286286

287+
void BubbleModel::clearInvalidBubbles()
288+
{
289+
for (int i = m_bubbles.count() - 1; i >= 0; i--) {
290+
auto bubble = m_bubbles.at(i);
291+
if (!bubble->isValid()) {
292+
remove(bubble);
293+
}
294+
}
295+
}
296+
287297
int BubbleModel::replaceBubbleIndex(const BubbleItem *bubble) const
288298
{
289299
if (bubble->isReplace()) {

panels/notification/bubble/bubblemodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class BubbleModel : public QAbstractListModel
6464
qint64 delayRemovedBubble() const;
6565
void setDelayRemovedBubble(qint64 newDelayRemovedBubble);
6666

67+
void clearInvalidBubbles();
68+
6769
signals:
6870
void delayRemovedBubbleChanged();
6971

panels/notification/bubble/bubblepanel.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ void BubblePanel::onBubbleCountChanged()
116116
void BubblePanel::addBubble(qint64 id)
117117
{
118118
const auto entity = m_accessor->fetchEntity(id);
119+
120+
// Validate entity before creating bubble to prevent invalid notification banners
121+
if (!entity.isValid()) {
122+
qWarning(notifyLog) << "Failed to add bubble: invalid entity for id" << id << "appName:" << entity.appName() << "cTime:" << entity.cTime();
123+
return;
124+
}
125+
119126
auto bubble = new BubbleItem(entity);
120127
const auto enabled = enablePreview(entity.appId());
121128
bubble->setEnablePreview(enabled);
@@ -135,11 +142,15 @@ void BubblePanel::closeBubble(qint64 id)
135142
if (entity.isValid()) {
136143
id = entity.bubbleId();
137144
} else {
145+
qDebug(notifyLog) << "Entity not found or invalid for close bubble, using storage id lookup:" << id;
138146
id = m_bubbles->getBubbleIdByStorageId(id);
139147
}
140148

141149
if (id > 0) {
142150
m_bubbles->removeById(id);
151+
} else {
152+
qWarning(notifyLog) << "Failed to close bubble: invalid bubble id for entity id:" << id << "appName:" << entity.appName() << "cTime:" << entity.cTime();
153+
clearInvalidBubbles();
143154
}
144155
}
145156

@@ -172,6 +183,11 @@ bool BubblePanel::enablePreview(const QString &appId) const
172183
return value.toBool();
173184
}
174185

186+
void BubblePanel::clearInvalidBubbles()
187+
{
188+
m_bubbles->clearInvalidBubbles();
189+
}
190+
175191
BubbleItem *BubblePanel::bubbleItem(int index)
176192
{
177193
if (index < 0 || index >= m_bubbles->items().count())

panels/notification/bubble/bubblepanel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private Q_SLOTS:
5757
void onBubbleClosed(qint64 id, uint bubbleId, uint reason);
5858
void setVisible(const bool visible);
5959
bool enablePreview(const QString &appId) const;
60+
void clearInvalidBubbles();
6061

6162
BubbleItem *bubbleItem(int index);
6263

panels/notification/common/notifyentity.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ NotifyEntity::NotifyEntity(qint64 id, const QString &appName)
5353
{
5454
d->id = id;
5555
d->appName = appName;
56+
d->cTime = QDateTime::currentMSecsSinceEpoch();
5657
}
5758

5859
NotifyEntity::NotifyEntity(const QString &appName, uint replacesId, const QString &appIcon, const QString &summary,
@@ -117,7 +118,7 @@ bool NotifyEntity::operator!=(const NotifyEntity &other) const
117118

118119
bool NotifyEntity::isValid() const
119120
{
120-
return d && d->id > 0;
121+
return d && d->id > 0 && d->cTime > 0;
121122
}
122123

123124
qint64 NotifyEntity::id() const

panels/notification/server/notificationmanager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ void NotificationManager::onHandingPendingEntities()
643643
}
644644

645645
for (const auto &item : timeoutEntities) {
646+
// Validate entity before processing timeout to prevent race conditions
647+
if (!item.isValid()) {
648+
qWarning(notifyLog) << "Skipping timeout processing for invalid entity id:" << item.id() << "appName:" << item.appName()
649+
<< "cTime:" << item.cTime();
650+
continue;
651+
}
652+
646653
qDebug(notifyLog) << "Expired for the notification " << item.id() << item.appName();
647654
notificationClosed(item.id(), item.bubbleId(), NotifyEntity::Expired);
648655
}

0 commit comments

Comments
 (0)