blob: 6d6c84b1df3ee83741f34d7b1fff4f86fd237f39 [file] [log] [blame]
miguelg23cd2dd72016-04-21 15:24:031// 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
miguelgabf610a2017-06-02 17:31:438#include <map>
miguelgbfe768352017-03-20 15:34:319#include <memory>
miguelg23cd2dd72016-04-21 15:24:0310#include <set>
11#include <string>
miguelg87e986d2016-07-08 18:04:4412
miguelgbfe768352017-03-20 15:34:3113#include "base/callback_forward.h"
miguelg23cd2dd72016-04-21 15:24:0314#include "base/macros.h"
Peter Beverlooae64ba92017-09-08 21:02:4515#include "base/optional.h"
16#include "base/strings/string16.h"
miguelg87e986d2016-07-08 18:04:4417#include "chrome/browser/notifications/notification_common.h"
miguelg23cd2dd72016-04-21 15:24:0318#include "components/keyed_service/core/keyed_service.h"
19
miguelgabf610a2017-06-02 17:31:4320class NotificationHandler;
miguelg23cd2dd72016-04-21 15:24:0321class Profile;
22
Evan Stade4755cf22017-10-17 18:35:4323namespace message_center {
24class Notification;
25}
26
miguelg23cd2dd72016-04-21 15:24:0327// 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.
miguelg23cd2dd72016-04-21 15:24:0333class NotificationDisplayService : public KeyedService {
34 public:
miguelgbfe768352017-03-20 15:34:3135 using DisplayedNotificationsCallback =
36 base::Callback<void(std::unique_ptr<std::set<std::string>>,
37 bool /* supports_synchronization */)>;
Evan Stade6e154152017-11-10 01:58:2538
39 static NotificationDisplayService* GetForProfile(Profile* profile);
40
Evan Stade5d6e4692017-12-08 17:43:5241 // 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
miguelgabf610a2017-06-02 17:31:4346 explicit NotificationDisplayService(Profile* profile);
47 ~NotificationDisplayService() override;
miguelg23cd2dd72016-04-21 15:24:0348
49 // Displays the |notification| identified by |notification_id|.
Evan Stade4ebefdf42017-09-21 23:50:1850 virtual void Display(
Peter Beverloo6dba2e102017-11-23 17:46:3351 NotificationHandler::Type notification_type,
Evan Stade4755cf22017-10-17 18:35:4352 const message_center::Notification& notification,
Evan Stade4ebefdf42017-09-21 23:50:1853 std::unique_ptr<NotificationCommon::Metadata> metadata = nullptr) = 0;
miguelg23cd2dd72016-04-21 15:24:0354
55 // Closes the notification identified by |notification_id|.
Peter Beverloo6dba2e102017-11-23 17:46:3356 virtual void Close(NotificationHandler::Type notification_type,
miguelg87e986d2016-07-08 18:04:4457 const std::string& notification_id) = 0;
miguelg23cd2dd72016-04-21 15:24:0358
miguelgbfe768352017-03-20 15:34:3159 // Writes the ids of all currently displaying notifications and
60 // invokes |callback| with the result once known.
peterb5298ae2017-04-11 21:52:5161 virtual void GetDisplayed(const DisplayedNotificationsCallback& callback) = 0;
miguelg23cd2dd72016-04-21 15:24:0362
miguelgabf610a2017-06-02 17:31:4363 // 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 Beverloo6dba2e102017-11-23 17:46:3368 NotificationHandler::Type notification_type,
Evan Stade7e48597c2017-11-03 22:04:4869 const GURL& origin,
miguelgabf610a2017-06-02 17:31:4370 const std::string& notification_id,
Peter Beverlooae64ba92017-09-08 21:02:4571 const base::Optional<int>& action_index,
72 const base::Optional<base::string16>& reply,
73 const base::Optional<bool>& by_user);
miguelgabf610a2017-06-02 17:31:4374
Evan Stadecc63b182017-09-26 16:05:1275 // Returns the notification handler that was registered for the given type.
76 // May return null.
miguelgabf610a2017-06-02 17:31:4377 NotificationHandler* GetNotificationHandler(
Peter Beverloo6dba2e102017-11-23 17:46:3378 NotificationHandler::Type notification_type);
miguelgabf610a2017-06-02 17:31:4379
miguelgabf610a2017-06-02 17:31:4380 // Registers an implementation object to handle notification operations
81 // for |notification_type|.
Peter Beverloo6dba2e102017-11-23 17:46:3382 void AddNotificationHandler(NotificationHandler::Type notification_type,
miguelgabf610a2017-06-02 17:31:4383 std::unique_ptr<NotificationHandler> handler);
84
Evan Stade0bf465e52017-10-03 18:12:0085 // Removes an implementation object added via AddNotificationHandler.
Peter Beverloo6dba2e102017-11-23 17:46:3386 void RemoveNotificationHandler(NotificationHandler::Type notification_type);
Evan Stade0bf465e52017-10-03 18:12:0087
Evan Stadecc63b182017-09-26 16:05:1288 private:
Peter Beverloo6dba2e102017-11-23 17:46:3389 std::map<NotificationHandler::Type, std::unique_ptr<NotificationHandler>>
miguelgabf610a2017-06-02 17:31:4390 notification_handlers_;
91 Profile* profile_;
92
miguelg23cd2dd72016-04-21 15:24:0393 DISALLOW_COPY_AND_ASSIGN(NotificationDisplayService);
94};
95
96#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_