miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 1 | // Copyright 2016 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_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 7 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 8 | #include <map> |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 9 | #include <memory> |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 12 | |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 13 | #include "base/callback_forward.h" |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 14 | #include "base/macros.h" |
Peter Beverloo | ae64ba9 | 2017-09-08 21:02:45 | [diff] [blame] | 15 | #include "base/optional.h" |
| 16 | #include "base/strings/string16.h" |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 17 | #include "chrome/browser/notifications/notification_common.h" |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 18 | #include "components/keyed_service/core/keyed_service.h" |
| 19 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 20 | class NotificationHandler; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 21 | class Profile; |
| 22 | |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 23 | namespace message_center { |
| 24 | class Notification; |
| 25 | } |
| 26 | |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 27 | // Profile-bound service that enables notifications to be displayed and |
| 28 | // interacted with on the user's screen, orthogonal of whether this |
| 29 | // functionality is provided by the browser or by the operating system. An |
| 30 | // instance can be retrieved through the NotificationDisplayServiceFactory. |
| 31 | // |
| 32 | // TODO(peter): Add a NotificationHandler mechanism for registering listeners. |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 33 | class NotificationDisplayService : public KeyedService { |
| 34 | public: |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 35 | using DisplayedNotificationsCallback = |
| 36 | base::Callback<void(std::unique_ptr<std::set<std::string>>, |
| 37 | bool /* supports_synchronization */)>; |
Evan Stade | 6e15415 | 2017-11-10 01:58:25 | [diff] [blame] | 38 | |
| 39 | static NotificationDisplayService* GetForProfile(Profile* profile); |
| 40 | |
Evan Stade | 5d6e469 | 2017-12-08 17:43:52 | [diff] [blame] | 41 | // Returns the NDS for system notifications which aren't tied to a particular |
| 42 | // user. Currently only implemented on Chrome OS. TODO(estade): implement |
| 43 | // elsewhere as needed. |
| 44 | static NotificationDisplayService* GetForSystemNotifications(); |
| 45 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 46 | explicit NotificationDisplayService(Profile* profile); |
| 47 | ~NotificationDisplayService() override; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 48 | |
| 49 | // Displays the |notification| identified by |notification_id|. |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 50 | virtual void Display( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 51 | NotificationHandler::Type notification_type, |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 52 | const message_center::Notification& notification, |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 53 | std::unique_ptr<NotificationCommon::Metadata> metadata = nullptr) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 54 | |
| 55 | // Closes the notification identified by |notification_id|. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 56 | virtual void Close(NotificationHandler::Type notification_type, |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 57 | const std::string& notification_id) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 58 | |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 59 | // Writes the ids of all currently displaying notifications and |
| 60 | // invokes |callback| with the result once known. |
peter | b5298ae | 2017-04-11 21:52:51 | [diff] [blame] | 61 | virtual void GetDisplayed(const DisplayedNotificationsCallback& callback) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 62 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 63 | // Used to propagate back events originate from the user (click, close...). |
| 64 | // The events are received and dispatched to the right consumer depending on |
| 65 | // the type of notification. Consumers include, service workers, pages, |
| 66 | // extensions... |
| 67 | void ProcessNotificationOperation(NotificationCommon::Operation operation, |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 68 | NotificationHandler::Type notification_type, |
Evan Stade | 7e48597c | 2017-11-03 22:04:48 | [diff] [blame] | 69 | const GURL& origin, |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 70 | const std::string& notification_id, |
Peter Beverloo | ae64ba9 | 2017-09-08 21:02:45 | [diff] [blame] | 71 | const base::Optional<int>& action_index, |
| 72 | const base::Optional<base::string16>& reply, |
| 73 | const base::Optional<bool>& by_user); |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 74 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 75 | // Returns the notification handler that was registered for the given type. |
| 76 | // May return null. |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 77 | NotificationHandler* GetNotificationHandler( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 78 | NotificationHandler::Type notification_type); |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 79 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 80 | // Registers an implementation object to handle notification operations |
| 81 | // for |notification_type|. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 82 | void AddNotificationHandler(NotificationHandler::Type notification_type, |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 83 | std::unique_ptr<NotificationHandler> handler); |
| 84 | |
Evan Stade | 0bf465e5 | 2017-10-03 18:12:00 | [diff] [blame] | 85 | // Removes an implementation object added via AddNotificationHandler. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 86 | void RemoveNotificationHandler(NotificationHandler::Type notification_type); |
Evan Stade | 0bf465e5 | 2017-10-03 18:12:00 | [diff] [blame] | 87 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 88 | private: |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 89 | std::map<NotificationHandler::Type, std::unique_ptr<NotificationHandler>> |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 90 | notification_handlers_; |
| 91 | Profile* profile_; |
| 92 | |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(NotificationDisplayService); |
| 94 | }; |
| 95 | |
| 96 | #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_ |