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