| 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/extensions/api/notifications/extension_notification_dis
play_helper.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/notifications/notification.h" |
| 12 #include "chrome/browser/notifications/notification_display_service.h" |
| 13 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 ExtensionNotificationDisplayHelper::ExtensionNotificationDisplayHelper( |
| 19 Profile* profile) |
| 20 : profile_(profile) {} |
| 21 |
| 22 ExtensionNotificationDisplayHelper::~ExtensionNotificationDisplayHelper() {} |
| 23 |
| 24 void ExtensionNotificationDisplayHelper::Display( |
| 25 const Notification& notification) { |
| 26 // Remove the previous version of this notification if the |notification| is |
| 27 // updating another notification. |
| 28 EraseDataForNotificationId(notification.delegate_id()); |
| 29 |
| 30 notifications_.push_back(base::MakeUnique<Notification>(notification)); |
| 31 |
| 32 GetDisplayService()->Display(NotificationCommon::EXTENSION, |
| 33 notification.delegate_id(), notification); |
| 34 } |
| 35 |
| 36 Notification* ExtensionNotificationDisplayHelper::GetByNotificationId( |
| 37 const std::string& notification_id) { |
| 38 for (const auto& notification : notifications_) { |
| 39 if (notification->delegate_id() == notification_id) |
| 40 return notification.get(); |
| 41 } |
| 42 |
| 43 return nullptr; |
| 44 } |
| 45 |
| 46 std::set<std::string> |
| 47 ExtensionNotificationDisplayHelper::GetNotificationIdsForExtension( |
| 48 const GURL& extension_origin) const { |
| 49 std::set<std::string> notification_ids; |
| 50 for (const auto& notification : notifications_) { |
| 51 if (notification->origin_url() == extension_origin) |
| 52 notification_ids.insert(notification->delegate_id()); |
| 53 } |
| 54 |
| 55 return notification_ids; |
| 56 } |
| 57 |
| 58 bool ExtensionNotificationDisplayHelper::EraseDataForNotificationId( |
| 59 const std::string& notification_id) { |
| 60 auto iter = std::find_if( |
| 61 notifications_.begin(), notifications_.end(), |
| 62 [notification_id](const std::unique_ptr<Notification>& notification) { |
| 63 return notification->delegate_id() == notification_id; |
| 64 }); |
| 65 |
| 66 if (iter == notifications_.end()) |
| 67 return false; |
| 68 |
| 69 notifications_.erase(iter); |
| 70 return true; |
| 71 } |
| 72 |
| 73 bool ExtensionNotificationDisplayHelper::Close( |
| 74 const std::string& notification_id) { |
| 75 if (!EraseDataForNotificationId(notification_id)) |
| 76 return false; |
| 77 |
| 78 GetDisplayService()->Close(NotificationCommon::EXTENSION, notification_id); |
| 79 return true; |
| 80 } |
| 81 |
| 82 void ExtensionNotificationDisplayHelper::Shutdown() { |
| 83 // Do not call GetDisplayService()->Close() here. The Shutdown method is |
| 84 // called upon profile destruction and closing a notification requires a |
| 85 // profile, as it may dispatch an event to the extension. |
| 86 |
| 87 notifications_.clear(); |
| 88 } |
| 89 |
| 90 NotificationDisplayService* |
| 91 ExtensionNotificationDisplayHelper::GetDisplayService() { |
| 92 return NotificationDisplayServiceFactory::GetForProfile(profile_); |
| 93 } |
| 94 |
| 95 } // namespace extensions |
| OLD | NEW |