Commit ab3f2b48 authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

Only mute web notifications

Other types of notifications shown by Chrome are typically features
explicitly triggered by the user and should still be shown even if the
user is currently recording the whole screen.

Bug: 1131375
Change-Id: I06bc335f8b556fb9105d99314ec1070a653e7810
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2437397
Commit-Queue: Richard Knoll <knollr@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814283}
parent 0f0be9bf
...@@ -60,6 +60,12 @@ class ScreenCaptureNotificationBlocker ...@@ -60,6 +60,12 @@ class ScreenCaptureNotificationBlocker
}; };
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
bool IsWebNotification(NotificationHandler::Type notification_type) {
return notification_type == NotificationHandler::Type::WEB_PERSISTENT ||
notification_type == NotificationHandler::Type::WEB_NON_PERSISTENT ||
notification_type == NotificationHandler::Type::EXTENSION;
}
} // namespace } // namespace
NotificationDisplayQueue::NotificationDisplayQueue( NotificationDisplayQueue::NotificationDisplayQueue(
...@@ -84,11 +90,10 @@ void NotificationDisplayQueue::OnBlockingStateChanged() { ...@@ -84,11 +90,10 @@ void NotificationDisplayQueue::OnBlockingStateChanged() {
MaybeDisplayQueuedNotifications(); MaybeDisplayQueuedNotifications();
} }
bool NotificationDisplayQueue::ShouldEnqueueNotifications() { bool NotificationDisplayQueue::ShouldEnqueueNotifications(
return std::any_of(blockers_.begin(), blockers_.end(), NotificationHandler::Type notification_type) const {
[](const std::unique_ptr<NotificationBlocker>& blocker) { return IsWebNotification(notification_type) &&
return blocker->ShouldBlockNotifications(); IsAnyNotificationBlockerActive();
});
} }
void NotificationDisplayQueue::EnqueueNotification( void NotificationDisplayQueue::EnqueueNotification(
...@@ -112,7 +117,8 @@ void NotificationDisplayQueue::RemoveQueuedNotification( ...@@ -112,7 +117,8 @@ void NotificationDisplayQueue::RemoveQueuedNotification(
queued_notifications_.erase(it); queued_notifications_.erase(it);
} }
std::set<std::string> NotificationDisplayQueue::GetQueuedNotificationIds() { std::set<std::string> NotificationDisplayQueue::GetQueuedNotificationIds()
const {
std::set<std::string> notification_ids; std::set<std::string> notification_ids;
for (const QueuedNotification& queued : queued_notifications_) for (const QueuedNotification& queued : queued_notifications_)
notification_ids.insert(queued.notification.id()); notification_ids.insert(queued.notification.id());
...@@ -136,7 +142,7 @@ void NotificationDisplayQueue::SetNotificationBlockers( ...@@ -136,7 +142,7 @@ void NotificationDisplayQueue::SetNotificationBlockers(
} }
void NotificationDisplayQueue::MaybeDisplayQueuedNotifications() { void NotificationDisplayQueue::MaybeDisplayQueuedNotifications() {
if (ShouldEnqueueNotifications()) if (IsAnyNotificationBlockerActive())
return; return;
std::vector<QueuedNotification> queued_notifications = std::vector<QueuedNotification> queued_notifications =
...@@ -150,6 +156,13 @@ void NotificationDisplayQueue::MaybeDisplayQueuedNotifications() { ...@@ -150,6 +156,13 @@ void NotificationDisplayQueue::MaybeDisplayQueuedNotifications() {
} }
} }
bool NotificationDisplayQueue::IsAnyNotificationBlockerActive() const {
return std::any_of(blockers_.begin(), blockers_.end(),
[](const std::unique_ptr<NotificationBlocker>& blocker) {
return blocker->ShouldBlockNotifications();
});
}
NotificationDisplayQueue::QueuedNotification::QueuedNotification( NotificationDisplayQueue::QueuedNotification::QueuedNotification(
NotificationHandler::Type notification_type, NotificationHandler::Type notification_type,
const message_center::Notification& notification, const message_center::Notification& notification,
......
...@@ -37,9 +37,11 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer { ...@@ -37,9 +37,11 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer {
// NotificationBlocker::Observer: // NotificationBlocker::Observer:
void OnBlockingStateChanged() override; void OnBlockingStateChanged() override;
// Returns if we should currently queue up notifications. This is the case if // Returns if we should currently queue up |notification_type| notifications.
// at least one NotificationBlocker is active. // This is the case if at least one NotificationBlocker is active and
bool ShouldEnqueueNotifications(); // |notification_type| is a Web Notification.
bool ShouldEnqueueNotifications(
NotificationHandler::Type notification_type) const;
// Enqueue the passed |notification| to be shown once no blocker is active // Enqueue the passed |notification| to be shown once no blocker is active
// anymore. If there is already a notification with the same id queued, it // anymore. If there is already a notification with the same id queued, it
...@@ -54,7 +56,7 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer { ...@@ -54,7 +56,7 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer {
void RemoveQueuedNotification(const std::string& notification_id); void RemoveQueuedNotification(const std::string& notification_id);
// Returns a set of the currently queued notification ids. // Returns a set of the currently queued notification ids.
std::set<std::string> GetQueuedNotificationIds(); std::set<std::string> GetQueuedNotificationIds() const;
// Sets the list of |blockers| to be used and observers their state. // Sets the list of |blockers| to be used and observers their state.
void SetNotificationBlockers(NotificationBlockers blockers); void SetNotificationBlockers(NotificationBlockers blockers);
...@@ -64,6 +66,9 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer { ...@@ -64,6 +66,9 @@ class NotificationDisplayQueue : public NotificationBlocker::Observer {
// free all queued notifications if no blocker is active anymore. // free all queued notifications if no blocker is active anymore.
void MaybeDisplayQueuedNotifications(); void MaybeDisplayQueuedNotifications();
// Checks if any notification blocker is currently active.
bool IsAnyNotificationBlockerActive() const;
// Represents a queued notification. // Represents a queued notification.
struct QueuedNotification { struct QueuedNotification {
QueuedNotification(NotificationHandler::Type notification_type, QueuedNotification(NotificationHandler::Type notification_type,
......
...@@ -113,16 +113,25 @@ class NotificationDisplayQueueTest : public testing::Test { ...@@ -113,16 +113,25 @@ class NotificationDisplayQueueTest : public testing::Test {
TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithoutBlockers) { TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithoutBlockers) {
queue().SetNotificationBlockers({}); queue().SetNotificationBlockers({});
EXPECT_FALSE(queue().ShouldEnqueueNotifications()); EXPECT_FALSE(queue().ShouldEnqueueNotifications(
NotificationHandler::Type::WEB_PERSISTENT));
} }
TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithAllowingBlocker) { TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithAllowingBlocker) {
EXPECT_FALSE(queue().ShouldEnqueueNotifications()); EXPECT_FALSE(queue().ShouldEnqueueNotifications(
NotificationHandler::Type::WEB_PERSISTENT));
} }
TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithBlockingBlocker) { TEST_F(NotificationDisplayQueueTest, ShouldEnqueueWithBlockingBlocker) {
notification_blocker().SetShouldBlockNotifications(true); notification_blocker().SetShouldBlockNotifications(true);
EXPECT_TRUE(queue().ShouldEnqueueNotifications()); EXPECT_TRUE(queue().ShouldEnqueueNotifications(
NotificationHandler::Type::WEB_PERSISTENT));
}
TEST_F(NotificationDisplayQueueTest, ShouldEnqueueForNonWebNotification) {
notification_blocker().SetShouldBlockNotifications(true);
EXPECT_FALSE(
queue().ShouldEnqueueNotifications(NotificationHandler::Type::TRANSIENT));
} }
TEST_F(NotificationDisplayQueueTest, EnqueueNotification) { TEST_F(NotificationDisplayQueueTest, EnqueueNotification) {
......
...@@ -196,7 +196,7 @@ void NotificationDisplayServiceImpl::Display( ...@@ -196,7 +196,7 @@ void NotificationDisplayServiceImpl::Display(
for (auto& observer : observers_) for (auto& observer : observers_)
observer.OnNotificationDisplayed(notification, metadata.get()); observer.OnNotificationDisplayed(notification, metadata.get());
if (notification_queue_.ShouldEnqueueNotifications()) { if (notification_queue_.ShouldEnqueueNotifications(notification_type)) {
notification_queue_.EnqueueNotification(notification_type, notification, notification_queue_.EnqueueNotification(notification_type, notification,
std::move(metadata)); std::move(metadata));
} else { } else {
......
...@@ -81,8 +81,7 @@ message_center::Notification CreateNotification(const std::string& id) { ...@@ -81,8 +81,7 @@ message_center::Notification CreateNotification(const std::string& id) {
/*message=*/base::string16(), /*icon=*/gfx::Image(), /*message=*/base::string16(), /*icon=*/gfx::Image(),
/*display_source=*/base::string16(), /*display_source=*/base::string16(),
/*origin_url=*/GURL(), message_center::NotifierId(), /*origin_url=*/GURL(), message_center::NotifierId(),
message_center::RichNotificationData(), message_center::RichNotificationData(), /*delegate=*/nullptr);
base::MakeRefCounted<message_center::NotificationDelegate>());
} }
} // namespace } // namespace
...@@ -151,12 +150,12 @@ class NotificationDisplayServiceImplTest : public testing::Test { ...@@ -151,12 +150,12 @@ class NotificationDisplayServiceImplTest : public testing::Test {
} }
void DisplayNotification(const std::string id) { void DisplayNotification(const std::string id) {
service_->Display(NotificationHandler::Type::TRANSIENT, service_->Display(NotificationHandler::Type::WEB_PERSISTENT,
CreateNotification(id), /*metadata=*/nullptr); CreateNotification(id), /*metadata=*/nullptr);
} }
void CloseNotification(const std::string id) { void CloseNotification(const std::string id) {
service_->Close(NotificationHandler::Type::TRANSIENT, id); service_->Close(NotificationHandler::Type::WEB_PERSISTENT, id);
} }
private: private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment