OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "chrome/browser/notifications/notification_display_service.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/nullable_string16.h" |
| 9 #include "chrome/browser/notifications/non_persistent_notification_handler.h" |
| 10 #include "chrome/browser/notifications/notification_common.h" |
| 11 #include "chrome/browser/notifications/persistent_notification_handler.h" |
| 12 #include "extensions/features/features.h" |
| 13 |
| 14 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 15 #include "chrome/browser/extensions/api/notifications/extension_notification_han
dler.h" |
| 16 #endif |
| 17 |
| 18 NotificationDisplayService::NotificationDisplayService(Profile* profile) |
| 19 : profile_(profile) { |
| 20 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, |
| 21 base::MakeUnique<NonPersistentNotificationHandler>()); |
| 22 AddNotificationHandler(NotificationCommon::PERSISTENT, |
| 23 base::MakeUnique<PersistentNotificationHandler>()); |
| 24 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 25 AddNotificationHandler( |
| 26 NotificationCommon::EXTENSION, |
| 27 base::MakeUnique<extensions::ExtensionNotificationHandler>()); |
| 28 #endif |
| 29 } |
| 30 |
| 31 NotificationDisplayService::~NotificationDisplayService() = default; |
| 32 |
| 33 void NotificationDisplayService::AddNotificationHandler( |
| 34 NotificationCommon::Type notification_type, |
| 35 std::unique_ptr<NotificationHandler> handler) { |
| 36 DCHECK(handler); |
| 37 DCHECK_EQ(notification_handlers_.count(notification_type), 0u); |
| 38 notification_handlers_[notification_type] = std::move(handler); |
| 39 } |
| 40 |
| 41 NotificationHandler* NotificationDisplayService::GetNotificationHandler( |
| 42 NotificationCommon::Type notification_type) { |
| 43 DCHECK(notification_handlers_.find(notification_type) != |
| 44 notification_handlers_.end()) |
| 45 << notification_type << " is not registered."; |
| 46 return notification_handlers_[notification_type].get(); |
| 47 } |
| 48 |
| 49 void NotificationDisplayService::ProcessNotificationOperation( |
| 50 NotificationCommon::Operation operation, |
| 51 NotificationCommon::Type notification_type, |
| 52 const std::string& origin, |
| 53 const std::string& notification_id, |
| 54 int action_index, |
| 55 const base::NullableString16& reply) { |
| 56 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 57 DCHECK(handler); |
| 58 if (!handler) { |
| 59 LOG(ERROR) << "Unable to find a handler for " << notification_type; |
| 60 return; |
| 61 } |
| 62 switch (operation) { |
| 63 case NotificationCommon::CLICK: |
| 64 handler->OnClick(profile_, origin, notification_id, action_index, reply); |
| 65 break; |
| 66 case NotificationCommon::CLOSE: |
| 67 handler->OnClose(profile_, origin, notification_id, true /* by_user */); |
| 68 break; |
| 69 case NotificationCommon::SETTINGS: |
| 70 handler->OpenSettings(profile_); |
| 71 break; |
| 72 } |
| 73 } |
OLD | NEW |