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_IMPL_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_IMPL_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/containers/queue.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/optional.h"
15 #include "base/strings/string16.h"
16 #include "chrome/browser/notifications/notification_common.h"
17 #include "chrome/browser/notifications/notification_display_queue.h"
18 #include "chrome/browser/notifications/notification_display_service.h"
19 #include "chrome/browser/notifications/notification_handler.h"
20 #include "chrome/browser/notifications/notification_platform_bridge_delegator.h"
21 
22 class GURL;
23 class Profile;
24 
25 namespace user_prefs {
26 class PrefRegistrySyncable;
27 }
28 
29 // Implementation of the NotificationDisplayService interface. Methods that are
30 // not available in the base interface should only be used by the platform
31 // notification bridges.
32 class NotificationDisplayServiceImpl : public NotificationDisplayService {
33  public:
34   // Note that |profile| might be nullptr for notification display service used
35   // for system notifications. The system instance is owned by
36   // SystemNotificationHelper, and is only expected to handle TRANSIENT
37   // notifications.
38   explicit NotificationDisplayServiceImpl(Profile* profile);
39   NotificationDisplayServiceImpl(const NotificationDisplayServiceImpl&) =
40       delete;
41   NotificationDisplayServiceImpl& operator=(
42       const NotificationDisplayServiceImpl&) = delete;
43   ~NotificationDisplayServiceImpl() override;
44 
45   // Returns an instance of the display service implementation for the given
46   // |profile|. This should be removed in favor of multiple statics for handling
47   // the individual notification operations.
48   static NotificationDisplayServiceImpl* GetForProfile(Profile* profile);
49 
50   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
51 
52   // Used to propagate back events originate from the user. The events are
53   // received and dispatched to the right consumer depending on the type of
54   // notification. Consumers include, service workers, pages, extensions...
55   //
56   // TODO(peter): Remove this in favor of multiple targeted methods.
57   virtual void ProcessNotificationOperation(
58       NotificationCommon::Operation operation,
59       NotificationHandler::Type notification_type,
60       const GURL& origin,
61       const std::string& notification_id,
62       const base::Optional<int>& action_index,
63       const base::Optional<base::string16>& reply,
64       const base::Optional<bool>& by_user);
65 
66   // Registers an implementation object to handle notification operations
67   // for |notification_type|.
68   void AddNotificationHandler(NotificationHandler::Type notification_type,
69                               std::unique_ptr<NotificationHandler> handler);
70 
71   // Returns the notification handler that was registered for the given type.
72   // May return null.
73   NotificationHandler* GetNotificationHandler(
74       NotificationHandler::Type notification_type);
75 
76   // NotificationDisplayService implementation:
77   void Shutdown() override;
78   void Display(NotificationHandler::Type notification_type,
79                const message_center::Notification& notification,
80                std::unique_ptr<NotificationCommon::Metadata> metadata) override;
81   void Close(NotificationHandler::Type notification_type,
82              const std::string& notification_id) override;
83   void GetDisplayed(DisplayedNotificationsCallback callback) override;
84   void AddObserver(Observer* observer) override;
85   void RemoveObserver(Observer* observer) override;
86 
87   static void ProfileLoadedCallback(NotificationCommon::Operation operation,
88                                     NotificationHandler::Type notification_type,
89                                     const GURL& origin,
90                                     const std::string& notification_id,
91                                     const base::Optional<int>& action_index,
92                                     const base::Optional<base::string16>& reply,
93                                     const base::Optional<bool>& by_user,
94                                     Profile* profile);
95 
96   // Sets the list of |blockers| to be used by the |notification_queue_|. Only
97   // used in tests.
98   void SetBlockersForTesting(
99       NotificationDisplayQueue::NotificationBlockers blockers);
100 
101   // Sets the platform bridge delegator for tests.
102   void SetNotificationPlatformBridgeDelegatorForTesting(
103       std::unique_ptr<NotificationPlatformBridgeDelegator> bridge_delegator);
104 
105   // Sets an implementation object to handle notification operations for
106   // |notification_type| and overrides any existing ones.
107   void OverrideNotificationHandlerForTesting(
108       NotificationHandler::Type notification_type,
109       std::unique_ptr<NotificationHandler> handler);
110 
111  private:
112   // Called when the NotificationPlatformBridgeDelegator has been initialized.
113   void OnNotificationPlatformBridgeReady();
114 
115   // Called after getting displayed notifications from the bridge so we can add
116   // any currently queued notification ids.
117   void OnGetDisplayed(DisplayedNotificationsCallback callback,
118                       std::set<std::string> notification_ids,
119                       bool supports_synchronization);
120 
121   Profile* profile_;
122 
123   // This NotificationPlatformBridgeDelegator delegates to either the native
124   // bridge or to the MessageCenter if there is no native bridge or it does not
125   // support certain notification types.
126   std::unique_ptr<NotificationPlatformBridgeDelegator> bridge_delegator_;
127 
128   // Tasks that need to be run once the display bridge has been initialized.
129   base::queue<base::OnceClosure> actions_;
130 
131   // Boolean tracking whether the |bridge_delegator_| has been initialized.
132   bool bridge_delegator_initialized_ = false;
133 
134   // Notification queue that holds on to notifications instead of displaying
135   // them if certain blockers are temporarily active.
136   NotificationDisplayQueue notification_queue_{this};
137 
138   // Map containing the notification handlers responsible for processing events.
139   std::map<NotificationHandler::Type, std::unique_ptr<NotificationHandler>>
140       notification_handlers_;
141 
142   base::ObserverList<Observer> observers_;
143 
144   base::WeakPtrFactory<NotificationDisplayServiceImpl> weak_factory_{this};
145 };
146 
147 #endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_IMPL_H_
148