blob: 8db23c841cc0bc12e575bece79967d1c886d9cb5 [file] [log] [blame]
// Copyright 2016 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 "ui/message_center/views/message_view_factory.h"
#include <vector>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/lazy_instance.h"
#include "ui/message_center/public/cpp/features.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/views/notification_view_md.h"
#if !defined(OS_CHROMEOS)
#include "ui/message_center/views/notification_view.h"
#endif
namespace message_center {
namespace {
using MessageViewCustomFactoryMap =
std::map<std::string, MessageViewFactory::CustomMessageViewFactoryFunction>;
base::LazyInstance<MessageViewCustomFactoryMap>::Leaky g_custom_view_factories =
LAZY_INSTANCE_INITIALIZER;
std::unique_ptr<MessageView> GetCustomNotificationView(
const Notification& notification) {
MessageViewCustomFactoryMap* factories = g_custom_view_factories.Pointer();
auto iter = factories->find(notification.custom_view_type());
DCHECK(iter != factories->end());
return iter->second.Run(notification);
}
} // namespace
// static
MessageView* MessageViewFactory::Create(const Notification& notification) {
MessageView* notification_view = nullptr;
switch (notification.type()) {
case NOTIFICATION_TYPE_BASE_FORMAT:
case NOTIFICATION_TYPE_IMAGE:
case NOTIFICATION_TYPE_MULTIPLE:
case NOTIFICATION_TYPE_SIMPLE:
case NOTIFICATION_TYPE_PROGRESS:
// Rely on default construction after the switch.
break;
case NOTIFICATION_TYPE_CUSTOM:
notification_view = GetCustomNotificationView(notification).release();
break;
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 (!notification_view) {
#if defined(OS_CHROMEOS)
notification_view = new NotificationViewMD(notification);
#else
// All above roads lead to the generic NotificationView.
if (base::FeatureList::IsEnabled(message_center::kNewStyleNotifications))
notification_view = new NotificationViewMD(notification);
else
notification_view = new NotificationView(notification);
#endif
}
return notification_view;
}
// 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::ClearCustomNotificationViewFactoryForTest(
const std::string& custom_view_type) {
g_custom_view_factories.Get().erase(custom_view_type);
}
} // namespace message_center