notification_list.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/message_center/notification_list.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/check.h"
  8. #include "base/containers/adapters.h"
  9. #include "base/time/time.h"
  10. #include "base/values.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "ui/gfx/image/image.h"
  13. #include "ui/message_center/message_center.h"
  14. #include "ui/message_center/notification_blocker.h"
  15. #include "ui/message_center/public/cpp/message_center_constants.h"
  16. #include "ui/message_center/public/cpp/notification.h"
  17. #include "ui/message_center/public/cpp/notification_types.h"
  18. namespace message_center {
  19. namespace {
  20. bool ShouldShowNotificationAsPopup(const Notification& notification,
  21. const NotificationBlockers& blockers,
  22. const NotificationBlocker* except) {
  23. for (auto* blocker : blockers) {
  24. if (blocker != except &&
  25. !blocker->ShouldShowNotificationAsPopup(notification))
  26. return false;
  27. }
  28. return true;
  29. }
  30. } // namespace
  31. bool ComparePriorityTimestampSerial::operator()(Notification* n1,
  32. Notification* n2) const {
  33. if (n1->priority() > n2->priority()) // Higher pri go first.
  34. return true;
  35. if (n1->priority() < n2->priority())
  36. return false;
  37. return CompareTimestampSerial()(n1, n2);
  38. }
  39. bool CompareTimestampSerial::operator()(Notification* n1,
  40. Notification* n2) const {
  41. if (n1->timestamp() > n2->timestamp()) // Newer come first.
  42. return true;
  43. if (n1->timestamp() < n2->timestamp())
  44. return false;
  45. if (n1->serial_number() > n2->serial_number()) // Newer come first.
  46. return true;
  47. if (n1->serial_number() < n2->serial_number())
  48. return false;
  49. return false;
  50. }
  51. bool NotificationList::NotificationState::operator!=(
  52. const NotificationState& other) const {
  53. return shown_as_popup != other.shown_as_popup || is_read != other.is_read;
  54. }
  55. NotificationList::NotificationList(MessageCenter* message_center)
  56. : message_center_(message_center), quiet_mode_(false) {}
  57. NotificationList::~NotificationList() = default;
  58. void NotificationList::SetNotificationsShown(
  59. const NotificationBlockers& blockers,
  60. std::set<std::string>* updated_ids) {
  61. Notifications notifications = GetVisibleNotifications(blockers);
  62. for (Notification* notification : notifications) {
  63. NotificationState* state = &GetNotification(notification->id())->second;
  64. const NotificationState original_state = *state;
  65. state->shown_as_popup = true;
  66. state->is_read = true;
  67. if (updated_ids && (original_state != *state))
  68. updated_ids->insert(notification->id());
  69. }
  70. }
  71. void NotificationList::AddNotification(
  72. std::unique_ptr<Notification> notification) {
  73. PushNotification(std::move(notification));
  74. }
  75. void NotificationList::UpdateNotificationMessage(
  76. const std::string& old_id,
  77. std::unique_ptr<Notification> new_notification) {
  78. auto iter = GetNotification(old_id);
  79. if (iter == notifications_.end())
  80. return;
  81. NotificationState state = iter->second;
  82. if ((new_notification->renotify() ||
  83. !message_center_->HasMessageCenterView()) &&
  84. !quiet_mode_) {
  85. state = NotificationState();
  86. }
  87. // Do not use EraseNotification and PushNotification, since we don't want to
  88. // change unread counts nor to update is_read/shown_as_popup states.
  89. notifications_.erase(iter);
  90. // We really don't want duplicate IDs.
  91. DCHECK(GetNotification(new_notification->id()) == notifications_.end());
  92. notifications_.emplace(std::move(new_notification), state);
  93. }
  94. void NotificationList::RemoveNotification(const std::string& id) {
  95. EraseNotification(GetNotification(id));
  96. }
  97. NotificationList::Notifications NotificationList::GetNotifications() const {
  98. Notifications notifications;
  99. for (const auto& tuple : notifications_)
  100. notifications.insert(tuple.first.get());
  101. return notifications;
  102. }
  103. NotificationList::Notifications NotificationList::GetNotificationsByNotifierId(
  104. const NotifierId& notifier_id) const {
  105. Notifications notifications;
  106. for (const auto& tuple : notifications_) {
  107. Notification* notification = tuple.first.get();
  108. if (notification->notifier_id() == notifier_id)
  109. notifications.insert(notification);
  110. }
  111. return notifications;
  112. }
  113. NotificationList::Notifications NotificationList::GetNotificationsByAppId(
  114. const std::string& app_id) const {
  115. Notifications notifications;
  116. for (const auto& tuple : notifications_) {
  117. Notification* notification = tuple.first.get();
  118. if (notification->notifier_id().id == app_id)
  119. notifications.insert(notification);
  120. }
  121. return notifications;
  122. }
  123. NotificationList::Notifications NotificationList::GetNotificationsByOriginUrl(
  124. const GURL& source_url) const {
  125. Notifications notifications;
  126. for (const auto& tuple : notifications_) {
  127. Notification* notification = tuple.first.get();
  128. if (notification->origin_url() == source_url)
  129. notifications.insert(notification);
  130. }
  131. return notifications;
  132. }
  133. bool NotificationList::SetNotificationIcon(const std::string& notification_id,
  134. const ui::ImageModel& image) {
  135. auto iter = GetNotification(notification_id);
  136. if (iter == notifications_.end())
  137. return false;
  138. iter->first->set_icon(image);
  139. return true;
  140. }
  141. bool NotificationList::SetNotificationImage(const std::string& notification_id,
  142. const gfx::Image& image) {
  143. auto iter = GetNotification(notification_id);
  144. if (iter == notifications_.end())
  145. return false;
  146. iter->first->set_image(image);
  147. return true;
  148. }
  149. bool NotificationList::HasNotificationOfType(
  150. const std::string& id,
  151. const NotificationType type) const {
  152. auto iter = GetNotification(id);
  153. if (iter == notifications_.end())
  154. return false;
  155. return iter->first->type() == type;
  156. }
  157. bool NotificationList::HasPopupNotifications(
  158. const NotificationBlockers& blockers) const {
  159. for (const auto& tuple : notifications_) {
  160. if (tuple.first->priority() < DEFAULT_PRIORITY)
  161. break;
  162. if (!tuple.second.shown_as_popup &&
  163. ShouldShowNotificationAsPopup(*tuple.first, blockers,
  164. /*except=*/nullptr)) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. NotificationList::PopupNotifications NotificationList::GetPopupNotifications(
  171. const NotificationBlockers& blockers,
  172. std::list<std::string>* blocked) {
  173. PopupNotifications result;
  174. size_t default_priority_popup_count = 0;
  175. // Collect notifications that should be shown as popups. Start from oldest.
  176. for (auto& [notification, state] : base::Reversed(notifications_)) {
  177. if (state.shown_as_popup)
  178. continue;
  179. // No popups for LOW/MIN priority.
  180. if (notification->priority() < DEFAULT_PRIORITY)
  181. continue;
  182. // Group child notifications are shown in their parent's popup.
  183. if (notification->group_child())
  184. continue;
  185. if (!ShouldShowNotificationAsPopup(*notification, blockers,
  186. /*except=*/nullptr)) {
  187. if (state.is_read)
  188. state.shown_as_popup = true;
  189. if (blocked)
  190. blocked->push_back(notification->id());
  191. continue;
  192. }
  193. // Checking limits. No limits for HIGH/MAX priority. DEFAULT priority
  194. // will return at most kMaxVisiblePopupNotifications entries. If the
  195. // popup entries are more, older entries are used. see crbug.com/165768
  196. if (notification->priority() == DEFAULT_PRIORITY &&
  197. default_priority_popup_count++ >= kMaxVisiblePopupNotifications) {
  198. continue;
  199. }
  200. result.insert(notification.get());
  201. }
  202. return result;
  203. }
  204. NotificationList::PopupNotifications
  205. NotificationList::GetPopupNotificationsWithoutBlocker(
  206. const NotificationBlockers& blockers,
  207. const NotificationBlocker& blocker) const {
  208. PopupNotifications result;
  209. // Collect notifications that should be shown as popups, starting with the
  210. // newest.
  211. // TODO(1276903): see if we can merge this logic with `GetPopupNotifications`.
  212. // In particular, we could pass an optional blocker argument that would be
  213. // bypassed if specified.
  214. for (const auto& iter : notifications_) {
  215. const NotificationState* state = &iter.second;
  216. Notification* notification = iter.first.get();
  217. if (state->shown_as_popup)
  218. continue;
  219. // No popups for LOW/MIN priority.
  220. if (notification->priority() < DEFAULT_PRIORITY)
  221. continue;
  222. // Group child notifications are shown in their parent's popup.
  223. if (notification->group_child())
  224. continue;
  225. if (!ShouldShowNotificationAsPopup(*notification, blockers, &blocker))
  226. continue;
  227. result.insert(notification);
  228. }
  229. return result;
  230. }
  231. void NotificationList::MarkSinglePopupAsShown(const std::string& id,
  232. bool mark_notification_as_read) {
  233. auto iter = GetNotification(id);
  234. DCHECK(iter != notifications_.end());
  235. NotificationState* state = &iter->second;
  236. const Notification& notification = *iter->first;
  237. if (iter->second.shown_as_popup)
  238. return;
  239. // System notification is marked as shown only when marked as read.
  240. if (notification.priority() != SYSTEM_PRIORITY || mark_notification_as_read)
  241. state->shown_as_popup = true;
  242. // The popup notification is already marked as read when it's displayed.
  243. // Set the is_read back to false if necessary.
  244. if (!mark_notification_as_read)
  245. state->is_read = false;
  246. }
  247. void NotificationList::MarkSinglePopupAsDisplayed(const std::string& id) {
  248. auto iter = GetNotification(id);
  249. if (iter == notifications_.end())
  250. return;
  251. NotificationState* state = &iter->second;
  252. if (state->shown_as_popup)
  253. return;
  254. state->is_read = true;
  255. }
  256. void NotificationList::ResetSinglePopup(const std::string& id) {
  257. auto iter = GetNotification(id);
  258. DCHECK(iter != notifications_.end());
  259. NotificationState* state = &iter->second;
  260. // `shown_as_popup` should be true if quiet mode is enabled.
  261. state->shown_as_popup = quiet_mode_;
  262. state->is_read = false;
  263. }
  264. NotificationDelegate* NotificationList::GetNotificationDelegate(
  265. const std::string& id) {
  266. auto iter = GetNotification(id);
  267. if (iter == notifications_.end())
  268. return nullptr;
  269. return iter->first->delegate();
  270. }
  271. void NotificationList::SetQuietMode(bool quiet_mode) {
  272. quiet_mode_ = quiet_mode;
  273. if (quiet_mode_) {
  274. // To prevent popups showing in quiet mode, mark all notifications'
  275. // `shown_as_popup` to true.
  276. for (auto& tuple : notifications_)
  277. tuple.second.shown_as_popup = true;
  278. }
  279. }
  280. Notification* NotificationList::GetNotificationById(const std::string& id) {
  281. auto iter = GetNotification(id);
  282. if (iter != notifications_.end())
  283. return iter->first.get();
  284. return nullptr;
  285. }
  286. NotificationList::Notifications NotificationList::GetVisibleNotifications(
  287. const NotificationBlockers& blockers) const {
  288. Notifications result;
  289. for (const auto& tuple : notifications_) {
  290. bool should_show = true;
  291. for (size_t i = 0; i < blockers.size(); ++i) {
  292. if (!blockers[i]->ShouldShowNotification(*tuple.first)) {
  293. should_show = false;
  294. break;
  295. }
  296. }
  297. if (should_show)
  298. result.insert(tuple.first.get());
  299. }
  300. return result;
  301. }
  302. size_t NotificationList::NotificationCount(
  303. const NotificationBlockers& blockers) const {
  304. return GetVisibleNotifications(blockers).size();
  305. }
  306. NotificationList::OwnedNotifications::iterator
  307. NotificationList::GetNotification(const std::string& id) {
  308. for (auto iter = notifications_.begin(); iter != notifications_.end();
  309. ++iter) {
  310. if (iter->first->id() == id)
  311. return iter;
  312. }
  313. return notifications_.end();
  314. }
  315. NotificationList::OwnedNotifications::const_iterator
  316. NotificationList::GetNotification(const std::string& id) const {
  317. for (auto iter = notifications_.begin(); iter != notifications_.end();
  318. ++iter) {
  319. if (iter->first->id() == id)
  320. return iter;
  321. }
  322. return notifications_.end();
  323. }
  324. void NotificationList::EraseNotification(OwnedNotifications::iterator iter) {
  325. notifications_.erase(iter);
  326. }
  327. void NotificationList::PushNotification(
  328. std::unique_ptr<Notification> notification) {
  329. // Ensure that notification.id is unique by erasing any existing
  330. // notification with the same id (shouldn't normally happen).
  331. auto iter = GetNotification(notification->id());
  332. NotificationState state;
  333. if (iter != notifications_.end()) {
  334. state = iter->second;
  335. EraseNotification(iter);
  336. } else {
  337. // For critical ChromeOS system notifications, we ignore the standard quiet
  338. // mode behaviour and show the notification anyways.
  339. bool effective_quiet_mode = quiet_mode_;
  340. #if BUILDFLAG(IS_CHROMEOS_ASH)
  341. effective_quiet_mode &= notification->system_notification_warning_level() !=
  342. SystemNotificationWarningLevel::CRITICAL_WARNING;
  343. #endif
  344. // TODO(mukai): needs to distinguish if a notification is dismissed by
  345. // the quiet mode or user operation.
  346. state.shown_as_popup =
  347. message_center_->IsMessageCenterVisible() || effective_quiet_mode;
  348. }
  349. if (notification->priority() == MIN_PRIORITY)
  350. state.is_read = true;
  351. notifications_.emplace(std::move(notification), state);
  352. }
  353. } // namespace message_center