| 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/stub_notification_display_service.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 |
| 11 // static |
| 12 std::unique_ptr<KeyedService> StubNotificationDisplayService::FactoryForTests( |
| 13 content::BrowserContext* /* browser_context */) { |
| 14 return base::MakeUnique<StubNotificationDisplayService>(); |
| 15 } |
| 16 |
| 17 StubNotificationDisplayService::StubNotificationDisplayService() = default; |
| 18 |
| 19 StubNotificationDisplayService::~StubNotificationDisplayService() = default; |
| 20 |
| 21 void StubNotificationDisplayService::RemoveNotification( |
| 22 NotificationCommon::Type notification_type, |
| 23 const std::string& notification_id, |
| 24 bool by_user) { |
| 25 auto iter = std::find_if( |
| 26 notifications_.begin(), notifications_.end(), |
| 27 [notification_type, notification_id](const NotificationData& data) { |
| 28 return data.first == notification_type && |
| 29 data.second.delegate_id() == notification_id; |
| 30 }); |
| 31 |
| 32 if (iter == notifications_.end()) |
| 33 return; |
| 34 |
| 35 // TODO(peter): Invoke the handlers when that has been generalized. |
| 36 iter->second.delegate()->Close(by_user); |
| 37 |
| 38 notifications_.erase(iter); |
| 39 } |
| 40 |
| 41 void StubNotificationDisplayService::RemoveAllNotifications( |
| 42 NotificationCommon::Type notification_type, |
| 43 bool by_user) { |
| 44 for (auto iter = notifications_.begin(); iter != notifications_.end();) { |
| 45 if (iter->first == notification_type) { |
| 46 // TODO(peter): Invoke the handlers when that has been generalized. |
| 47 iter->second.delegate()->Close(by_user); |
| 48 |
| 49 iter = notifications_.erase(iter); |
| 50 } else { |
| 51 iter++; |
| 52 } |
| 53 } |
| 54 } |
| 55 |
| 56 void StubNotificationDisplayService::Display( |
| 57 NotificationCommon::Type notification_type, |
| 58 const std::string& notification_id, |
| 59 const Notification& notification) { |
| 60 // This mimics notification replacement behaviour; the Close() method on a |
| 61 // notification's delegate is not meant to be invoked in this situation. |
| 62 Close(notification_type, notification_id); |
| 63 |
| 64 notifications_.emplace_back(notification_type, notification); |
| 65 } |
| 66 |
| 67 void StubNotificationDisplayService::Close( |
| 68 NotificationCommon::Type notification_type, |
| 69 const std::string& notification_id) { |
| 70 notifications_.erase( |
| 71 std::remove_if( |
| 72 notifications_.begin(), notifications_.end(), |
| 73 [notification_type, notification_id](const NotificationData& data) { |
| 74 return data.first == notification_type && |
| 75 data.second.delegate_id() == notification_id; |
| 76 }), |
| 77 notifications_.end()); |
| 78 } |
| 79 |
| 80 void StubNotificationDisplayService::GetDisplayed( |
| 81 const DisplayedNotificationsCallback& callback) const { |
| 82 std::unique_ptr<std::set<std::string>> notifications = |
| 83 base::MakeUnique<std::set<std::string>>(); |
| 84 |
| 85 for (const auto& notification_data : notifications_) |
| 86 notifications->insert(notification_data.second.delegate_id()); |
| 87 |
| 88 callback.Run(std::move(notifications), true /* supports_synchronization */); |
| 89 } |
| OLD | NEW |