Chromium Code Reviews| 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() {} | |
|
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
| |
| 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 const Notification* ExtensionNotificationDisplayHelper::GetByNotificationId( | |
| 37 const std::string& notification_id) { | |
| 38 for (const std::unique_ptr<Notification>& 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 std::unique_ptr<Notification>& notification : notifications_) { | |
| 51 if (notification->origin_url() == extension_origin) | |
| 52 notification_ids.insert(notification->delegate_id()); | |
| 53 } | |
|
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
| |
| 54 | |
| 55 return notification_ids; | |
| 56 } | |
| 57 | |
| 58 const ExtensionNotificationDisplayHelper::NotificationVector& | |
| 59 ExtensionNotificationDisplayHelper::GetNotificationsForTests() const { | |
| 60 return notifications_; | |
| 61 } | |
| 62 | |
| 63 bool ExtensionNotificationDisplayHelper::EraseDataForNotificationId( | |
| 64 const std::string& notification_id) { | |
| 65 auto iter = std::find_if( | |
| 66 notifications_.begin(), notifications_.end(), | |
| 67 [notification_id](const std::unique_ptr<Notification>& notification) { | |
| 68 return notification->delegate_id() == notification_id; | |
| 69 }); | |
| 70 | |
| 71 if (iter == notifications_.end()) | |
| 72 return false; | |
| 73 | |
| 74 notifications_.erase(iter); | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 bool ExtensionNotificationDisplayHelper::Close( | |
| 79 const std::string& notification_id) { | |
| 80 if (!EraseDataForNotificationId(notification_id)) | |
| 81 return false; | |
| 82 | |
| 83 GetDisplayService()->Close(NotificationCommon::EXTENSION, notification_id); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 NotificationDisplayService* | |
| 88 ExtensionNotificationDisplayHelper::GetDisplayService() { | |
| 89 return NotificationDisplayServiceFactory::GetForProfile(profile_); | |
| 90 } | |
| 91 | |
| 92 } // namespace extensions | |
| OLD | NEW |