summaryrefslogtreecommitdiffstats
path: root/content/child/notifications/notification_manager.h
blob: 8b69a0b0e42913f81d28df0a3edb812a6cf0e2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_
#define CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <set>
#include <vector>

#include "base/id_map.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "content/child/notifications/notification_dispatcher.h"
#include "content/child/notifications/pending_notifications_tracker.h"
#include "content/common/platform_notification_messages.h"
#include "content/public/child/worker_thread.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationManager.h"

namespace content {

struct NotificationResources;
struct PlatformNotificationData;
class ThreadSafeSender;

class NotificationManager : public blink::WebNotificationManager,
                            public WorkerThread::Observer {
 public:
  ~NotificationManager() override;

  // |thread_safe_sender| and |notification_dispatcher| are used if
  // calling this leads to construction.
  static NotificationManager* ThreadSpecificInstance(
      ThreadSafeSender* thread_safe_sender,
      base::SingleThreadTaskRunner* main_thread_task_runner,
      NotificationDispatcher* notification_dispatcher);

  // WorkerThread::Observer implementation.
  void WillStopCurrentWorkerThread() override;

  // blink::WebNotificationManager implementation.
  void show(const blink::WebSecurityOrigin& origin,
            const blink::WebNotificationData& notification_data,
            blink::WebNotificationDelegate* delegate) override;
  void showPersistent(
      const blink::WebSecurityOrigin& origin,
      const blink::WebNotificationData& notification_data,
      blink::WebServiceWorkerRegistration* service_worker_registration,
      blink::WebNotificationShowCallbacks* callbacks) override;
  void getNotifications(
      const blink::WebString& filter_tag,
      blink::WebServiceWorkerRegistration* service_worker_registration,
      blink::WebNotificationGetCallbacks* callbacks) override;
  void close(blink::WebNotificationDelegate* delegate) override;
  void closePersistent(const blink::WebSecurityOrigin& origin,
                       int64_t persistent_notification_id) override;
  void notifyDelegateDestroyed(
      blink::WebNotificationDelegate* delegate) override;
  blink::WebNotificationPermission checkPermission(
      const blink::WebSecurityOrigin& origin) override;
  size_t maxActions() override;

  // Called by the NotificationDispatcher.
  bool OnMessageReceived(const IPC::Message& message);

 private:
  NotificationManager(ThreadSafeSender* thread_safe_sender,
                      base::SingleThreadTaskRunner* main_thread_task_runner,
                      NotificationDispatcher* notification_dispatcher);

  // IPC message handlers.
  void OnDidShow(int notification_id);
  void OnDidShowPersistent(int request_id, bool success);
  void OnDidClose(int notification_id);
  void OnDidClick(int notification_id);
  void OnDidGetNotifications(
      int request_id,
      const std::vector<PersistentNotificationInfo>& notification_infos);

  // To be called when a page notification is ready to be displayed. Will
  // inform the browser process about all available data. The |delegate|,
  // owned by Blink, will be used to feed back events associated with the
  // notification to the JavaScript object.
  void DisplayPageNotification(
      const blink::WebSecurityOrigin& origin,
      const blink::WebNotificationData& notification_data,
      blink::WebNotificationDelegate* delegate,
      const NotificationResources& notification_resources);

  // To be called when a persistent notification is ready to be displayed. Will
  // inform the browser process about all available data. The |callbacks| will
  // be used to inform the Promise pending in Blink that the notification has
  // been send to the browser process to be displayed.
  void DisplayPersistentNotification(
      const blink::WebSecurityOrigin& origin,
      const blink::WebNotificationData& notification_data,
      int64_t service_worker_registration_id,
      scoped_ptr<blink::WebNotificationShowCallbacks> callbacks,
      const NotificationResources& notification_resources);

  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
  scoped_refptr<NotificationDispatcher> notification_dispatcher_;

  // Tracker which stores all pending Notifications, both page and persistent
  // ones, until all their associated resources have been fetched.
  PendingNotificationsTracker notifications_tracker_;

  // Tracks pending requests for getting a list of notifications.
  IDMap<blink::WebNotificationGetCallbacks, IDMapOwnPointer>
      pending_get_notification_requests_;

  // Tracks pending requests for displaying persistent notifications.
  IDMap<blink::WebNotificationShowCallbacks, IDMapOwnPointer>
      pending_show_notification_requests_;

  // Map to store the delegate associated with a notification request Id.
  std::map<int, blink::WebNotificationDelegate*> active_page_notifications_;

  DISALLOW_COPY_AND_ASSIGN(NotificationManager);
};

}  // namespace content

#endif  // CONTENT_CHILD_NOTIFICATIONS_NOTIFICATION_MANAGER_H_