Chromium Code Reviews| Index: chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc |
| diff --git a/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc b/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f6f507d6034c282fc4cfa38829e0b9b9cf2593eb |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2017 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 "chrome/browser/extensions/api/notifications/extension_notification_display_helper.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/notifications/notification.h" |
| +#include "chrome/browser/notifications/notification_display_service.h" |
| +#include "chrome/browser/notifications/notification_display_service_factory.h" |
| +#include "url/gurl.h" |
| + |
| +namespace extensions { |
| + |
| +ExtensionNotificationDisplayHelper::ExtensionNotificationDisplayHelper( |
| + Profile* profile) |
| + : profile_(profile) {} |
| + |
| +ExtensionNotificationDisplayHelper::~ExtensionNotificationDisplayHelper() {} |
| + |
| +void ExtensionNotificationDisplayHelper::Display( |
| + const Notification& notification) { |
| + // Remove the previous version of this notification if the |notification| is |
| + // updating another notification. |
| + EraseDataForNotificationId(notification.delegate_id()); |
| + |
| + notifications_.push_back(base::MakeUnique<Notification>(notification)); |
| + |
| + GetDisplayService()->Display(NotificationCommon::EXTENSION, |
| + notification.delegate_id(), notification); |
| +} |
| + |
| +Notification* ExtensionNotificationDisplayHelper::GetByNotificationId( |
| + const std::string& notification_id) { |
| + for (const std::unique_ptr<Notification>& notification : notifications_) { |
| + if (notification->delegate_id() == notification_id) |
| + return notification.get(); |
| + } |
| + |
| + return nullptr; |
| +} |
| + |
| +std::set<std::string> |
| +ExtensionNotificationDisplayHelper::GetNotificationIdsForExtension( |
| + const GURL& extension_origin) const { |
| + std::set<std::string> notification_ids; |
| + for (const std::unique_ptr<Notification>& notification : notifications_) { |
| + if (notification->origin_url() == extension_origin) |
| + notification_ids.insert(notification->delegate_id()); |
| + } |
| + |
| + return notification_ids; |
| +} |
| + |
| +bool ExtensionNotificationDisplayHelper::EraseDataForNotificationId( |
| + const std::string& notification_id) { |
| + auto iter = std::find_if( |
| + notifications_.begin(), notifications_.end(), |
| + [notification_id](const std::unique_ptr<Notification>& notification) { |
| + return notification->delegate_id() == notification_id; |
| + }); |
| + |
| + if (iter == notifications_.end()) |
| + return false; |
| + |
| + notifications_.erase(iter); |
| + return true; |
| +} |
| + |
| +bool ExtensionNotificationDisplayHelper::Close( |
| + const std::string& notification_id) { |
| + if (!EraseDataForNotificationId(notification_id)) |
| + return false; |
|
dewittj
2017/03/01 17:59:08
This early return is concerning if the notificatio
Peter Beverloo
2017/04/05 23:22:11
We now have a mechanism to synchronise the shown n
|
| + |
| + GetDisplayService()->Close(NotificationCommon::EXTENSION, notification_id); |
| + return true; |
| +} |
| + |
| +void ExtensionNotificationDisplayHelper::Shutdown() { |
| + for (const std::unique_ptr<Notification>& notification : notifications_) { |
| + GetDisplayService()->Close(NotificationCommon::EXTENSION, |
| + notification->delegate_id()); |
| + } |
| + |
| + notifications_.clear(); |
| +} |
| + |
| +NotificationDisplayService* |
| +ExtensionNotificationDisplayHelper::GetDisplayService() { |
| + return NotificationDisplayServiceFactory::GetForProfile(profile_); |
| +} |
| + |
| +} // namespace extensions |