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..2e98f2eb8e0769cd5fdd94594543310eb6d013f3 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/notifications/extension_notification_display_helper.cc |
| @@ -0,0 +1,92 @@ |
| +// 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() {} |
|
Miguel Garcia
2017/02/21 10:56:43
Shouldn't this close all displayed notifications?
Peter Beverloo
2017/02/21 17:23:50
Moved to a Shutdown() function. (The destructor ac
|
| + |
| +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); |
| +} |
| + |
| +const 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()); |
| + } |
|
Miguel Garcia
2017/02/21 10:56:43
Shouldn't we be duplicating the logic we have in c
Peter Beverloo
2017/02/21 17:23:50
No. There is no risk of left-over logic since the
Miguel Garcia
2017/02/22 14:46:18
I'm still not convinced sorry, if you use extensio
Peter Beverloo
2017/02/28 01:08:59
Ah, different form of notification sync.
Yeah, th
|
| + |
| + return notification_ids; |
| +} |
| + |
| +const ExtensionNotificationDisplayHelper::NotificationVector& |
| +ExtensionNotificationDisplayHelper::GetNotificationsForTests() const { |
| + return notifications_; |
| +} |
| + |
| +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; |
| + |
| + GetDisplayService()->Close(NotificationCommon::EXTENSION, notification_id); |
| + return true; |
| +} |
| + |
| +NotificationDisplayService* |
| +ExtensionNotificationDisplayHelper::GetDisplayService() { |
| + return NotificationDisplayServiceFactory::GetForProfile(profile_); |
| +} |
| + |
| +} // namespace extensions |