peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 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 | #ifndef CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
Peter Beverloo | b3310e6 | 2017-08-23 18:52:54 | [diff] [blame] | 12 | #include "base/callback.h" |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 13 | #include "base/macros.h" |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 14 | #include "base/optional.h" |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 15 | #include "chrome/browser/notifications/notification_common.h" |
| 16 | #include "chrome/browser/notifications/notification_display_service.h" |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 17 | #include "ui/message_center/notification.h" |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 18 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 19 | class Profile; |
| 20 | |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 21 | // Implementation of the NotificationDisplayService interface that can be used |
| 22 | // for testing purposes. Supports additional methods enabling instrumenting the |
| 23 | // faked underlying notification system. |
| 24 | class StubNotificationDisplayService : public NotificationDisplayService { |
| 25 | public: |
| 26 | // Factory function to be used with NotificationDisplayServiceFactory's |
| 27 | // SetTestingFactory method, overriding the default display service. |
| 28 | static std::unique_ptr<KeyedService> FactoryForTests( |
| 29 | content::BrowserContext* browser_context); |
| 30 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 31 | explicit StubNotificationDisplayService(Profile* profile); |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 32 | ~StubNotificationDisplayService() override; |
| 33 | |
Peter Beverloo | b3310e6 | 2017-08-23 18:52:54 | [diff] [blame] | 34 | // Sets |closure| to be invoked when any notification has been added. |
| 35 | void SetNotificationAddedClosure(base::RepeatingClosure closure); |
| 36 | |
Peter Beverloo | f91afc84 | 2017-08-14 16:09:50 | [diff] [blame] | 37 | // Returns a vector of the displayed Notification objects. |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 38 | std::vector<message_center::Notification> GetDisplayedNotificationsForType( |
Peter Beverloo | f91afc84 | 2017-08-14 16:09:50 | [diff] [blame] | 39 | NotificationCommon::Type type) const; |
| 40 | |
Evan Stade | 29036d5 | 2017-11-14 18:11:42 | [diff] [blame] | 41 | base::Optional<message_center::Notification> GetNotification( |
| 42 | const std::string& notification_id); |
| 43 | |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 44 | const NotificationCommon::Metadata* GetMetadataForNotification( |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 45 | const message_center::Notification& notification); |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 46 | |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 47 | // Simulates the notification identified by |notification_id| being closed due |
| 48 | // to external events, such as the user dismissing it when |by_user| is set. |
| 49 | // When |silent| is set, the notification handlers won't be informed of the |
| 50 | // change to immitate behaviour of operating systems that don't inform apps |
| 51 | // about removed notifications. |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 52 | void RemoveNotification(NotificationCommon::Type notification_type, |
| 53 | const std::string& notification_id, |
Peter Beverloo | f91afc84 | 2017-08-14 16:09:50 | [diff] [blame] | 54 | bool by_user, |
Peter Beverloo | 14d352b | 2017-08-22 19:32:41 | [diff] [blame] | 55 | bool silent); |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 56 | |
| 57 | // Removes all notifications shown by this display service. |
| 58 | void RemoveAllNotifications(NotificationCommon::Type notification_type, |
| 59 | bool by_user); |
| 60 | |
| 61 | // NotificationDisplayService implementation: |
| 62 | void Display(NotificationCommon::Type notification_type, |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 63 | const message_center::Notification& notification, |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 64 | std::unique_ptr<NotificationCommon::Metadata> metadata) override; |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 65 | void Close(NotificationCommon::Type notification_type, |
| 66 | const std::string& notification_id) override; |
peter | b5298ae | 2017-04-11 21:52:51 | [diff] [blame] | 67 | void GetDisplayed(const DisplayedNotificationsCallback& callback) override; |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | // Data to store for a notification that's being shown through this service. |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 71 | struct NotificationData { |
| 72 | NotificationData(NotificationCommon::Type type, |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 73 | const message_center::Notification& notification, |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 74 | std::unique_ptr<NotificationCommon::Metadata> metadata); |
| 75 | NotificationData(NotificationData&& other); |
| 76 | ~NotificationData(); |
| 77 | |
| 78 | NotificationData& operator=(NotificationData&& other); |
| 79 | |
| 80 | NotificationCommon::Type type; |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 81 | message_center::Notification notification; |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 82 | std::unique_ptr<NotificationCommon::Metadata> metadata; |
| 83 | }; |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 84 | |
Peter Beverloo | b3310e6 | 2017-08-23 18:52:54 | [diff] [blame] | 85 | base::RepeatingClosure notification_added_closure_; |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 86 | std::vector<NotificationData> notifications_; |
miguelg | 30dfc2e | 2017-06-14 18:18:09 | [diff] [blame] | 87 | Profile* profile_; |
peter | 28d8b73 | 2017-04-11 17:36:17 | [diff] [blame] | 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(StubNotificationDisplayService); |
| 90 | }; |
| 91 | |
| 92 | #endif // CHROME_BROWSER_NOTIFICATIONS_STUB_NOTIFICATION_DISPLAY_SERVICE_H_ |