message_view_factory.cc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 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 "ash/system/message_center/message_view_factory.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/system/message_center/ash_notification_view.h"
  9. #include "base/lazy_instance.h"
  10. #include "base/logging.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "ui/message_center/public/cpp/notification_types.h"
  13. #include "ui/message_center/views/notification_view.h"
  14. namespace ash {
  15. namespace {
  16. using MessageViewCustomFactoryMap =
  17. std::map<std::string, MessageViewFactory::CustomMessageViewFactoryFunction>;
  18. base::LazyInstance<MessageViewCustomFactoryMap>::Leaky g_custom_view_factories =
  19. LAZY_INSTANCE_INITIALIZER;
  20. std::unique_ptr<message_center::MessageView> GetCustomNotificationView(
  21. const message_center::Notification& notification,
  22. bool shown_in_popup) {
  23. MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
  24. auto iter = factories->find(notification.custom_view_type());
  25. DCHECK(iter != factories->end());
  26. return iter->second.Run(notification, shown_in_popup);
  27. }
  28. } // namespace
  29. // static
  30. std::unique_ptr<message_center::MessageView> MessageViewFactory::Create(
  31. const message_center::Notification& notification,
  32. bool shown_in_popup) {
  33. switch (notification.type()) {
  34. case message_center::DEPRECATED_NOTIFICATION_TYPE_BASE_FORMAT:
  35. case message_center::NOTIFICATION_TYPE_IMAGE:
  36. case message_center::NOTIFICATION_TYPE_MULTIPLE:
  37. case message_center::NOTIFICATION_TYPE_SIMPLE:
  38. case message_center::NOTIFICATION_TYPE_PROGRESS:
  39. // Rely on default construction after the switch.
  40. break;
  41. case message_center::NOTIFICATION_TYPE_CUSTOM:
  42. return GetCustomNotificationView(notification, shown_in_popup);
  43. default:
  44. // If the caller asks for an unrecognized kind of view (entirely possible
  45. // if an application is running on an older version of this code that
  46. // doesn't have the requested kind of notification template), we'll fall
  47. // back to a notification instance that will provide at least basic
  48. // functionality.
  49. LOG(WARNING) << "Unable to fulfill request for unrecognized or"
  50. << "unsupported notification type " << notification.type()
  51. << ". Falling back to simple notification type.";
  52. break;
  53. }
  54. if (ash::features::IsNotificationsRefreshEnabled())
  55. return std::make_unique<AshNotificationView>(notification, shown_in_popup);
  56. return std::make_unique<message_center::NotificationView>(notification);
  57. }
  58. // static
  59. void MessageViewFactory::SetCustomNotificationViewFactory(
  60. const std::string& custom_view_type,
  61. const CustomMessageViewFactoryFunction& factory_function) {
  62. MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
  63. DCHECK(factories->find(custom_view_type) == factories->end());
  64. factories->emplace(custom_view_type, factory_function);
  65. }
  66. // static
  67. bool MessageViewFactory::HasCustomNotificationViewFactory(
  68. const std::string& custom_view_type) {
  69. MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
  70. return factories->find(custom_view_type) != factories->end();
  71. }
  72. // static
  73. void MessageViewFactory::ClearCustomNotificationViewFactory(
  74. const std::string& custom_view_type) {
  75. g_custom_view_factories.Get().erase(custom_view_type);
  76. }
  77. } // namespace ash