1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/system/message_center/message_view_factory.h"
- #include <memory>
- #include <vector>
- #include "ash/constants/ash_features.h"
- #include "ash/system/message_center/ash_notification_view.h"
- #include "base/lazy_instance.h"
- #include "base/logging.h"
- #include "build/chromeos_buildflags.h"
- #include "ui/message_center/public/cpp/notification_types.h"
- #include "ui/message_center/views/notification_view.h"
- namespace ash {
- namespace {
- using MessageViewCustomFactoryMap =
- std::map<std::string, MessageViewFactory::CustomMessageViewFactoryFunction>;
- base::LazyInstance<MessageViewCustomFactoryMap>::Leaky g_custom_view_factories =
- LAZY_INSTANCE_INITIALIZER;
- std::unique_ptr<message_center::MessageView> GetCustomNotificationView(
- const message_center::Notification& notification,
- bool shown_in_popup) {
- MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
- auto iter = factories->find(notification.custom_view_type());
- DCHECK(iter != factories->end());
- return iter->second.Run(notification, shown_in_popup);
- }
- } // namespace
- // static
- std::unique_ptr<message_center::MessageView> MessageViewFactory::Create(
- const message_center::Notification& notification,
- bool shown_in_popup) {
- switch (notification.type()) {
- case message_center::DEPRECATED_NOTIFICATION_TYPE_BASE_FORMAT:
- case message_center::NOTIFICATION_TYPE_IMAGE:
- case message_center::NOTIFICATION_TYPE_MULTIPLE:
- case message_center::NOTIFICATION_TYPE_SIMPLE:
- case message_center::NOTIFICATION_TYPE_PROGRESS:
- // Rely on default construction after the switch.
- break;
- case message_center::NOTIFICATION_TYPE_CUSTOM:
- return GetCustomNotificationView(notification, shown_in_popup);
- default:
- // If the caller asks for an unrecognized kind of view (entirely possible
- // if an application is running on an older version of this code that
- // doesn't have the requested kind of notification template), we'll fall
- // back to a notification instance that will provide at least basic
- // functionality.
- LOG(WARNING) << "Unable to fulfill request for unrecognized or"
- << "unsupported notification type " << notification.type()
- << ". Falling back to simple notification type.";
- break;
- }
- if (ash::features::IsNotificationsRefreshEnabled())
- return std::make_unique<AshNotificationView>(notification, shown_in_popup);
- return std::make_unique<message_center::NotificationView>(notification);
- }
- // static
- void MessageViewFactory::SetCustomNotificationViewFactory(
- const std::string& custom_view_type,
- const CustomMessageViewFactoryFunction& factory_function) {
- MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
- DCHECK(factories->find(custom_view_type) == factories->end());
- factories->emplace(custom_view_type, factory_function);
- }
- // static
- bool MessageViewFactory::HasCustomNotificationViewFactory(
- const std::string& custom_view_type) {
- MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
- return factories->find(custom_view_type) != factories->end();
- }
- // static
- void MessageViewFactory::ClearCustomNotificationViewFactory(
- const std::string& custom_view_type) {
- g_custom_view_factories.Get().erase(custom_view_type);
- }
- } // namespace ash
|